G11_C3
Interaction between game components
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 |
Class Structure
Preparation and Reference
Prerequisites
FOR TEACHER
FOR STUDENTS
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
Choose the correct output:
B
55
Error
265
Q.1
A
Error
B
C
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)
What would be the correct ouput if the syntax is corrected?
A
55
55
265
Q.2
A
45
B
C
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?
Let us revise our previous class
GREAT!
B
Bot with pre-coded movement direction
B
Absence of judgement in precoded movements
Why is it not working!!
Recognizing user input correctly in a game
Let me press a key!!
Wow!! the paddle moved!
Decision Making for User Interaction
Watch TV
Is my
favourite
show on?
Is studies
over?
Decision Levels
if yes
if yes
Watch TV
Which key?
Multilevel decisions
LEVEL 1
LEVEL 2
Making the paddle move right with user command
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?
Extra Decision Level
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()
Introduction to "Functions"
Input
Output
So many tasks happening at the backend!
Introduction to "Functions"
How do I know if
the brick and paddle are colliding?
Check for collision
Change the ball movement direction
Paddle and ball collision
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
Student Activity 1
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.
Student Activity 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:
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()
Problems left to address!
GREAT!
Student Additional Activity
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)
SAA : Solution
Choose the correct command for detecting a collision between the paddle and ball?
B
ball.collide point(paddle.x,paddle.y)
paddle.collide point(ball.x,ball.y)
pygame.rect(x,y,width,height)
Q1
A
B
C
paddle.collide point(ball.x,ball.y)
ball.collide point(paddle.x,paddle.y)
A
A
Both are the correct syntax for detecting a collision.
What do we use to take multiple levels of decision?
B
if statement
Nested if statement
Q2
A
Nested if statement
B
C
Nested if else statements are used for multiple levels of decision
if some_condition_is_true:
if another_condition_is_true:
Execute_a_code
Consecutive if statements for each decision
Links Table
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) |
Copy of G11 C3
By anjali_sharma
Copy of G11 C3
- 102