G12_C5_For_Teacher
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.
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.
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
There was only a single enemy object in the last class. Now, we require many enemy objects (asteroids). These asteroids should appear from a random axis with a random speed.
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
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 values to them individually
B
B
List helps us to organise our data better than variable where multiple related data is to be stored together with unique position
enemy=[enemy1,enemy2,enemy3,enemy4,enemy5]
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)
0
Values
Indexes
1
2
3
4
5
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
items=[]
for i in range(1,11):
items.append(i)
print(items)
Step 1: Add Image for enemy object
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)
Game loop
Remove this line from the code.
Step 2: Create list for enemies
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 3: Append enemies on constant loctation
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.
Game loop
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
Hey, I am getting a single enemy object because each object is overlapped in the same location. How to fix it ?
I have to create these enemies in a random location on the screen. So I have to use the random library.
All these Events have random output
import random
print("The random number is ",random.randint(1,20))
print random integer from 1 to 20.
import random library to use functions of random library
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
But these enemies are not moving. All are static. How to give them velocity so that it can move?
Game loop
xvel=[]
yvel=[]
angle=0
change=0
distance=5
forward=False
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.
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))
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)
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
Increase in i to fetch next index value
Enemies are moving in x-direction but after hitting the boundaries, enemies are not coming back.
Make forward = False when the up arrow key is released.
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)
check the boundaries of the left and right sides.
GREAT!
B
Hint 1: We can update the yvel[ ].
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]
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)
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))
Q.1
The list is a collection of different kinds of values, like integer, string, real. So the C option is incorrect.
Q.2
random function generates any number between a given range. Here it can generate any number between 10 and20. So D option is correct.
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.
Task: Make a list of 10 numbers that store random numbers from 1 to 100.
Output :
Solution:
B
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
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.
Output :
Solution:
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