G12_C3

Object Rotation and Event Handling

Activity Flow Slide No. Topic Time
TA 4-7 Warm-up quiz + revision 3 min
8-11 Rotation explaination 5 min
12-26 TA -coding + event Intro 15 min
27-30- SA- coding 10 min
Wrap - Up 31-33 Quiz 2
34-35 Additional Activity-1 5 min
36-38 Additional Activity-2 5 min
Slide No. Topic
12-26 TA-Code
17-26 Event Handling

CLASS STRUCTURE

FOR PREPERATION & REFERENCE

TA: Teacher Activity        SA: Student Activity        SAA: Student Additional Activity

Prerequisites

FOR STUDENTS

  1. Computer with Internet connection.

  2. Latest browser installed.

  3. Spyder IDE 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)

What will be x and y velocity of the car?

xvel=0, yvel=5

xvel=5, yvel=0

Q.1

xvel=0, yvel=0

A

B

D

xvel=5, yvel=5

C

The X-axis is the horizontal axis and the Y-axis is the vertical axis. Here the car is moving only horizontally so yvel will be 0.

X is zero at left and increasing as going to the right side. So xvel of car moving to the right will be positive.

The right answer is option 2

B

xvel=5, yvel=0

Which of these keywords we can use to take a decision in the game?

if

import

Q.2

load

A

B

D

rect

C

The syntax for if is:

if (condition):

  true block

The true block will run if the condition is true.  So we can take the decision of running the code depending upon if the condition is true or not.

A

if

Are you all ready to start today's class?

Let's move ahead

1

2

3

Let's Fix the Paintings

I have some images placed on the wall.

How much should I rotate the spaceship image to make it go up?

How much should I rotate the building image to make it look straight?

Help me rotate these to the correct positions.

How much should I rotate the car image?

Let us see how this object is rotating?

Clockwise

Anticlockwise

These arrows shows clockwise direction

Clockwise is the same direction the hands of clock move

These arrows shows anticlockwise direction

Anticlockwise is the opposite direction the hands of clock move

Spaceship rotation

TA1:

pygame.init()
clock=pygame.time.Clock()
screen = pygame.display.set_mode((400,600))
pygame.display.set_caption("Asteroid")
background_image = pygame.image.load("bg2.jpg").convert()

BLUE=(0,0,255)

player=pygame.Rect(200,200,30,30)

player_image = pygame.image.load("s4.png").convert_alpha()

pygame.display.update()
if enemy.y < -250 or enemy.y > 850:  
    yvel = -1*yvel
    
     screen.blit(newimage ,player)
  
  pygame.draw.rect(screen,WHITE,enemy)

  pygame.display.update()
  clock.tick(30)

Step 1: Upload spaceship/player image

Link of solution :

This loads the spaceship image.

convert_alpha() function converts the screen to the same 'pixel format' as the display.

blit() function displays image

Display spaceship/ player output

enemy

Player

xvel=2
yvel=3

angle=0


change=0


while True:
  screen.blit(background_image,[0,0])

Step 2: Set the variables

The angle is a variable, at the beginning, the spaceship is not moving, since it is zero.

change will denote how much we want to change rotation speed.

 if enemy.y < -250 or enemy.y > 850:  
    yvel = -1*yvel
 
  angle += change
  
   
   
  newimage=pygame.transform.rotate(player_image,angle) 
  
  
  
  screen.blit(newimage ,player)
  
  pygame.draw.rect(screen,WHITE,enemy)

angle is a variable that shows rotation point

Now we are updating the screen with a new image.

Step 3: Use of Transform Function

change is a variable that we are adding in angle variable to find a new rotation axis.


for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()
      sys.exit()
    print(event)

Quit event that occurs on clicking the close button.

print all event data to console

Gives type of event

All events

Output : When you move mouse on screen, you get the following output:

event.type

KEYDOWN

KEYUP

Types of keyboard events

while True:
  screen.blit(background_image,[0,0])
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()
      
      if event.type == pygame.KEYDOWN:
      
        if event.key == pygame.K_LEFT:
        
        
          change =3  
        

if key is pressed down the event is called  KEYDOWN and it will activate keydown event.

If the left arrow key is pressed then this event will activate the left key event.

'change' value of the variable becomes 3 when you press the left arrow key.

Step 4 : Checking events and rotating player

The spaceship continues to move even after the pressed key is released

 

 for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()
      
      
    if event.type == pygame.KEYUP:
    
        
      if event.key ==pygame.K_LEFT or event.key == pygame.K_RIGHT:
      
        
          change= 0
          
          
     if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_LEFT:
        change =3       

Checking if KEYUP event has occurred.

Checking which one out of up and down arrow key triggered the event

or key will check if any of the conditions placed on both sides is true or not.

stop the player from rotating by making change=0

Step 5 : Changes in while loop

Output: Player rotating left on pressing the left arrow key.

B

Task: Add Event on the Right Arrow key so that your image should rotate clockwise.

 

Earlier we created Recttangle of Blue color.First We set Color of Rectangle then we create rectangle and after that we have to update the screen.

 

Hint :

1. Check if right arrow key is pressed.

2. Set change=-3 to rotate it clockwise

3. Following code we used earlier to check if left arrow key is pressed and then rotate the player.

 

 

 

 

 

 

 

Student Activity 1- Link :

Solution : Using right arrow key to rotate the player

 

What is the use of pygame.transform.rotate() function?

To display Image

To change Caption

Q.1

To generate Angle

A

B

D

To rotate Image

C

The transform.rotate() function rotates the image at given angle.

eg:

 pygame.transform.rotate(img, 30)

So option C is correct.

C

To rotate Image

pygame.KEYDOWN

pygame.K_LEFT

Q.2

pygame.KEYUP

A

B

D

pygame.K_RIGHT

C

KEYUP events occur when we release the key. So option D is correct.

KEYDOWN event occurs when we press a key down.

Therefore option A is incorrect.

K_LEFT AND K_RIGHT are the names of the left and right arrow keys. So option B and C are incorrect.

Which event works when the user releases the key?

D

pygame.KEYUP

 Task: Rotate the spaceship using the up and the down arrow keys.

Hint :

1. Use the if condition to check if UP and DOWN arrow keys are pressed.

2. Set the value of change either -3 or +3, inside the if condition to rotate the spaceship.

ADDITIONAL ACTIVITY - 1

if event.type == pygame.KEYUP:
        
      if  event.key == pygame.K_UP or event.key==pygame.K_DOWN :
        change= 0
           
    if event.type == pygame.KEYDOWN:
              
      if event.key == pygame.K_UP:
      
        change=3
        
      if event.key==pygame.K_DOWN:
      
          change=-3

Checking if KEYDOWN event has occurred

Checking which one out of the up and down arrow key triggered the event

ADDITIONAL ACTIVITY - 1 SOLUTION

 Main Activity: Swapping values of two variables 

X=10

Y=20

ADDITIONAL ACTIVITY -2

x,y=10,20

print(x,y)

x,y=y,x

print(x,y)

Output :

20

10

print value of x and y after swapping

Swap value of x and y variable

print value before interchanging (Swapping)

Storing value 10 and 20 in variable x and y

ADDITIONAL ACTIVITY - 2 SOLUTION

Activity NAME Link
Teacher Activity -1 Asteroid2
Teacher Activity-2 TA1- Solution
Teacher Activity-3 SA1-Solution 
Teacher Activity-4 AA1-solution
Student Activity -1 Asteroid1.5

Links Table

Copy of G 12_C03_For_Teacher

By anjali_sharma

Copy of G 12_C03_For_Teacher

  • 80