G12_C8_For_Teacher

Collosion of Enemy and Player

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 so far

C1-Introduction to python programming

i. Adding Background

ii. Rectangle Creation

C2-Variable and Conditional Programming

i. Game Loop

ii. if condition

 

What we did so far

C3-  Object rotation and Event Handling

i. Rotation of Spaceship

ii. Event handling on Keyboard

C4- Flying the spaceship with function

i. Function Creation

ii. Angle rotation of Space ship

What we did so far

C5-Asteroid Creation

i.List

ii.random

 

C6- Bullet Firing

i. Ready and Fire State

ii.Angle movement of the bullet

What we did in the last class?

Same images

(WARM-UP QUIZ)

Same images

import random
mylist = ["apple", "banana", "cherry"]
print(random.choice(mylist))

What would be output of following code?

A

Any one item

apple,banana,cherry 

 

Q.1

A

any two item

B

C

random.choice() print a single and random selected value from given list. So correct answer is A

D

None of the above

 

if two rectangles are moving in following axis, would it collision?

a = pygame.Rect((1, 1), (4, 6))
b = pygame.Rect((0, 0), (6, 4))

B

Yes

May be

Q.2

A

No

B

C

For collision, axis must be same of object. in given scenario, axis is not same of both object. So answer is B.

D

Depends on moving speed

Same images

Game Feature to be added

In previous class, player/spaceship destroyed the asteroids/enemies using bullet.

In this class we are going to learn

1. Collision of Enemy and Player

2. Create a score when bullet hits the enemy

 

John went to watch football match with his friends. He saw scoreboard there. At the beginning of match there was score 0, but as a team make a goal, score increase by .1

In the same manner when a bullet hit the  enemy, we increase the score

GREAT!

Same images

Same images

Teacher Activity

Step 1 - Create variables for score and font

 

Same images

evlx=[]
evly=[]

enemySpeed=1

score=0

game_font=pygame.font.Font('freesansbold.ttf', 12)

for i in range(1,enemycount):

Declare a variable score

and set it with 0

Set the font type to show the score.

i+=1     
    if bullet.colliderect(enemy):
      enemy.y=random.choice([random.randint(-250,0),random.randint(600,840)])
      enemy.x=random.choice([random.randint(-250,0),random.randint(400,640)])
      
      score+=1
      
    screen.blit(enemy_image,enemy)
 screen.blit(newimg , player)
  
  scoretext=game_font.render("Score : " + str(score),False,(200,200,200))
  screen.blit(scoretext,(10,10))
  
  pygame.display.update()

Increase in score by one when bullet hit the enemy

Render this score on screen and blit it.

Step 2 -  Display Score

Output

When Game should be over

Answer: When Enemy hit the spaceship/player

score=0

over=False

game_font=pygame.font.Font('freesansbold.ttf', 12)

game_over=pygame.font.Font('freesansbold.ttf', 30)

bullet=pygame.Rect(200,200,5,5)

Declare a variable over which will help us to check end of the game

Declare variable which will display the message the game is over.

Step 3 - Declare Over variable and Font

for enemy in enemies:
    enemy.x+=evlx[i]
    enemy.y+=evly[i]
    
    if enemy.colliderect(player):
      
      over=True 
      
     player_image=enemy_image
     
    if enemy.x < -250 or enemy.x > 650 or enemy.y < -250 or enemy.y > 850:  
      evlx[i] = -1*evlx[i]

Check if enemy hit the player

Change status of over variable to True

After collosion of player and enemey, player should destroy. So in place of player imager, set image of enemy

Step 4 - Enemy hit the Spaceship

 angle += change
  newimg=pygame.transform.rotate(player_image,angle)  
  
  if over==True:
  
      gameovertext=game_over.render("GAME OVER!",False,(100,200,100))
      
      screen.blit(gameovertext,(100,250)) 
  
  screen.blit(newimg , player)

If over variable is true that means enemy hits the player

print game over message and store it in gameovertext variable

blit the gameovertext variable on screen

Step 5 - Over the Game

Output

Same images

https://bit.ly/3hDg9sV

B

Task: in class you learned when enemy hit the player, player remove from screen. Modify this game that when enemy hit the player, all enemies should remove from space.

Same images

Same images

 i+=1    
    if bullet.colliderect(enemy):
        
        enemy.x=1000
        enemy.y=1000
        
      

Lists can not update

Size of the lists must be specified before its initialization

Q.1

The first item of list store on 0 index.

A

B

D

We can not delete items from lists.

C

The list always start from 0 index. So first item store on 0 index. Correct answer is D

 

1

Q

Which of the following is True regarding lists in Python?

Two rectangles are moving in a x-y plane. A rectangle has (6,8,10,20) coordinates and B rectangle has (6,8,,5,8) coordinate. If we use A.colliderect(B), What would be output?

 

A and B object will  collide.

A and B will  not Collide

 Only corner of rectange will touch, not collision there.

Depends on their velocity

A

B

C

D

Q 2

A and B are moving on same axis. A is on 6,8 coordinate and B is on 6,8 coordinate. So they will  collide. Correct answer is A

B

Additional Activity-1

Task: A python learner created a code to find x^y. But he done some mistake in his code. Help him to correct this code by filling correct code in blank space. 

Same images

Hint 1 : Passing argument 

Hint 2 : Receiving argument

Hint 3 : Return the variable

Additional Activity-1 Solution

Output

B

Additional Activity-2

Task: Count that how many enemies are destroyed by spaceship.

Same images

Output :

 

Hint:

1.initialize a variable named score with 0.

2.Write your code in given line.

Additional Activity-2 Solution

Output

Same images

Activity Activity Name Link
Teacher Activity 1 Bullet hitting Enemy
Teacher Activity 2 Score Display and Enemy hit the Spaceship
Teacher Activity 3 Remove Enemies
Student Template Bullet hitting Enemy
AA1 Function of pow
AA2 print Score

Links Table

https://bit.ly/361Zqu7

Copy of Copy of G12_C8_New_For_Teacher

By anjali_sharma

Copy of Copy of G12_C8_New_For_Teacher

  • 121