Pygame

From ICO wiki
Revision as of 13:55, 9 March 2016 by Lvosandi (talk | contribs)
Jump to navigationJump to search

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), (0, 0, 200, 300))
        screen.blit(bird, (100, 100), (0, 0, 17, 18))
        pygame.display.flip()

if __name__ == "__main__":
    __main__()

Download graphics:

wget http://enos.itcollege.ee/~lvosandi/floppy-gfx.zip
unzip floppy-gfx.zip


Challenges

  • Implement panning of background
  • Draw the pipes on the screen, use pygame.transform.flip to flip the pipe image
  • Add collision detection