[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[pygame] Re:Immovable Car, and TypeError
ok, now my code is error free...kewl. however, now I only see the car in the top left corner, and it doesn't move, I should seeing it in the bottom middle, and it should move. this was the case for a short while, I had the car on the bottom middle, moving back and forth like a charm, however, the car was also blitted, to the top left corner. then I fiddled with the code, trying to give the screen a solid background, of the road pic i have. that wasn't working out, so I tried to go back. now i only have to car in the top left corner, and it's not moving, and i don't have the car in the bottom center. I have no idea why, since i believe the code is the same as it was when it was working in it's own strange way. anyway, here's the code as it stands, i don't expect you guys to keep bailing me out, but once I get the basics of this "game" figured out, I should leave ya alone for awhile:)
#!C:\Python21
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('chars', 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()
#Load Background
def load_back(file):
"loads background, prepares it for play"
file = os.path.join('chars',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 = 6
FRAMES_PER_SEC = 50
#some globals for friendly access
dirtyrects = [] # list of update_rects
next_tick = 0 # used for timing
class Img: pass # container for images
class Back: pass #container for background
def wait_frame():
"wait for the correct fps time to expire"
global next_tick
this_tick = pygame.time.get_ticks()
if this_tick < next_tick:
pygame.time.delay(next_tick - this_tick)
next_tick = this_tick + (1000/FRAMES_PER_SEC)
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 Road(Actor):
"The proving grounds"
def __init__(self):
Actor.__init__(self, Back.road)
self.rect.centerx = SCREENRECT.centerx
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)
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)
Back.road = load_back('road.bmp')
#Create the background
background = "pygame.Surface(screen.get_size())"
background.fill((25,175,50))
#flip display/show background
screen.blit(background,(0,0))
screen.blit(Back.road, (0,0))
screen.blit(Img.car, (0,0))
pygame.display.flip()
#initialize characters
player = Player()
while 1:
wait_frame()
# Gather Events
pygame.event.pump()
if pygame.event.peek() == 0:
pygame.event.set_grab(1)
else:
pygame.event.set_grab(0)
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)
# Make sure car is on road
pygame.display.update(dirtyrects)
dirtyrects = []
if __name__ == '__main__':
main()
I'm assuming to get rid of the car in the top left corner, I'll just not blit it there...:) but how do i get the car in the bottom center? also, how do i keep the car from erasing the road when it drives over it? that was why i tried making the road pic the background in the first place... thanks in advance
c ya round,
josh