G11_C3
Activity Flow | Slide No. | Topic | Time |
---|---|---|---|
TA | 4-8 | Warmup+Quiz | 5 min |
9-10 | Revision | 1 min | |
11-21 | Encoding User Iteraction in the Game | 10 min | |
22-26 | Paddle-Ball Collision | 5 min | |
SA | 27-29 | Paddle Movement | 5 min |
30-32 | Paddle Collision | 2 min | |
TA | 33-35 | Preview to Next Class | 2min |
SA | 36-39 | Additional activity | 5 min |
Wrap-Up | 40-44 | Wrap up+Quiz | 5 min |
Slide No. | Topic |
---|---|
19 | TA 1 code solution |
21 | TA 2 code solution |
26 | TA 3 code solution |
29 | SA 1 code solution |
31 | SA 2 code solution |
38 | SAA1 code solution |
1. Computer with an Internet connection.
2. The latest browser installed.
1. Computer with an Internet connection.
2. The latest browser installed.
3. Spyder installed.
4. Projetor to present the screen
Since python is indent sensitive, so, missing tab space in the original question will throw an indentation syntax error.
x=11
if x>25:
x=x-5
if x<10:
x=x+5
if x>10:
x=x*5
if x<25:
x=x-2*5
print(x)
If statement syntax is incorrect
x=11
if x>25:
x=x-5
if x<10:
x=x+5
if x>10:
x=x*5
if x<25:
x=x-2*5
print(x)
x=11
if x>25:
x=x-5
if x<10:
x=x+5
if x>10:
x=x*5
if x<25:
x=x-2*5
print(x)
x=11
x>25?
False
x<10?
False
x>10?
True
x=11*5
x=55
x<25?
False
x=55
How did
we make this happen?
B
B
Why is it not working!!
Let me press a key!!
Wow!! the paddle moved!
Watch TV
Is my
favourite
show on?
Is studies
over?
Decision Levels
if yes
if yes
Watch TV
Which key?
LEVEL 1
LEVEL 2
import pygame
pygame.init()
WHITE = (255,255,255)
DARKBLUE = (36,90,190)
LIGHTBLUE = (0,176,240)
size = (600, 600)
screen = pygame.display.set_mode(size)
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(): # User did something
if event.type == pygame.QUIT: # If user clicked close
carryOn = False # Flag that we are done so we exit this loop
screen.fill(DARKBLUE)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
paddle.x+=paddlex
pygame.draw.rect(screen,LIGHTBLUE,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
pygame.draw.rect(screen,WHITE ,ball)
pygame.time.wait(20)
pygame.display.flip()
pygame.quit()
Which key?
LEVEL 1
LEVEL 2
LEVEL 3?
import pygame
pygame.init()
WHITE = (255,255,255)
DARKBLUE = (36,90,190)
LIGHTBLUE = (0,176,240)
size = (600, 600)
screen = pygame.display.set_mode(size)
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(): # User did something
if event.type == pygame.QUIT: # If user clicked close
carryOn = False # Flag that we are done so we exit this loop
screen.fill(DARKBLUE)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
if paddle.x<540:
paddle.x+=paddlex
pygame.draw.rect(screen,LIGHTBLUE,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
pygame.draw.rect(screen,WHITE ,ball)
pygame.time.wait(20)
pygame.display.flip()
pygame.quit()
Input
Output
So many tasks happening at the backend!
How do I know if
the brick and paddle are colliding?
Check for collision
Change the ball movement direction
import pygame
pygame.init()
WHITE = (255,255,255)
DARKBLUE = (36,90,190)
LIGHTBLUE = (0,176,240)
size = (600, 600)
screen = pygame.display.set_mode(size)
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(): # User did something
if event.type == pygame.QUIT: # If user clicked close
carryOn = False # Flag that we are done so we exit this loop
screen.fill(DARKBLUE)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
if paddle.x<540:
paddle.x+=paddlex
pygame.draw.rect(screen,LIGHTBLUE,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,WHITE ,ball)
pygame.time.wait(20)
pygame.display.flip()
pygame.quit()
Make the paddle move left when you press the left key
import pygame
pygame.init()
WHITE = (255,255,255)
DARKBLUE = (36,90,190)
LIGHTBLUE = (0,176,240)
size = (600, 600)
screen = pygame.display.set_mode(size)
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(): # User did something
if event.type == pygame.QUIT: # If user clicked close
carryOn = False # Flag that we are done so we exit this loop
screen.fill(DARKBLUE)
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,LIGHTBLUE,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
pygame.draw.rect(screen,WHITE ,ball)
pygame.time.wait(20)
pygame.display.flip()
pygame.quit()
Now, code to check if the ball collides with the paddle, if yes change the direction of the ball.
import pygame
pygame.init()
WHITE = (255,255,255)
DARKBLUE = (36,90,190)
LIGHTBLUE = (0,176,240)
size = (600, 600)
screen = pygame.display.set_mode(size)
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(): # User did something
if event.type == pygame.QUIT: # If user clicked close
carryOn = False # Flag that we are done so we exit this loop
screen.fill(DARKBLUE)
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,LIGHTBLUE,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,WHITE ,ball)
pygame.time.wait(20)
pygame.display.flip()
pygame.quit()
num1=90
num2=50
choice=4
if choice==1:
print(num1+num2)
if choice==2:
print(num1-num2)
if choice==3:
print(num1*num2)
if chocie==4:
print(num1/num2)
Both are the correct syntax for detecting a collision.
Nested if else statements are used for multiple levels of decision
if some_condition_is_true:
if another_condition_is_true:
Execute_a_code
Activity | Activity Name | Link |
---|---|---|
Teacher activity 1 | Paddle movement rift (Solution) | |
Teacher Activity 1 | Paddle right movement | |
Teacher Activity 2 | Paddle right boundary restriction | |
Teacher Activity 3 | Paddle-ball collision (solution) |
|
Student Activity 1 | Paddle left movement | |
Teacher Activity 4 | Paddle left movement (SA1 Solution) |
|
Teacher Activity 5 | Paddle-ball collision (SA2 solution) |
|
Teacher Activity 6 | Calculator creation solution (SAA1 Solution) |