PyGame Zero
Flappy Bird Workshop
Marek Mansell
python.sk/navody/flappy
FabLab Bratislava
Obrázky prevzaté z www.fablab.sk
Projekt RoboCoop
Obrázky prevzaté z robocoop.eu
Python?
python.sk/navody/flappy/
Flappy Bird
Flappy
Pozadie
(nehýbe sa)
Pipe
(hýbe sa)
Pipe
(hýbe sa)
Stlačenie medzerníka - Flappy ide hore
python.sk/navody/flappy/
PyGame Zero a Mu editor
Zmena
módu
Súčasný
mód
python.sk/navody/flappy/
Otvorenie súboru "game.py"
Otvor
súbor
python.sk/navody/flappy/
Vytvorenie okna hry
HEIGHT = 500
WIDTH = 500
TITLE = "My Flappy Game"
Spustenie hry
Ukončenie hry
python.sk/navody/flappy/
Herná slučka (Game Loop)
Spracuj vstupy
(napr. klávesnica)
"Update" hry (pozícia postáv)
"Render" hry
(zmeny sa zobrazia)
python.sk/navody/flappy/
Game Loop v PyGame Zero
HEIGHT = 500
WIDTH = 500
TITLE = "My Flappy Game"
def update():
pass
def draw():
pass
python.sk/navody/flappy/
Obrázky v PyGame Zero
python.sk/navody/flappy/
Zobrazenie Flappyho
HEIGHT = 500
WIDTH = 500
TITLE = "My Flappy Game"
flappy = Actor("flappy")
flappy.x = WIDTH / 2
flappy.y = HEIGHT / 2
flappy.dy = 0
def update():
pass
def draw():
flappy.draw()
python.sk/navody/flappy/
Súradnice
0, 0
X
Y
500, 500
python.sk/navody/flappy/
Padanie
HEIGHT = 500
WIDTH = 500
TITLE = "My Flappy Game"
GRAVITY = 0.3
flappy = Actor("flappy")
flappy.x = WIDTH / 2
flappy.y = HEIGHT / 2
flappy.dy = 0
def update():
flappy.y += GRAVITY
def draw():
flappy.draw()
python.sk/navody/flappy/
Padanie (s akceleráciou)
HEIGHT = 500
WIDTH = 500
TITLE = "My Flappy Game"
GRAVITY = 0.3
flappy = Actor("flappy")
flappy.x = WIDTH / 2
flappy.y = HEIGHT / 2
flappy.dy = 0
def update():
flappy.y += flappy.dy
flappy.dy += GRAVITY
def draw():
flappy.draw()
python.sk/navody/flappy/
Pozadie
HEIGHT = 640
WIDTH = 360
TITLE = "My Flappy Game"
GRAVITY = 0.3
flappy = Actor("flappy")
flappy.x = WIDTH / 2
flappy.y = HEIGHT / 2
flappy.dy = 0
def update():
flappy.y += flappy.dy
flappy.dy += GRAVITY
def draw():
screen.blit("bg", (0, 0))
flappy.draw()
Medzerník
HEIGHT = 640
WIDTH = 360
TITLE = "My Flappy Game"
GRAVITY = 0.3
FLAP_SPEED = 6.5
def on_key_down(key):
if key == keys.SPACE:
flappy.dy = -FLAP_SPEED
python.sk/navody/flappy/
"Vyletenie" z hry
def update():
flappy.y += flappy.dy
flappy.dy += GRAVITY
if flappy.top > HEIGHT:
quit()
if flappy.bottom < 0:
quit()
python.sk/navody/flappy/
Prvá "rúra"
GRAVITY = 0.3
FLAP_SPEED = 6.5
SPEED = 3
pipe_upper = Actor("pipe_upper")
pipe_upper.left = WIDTH
pipe_upper.top = 0
def update():
flappy.y += flappy.dy
flappy.dy += GRAVITY
pipe_upper.left -= SPEED
def draw():
screen.blit("bg", (0, 0))
flappy.draw()
pipe_upper.draw()
Kolízia s rúrou
def update():
flappy.y += flappy.dy
flappy.dy += GRAVITY
pipe_upper.left -= SPEED
if flappy.top > HEIGHT:
quit()
if flappy.bottom < 0:
quit()
if pipe_upper.colliderect(flappy):
quit()
python.sk/navody/flappy/
Druhá "rúra"
pipe_lower = Actor("pipe_lower")
pipe_lower.left = 1.5 * WIDTH
pipe_lower.bottom = HEIGHT
pipe_upper.left -= SPEED
pipe_lower.left -= SPEED
if pipe_lower.colliderect(flappy):
quit()
pipe_lower.draw()
update
update
draw
python.sk/navody/flappy/
Opakovanie rúr
if pipe_upper.right < 0:
pipe_upper.left = WIDTH
if pipe_lower.right < 0:
pipe_lower.left = WIDTH
update
python.sk/navody/flappy/
Vylepšenia
- Bodovanie - pripočítavanie bodov
- Pauznutie hry pri stlačení klávesy P
- Spustenie hry až po prvom stlačení medzerníku
- Náhodne vyskytujúce sa rúry
...
vymyslite ďalšie :-)
python.sk/navody/flappy/
Ďalšie možnosti PyGame Zero
python.sk/navody/flappy/
Video na PyGame Zero
python.sk/navody/flappy/
Problém?
python.sk/navody/flappy/
Spätná väzba
- Čo ste sa naučili?
- Bolo to prínosné?
- Čo som mohol spraviť inak?
- Chápali ste tomu?
- Privítali by ste pokračovanie?
- Privítali by ste aj iné Python témy?
Prezentácia:
slides.com/marekmansell/pygame-zero-flappy
PyGame Zero Workshop - Flappy Bird
By Marek Mansell
PyGame Zero Workshop - Flappy Bird
- 795