25
Girl Coder Map Mover

Teaching Python to 9 Year Old Girl - map mover

Embed Size (px)

Citation preview

Page 1: Teaching Python to 9 Year Old Girl -  map mover

Girl Coder

Map Mover

Page 2: Teaching Python to 9 Year Old Girl -  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. )

Page 3: Teaching Python to 9 Year Old Girl -  map mover

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")

Page 4: Teaching Python to 9 Year Old Girl -  map mover

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))

Page 5: Teaching Python to 9 Year Old Girl -  map mover

Set Map x and y Variables

Above while loopmap_x = 0

map_y = 0

Page 6: Teaching Python to 9 Year Old Girl -  map mover

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.

Page 7: Teaching Python to 9 Year Old Girl -  map mover

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

Page 8: Teaching Python to 9 Year Old Girl -  map mover

Create Variable for Direction

Insert line below above while loopdirection = “right”

Page 9: Teaching Python to 9 Year Old Girl -  map mover

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

Page 10: Teaching Python to 9 Year Old Girl -  map mover

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.

Page 11: Teaching Python to 9 Year Old Girl -  map mover

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():

Page 12: Teaching Python to 9 Year Old Girl -  map mover

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.

Page 13: Teaching Python to 9 Year Old Girl -  map mover

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.

Page 14: Teaching Python to 9 Year Old Girl -  map mover

Girl Load

Above while loop, insert the line below

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

Page 15: Teaching Python to 9 Year Old Girl -  map mover

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.

Page 16: Teaching Python to 9 Year Old Girl -  map mover

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

Page 17: Teaching Python to 9 Year Old Girl -  map mover

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.

Page 18: Teaching Python to 9 Year Old Girl -  map mover

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.

Page 19: Teaching Python to 9 Year Old Girl -  map mover

Yay! You’re FinishedAdditional Information follows

Page 20: Teaching Python to 9 Year Old Girl -  map mover

Vocabularyimport libraries

pygame initialization

main while loop or main game loop

for loop

if statement

Page 21: Teaching Python to 9 Year Old Girl -  map mover

for loop code block

for loopcodeblock

Page 22: Teaching Python to 9 Year Old Girl -  map mover

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()

Page 23: Teaching Python to 9 Year Old Girl -  map mover