Comparison

data

Datatype: BOOLEAN (bool)

Syntax:

 

False

Example:

indian=True

Boolean

True

Fractional

data

Datatype: FLOATING POINT (float)

Syntax:

Nothing specific

Example:

weight=72.5

Floating point

Numeric data

Datatype: INTEGER (int)

Syntax:

Nothing specific

Example:

age=68

Integer

Text-based data

Datatype: STRING (str)

Syntax:

Place in

" " (or) ' '

Example:

name="David"

String

23

Are all data same?

name="David"
age=68
print(name+age)
name="David"
age=68
age=str(age)
print(name+age)
name="David"
name=int(name)
age=68
print(name+age)
id="5"
id=int(id)
age=68
print(name+age)

Step 1 : Add name and age variable without type conversion. This will throw an error.

Step 2 : Convert age variable to string. Add name and age variable. This will print "David68".

Step 3 : Convert name variable to int. Add name and age variable. This will throw an error. String to be converted to numeric has to be compatible.

Step 4: Create an "id" variable and assign a numeric value as a string. Convert this to integer and add to age. This will print 73.

Teacher Activity 1

Detecting Various Keyboard Inputs

event.key:

K_RETURN

K_UP

K_LEFT

K_DOWN

K_RIGHT

event.type:

KEYDOWN

Navigate Car

Accelerate Car

Key is pressed

Mouse is clicked

Perform some tasks

Perform some tasks

pygame.event

Events in pygame

Movement of car

    #Create a variable to store absolute path of car image on your computer
    carImg_location="C:/Users/dell/Documents/img/car.png"
    #Load the car image and use convert_alpha() to convert to compatible pixel format
    carImg=pygame.image.load(carImg_location).convert_alpha()
    #Display the car image at location (130,500)
    screen.blit(carImg,[130,500])

Student Activity 1: Solution

    #Change location of background image according to the absolute path you have got in your computer
    bgImg_location= "C:/Users/dell/Documents/img/back_ground.jpg"             

1

2

Student Activity 1: Code Addition

1. Get car image location.

1

12 bgImg_location=""

Insert background image location.

2. Load image and convert.

pygame.image.load(location)
convert_alpha()

3. Display car image.

screen.blit(image,location)

2

bgImg=pygame.image.load("Absolute Path").convert_alpha()

Teacher Activity 2: Coding

bgImg_scaled=pygame.transform.smoothscale(bgImg,(600,600))
screen.blit(bgImg_scaled,[0,0])
pygame.display.flip()

1. Load the image using the "image.load()" function of the "pygame module". Pass the absolute path you had copied in the form of a string as a parameter to this function. The "convert_alpha()" function helps convert the image loaded into a pixel format that favors fast display/ blitting.

2. This "transform.smoothscale()" function helps to smoothly scale an image to the desired size. The function takes 2 parameters: the image to be scaled and the size to which we wish to scale (here, the size of our screen)

3. The "blit()" function helps display the image at the desired location. It will take 2 parameters: the object to be displayed and its location (here, at (0,0))

4. The "display.flip()" functions updates the screen display and allows us to visualize changes we make to the screen display.

Game Start

End of one game loop iteration

Measure time

"gameTime" variable

Select font

Select content

Display

concatenate

Hints

Displaying Game Time

Template:

Student Activity 1

Try to code to be able to display game time elapsed on the screen.

Time elapsed: XXX seconds

Student Activity 1 Solution

import pygame
import time
pygame.init() 
t1=time.time()

Step 1: Import time module

Step 2: Create t1 as soon as the game begins

import pygame
import time
screen.blit(carImg,[carx,cary])
t2=time.time()

Step 3: Create t2 all major events of the loop finished and final car image is on screen.

Step4: Create "gametime" variable to store t2-t1

game_time=t2-t1

Step5: Display "gametime" variable on screen

font = pygame.font.Font(None, 24)
text = font.render("TIME ELAPSED: " + str(game_time)+"seconds", 1, (255,255,255))
screen.blit(text, (20,10))

Teacher Activity 3

#Check if Key is pressed
if event.type==pygame.KEYDOWN:
  #Check if key pressed is "Enter"
        if event.key==pygame.K_RETURN:
        #Check if game time is within threshold
            if game_time>=threshold and game_time<=(threshold+10):
                    #Decrement "cary" to make car move forward
                    cary-=100 
                    #Increment "threshold" by 10
                    threshold+=10
threshold=0

Step 1: Create a variable "threshold" outside game loop and set it to 0 to begin.

Step 2: Perform necessary steps to achieve goal

Setting boosting restrictions

10 second intervals

0

10

20

30

...

1 use of boost allowed

t< gameTime <(t+10)

Check

??

Allow Boost

Increase threshold by 10

YES

Lower threshold

(t)

Upper

 threshold

(t+10)

Lower threshold

(t)

Upper

 threshold

(t+10)

Lower threshold

(t)

Upper

 threshold

(t+10)

Now, the boost restrictions!

Simultaneous multiple condition checks

and

t

t+10

Game Time

<

<

gameTime>t
gameTime<(t+10)

True

True

True

False

False

False

True

False

True

False

How do I check multiple conditions?

Teacher Activity 2

import pygame
import time
pygame.init() 
t1=time.time()

Step 1: Import time module

Step 2: Create t1 as soon as the game begins

import pygame
import time
screen.blit(carImg,[carx,cary])
t2=time.time()

Step 3: Create t2 all major events of the loop finished and final car image is on screen.

Step4: Create "gametime" variable to store t2-t1

game_time=t2-t1

Teacher Activity 1

import time
t1=time.time()
time.sleep(5)
t2=time.time()
print(t2-t1)

Step 1: Import time module

import time

Step 2: Record and print a time point.

Note: Large output as it is total elapsed time since Jan 1,1970 till now in "seconds".

import time
t1=time.time()
print(t1)

Step 3: Introduce a delay and record timepoints before and after the delay. Subtract to show time elapsed is equal to delay.

Sample Slides

By anjali_sharma