0 Members and 1 Guest are viewing this topic.
I am intermediate At python i mainly have used it for pretty simple games. This is my first attempt at a more difficult game. I almost have the basics of the game working right now I need a sprite though and I'm trying to decide what to use.and it wasn't that I didn't know how to do it it was that i wanted to try and find an optimized way of doing it without looping through every single pixel.
#import modulesimport pygame, math, random, osfrom pygame.locals import *#Check to see if pygame is working rightif not pygame.font: print 'Warning, fonts disabled'if not pygame.mixer: print 'Warning, sound disabled'#define any needed global variables here #Create a function to load images for sprites.def load_image(name, colorkey=None): fullname = os.path.join('data', name) try: image = pygame.image.load(fullname) #If an error exit except pygame.error, message: print 'Cannot load image:', fullname raise SystemExit, message image = image.convert() if colorkey is not None: if colorkey is -1: colorkey = image.get_at((0,0)) image.set_colorkey(colorkey, RLEACCEL) return image, image.get_rect()#create a function to load sounddef load_sound(name): class NoneSound: def play(self): pass if not pygame.mixer or not pygame.mixer.get_init(): return NoneSound() fullname = os.path.join('data', name) try: sound = pygame.mixer.Sound(fullname) #if an error exit except pygame.error, message: print 'Cannot load sound:', fullname raise SystemExit, message return sound#classes for our game objectsclass Player(pygame.sprite.Sprite): #moves a clenched fist on the screen, following the mouse def __init__(self): pygame.sprite.Sprite.__init__(self) #call Sprite initializer self.image, self.rect = load_image('sprite.bmp', -1) self.direction=0 def update(self): #move the player based on direction chosen if self.direction == 0: return elif self.direction == 1: newpos = self.rect.move((0, -2)) self.rect = newpos elif self.direction == 2: newpos = self.rect.move((0, 2)) self.rect = newpos elif self.direction == 3: newpos = self.rect.move((-2, 0)) self.rect = newpos elif self.direction == 4: newpos = self.rect.move((2, 0)) self.rect = newpos def up(self): self.direction=1 def down(self): self.direction=2 def left(self): self.direction=3 def right(self): self.direction=4 def still(self): self.direction=0#function to load the game enginedef game_engine(): #nothing done here yet return#function for playing the gamedef play_game(): #load the game engine game_engine() #start the game #Initialize Everything pygame.init() screen = pygame.display.set_mode((975, 325)) pygame.display.set_caption('Physics!') pygame.mouse.set_visible(1) turn = 0 win = False lose = False achievements = 0 #Create The Backgound background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((250, 250, 250)) #Display The Background screen.blit(background, (0, 0)) pygame.display.flip() #Prepare Game Objects clock = pygame.time.Clock() player = Player() allsprites = pygame.sprite.RenderPlain((player)) #Main Loop while 1: clock.tick(25) #Handle Input Events for event in pygame.event.get(): if event.type == QUIT: return elif event.type == KEYDOWN and event.key == K_ESCAPE: return if event.type == KEYDOWN and event.key == K_UP: player.up() elif event.type == KEYDOWN and event.key == K_DOWN: player.down() elif event.type == KEYDOWN and event.key == K_LEFT: player.left() elif event.type == KEYDOWN and event.key == K_RIGHT: player.right() else: player.still() allsprites.update() #Draw Everything screen.blit(background, (0, 0)) allsprites.draw(screen) pygame.display.flip()def main(): #define needed local variables here #start game play_game() if __name__ == '__main__': main()