2009 302

From XPUB & Lens-Based wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Reading

There is a reading for next week. Copies are available in the office (I left a pile on my/Aymeric's desk).

Morning

Talking PyGame

http://www.youtube.com/watch?v=LLUMM18Ln5Q

The (Py)Game of life

We looked today at a Python implementation of Conway's Game of life

Media:PyGameOfLife.zip

beginning of space invaders, follow the enemy!

import pygame
from pygame.locals import *
from sys import exit
from random import *

clock = pygame.time.Clock()
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)

class Anybody(object):
    def __init__(self, x, y, color=(255, 255, 255)):
        self.x = x
        self.y = y
        self.color = color
        self.width = 20
        self.height = 20
        
    def draw(self, screen):
        pygame.draw.rect(screen, self.color, Rect(self.x, self.y, self.width, self.height))
    def chase(self, enemy):
        if enemy.x < self.x:
            self.x -= 1
        else:
            self.x += 1

player = Anybody(100, 400, (0, 255, 0))
invader = Anybody(100, 50)


while True:
    #1 Handling the events
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        elif event.type == KEYDOWN and event.key == K_ESCAPE:
            exit()
        elif event.type == KEYDOWN:
            if event.key == K_RIGHT:
                player.x += 20
            elif event.key == K_LEFT:
                player.x -= 20

    #2 Draw the frame
    # blank the screen
    pygame.draw.rect(screen, (0, 0, 0), Rect(0, 0, 640, 480))
    player.draw(screen)
    invader.draw(screen)
    invader.chase(player)
    pygame.display.update()
    clock.tick(30)