G12_C10

The Bird Class

Activity Flow Slide No. Topic Time

Class Structure

Slide No. Topic

Preparation and Reference

Prerequisites

FOR STUDENTS

  1. Computer with Internet connection.

  2. Spyder IDE installed. 

  3. "pygame" package installed.

FOR TEACHER

  1. Computer with Internet connection.

  2. Latest browser installed.

  3. "pygame" package installed.

  4. Spyder IDE installed.

(WARM-UP QUIZ)

C

What will be the output of the following code snippet?

Q.1

my_dictionary = {}
x1, y1, z1 = 1, 2, 3
my_dictionary["A"] = x1 + y1 - z1;
x2, y2, z2 = 10, 20, 40
my_dictionary["B"] = x2 + y2 - z2;
print(my_dictionary)
{'A': 0, 'B': -10}
{'A': 5, 'B': -10}
{'A': 0, 'B': 5}
{'A': 10, 'B': 5}

The correct answer is option A

C

What will be the output of the following code snippet?

Q.2

class Car:
  def __init__(self,model,cost,color):  
    self.model=model
    self.cost=cost
    self.color=color
  def display(self):
    print("Model is",self.model)
    print("Cost is",self.cost)
    print("Color is",self.color)
obj1 = Car("Van","INR 100000","White")
obj2 = Car("Sports Car","INR 200000","Red")
obj1.display()	
obj2.display()
{'A': 0, 'B': -10}
{'A': 5, 'B': -10}
{'A': 0, 'B': 5}
{'A': 10, 'B': 5}

The correct answer is option A

Teacher Activity 1 template: https://bit.ly/3rpPGns

Creating the game screen

Let's begin by creating the basic game screen.

  • Import pygame
  • Initialize pygame
  • Create the screen
  • Set screen caption
  • Create the game loop
  • Finish the game
init()
import 
display.set_mode()
display.set_caption()
while True:
pygame.quit()

Steps to be followed:

import pygame, sys
pygame.init()
clock=pygame.time.Clock()
width=400
height=600
screen = pygame.display.set_mode((width,height))
while True:
    screen.fill((50,150,255))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
   
    pygame.display.update()
    
pygame.quit()

Solution link for Teacher Activity 1: https://bit.ly/3io5vH4

Adding background to the game window and declaration of Bird class

images={}
images["bg1"] = pygame.image.load("D:/Flappy for AI/bg1.png").convert_alpha()
images["base"] = pygame.image.load("D:/Flappy for AI/base.png").convert_alpha()
images["bird"] = pygame.image.load("D:/Flappy for AI/bird.png").convert_alpha()
...
class Bird:
    r=pygame.Rect(100,250,30,30)
    speed=0
    g=0.5

Teacher Activity 2 template: https://bit.ly/2W25wZK

Teacher Activity 2 solution: https://bit.ly/3xUBEMK

Output Teacher Activity 2

Student Activity

Implementation of display() of Bird class

def display(self):
  screen.blit(images["bird"],self.r)
...
bird = Bird()
...
bird.display()
screen.blit(images["base"],[0,550])

Student Activity template:https://bit.ly/3isTYGk

Student Activity solution: https://bit.ly/2ToN7p4

Output Student Activity

Now let's change the starting coordinates of the bird.

Student Additional Activity 1

Template link: https://bit.ly/3rigzJT

Hints:

  • Change the starting coordinates of the bird image.
  • For example, you can change the starting coordinates to (300, 300), as shown in figure.

Student Additional Activity 1 solution

class Bird:
    r=pygame.Rect(300,300,30,30)
    speed=0
    g=0.5

Student Additional Activity 1 solution: https://bit.ly/2UUx7LY

Student Additional Activity 2

Template link:

Can you figure out a way, in which, we can make the flappy bird jump, double its current pixel values?

Hints:

G12_C10

By anjali_sharma