G12_C5_For_Teacher

Asteroid Creation

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

Class Structure

Slide No. Topic
23-25 Function intro code
31-32 TA- Code

Preparation and Reference

Prerequisites

FOR STUDENTS

  1. Computer with Internet connection.

  2. Spyder IDE installed. 

  3. "pygame" package installed.

FOR TEACHER

  1. Computer with Internet connection.

  2. Latest browser installed.

  3. "pygame" package installed.

  4. Spyder IDE installed.

What we did in the last class?

(WARM-UP QUIZ)

Which syntax is correct to define function in python?

def fun():

     Body

int fun()  

{    Body    }

Q.1

define fun()

{    Body    }

def keyword is use to define function in python.

So option A is correct.

fun() :

      Body

C

B

A

A

D

def fun():

     Body

Which syntax is correct to access math library?

include MATH

import mathfunc

Q.2

import math

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.

add mathematics

A

C

D

import math

B

B

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

A Revision Tour to the last class

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?

How to create many variable?

  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

John moved into a new study room!

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)

Creating list in python

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

Add Element in List

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

Teacher Activity -Appending items in list

Task: Append 10 numbers in list and print output.

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.

Teacher Activity -Appending items in list

Solution link: https://bit.ly/3qmuU7o

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.

 

 

Output

Random Example

 All these Events have random output

In Python, we use random.randint() to find random integer numbers.

import random


print("The random number is ",random.randint(1,20))

Example of Random library

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

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

xvel=[]
yvel=[]

angle=0
change=0
distance=5
forward=False

Step 6 : Create list for velocity of enemies on the x and y axis

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))

Step 7 : Append random velocity in x-axis

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

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

Step 9 : Add velocity on x-axis

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.

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

check the boundaries of the left and right sides.

Output

GREAT!

B

Student Activity: https://bit.ly/3qmuU7o

Task: Change the enemy's y-axis value to make the enemies move vertically.

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]

Output of Student Activity

Student Activity - https://bit.ly/2SYJKEU/strong

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))

Required output

Can contain only one datatype values

Q.1

can be created empty

It can store String values

 

It can store integral values

C

D

Which of the following is incorrect about list?

It can store String values

 

The list is a collection of different kinds of values, like integer, string, real. So the C option is incorrect.

A

B

B

What would be output of following code segment:import randomprint (random.randint(10,20))

15

10

18

Q.2

Any value between 10 to 20

C

D

A

D

B

Any value between 10 to 20

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.

ADDITIONAL ACTIVITY - 1

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

ADDITIONAL ACTIVITY - 2

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

Copy of G12_C5_New_For_Teacher

By anjali_sharma

Copy of G12_C5_New_For_Teacher

  • 87