G11_C5

Understanding and creating the game loop

Activity Flow Slide No. Topic Time
TA 4-7 Quiz 1 min
8-9 Revision 2 min
10-23 While loop 3 min
24-29 Events 3 min
SA 30-37 Game loop -Coding 15
Wrap - Up 38-41 Quiz 1 min
SA 42 Additional Activity 5 min
Slide No. Topic
11 Random number code
23-25 Basic pygame code
31-32 Making rectangles
36 SA Code- Creating enemy rect.
45 Additional Activity- Code

Class Structure

Preparation and Reference

Prerequisites

FOR TEACHER

FOR STUDENTS

1. Computer with an Internet connection.

2. The latest browser installed.

3. Spyder IDE.

4. Projector to present the screen.

1. Computer with an Internet connection.

2. The latest browser installed.

3. Spyder IDE.

x=5
for i in range(3):
  x*=5
  print(x)
625
25
125
625
125

Try to predict the output for the following code:

C

Q.1

A

B

C

loop begins

x=5

loop ends

printed

printed

printed

loop no. i is: x becomes:
1 0 5*5=25
2 1 25*5=125
3 2 125*5=625
5
x=5
for i in range(3):
  x*=5
print(x)
625
125

Try to predict the output for the following code:

A

Q.2

A

B

C

loop begins

x=5

loop ends

x=625

(printed)

loop no. i is: x becomes:
1 0 5*5=25
2 1 25*5=125
3 2 125*5=625

B

Do you remember the  problem we encountered in brick-ball collisions?

Revision

Do you remember how we created the brick wall in the previous class?

The mystery of the brick reappearance

What am I missing?

import pygame
pygame.init() 
screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("Breakout Game")
paddle=pygame.Rect(300,500,60,10)
ball=pygame.Rect(200,250,10,10)
ballx=-1
bally=-1
paddlex=2
carryOn = True
while carryOn:
    for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                  carryOn = False            
    screen.fill((36,90,190))
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_RIGHT:
            if paddle.x<540: 
                paddle.x+=paddlex
        if event.key == pygame.K_LEFT:
            if paddle.x>0:
                paddle.x-=paddlex
           
    pygame.draw.rect(screen,(0,176,240),paddle)
    ball.x=ball.x+ballx
    ball.y=ball.y+bally
    if ball.x>=590:
        ballx=-ballx
    if ball.x<=10:
        ballx=-ballx
    if ball.y>=590:
        bally=-bally
    if ball.y<=10:
        bally=-bally
    if paddle.collidepoint(ball.x,ball.y):
        bally=-bally
   
    pygame.draw.rect(screen,(255,255,255) ,ball)
    
    for i in range(7):
     brick=pygame.Rect(10 + i* 100,60,80,30)
     pygame.draw.rect(screen,(255,0,0),brick)
    for i in range(7):
     brick=pygame.Rect(10 + i* 100,60,80,30)
     pygame.draw.rect(screen,(255,100,0),brick)
     if brick.collidepoint(ball.x,ball.y):   
      pygame.draw.rect(screen,(36,90,190),brick)
      
    
    pygame.display.flip()
pygame.quit()
    

Unexplored code!

What going on here?

carryOn = True
while carryOn:
    for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                  carryOn = False
                  pygame.quit()

Some déjà vu !

We have learned this already!

carryOn = True
while carryOn:
    for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                  carryOn = False
                  pygame.quit()

Something new!

This looks new!!

Is Daisy busy?

YES

NO! She is coming this way!!

Let's keep decorating

Continuous decision making

while(condition):
Execute
Execute
Execute
Execute

colon

tab

space

maria_busy=True
while maria_busy:
  print("Keep decorating!")
  maria_busy=False
print("Get ready to surprise!")

How does the computer do this?

maria_busy=True
while maria_busy:
  print("Keep decorating!")
  maria_busy=False
print("Get ready to surprise!")

Teacher Activity 1

It's getting clearer!!

carryOn = True
while carryOn:
    for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                  carryOn = False  
                  pygame.quit()

Flow of the "while" loop in the game

for i in range(5):
  #Execute task
  
i=0 Execute task once
1 Execute once more
2 Execute again
... ...
4 Execute for the 5th time

Creating a "while" loop

