G12_C5_For_Teacher

Asteroid Creation

* 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

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

What will be the color of the below code? (0, 225, 0)

Red

Green

Q.2

A

B

Blue

C

(Red, Green, Blue)
(0, 225, 0)

B

Green

Here, red and blue are given '0' value, so the only color left is green.

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

B

B

C

D

import math

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

How to Create many variables?

  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.

John moved into a new study room!

B

book shelf

Collection of crayon

almirah

list

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

Creating list in python

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

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.

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

GREAT!

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?

Can contain only one datatype values

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

can be created empty

It can store String values

 

It can store integral values

What would be output of following code segment:

import random

print(random.randint(10,20))

15

10

18

Q.2

Any value between 10 to 20

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

Copy of G12_C5_New_For_Teacher

By anjali_sharma

Copy of G12_C5_New_For_Teacher

  • 101