2009 302: Difference between revisions

From XPUB & Lens-Based wiki
(New page: == Morning == Talking PyGame)
 
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Reading ==
There is a reading for next week. Copies are available in the office (I left a pile on my/Aymeric's desk).
== Morning ==
== Morning ==


Talking [[PyGame]]
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]]
<p>
beginning of space invaders, follow the enemy!
</p>
<source lang="python">
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)
</source>

Latest revision as of 14:21, 25 May 2009

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)