G12_C5_For_Teacher
* Note to Illustrator***
1. Similar gif where the asteroid
should move.
***Note to the illustrator***
Global: Please ensure the consistency for image size, text box, code snippets, text, and font size are maintained throughout. Please ensure the illustrator implements that.
Activity Flow | Slide No. | Topic | Time |
---|---|---|---|
TA | Last class revision+quiz | 1 min | |
Functions introduction | 6 min | ||
Calculation of new x,y | 6 min | ||
Teacher activity | 8 min | ||
SA | Student activity | 8 min | |
Wrap - Up | Quiz | 5 min | |
Additional activity |
Slide No. | Topic |
---|---|
23-25 | Function intro code |
31-32 | TA- Code |
def keyword is use to define function in python.
So option A is correct.
Here, red and blue are given '0' value, so the only color left is green.
import math is the correct way. math library is used to access functions of maths. For example, sqrt(),sin(),cos().
Therefore option B is correct.
There was only a single enemy object in the last class.
Now, we require many enemy objects (asteroids). These asteroid should appear from a random axis and random speed.
A Revision Tour to the last class
xvel=2
yvel=3
enemy.x=enemy.x + xvel
enemy.y=enemy.y + yvel
if enemy.x < -250 or enemy.x > 650 :
xvel = -1*xvel
if enemy.y < -250 or enemy.y > 850:
yvel = -1*yvel
xvel_1=2
yvel_1=3
xvel_2=4
yvel_2=5
xvel_3=6
yvel_3=7
xvel_4=5
yvel_4=4
xvel_5=8
yvel_5=9
xvel_6=2
yvel_6=5
This is one approach to create many objects and assign value to them individually
Is it not tedious to create a new variable every time for a new enemy character?
we will follow a smart python programmer's approach to create a list instead of a variables to store multiple information in 1 place.
what is a list and how do we create it
let me give you an example of list
B
***Note to illustrator*** 1. We need to show a boy trying to keep his study items like book, stationary box, pen stand, then again one more book in 1 square shaped container. 2.few moments later the boy goes back to find stationary box from the container and he finds all messy and empties the entire box to find it. 3. Her mother then comes to him and tells her that "I have created a shelf to place all your items sequentially where each section has a number written on it 1,2,3,4,5. She places all the above objects sequentially. She tells "With this container it can store multiple items of any type and they are identified easily bcoz they are placed in a separate section with an index no. " 4. The boy becomes happy and takes an object from the shelf positioned at index 3.
B
book shelf
Collection of crayon
almirah
list
Lists are used to store multiple items in a single variable. Each item of the list is stored at a unique index number
value=['red','green','blue','yellow','white','black']
print(value)
values
indexes
value is a list, holding the name of the colors as the list of items
append is the function to store value at the last location
value=['red','green','blue','yellow','white','black']
print(value)
value.append('Grey')
value.append('cyan')
Indexes
Values
0
1
2
3
4
5
6
7
Create an empty list
Run the loop from 1 to 10. Range function takes 2 arguments. It starts from the given value (First argument) and ends with stop -1(stop is the second argument).
append value of i in items list
print item list
value is a list, holding the name of the colors as the list of items
items=[]
for i in range(1,11):
items.append(i)
print(items)
Same images
Game loop
earlier we were using rectangle as enemy image, Now add image of enemy.
player=pygame.Rect(200,200,30,30)
player_image = pygame.image.load("s4.png").convert_alpha()
#adding enemy image
WHITE=(255,255,255)
enemy=pygame.Rect(100,100,30,30)
enemy_image = pygame.image.load("e3.png").convert_alpha()
Remove this line from the code because we want to add an image of the enemy
if enemy.y < -250 or enemy.y > 850:
yvel = -1*yvel
screen.blit(newimg , player)
pygame.draw.rect(screen,WHITE,enemy)
pygame.display.update()
clock.tick(30)
Remove this line from the code.
Step 1: Add Image for enemy object
We are creating 10 enemies. So the enemycount is a list of 10.
enemies is a blank list. It will help to create enemies randomly.
angle=0
change=0
distance=5
forward=False
enemycount=10
enemies=[]
Step 2: Create list for enemies
Game loop
angle=0
change=0
distance=5
forward=False
enemycount=10
enemies=[]
for i in range(0,enemycount):
enemies.append(pygame.Rect(100,200,20,20))
Run for loop from 0 to
enemycount.
append rectangle at the given axis
to the
enemies list.
Step 3: Append enemies on constant loctation
Game loop
if event.key ==pygame.K_RIGHT:
change = -6
if event.key == pygame.K_UP:
forward = True
for enemy in enemies:
screen.blit(enemy_image,enemy)
if forward:
player.x, player.y=newxy(player.x, player.y, distance, angle)
Run the for loop . This loop will blit the enemies on screen.
Step 4: Blit enemies on screen
Ouput
Hey, I am getting a single enemy object
because each object is overlapped in the same location.
I have to create these enemies in a random location on the screen.
So I have to use the random library.
***Note to the illustrator ***
Create talk bubble for both the above statements
Random Example
In Python, we use random.randint() to find random integer numbers.
All these Events have
random output
*** Note to illustrator ***
Add this comment in talk bubble
Example of Random library
import random
print("The random number is ",random.randint(1,20))
import random library to use
functions of random library
print random integer from 1 to 20.
Game loop
import randome # at the first line of game
angle=0
change=0
distance=5
forward=False
enemycount=10
enemies=[]
for i in range(0,enemycount):
enemies.append(pygame.Rect(random.randint(0,400),random.randint(0,600),20,20))
Run for loop from 1 to
eenemycount.
append rectangle of random
x and y locations to the
enemies list. Before using this add
random library at beginning of
program
Step 5 : Append enemies on random location
But these enemies are not moving.
All are static. How to give them
velocity so that it can move?
Game loop
Same images
in the last class, we mentioned the value of xvel =2 and yvel=3 because there was only one enemy. In this class, we want to create many enemies and these enemies should move randomly. So we are using xvel and yvel as list.
xvel=[]
yvel=[]
angle=0
change=0
distance=5
forward=False
Step 6 : Create list for velocity of enemies on the x and y axis
Game loop
xvel list appends random integer between -3 to 3.
import randome # at the first line of game
angle=0
change=0
distance=5
forward=False
enemycount=10
enemies=[]
for i in range(0,enemycount):
enemies.append(pygame.Rect(random.randint(0,400),random.randint(0,600),20,20))
xvel.append(random.randint(-3,3))
Step 7 : Append random velocity in x-axis
Same images
Since we want to add
velocity for each enemy, cut this code from previous code.
angle += change
newimg=pygame.transform.rotate(player_image,angle)
enemy.x=enemy.x + xvel
enemy.y=enemy.y + yvel
if enemy.x < -250 or enemy.x > 650 :
xvel = -1*xvel
if enemy.y < -250 or enemy.y > 850:
yvel = -1*yvel
screen.blit(newimg , player)
Step 8 : Add velocity on x-axis
Same images
i variable works as index here. We will use this index to fetch value from xvel[] and assign to enemy
if event.key == pygame.K_UP:
forward = True
i=0
for enemy in enemies:
enemy.x=enemy.x+xvel[i]
i+=1
screen.blit(enemy_image,enemy)
angle += change
newimg=pygame.transform.rotate(player_image,angle)
Assign velocity to enemy in x-direction
Step 9 : Add velocity on x-axis
Increase in i to fetch next
index value
Enemies are moving in x-direction but after hitting the boundaries, enemies are not coming back.
Moving Enemies object in x-axis.
Make forward = False when the up arrow key is released.
Same images
check the boundaries of the left and right sides.
if enemies cross the boundaries of x-direction then change the direction.
for enemy in enemies:
enemy.x+=xvel[i]
if enemy.x < -250 or enemy.x > 650:
xvel[i] = -1*xvel[i]
i+=1
screen.blit(enemy_image,enemy)
Step 11: Check Boundries of enemies in x-axis
Output
Same images
Same images
B
Hint 1: We can update the yvel[ ].
Task: Change the enemy's y-axis value to make the enemies move vertically.
Same images
Hint 2: Add velocity for y-direction.
Hint 3 : Check boundries for y-direction.
for i in range(1,enemycount):
enemies.append(pygame.Rect(random.randint(0,400),random.randint(0,600),20,20))
xvel.append(random.randint(-3,3))
if enemy.x < -250 or enemy.x > 650:
xvel[i] = -1*xvel[i]
for enemy in enemies:
enemy.x+=xvel[i]
Output of Student Activity
Same images
for i in range(1,enemycount):
enemies.append(pygame.Rect(random.randint(0,400),random.randint(0,600),20,20))
evlx.append(random.randint(-3,3))
evly.append(random.randint(-3,3))
i=0
for enemy in enemies:
enemy.x+=evlx[i]
enemy.y+=evly[i]
if enemy.x < -250 or enemy.x > 650:
evlx[i] = -1*evlx[i]
if enemy.y < -250 or enemy.y > 850:
evly[i] = -1*evly[i]
i+=1
screen.blit(enemy_image,enemy)
Required output
Same images
B
Hint:
1. Create an empty list.
2. Apply for loop up to 10 times
and generate number through random function.
3. Append these number in list.
Additional Activity-1
Task: Make a list of 10 numbers that store random numbers from 1 to 100.
Same images
Output :
Solution:
B
Hint:
1.Crate a list and add items using range ()
2. Apply list method like pop() and remove() to delete method from list.
3. print output after each deletion.
Additional Activity-2
Task: Crate a list of 10 items using range().
Remove elements from list as following requirement.
1. remove from the desired location
2. remove element from the range of locations
Same images
Output :
Solution:
Same images
Which of the following is incorrect about list?
Q.1
A
C
B
D
The list is a collection of different kinds of values, like integer, string, real. So the C option is incorrect.
C
What would be output of following code segment:
import random
print(random.randint(10,20))
Q.2
A
C
B
D
random function generates any
number between a given range.
Here it can generate any number between 10 and20. So D option is correct.
A
Same images
Activity | Activity Name | Link |
---|---|---|
Teacher Activity 1 | Asteroid3 | |
Teacher Activity 2 | enemy creation-final solution after the class | |
Teacher Activity 3 | Solution of Student Activity | |
Student Activity 1 | asteroid creation | |
Additional Activity-1 | List of 10 number | |
Additional Activity-2 | List operation | |
Links Table