teaching python to 9 year old girl - map mover
TRANSCRIPT
![Page 1: Teaching Python to 9 Year Old Girl - map mover](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/1.jpg)
Girl Coder
Map Mover
![Page 2: Teaching Python to 9 Year Old Girl - map mover](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/2.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/3.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/4.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/5.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/6.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/7.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/8.jpg)
Create Variable for Direction
Insert line below above while loopdirection = “right”
![Page 9: Teaching Python to 9 Year Old Girl - map mover](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/9.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/10.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/11.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/12.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/13.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/14.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/15.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/16.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/17.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/18.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/19.jpg)
Yay! You’re FinishedAdditional Information follows
![Page 20: Teaching Python to 9 Year Old Girl - map mover](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/20.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/21.jpg)
for loop code block
for loopcodeblock
![Page 22: Teaching Python to 9 Year Old Girl - map mover](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/22.jpg)
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](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/23.jpg)
![Page 24: Teaching Python to 9 Year Old Girl - map mover](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/24.jpg)
Target Students9 year old girlCompleted the following drills at least 5 times per drill over several months:● The Blank Screen● The Stationary Square● The Moving Square
![Page 25: Teaching Python to 9 Year Old Girl - map mover](https://reader030.vdocuments.site/reader030/viewer/2022032421/55a79fd51a28ab25438b46ec/html5/thumbnails/25.jpg)
Alternate LessonsIf the student is older than 9 and has a mobile phone, suggest these:
● Mouse and Touchscreen Control● Going Mobile - Python on Android