G11_C3
(Location, Color)
x,y,z
r,g,b
Vertex Attribute
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. Projector 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?
Are 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()
Teacher Activity 1
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()
Teacher Activity 2
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():
if event.type == pygame.QUIT:
carryOn = False
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()
Teacher Activity 3
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():
if event.type == pygame.QUIT:
carryOn = False
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 right movement | |
TEACHER ACTIVITY 2 | Paddle right movement with Restriction | |
TEACHER ACTIVITY 3 | Paddle-ball collision | |
TEACHER ACTIVITY 1 SOLUTION | Solution of TA1 | |
TEACHER ACTIVITY 2 SOLUTION | Solution of TA2 | |
TEACHER ACTIVITY 3 SOLUTION | Solution of TA3 | |
STUDENT ACTIVITY 1 | Paddle left movement with Restriction |
|
STUDENT ACTIVITY 2 | Paddle-ball collision | |
TEACHER REFERENCE: STUDENT ACTIVITY 1 SOLUTION | Solution of SA1 | |
TEACHER REFERENCE: STUDENT ACTIVITY 2 SOLUTION | Solution of SA2 | |
STUDENT ADDITIONAL ACTIVITY 1 | Calculator Creation | |
TEACHER REFERENCE: STUDENT ADDITIONAL ACTIVITY 1 SOLUTION | Solution of SAA1 |