[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[pygame] Invisible Pygame Car!?
I'm working on a driving/shooting game. The car however won't show up on the screen. When it did show up, i could only move it a few pixels left and right, instead of the width of the screen. I'm thinking of making the road image, part of the background. Any suggestions/comments will be appreciated. Most of the code is taken from the Aliens example, and I've learned a lot about updating rectangles in the process. Thanks in advance, and here's the code...
import whrandom, os.path, sys
import pygame, pygame.image
from pygame.locals import *
#image loader
def load_image(file, transparent):
"loads an image, prepares it for play"
file = os.path.join('data', file)
try:
surface = pygame.image.load(file)
except pygame.error:
raise SystemExit, 'Could not load image "%s" %s'%(file, pygame.get_error())
if transparent:
corner = surface.get_at((0, 0))
surface.set_colorkey(corner, RLEACCEL)
return surface.convert()
def load_back(file):
"loads background, prepares it for play"
file = os.path.join('background', file)
try:
surface = pygame.image.load(file)
except pygame.error:
raise SystemExit, 'Could not load image "%s" %s'% (file, pygame.get_error())
return surface.convert()
#constants
SCREENRECT = Rect(0, 0, 350, 450)
PLAYER_SPEED = 5
#some globals for friendly access
dirtyrects = [] # list of update_rects
next_tick = 0 # used for timing
class Img: pass # container for images
class Bg: pass # container for backgrounds
class Actor:
"An enhanced sort of sprite class"
def __init__(self, image):
self.image = image
self.rect = image.get_rect()
def update(self):
"update the sprite state for this frame"
pass
def draw(self, screen):
"draws the sprite into the screen"
r = screen.blit(self.image, self.rect)
dirtyrects.append(r)
def erase(self, screen, background):
"gets the sprite off of the screen"
r = screen.blit(background, self.rect, self.rect)
dirtyrects.append(r)
class Player(Actor):
"Cheer for our hero"
def __init__(self):
Actor.__init__(self, Img.car)
self.alive = 1
self.reloading = 0
self.rect.centerx = SCREENRECT.centerx
self.rect.bottom = SCREENRECT.bottom - 5
def move(self, direction):
self.rect = self.rect.move(direction*PLAYER_SPEED, 0).clamp(road)
class Road(Actor):
"The proving grounds"
def __init__(self):
Actor.__init__(self, Bg.road)
self.rect.centerx = SCREENRECT.centerx
def main():
global dirtyrects
#initiate pygame/display settings
pygame.init()
screen = pygame.display.set_mode(SCREENRECT.size, 0)
pygame.display.set_caption('Cop Chase')
pygame.mouse.set_visible(0)
# Load the Resources
Img.cop = load_image('cop.bmp', 1)
Img.car = load_image('car.bmp', 1)
###Bg.road = load_back('road.bmp')
#create background
background = "pygame.Surface(screen.get_size())"
background.fill((25,150,25))
#flip display/show background
screen.blit(background,(0,0))
screen.blit(Bg.road, (0,0))
pygame.display.flip()
while 1:
# Initialize Game Actors
player = Player()
#quit game
pygame.event.pump()
keystate = pygame.key.get_pressed()
if keystate[K_ESCAPE] or pygame.event.peek(QUIT):
break
# Clear screen and update actors
for actor in [player]:
actor.erase(screen, background)
actor.update()
# Move the player
direction = keystate[K_RIGHT] - keystate[K_LEFT]
player.move(direction)
# Draw everybody
for actor in [player]:
actor.draw(screen)
pygame.display.update(dirtyrects)
dirtyrects = []
if __name__ == '__main__':
main()