G12_C4_For Teacher Reference

Flying the spaceship with function

Activity Flow Slide No. Topic Time
TA 4-8 Last class revision+quiz 1 min
9-15 Functions introduction 6 min
16-19 Calculation of new x,y 6 min
21-25 Teacher activity 8 min
SA 26-31 Student activity 8 min
Wrap - Up 32-35 Quiz 5 min
36-40 Additional activity 10 min

Class Structure

Slide No. Topic
13-14 Function intro code
21-25 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 function do we use to rotate the image?

A

pygame.transform.rotate()

pygame.transform.rotate()

screen.flip()

Q.1

A

pygame.rotate()

B

C

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

So option A is correct.

Which of these events will occur if we press a key?

B

Quit

KEYDOWN

MOSE MOVEMENT

Q.2

A

KEYDOWN

B

C

When a key is pressed KEYDOWN event occurs and when the key is released KEYUP event occurs. Therefore option B is correct.

John has moved to new house!

B

John does not like the walls

B

What are functions?

B

painter()

electrician()

Calling

Function

Function

Defining a function

Let's take an example

Definition of function

Function calling and storing returned value in c variable

Teacher Activity - finding the sum of two numbers using function.

Assigning values to variable

printing variable

GREAT!

John is playing football

Guess the location of the ball

y-axis

x-axis

(x, y)

(xnew, ynew)

distance

x' = distance * cos(angle)

y' = distance * sin(angle)

angle

x'

y'

xnew= x +x'

ynew= y+y'

+

+

Can't use player.y=player.y+speed

Use angle and distance to find new x and y

40°

5

x' = distance * cos(angle)

y' = distance * sin(angle)

xnew= x +x'

ynew= y+y'

5

Teacher Activity - player movement

Game loop

include math library to use cos() and sin() functions.

define a function newxy() that takes four variables and returns nx.

define distance=5 this is how much player will move after each frame.

Teacher Activity - player movement

Game loop

Convert the angle from degrees to radians.

This is to be done as the formula as it works with radian angle only.

 

Note: We have written angle+90 as in our game 0 degrees is towards up but in the formula it should be towards the right.

0 degree in game

0 degree in formula

Teacher Activity - player movement

Game loop

Create a variable forward, which will tell if the player should move forward or not.

​Set it to false as at the beginning of the game player is not moving.

Checking if the up arrow key is pressed and then making forward =True, to indicate that the player should move forwards.

Make forward = False when the up arrow key is released.

Checking if forward =True, and then calling the newxy() function to get new value for the player.x

B

Hint: We can update the newxy() function with the y-axis formula as we did for the x-axis.

Task: Change player's y-axis value to make the player move in y-axis.

y' = distance * sin(angle)

ynew= y+y'

Student Activity-1

Return and use the new y position as well

calculate the new y position by using the formula.

Code for generating new value of y and returning it.

Coordinate system in formula

Coordinate system in game

x-axis

y-axis

(0,0)

(0,0)

Different coordinate system

ynew= y - y'

Instead of adding y', we will subtract it to compensate for the direction of the y-axis.

Which of these is a right way to define a function?

def name():

      body of function

Q.1

A

C

B

D

Syntax for defining

a function is:

def function_name():

function body

C

def name:

      body of function

def():

     body of function

function name():

      body of function

Which mathematical function can help in finding the x-axis location if angle and distance travelled by the object is given?

cos()

sin()

tan()

Q.2

None

A

C

B

D

 

Formula to find new x is:

 

newx =oldx+(distance*cos(angle))

A

B

Hint:

1. Check if the player reached the left/right edge by checking if the x-axis value is 0 or 400

 

2. Make player.x=400 when it reaches left edge and player.x=0 when it reaches the right edge.

Task: Make the player spaceship move to alternate edges when it goes from the left or right edge.

ADDITIONAL ACTIVITY - 1

Solution:

B

Task: Make the player spaceship move to alternate edges when it goes from the top or bottom edge.

Hint:

1. Check if the player reached the top/bottom edge by checking if the y-axis value is 0 or 600

 

2. Make player.y=600 when it reaches top edge and player.x=0 when it reaches bottom edge.

ADDITIONAL ACTIVITY - 2

Solution:

Activity Activity Name Link
Teacher Activity 1 Asteroid3
Teacher Activity 2 Asteroid 4 -final solution after the class
Teacher Activity 3 AA-1 Solution
Teacher Activity 4 AA-2 Solution
Student Activity 1 Asteroid3.5

Links Table

Copy of G12_C4_For_Teacher

By anjali_sharma

Copy of G12_C4_For_Teacher

  • 92