G11_C5
Activity Flow | Slide No. | Topic | Time |
---|---|---|---|
TA | 3 | Ice-breaker | 1 min |
5-10 | Programming Intro | 2 min | |
11-16 | Object Oriented Programming | 3 min | |
17-22 | Introduction to Python + Spyder + Pygame | 3 min | |
23-28 | TA - Coding | 15 | |
SA | 29-31 | SA | 5 min |
Wrap - Up | 32-36 | Quiz | 1 min |
38 | Additional Activity | 5 min |
Slide No. | Topic |
---|---|
11 | Random number code |
23-25 | Basic pygame code |
31-32 | Making rectangles |
36 | SA Code- Creating enemy rect. |
45 | Additional Activity- Code |
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.
x=5
for i in range(3):
x*=5
print(x)
625
25
125
625
125
loop begins
x=5
loop ends
printed
printed
printed
loop no. | i is: | x becomes: |
---|---|---|
1 | 0 | 5*5=25 |
2 | 1 | 25*5=125 |
3 | 2 | 125*5=625 |
5
x=5
for i in range(3):
x*=5
print(x)
625
125
loop begins
x=5
loop ends
x=625
(printed)
loop no. | i is: | x becomes: |
---|---|---|
1 | 0 | 5*5=25 |
2 | 1 | 25*5=125 |
3 | 2 | 125*5=625 |
B
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()
carryOn = True
while carryOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
carryOn = False
pygame.quit()
carryOn = True
while carryOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
carryOn = False
pygame.quit()
Is friend busy?
YES
NO! She is coming this way!!
Let's keep decorating
colon
tab
space
maria_busy=True
while maria_busy:
print("Keep decorating!")
maria_busy=False
print("Get ready to surprise!")
maria_busy=True
while maria_busy:
print("Keep decorating!")
maria_busy=False
print("Get ready to surprise!")
carryOn = True
while carryOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
carryOn = False
pygame.quit()
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 |
i=0
while i<5:
print(i)
i=i+1
carryOn = True
while carryOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
carryOn = False
pygame.quit()
!!
Let me press a key!!
Wow!! the paddle moved!
Key is pressed
Mouse is clicked
Perform some tasks
Perform some tasks
How does the computer understand user has clicked / pressed something?
Queue of events
Event 1
Event 2
Event 3
.
.
.
But how do i know what exactly the user did??
Queue of events
for event in pygame.event.get()
Event 1
Event 2
Event 3
.
.
.
carryOn = True
while carryOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
carryOn = False
pygame.quit()
Now I am an expert!
Let us begin creating the game from scratch now!
Hints:
import pygame
pygame.init()
screen = pygame.display.set_mode((600,600))
paddle=pygame.Rect(300,500,60,10)
ball=pygame.Rect(200,250,10,10)
ballx=1
bally=1
paddlex=2
Let create the game loop now!
Hints:
import pygame
pygame.init()
screen = pygame.display.set_mode((600,600))
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
pygame.quit()
Hints:
Now fill the screen with a color of your choice
import pygame
pygame.init()
screen = pygame.display.set_mode((600,600))
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(0,176,240)
x=10
while False:
x=x+100
print(x)
10
110
Error
While loop set to false by default
x=10
10 is printed
No Change in x
x=5
i=0
while i<3:
x*=5
print(x)
i=i+1
625
25
125
625
125
loop begins
x=5
loop ends
printed
printed
printed
loop no. | i is: | x becomes: |
---|---|---|
1 | 0 | 5*5=25 |
2 | 0+1=1 | 25*5=125 |
3 | 1+1=2 | 125*5=625 |
Hints:
Let's create a game
Activity | Activity Name | Link |
---|---|---|
Teacher Activity 1 | Space Invader game demo | https://replit.com/@ShubhamVerma9/DemoSpaceInvaders#main.py |
Teacher Activity 2 | Space Invader 0.5 |
https://bit.ly/3uePVRU |
Student Activity 1 | Space Invader -0 |
https://bit.ly/3va7RhO |
Additional Activity 1- Solution | Space Invader 0.5 + AA |
https://bit.ly/2SeaAZ2 |