Pygame: Difference between revisions
From ICO wiki
Jump to navigationJump to search
No edit summary |
|||
Line 36: | Line 36: | ||
elif event.type == KEYDOWN and event.key == K_ESCAPE: | elif event.type == KEYDOWN and event.key == K_ESCAPE: | ||
return | return | ||
screen.blit(background, (50, 50 | screen.blit(background, (50, 50)) | ||
screen.blit(bird, (100, 100 | screen.blit(bird, (100, 100)) | ||
pygame.display.flip() | pygame.display.flip() | ||
Line 54: | Line 54: | ||
* Implement panning of background | * Implement panning of background | ||
* Draw the pipes on the screen, use [http://www.pygame.org/docs/ref/transform.html#pygame.transform.flip pygame.transform.flip] to flip the pipe image | * Draw the pipes on the screen, use [http://www.pygame.org/docs/ref/transform.html#pygame.transform.flip pygame.transform.flip] to flip the pipe image | ||
==Improved example== | ==Improved example== |
Revision as of 09:35, 22 March 2016
Pygame is a library for programming games with Python. It handles event loop and simplifies drawing the scene.
Installation
Ubuntu users can simply apt-get the package:
apt-get install python-pygame
Windows users should download pygame .whl file corresponding to their Python runtime version. On Python 3.5 use following to install the module:
pip3 install pygame-1.9.2a0-cp35-none-win_amd64.whl
Code example
An example pygame code:
import os, sys
import pygame
from pygame.locals import *
def __main__():
pygame.init()
pipe = pygame.image.load("img/pipe.png")
bird = pygame.image.load("img/bird.png")
background = pygame.image.load("img/bg.png")
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption('Floppy Bird')
pygame.mouse.set_visible(0)
while True:
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN and event.key == K_ESCAPE:
return
screen.blit(background, (50, 50))
screen.blit(bird, (100, 100))
pygame.display.flip()
if __name__ == "__main__":
__main__()
Download graphics:
wget http://enos.itcollege.ee/~lvosandi/floppy-gfx.zip unzip floppy-gfx.zip
Execrises:
- Implement panning of background
- Draw the pipes on the screen, use pygame.transform.flip to flip the pipe image
Improved example
import os, sys
import pygame
from pygame.locals import *
def __main__():
pygame.init()
pipe = pygame.image.load("img/pipe.png")
bird = pygame.image.load("img/bird.png")
background = pygame.image.load("img/bg.png")
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('Floppy Bird')
pygame.mouse.set_visible(0)
print "Pipe dimensions:", pipe.get_size()
print "Background dimensions:", background.get_size()
y = 0
vy = 0
level = [0, 1, 0, 0, 2, 0, 0, 3,0, 1, 0, 0, 2, 0, 0, 3,0, 1, 0, 0, 2, 0, 0, 3,]
antipipe = pygame.transform.flip(pipe, 0, 1)
frames = 0 # Here we count frames
scene = pygame.Surface((240,180)) # This is where we compose the scene
while True:
scene.fill(0) # This fills the buffer with black pixels
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN and event.key == K_ESCAPE:
return
elif event.type == KEYDOWN and event.key == K_SPACE:
vy -= 0.02
print "Space was pressed!"
vy += 0.0001
y += vy
for i in range(0, 3):
scene.blit(background, (i*148 - (frames/10) % 148, 0), (0, 0, 200, 300))
frames += 1
for offset, pipe_height in enumerate(level):
if pipe_height == 0: continue # Skip pipes whose height is 0
scene.blit(pipe, (400-frames/2 + offset * 32, 180 - pipe_height * 32), (0, 0, 100,160))
scene.blit(antipipe, (400-frames/2 + offset * 32, 180 - pipe_height * 32 - 250), (0, 0, 100,160))
scene.blit(bird, (100, 100+int(y)), (18 * (int(frames/100) % 3), 0, 18, 13))
pygame.transform.scale(scene, screen.get_size(), screen) # Here we scale it up and paste to screen
pygame.display.flip()
if __name__ == "__main__":
__main__()
Exercises:
- Add collision detection
- Add text overlays and scoring using pygame.font
- Attempt to get fixed FPS
Almost there!
import os, sys
import pygame
from pygame.locals import *
def main_loop(screen):
pipe = pygame.image.load("img/pipe.png")
bird = pygame.image.load("img/bird.png")
background = pygame.image.load("img/bg.png")
pygame.display.set_caption('Floppy Bird')
pygame.mouse.set_visible(0)
print "Pipe dimensions:", pipe.get_size()
print "Background dimensions:", background.get_size()
y = 100 # Approximately in the vertical center of the screen
vy = 0
level = [0, 1, 0, 0, 2, 0, 0, 3,0, 1, 0, 0, 2, 0, 0, 3,0, 1, 0, 0, 2, 0, 0, 3,]
antipipe = pygame.transform.flip(pipe, 0, 1)
frames = 0 # Here we count frames
scene = pygame.Surface((240,180)) # This is where we compose the scene
while True:
scene.fill(0) # This fills the buffer with black pixels
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN and event.key == K_ESCAPE:
return
elif event.type == KEYDOWN and event.key == K_SPACE:
vy -= 0.02
print "Space was pressed!"
vy += 0.0001
y += vy
for i in range(0, 3):
scene.blit(background, (i*148 - (frames/10) % 148, 0), (0, 0, 200, 300))
frames += 1
# This is basically the inverse of calculation of px below
current_pipe = int((frames/2-400+100)/32)
print "current pipe is:", current_pipe
for offset, pipe_height in enumerate(level):
if pipe_height == 0: continue # Skip pipes whose height is 0
px = 400-frames/2 + offset * 32
py = 180 - pipe_height * 32
scene.blit(pipe, (px, py), (0, 0, 32,160))
scene.blit(antipipe, (px, py - 250), (0, 0, 32,160))
# We initialized y = 100 above ;)
if offset == current_pipe: # Highlight the next pipe we could bump into
if y > py:
#pygame.draw.rect(scene, (255,0,0), (px, py, 32, 160))
print "Game over: You bumped into pipe below!"
return
if y < py-250:
#pygame.draw.rect(scene, (255,0,0), (px, py-250, 32, 160))
print "Game over: You bumped into pipe above!"
return
if current_pipe > len(level):
print "You win! You've passed all the roadblocks!"
return
# This will return scene bitmap height and width in pixels
width, height = scene.get_size()
if y > height:
print "Game over: Bird fell down!"
return
if y < 0:
print "Game over: You're aiming too high!"
return
scene.blit(bird, (100, int(y)), (18 * (int(frames/100) % 3), 0, 18, 13))
pygame.transform.scale(scene, screen.get_size(), screen) # Here we scale it up and paste to screen
pygame.display.flip()
def game_over(screen, score):
while True:
# This does nothing yet!
return
def __main__():
pygame.init()
screen = pygame.display.set_mode((800,600))
score = main_loop(screen)
game_over(screen, score)
if __name__ == "__main__":
__main__()