G11_C2_For TeacherReference

Variable and Conditional programming

Activity Flow Slide No. Topic Time
TA 5-6 Warm-up Quiz 1 min
7 Recap 1 min
8-11 Understanding variables 5 min
12 Arithmetic operations 3 min
13-15 TA - Coding (ball movement) 3 min
SA 16-19 SA-coding (ball movement) 4 min
TA 20-24 Decision Making 5 min
25-27 TA - Coding (ball movement with restriction) 3 min
28-31 SA - Coding (ball movement with restriction) 5 min
Wrap - Up 32-34 Quiz 2 min
35 Additional Activity 10 min
Slide No. Topic
14 TA Code- Ball movement 
18 SA Code- Ball movement 
26 TA Code- Ball movement with restrictions
30 SA Code- Ball movement with restrictions
37 Additional Activity 1- Code
39 Additional Activity 2- Code

Class Structure

Preparation and Reference

Prerequisites

FOR TEACHER

FOR STUDENTS

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.

(WARM-UP QUIZ)

Q1.

B

Rect(x,y,width,height)

pygame.Rect(x,y,width,height)

pygame.rect(x,y,width,height)

A

pygame.Rect(x,y,width,height)

B

C

Option B is the correct syntax for creating a rectangle. We pass the x-position followed by the y-position, then the width and the height of the rectangle.

Choose the correct command for creating a rectangle in python.

What is the correct order to see a rectangle on screen?

A. Draw the rectangle on the screen.

B.  Create a rectangle object

C. Pass the x coordinate , y coordinate, width and height.

Q2.

B

A-B-C

B-C-A

A-C-B

A

B-C-A

B

C

Option B is the correct. We first create the rectangle object using the pygame.Rect() command. Then we  pass the required parameters. Finally to see the rectangle, we need to draw the rectangle on the screen

 

Revision :

Data in our daily lives

Processing Data

Sender

Back Office

Receiver

Is the human memory enough?

Sender

Back Office

Receiver

How does the computer store our data?

Computer

Mother Board

Memory Slots

Mathematical Operations on Variables

Addition

Subtraction

Division

Multiplication

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
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)
    
    pygame.draw.rect(screen,LIGHTBLUE,paddle)
    ball.x=ball.x+ballx
    ball.y=ball.y+bally
    pygame.draw.rect(screen,WHITE ,ball)
    pygame.time.wait(20)
    pygame.display.flip()
pygame.quit()
    

Output :

Expected Output:

Altering Movement Direction

How to make the ball move in the diagonally upward direction?


paddle=pygame.Rect(300,500,60,10)
ball=pygame.Rect(200,250,10,10)
ballx=1
bally=1

Hint:

Alter values of ballx and bally

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
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)
    
    pygame.draw.rect(screen,LIGHTBLUE,paddle)
    ball.x=ball.x+ballx
    ball.y=ball.y+bally
    pygame.draw.rect(screen,WHITE ,ball)
    pygame.time.wait(20)
    pygame.display.flip()
pygame.quit()
    

Fault in our game!!

Ball moves out and disappears!!

Decision making

> 60?

Logical operations

Greater than

Lesser than

Equals to

How the computer makes decisions?

age>60?
True
age>60?
True
if
age>60:
discount=200

tab space

colon

Syntax:

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
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)
    
    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:
      ball.x=-ballx
    pygame.draw.rect(screen,WHITE ,ball)
    pygame.time.wait(20)
    pygame.display.flip()
pygame.quit()
    

Output:

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
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)
    
    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:
      ball.x=-ballx
      
    if ball.y>=590:
      bally=-bally
    if ball.y<=10:
      ball.y=-bally
      
    pygame.draw.rect(screen,WHITE ,ball)
    pygame.time.wait(20)
    pygame.display.flip()
pygame.quit()
    

Expected output

(WRAP-UP QUIZ)

Choose the correct output

Error

Q.1

x=5
if x<10:
print(x+4*10)

B

45

90

A

Error

B

C

Option B is correct. There is an indentation error as  the if statement should be followed by a tab space before each statement under the loop

Choose the correct output

True

Q.2

B

No output

12

A

True

B

C

x=10
x=x+6/3
if x==12:
  print("True")

According to Bodmas division will be executed first before addition, so the equation will be solved as:

x=10+2     x=12

Hence the logical check will print True.

So option B is correct.

Expected Output:

Paddle movement

SAA-1

How will you make the paddle move?

Hint:

- Paddle will only move in x direction.

-Create "paddlex" variable  

 

-Increment the paddle's x coordinate.

paddlex=2
paddle.x+=paddlex
import pygame

ballx=-1
bally=-1
paddlex=1
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)
    paddle.x=paddle.x+paddlex
    
    pygame.draw.rect(screen,LIGHTBLUE,paddle)
    ball.x=ball.x+ballx
    ball.y=ball.y+bally
    
      
   
    

SAA1 solution

SAA-2

Restricting paddle movement

How will you restrict the paddle within the screen?

Hint:

-Screen ranges from 0 to 600 in width.

-Paddle length is 60.

-So paddlex coordinate should be between 0 and 540.

 

 

-Similarly:

 

if paddle.x<0:
  paddlex=-paddlex
if paddle.x>540:
  paddlex=-paddlex

Expected Output:

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=1
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)
    paddle.x=paddle.x+paddlex

    if paddle.x>=540:
        paddlex=-paddlex
    if paddle.x<=60:
        paddlex=-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:
      ball.x=-ballx
      
    if ball.y>=590:
      bally=-bally
    if ball.y<=10:
      ball.y=-bally
      
    pygame.draw.rect(screen,WHITE ,ball)
    pygame.time.wait(20)
    pygame.display.flip()
pygame.quit()
    

SAA2 solution

Links Table

Activity Activity Name Link
Teacher Activity 1 Ball Movement
Teacher Activity 2 Ball Movement with restrictions
Teacher Activity 2(S) Teacher Activity 2 solution
Student Activity 1 Ball Movement
Teacher activity 3 Ball Movement with restrictions
Teacher Activity 3(S) Teacher Activity 3 solution
Student Additional Activity 1 Paddle Movement
Teacher activity 4 Paddle Movement
(Student Additional Activity 1-Solution)
Teacher activity 3 Paddle Movement with restrictions
(Student Additional Activity 2-Solution)

Copy of G11_C2

By anjali_sharma