G11_C3

Interaction between game components

(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

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. Projector 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 output 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?

Are 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()
    

Teacher Activity 1

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()
    

Teacher Activity 2

Introduction to "Functions"

Input

Output

So many tasks happening at the backend!

Introduction to "Functions"

True/False

How do I know if

the brick and paddle are colliding?

paddle
.
collidepoint(
ball.x,ball.y
)

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(): 
            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

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():
            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.

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 1

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

rect1.collidepoint(rect2.x,rect2.y)

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

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

G11_C3

By Sanjukta Bhattacharya