G12_C2_For Teacher Reference

Variables and Conditional Programming

True

False

Activity Flow Slide no Topic Time
TA 4 Last class revision + quiz 1 min
5-12 Class Introduction 2 mins
11-16 Variable activity on console 3 mins
17-22 Game loop 3 mins
23-28 TA 15 mins
SA 29-31 SA 5 mins
Wrap-Up 32-36 Quiz 1mins
38 Additional Activity 5 mins

CLASS STRUCTURE

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

Slide no Topic
23-25 Basic pygame code
31-32 Making rectangles
36 SA Code - Creating enemy rect.
45 Additional Activity - Code

FOR PREPERATION & 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 of these we can use to add pygame library to our code?

use pygame

import pygame

Q.1

A

B

initialize pygame

C

import statements are used to add any library to

your code.

B

import pygame

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.

Some interesting things to know

1. How computer can remember everything?

2. How flipbook, cricket game on TV and our game are related?

3. How ATM helps computer to take decision?

How much computer can remember?

B

Program's data is stored in memory

B

B

We store food in lunchboxes

B

name

color

age

String type <----- type(                    )

Programs store data in Variables

Teacher Activity - Variables and operations in console

Addition of two numbers

Changing sign of a number

Student Activity - Multiplication

Multiplying two numbers

GREAT!

Have you seen a flipbook?

Adding movement to the asteroid

import pygame, random, sys

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

BLUE=(0,0,255)
player=pygame.Rect(200,200,30,30)

WHITE=(255,255,255)
enemy=pygame.Rect(100,100,30,30)

while True:
  screen.blit(background_image,[0,0])
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()
      
  
  pygame.draw.rect(screen,BLUE,player)
  pygame.draw.rect(screen,WHITE,enemy)

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

Teacher Activity - Game Loop code

Game loop

1. Define a variable to hold velocity in x-axis

2. Update x-axis value to move astroid/enemy to left

Output:

Can ATMs dispense cash without
verifying the ATM password?

Condition: If password is correct?

If ( condition )

Dispense cash

No cash

No

Yes

Conditional Programming -
"if" instruction block

DO THAT

If (Condition):

Do this task

TRUE

DO THIS

FALSE

B

0

400

x

600

-250

Changing velocity to change direction

enemy.x =enemy.x + xvel

B

Game loop

CHANGING velocity to CHANGE direction

Change velocity when reading left edge

Change velocity when reaching right edge

B

Using "or" as task done for both condition is same

Use "or" to combine both if conditions as we have to change xvel in both.

Output:

B

Hint: We can update both x and y axis values to make asteroid move in diagonal.

Task: Make the asteroid move diagonally.

Code for making enemy move in y-axis

1. Define a variable to hold velocity in x-axis

Update Y-axis value by adding yvel

Hint: We can change yvel (as we did for x-axis) also to make asteroid move back and forth on y-axis.

Task: Make Astriod move back after reaching 850 or -250 on Y-axis

Which of these can help in storing the data in a program.

rect

event

Q.1

variable

A

B

D

blit

C

Variable can store data as follows:

marks =79

D

variable

Which out of these are correct velocities for moving diagonally?

xvel=3, yvel=0

xvel=0, yvel=3

Q.2

xvel=0, yvel=0

A

B

D

xvel=3, yvel=3

C

For moving diagonally, object should move in both x and y axis. So it requires some velocity in both directions to move diagonally.

C

xvel=3, yvel=3

ADDITIONAL ACTIVITY - 1

The "type()" function

Step 1: Write code in console:

Step 2: Click on RUN

Step 3: See Console for Output

x=10
y=20
type(x)
type(y)

ADDITIONAL ACTIVITY - 2

The type conversation function

Step 1: Open a new file

Step 2: Write code:

Step 3: Click on RUN

Step 4: See Console for Output

y="20"
y = int(y)
type(y)

Links Table

ACTIVITY  ACTIVITY NAME LINKS
Teacher Activity 1 Asteroids 1.1
Teacher Activity 2 Asteroids 2
Student Activity 1 Asteroids 1.5

Copy of G12_C2_For_Teacher

By anjali_sharma

Copy of G12_C2_For_Teacher

  • 96