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.
3. Spyder 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():
if event.type == pygame.QUIT:
carryOn = False
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 as required with the help of i
y:100
width: 80
height: 30
Student Activity 1
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():
if event.type == pygame.QUIT:
carryOn = False
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.
Hints:
Change color of brick to the screen fill color
Negate 'bally' to change the direction of the ball
Student Activity 2
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():
if event.type == pygame.QUIT:
carryOn = False
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 = ?
Student Additional Activity 1
for i in range(1,11):
print(23*i)
Student Additional Activity 1
Now create the yellow bricks!
-
Draw 'brick' on 'screen' with color yellow[RGB:(255,255,0)]
x: iterate at required with the help of i
y:140
width: 80
height: 30"
Student Additional Activity 2
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)
for i in range(6):
brick=pygame.Rect(10 + i* 100,140,80,30)
pygame.draw.rect(screen,(255,255,0),brick)
pygame.display.flip()
pygame.quit()
Activity | Activity Name | Link |
---|---|---|
TEACHER ACTIVITY 1 | For Loop | |
TEACHER ACTIVITY 2 | For loop for Red Bricks | |
TEACHER ACTIVITY 3 | Red Brick Creation | |
TEACHER ACTIVITY 1 SOLUTION | Solution of TA1 | |
TEACHER ACTIVITY 2 SOLUTION | Solution of TA2 | |
TEACHER ACTIVITY 3 SOLUTION | Solution of TA3 | |
STUDENT ACTIVITY 1 | Orange Brick Creation | |
STUDENT ACTIVITY 2 | Brick and Ball Collision | |
TEACHER REFERENCE: STUDENT ACTIVITY 1 SOLUTION | Solution of SA1 | |
TEACHER REFERENCE: STUDENT ACTIVITY 2 SOLUTION | Solution of SA2 | |
STUDENT ADDITIONAL ACTIVITY 1 | Multiplication Table | |
STUDENT ADDITIONAL ACTIVITY 2 | Yellow Brick Creation | |
TEACHER REFERENCE: STUDENT ADDITIONAL ACTIVITY 1 SOLUTION | Solution of SAA1 | |
TEACHER REFERENCE: STUDENT ADDITIONAL ACTIVITY 2 SOLUTION | Solution of SAA2 |
G11 C4_TeacherCopy
By Sanjukta Bhattacharya
G11 C4_TeacherCopy
- 129