Teaching Python to 9 Year Old Girl - map mover

Preview:

Citation preview

Girl Coder

Map Mover

Blank Screen1) import pygame and sys libraries2) initialize pygame (hint: pygame. )3) set size of main game screen to 480 by 320 (hint: size = )4) load screen display (hint: screen = pygame.display. )5) while loop (tip: indent everything under this)6) for event loop (tip: pygame.event.get())7) check for game quit. (hint: if event.type == )8) quit pygame9) exit system

10) update display (hint: pygame.display. )

Map Load

Above while loop insert the line belowChange map_name to the actual name of your map file.map = pygame.image.load("map_name.png")

Map Show

In while loopIndent one tab and insert the code in the pink box immediately above the line pygame.display.update()

screen.blit(map, (0, 0))

Set Map x and y Variables

Above while loopmap_x = 0

map_y = 0

Use Variables for Map Position

In while loop, edit map blit statement

old: screen.blit(map, (0, 0))

new: screen.blit(map, (map_x, map_y))

Replace the line above with the line below.

Move Map

In while loop add the code in the pink box to move the map to the left, making the girl appear to move the right.Place below for loop code block

map_x = map_x - 1

Create Variable for Direction

Insert line below above while loopdirection = “right”

If Direction and Move

In while loopIndent one tab and insert the two lines in the pink box

below the for loop code block - below sys.exit()

if direction == “right”:

map_x = map_x -1

if Statements for all 4 Directions

if direction == “right”:

map_x = map_x -1

Use the two lines below as an example and create if statements for left, up, and down. Test the game after you create each direction by changing the direction variable near the top of your program code.

direction = “right”

In your code, edit the line below to test each direction.

Keyboard Input - DownIn while loop, find for event loop. It looks like the code in the blue box. Once you find it, put the code in the pink box below it with one indent past the for.

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_DOWN:

direction = "down"

for event in pygame.event.get():

Test Keyboard Input

direction = “up”

At the top of your code, set the initial direction to “up”

Run the game. After the map starts moving, press the down arrow on your keyboard.

4 Direction Keyboard Input

Using the two lines of code below as an example, create if statements for up, right, and left. if event.key == pygame.K_DOWN:

direction = "down"

Test your program after you set each direction.

Girl Load

Above while loop, insert the line below

girl= pygame.image.load("girl.png")

Girl ShowIn while loop

Indent one tab and place line in pink box abovepygame.display.update()

and below screen.blit(map, (map_x, map_y)

screen.blit(girl, (100, 100))

Adjust the numbers 100, 100 so that the girl is in the center of the screen.

Bounds Detection

Edit move statements

if direction == "left": if map_x < 0: map_x = map_x + 1 if direction == "up": if map_y < 0: map_y = map_y + 1

Right Boundary if direction == "right":

if map_x > -1216 + 480:

map_x = map_x - 1

1216 is the width of the map. Change the number if the map is bigger or smaller.480 is the width of the screen.

Down Boundary if direction == "down":

if map_y > -896 + 320:

map_y = map_y - 1

896 is the height of the map. Change the number if the map is bigger or smaller.320 is the height of the screen.

Yay! You’re FinishedAdditional Information follows

Vocabularyimport libraries

pygame initialization

main while loop or main game loop

for loop

if statement

for loop code block

for loopcodeblock

import pygame, syspygame.init()size = (480, 320)screen = pygame.display.set_mode(size)map = pygame.image.load("caitlyn_map.png")girl = pygame.image.load("girl.png")map_x = 0map_y = 0direction = "up"

while True: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN: direction = "down" if event.key == pygame.K_UP: direction = "up" if event.key == pygame.K_RIGHT: direction = "right" if event.key == pygame.K_LEFT: direction = "left" if event.type == pygame.QUIT: pygame.quit() sys.exit() if direction == "left": if map_x < 0: map_x = map_x + 1 if direction == "up": if map_y < 0: map_y = map_y + 1 if direction == "down": if map_y > -896 + 320: map_y = map_y - 1 if direction == "right": if map_x > -1216 + 480: map_x = map_x - 1 screen.blit(map,(map_x,map_y)) screen.blit(girl, (240-32,160-32)) pygame.display.update()