G11_C5
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 |
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
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
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
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()
carryOn = True
while carryOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
carryOn = False
pygame.quit()
carryOn = True
while carryOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
carryOn = False
pygame.quit()
Is Daisy busy?
YES
NO! She is coming this way!!
Let's keep decorating
colon
tab
space
maria_busy=True
while maria_busy:
print("Keep decorating!")
maria_busy=False
print("Get ready to surprise!")
maria_busy=True
while maria_busy:
print("Keep decorating!")
maria_busy=False
print("Get ready to surprise!")
carryOn = True
while carryOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
carryOn = False
pygame.quit()
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 |
i=0
while i<5:
print(i)
i=i+1
carryOn = True
while carryOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
carryOn = False
pygame.quit()
!!
Let me press a key!!
Wow!! the paddle moved!
Key is pressed
Mouse is clicked
Perform some tasks
Perform some tasks
How does the computer understand user has clicked / pressed something?
Queue of events
Event 1
Event 2
Event 3
.
.
.
But how do I know what exactly the user did??
Queue of events
for event in pygame.event.get()
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!
Let us begin creating the game from scratch now!
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!
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()
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
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
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 |
Hints:
Print the numbers 1 to 50 using "while" loop.
num=1
while num<50:
print(num)
num+=1
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
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 |