Let us try to recreate the output of this "for" loop using a "while" loop.

i=0
while i<5:
  print(i)
  i=i+1

Teacher Activity 2

GREAT!

Some more new things!!

carryOn = True
while carryOn:
    for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                  carryOn = False  
                  pygame.quit()

What is an "event"?

!!

Let me press a key!!

Wow!! the paddle moved!

Let's recall

Key is pressed

Mouse is clicked

Perform some tasks

Perform some tasks

pygame.event

Events in pygame

How does the computer understand user has clicked / pressed something?

pygame.event.get()

Queue of events

Event 1

Event 2

Event 3

.

.

.

Getting Events

But how do I know what exactly the user did??

Queue of events

for
loop!!
for event in pygame.event.get()
event.type
pygame.QUIT
pygame.KEYDOWN

Determining the type of the event

Event 1

Event 2

Event 3

.

.

.

carryOn = True
while carryOn:
    for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                  carryOn = False  
                  pygame.quit()

Now I am an expert!

Some more new things!!

Let us begin creating the game from scratch now!

Student Activity 1

Hints:

import pygame
pygame.init() 
screen = pygame.display.set_mode((600,600))
paddle=pygame.Rect(300,500,60,10)
ball=pygame.Rect(200,250,10,10)
ballx=1
bally=1
paddlex=2

Let's create the game loop now!

Student Activity 2

Hints:

import pygame
pygame.init() 
screen = pygame.display.set_mode((600,600))
paddle=pygame.Rect(300,500,60,10)
ball=pygame.Rect(200,250,10,10)
ballx=1
bally=1
paddlex=2

carryOn = True
while carryOn:
    for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                  carryOn = False 
                  pygame.quit()

Student Activity 3

Hints:

Now fill the screen with a color of your choice.

import pygame
pygame.init() 
screen = pygame.display.set_mode((600,600))
paddle=pygame.Rect(300,500,60,10)
ball=pygame.Rect(200,250,10,10)
ballx=1
bally=1
paddlex=2

carryOn = True
while carryOn:
    for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                  carryOn = False 
    screen.fill(0,176,240)

x=10
while False:
  x=x+100
print(x)
10
110
Error

Try to predict the output for the following code:

C

Q.1

A

B

C

While loop set to false by default

x=10

10 is printed

No Change in x

x=5
i=0
while i<3:
  x*=5
  print(x)
  i=i+1
625
25
125
625
125

Try to predict the output for the following code:

C

Q.2

A

B

C

loop begins

x=5

loop ends

printed

printed

printed

loop no. i is: x becomes:
1 0 5*5=25
2 0+1=1 25*5=125
3 1+1=2 125*5=625

Student Additional Activity 1

Hints:

Print the numbers 1 to 50 using "while" loop.

num=1
while num<50:
  print(num)
  num+=1

SAA1:Solution

Student Additional Activity 2

Hints:

Print all even  numbers between 1 to 50 using "while" loop.

num=1
while num<50:
  if num%2==0:
  	print(num)
  num+=1

SAA2:Solution

Activity Activity Name Link
TEACHER ACTIVITY 1 While loop
TEACHER ACTIVITY 2 For loop encoding using while
TEACHER ACTIVITY 1 SOLUTION Solution of TA1
TEACHER ACTIVITY 2 SOLUTION Solution of TA2
STUDENT ACTIVITY 1 Game loop creation-I
STUDENT ACTIVITY 2 Game loop creation-II
STUDENT ACTIVITY 3 Game loop creation-III
TEACHER REFERENCE: STUDENT ACTIVITY 1 SOLUTION Solution of SA1
TEACHER REFERENCE: STUDENT ACTIVITY 2 SOLUTION Solution of SA2
TEACHER REFERENCE: STUDENT ACTIVITY 3 SOLUTION Solution of SA3
STUDENT ADDITIONAL ACTIVITY 1 Printing Natural Numbers
STUDENT ADDITIONAL ACTIVITY 2 Printing Even Numbers
TEACHER REFERENCE: STUDENT ADDITIONAL ACTIVITY 1 SOLUTION Solution of SAA1
TEACHER REFERENCE: STUDENT ADDITIONAL ACTIVITY 2 SOLUTION Solution of SAA2

G11_C5

By Sanjukta Bhattacharya