G11_C4

Creation of the brick wall

Activity Flow Slide No. Topic Time
TA 4-7 Warm up+Quiz+Revision 5 min
8-15 Understanding loops 7 min
16-21 Brick Creation 5 min
SA 22-26 Orange brick creation 5  min
27-31 Brick-ball collision 7 min
TA 32 Precap to next class 2min
Wrap - Up 33-36 Quiz 5 min
SA 38-40 Additional Activity 5 min
Slide No. Topic
14 TA1 solution
19 TA2 solution
21 TA3 solution
24 SA1 solution
30 SA2 solution
40 SAA1 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

x=5
7=y
print(x+y)

What would be the output of the

following  code:

C

12

SyntaxError:cannot assign to literal

Q.1

A

SyntaxError:cannot assign to literal

B

C

Syntax :
variable =value
value= variable

NameError: name 'y' is not defined

What would be the output of the

following  code:

B

91952

7

21952

Q.2

A

7

B

C

56/8=7
"**" has higher operator precedence than "/" , hence 2**3 gets executed first

print(56/2**3)

Let us revise our previous class

B

Please help me keep these boxes from point A to point B so that we can load the truck!

Ok Papa!!

Performing a repetitive task

A

A

B

Pick a box from point A and place in point B

Performing a repetitive task

A

B

Pick a box from point A and place in point B

5 times.

5!!

How many boxes?

Instructing a repetitive task performance

for i in range(5):
  #Execute task
  
i=0 Execute task once
1 Execute once more
2 Execute again
... ...
4 Execute for the 5th time

The for loop

for i in range(5):
  print(i)

for i in range(start ,stop)

default=0

stops at n-1 if stop=n

Understanding the for loop and the range function

80

20

30

10

110

210

310

410

510

height

width

spacing

X-coordinates

Creating the bricks

How can I get these numbers?!

Can you use the for loop to print series of

x location of the bricks?

10

110

210

310

410

510

for i in range(6):
  print(i,i*100+10)

Lets code for the red bricks

Coding for the red bricks

import pygame
pygame.init() 



screen = pygame.display.set_mode((600, 600))
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((36,90,190))
    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,(0,176,240),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,(255,255,255) ,ball)
    
    for i in range(6):
     brick=pygame.Rect(10 + i* 100,60,80,30)
      pygame.draw.rect(screen,(255,0,0),brick)
    pygame.display.flip()
pygame.quit()
    

Now create the orange bricks!

  • "Draw 'brick' on 'screen' with color orange[RGB:(255,100,0)]"

x: iterate at required with the help of i

y:100

width: 80

height: 30"

Code for the layer of orange bricks

import pygame
pygame.init() 



screen = pygame.display.set_mode((600, 600))
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((36,90,190))
    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,(0,176,240),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,(255,255,255) ,ball)
    
    for i in range(6):
     brick=pygame.Rect(10 + i* 100,60,80,30)
      pygame.draw.rect(screen,(255,0,0),brick)
    for i in range(6):
     brick=pygame.Rect(10 + i* 100,100,80,30)
      pygame.draw.rect(screen,(255,100,0),brick)
    
    pygame.display.flip()
pygame.quit()
    

Expected output

Brick and ball collided?

Brick disappears

Change color of brick to background color

Brick collided with ball??

Brick-ball collisions

Now let's code for ball-brick collisions.

Try to code

import pygame
pygame.init() 



screen = pygame.display.set_mode((600, 600))
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((36,90,190))
    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,(0,176,240),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,(255,255,255) ,ball)
    
    for i in range(7):
     brick=pygame.Rect(10 + i* 100,60,80,30)
     pygame.draw.rect(screen,(255,0,0),brick)
    for i in range(7):
     brick=pygame.Rect(10 + i* 100,60,80,30)
     pygame.draw.rect(screen,(255,100,0),brick)
     if brick.collidepoint(ball.x,ball.y):   
      pygame.draw.rect(screen,(36,90,190),brick)
      
    
    pygame.display.flip()
pygame.quit()
    

Why is the brick reappearing when the ball moves away?

A problem!

for i in range(10):
  if i%2==0:
    print(i)
  

Predict the output:

B

Q1

A

B

C

i%2 calculates remainder on dividing a number by 2.A remainder 0 would mean the number is even.Hence only the even numbers get printed

num=20
if num/10==2:
  for i in range(5):
    num+=5
print(num)

Predict the output:

C

25

45

45

Q.2

A

20

B

C

20/10 is equal to 2, hence the condition of the loop is satisfied .Hence 20 is incremented by 5 .So it becomes 25 , then 30, then 35, then 40 and final finally 45.

Can you print the values of the multiplication table of 23?

Hints:

23 * 1 = ?

23 * 2 = ?

23 * 3 = ?

.

.

.

.

23 * 10 = ?

A problem!

for i in range(1,11):
  print(23*i)

Links Table

Activity Activity Name Link
Teacher Activity 1(S) For loop_basic (Solution)
Teacher Activity 2(S) For loop_x coordinates (Solution)
Teacher Activity 3 Red brick creation_template
Student Activity 1 Orange brick creation_template
Teacher Activity 4 Orange brick creation
(SA1 Solution)
Teacher Activity 5 Multiplication tables (SAA1 Solution)

Copy of G11 C4

By anjali_sharma