Subversion

logrus

[/] [IronHorse/] [seekDude.py] - Rev 638 Go to most recent revision

Compare with Previous - Blame


import pygame
import sys
import time

import Steering
import vec2d

class WorkerSteering(Steering.SimpleVehicle):
        def __init__(self):
                self.target = vec2d.Vector(0.0,0.0)
                Steering.SimpleVehicle.__init__(self)
        def UpdateSteering(self):
                return self.Seek(self.target)

pygame.init()

size = width, height = 400, 300
speed = 6

screen = pygame.display.set_mode(size)

background = pygame.image.load("mexico-desert-vector.jpg")
background.convert()
background = pygame.transform.scale(background, size)
bgRect = pygame.Rect((0,0), size)

#load the dude image, convert to native pixel format, resize
dude = pygame.image.load("dude.png")
dude.convert()
#dude.set_colorkey((255,255,255), pygame.RLEACCEL)
dudeRect = dude.get_rect()
dudeRect.centerx = width/2
dudeRect.centery = height/2

#load the worker image, convert to native pixel format, resize
worker = pygame.image.load("worker.png")
worker.convert()
workerRect = worker.get_rect()
workerRect.centerx = width
workerRect.centery = height
workerVehicle = WorkerSteering()

#load the flag image, convert to native pixel format, resize
flag = pygame.image.load("flag.png")
flag.convert()
flagRect = flag.get_rect()

flags = list()

#title
demoname = "seekDude"
pygame.font.init()
f = pygame.font.Font("fonts/I Did This.ttf", 30)
fontsurface = f.render(demoname, True, (0,0,0))
fontrect = fontsurface.get_rect()
pygame.display.set_caption(demoname)

def updateWorker():
    workerVehicle.target.x = dudeRect.centerx
    workerVehicle.target.y = dudeRect.centery
    workerVehicle.Update()
    workerRect.centerx = workerVehicle.bounds.pos.x
    workerRect.centery = workerVehicle.bounds.pos.y


def updatePlayer():
    deltaVec = vec2d.Vector()
    if pygame.key.get_pressed()[pygame.K_w]:
        deltaVec.y = -1
    if pygame.key.get_pressed()[pygame.K_s]:
        deltaVec.y = 1
    if pygame.key.get_pressed()[pygame.K_a]:
        deltaVec.x = -1
    if pygame.key.get_pressed()[pygame.K_d]:
        deltaVec.x = 1   
    
    deltaVec.unit() #make sure we don't go any faster when moving diagonally
    deltaVec *= speed
    dudeRect.centerx += deltaVec.x
    dudeRect.centery += deltaVec.y
    
    if pygame.key.get_pressed()[pygame.K_SPACE]:
        dudePos = (dudeRect.centerx, dudeRect.centery)
        flags.append(dudePos)

def update():
    updatePlayer()
    updateWorker()

def draw():
    screen.blit(background,bgRect)
    screen.blit(dude,dudeRect)
    screen.blit(fontsurface,fontrect)
    for flagPos in flags:
        flagRect.centerx = flagPos[0]
        flagRect.centery = flagPos[1]
        screen.blit(flag,flagRect)
    screen.blit(worker,workerRect)

    pygame.display.flip()


timeNextUpdate = 0
updateEvery = 0.02
while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    now = time.clock()
    if now >= timeNextUpdate:
        update()
        timeNextUpdate = now + updateEvery
    draw()    

Powered by WebSVN v1.61