Subversion

logrus

[/] [IronHorse/] [moveDude.py] - Rev 631

Compare with Previous - Blame


import pygame
import sys
import time

import vec2d

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()
dudeRect = dude.get_rect()
dudeRect.centerx = width/2
dudeRect.centery = height/2

#title
demoname = "moveDude"
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 update():
    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

def draw():
    screen.blit(background,bgRect)
    screen.blit(dude,dudeRect)
    screen.blit(fontsurface,fontrect)
    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