#!/usr/bin/python # -*- coding: iso8859-15 -*- # first shoot'em um prototype # may be ported to DS later # it will use pygame (at least at the beginning) for input handling, graphics and sound import pygame from pygame.locals import * import os # constants SCREEN_X = 640 SCREEN_Y = 480 class Globals: def __init__(self): self.screen = None self.background = None self.level_event_list = [] self.clock = None self.event_type = {} self.die = False self.moving_objects = pygame.sprite.RenderUpdates() self.bullets_list = pygame.sprite.RenderUpdates() self.bulletfactory = BulletFactory() class Event: def __init__(self): self.time = 0 self.type = 0 class Object(pygame.sprite.Sprite): def __init__(self, frames, pos, speed): pygame.sprite.Sprite.__init__(self) self.speed = speed self.frames = frames self.image = None self.load() self.rect = self.image.get_rect().move(pos) self.dx = 0 self.dy = 0 def load(self): """ load sprite list """ index = 0 for f in self.frames: image = pygame.image.load(os.path.join('data', f)).convert() image.set_colorkey(pygame.Color(255,255,255, 0)) self.frames[index] = image index += 1 self.image = self.frames[0] def move(self, dx, dy): """ move the object by adding deltas passed """ self.speed = (dx, dy) globals.moving_objects.add(self) def update(self): self.rect.move_ip(self.speed) class BulletFactory(): """ BulletFactory, create bullets """ def create(self, type, pos): """ create bullt of type type """ if type == 0: return self.create_type0(pos) def create_type0(self, pos): """ create a bullet of type 0 temporary linear type """ bullet = Bullet(['bullet.bmp'], 0, pos) bullet.speed = (10,0) bullet.damage = 10 return bullet class Bullet(Object): def __init__(self, frames, type, pos): Object.__init__(self, frames, pos, (0, 0)) self.type = type class Player(Object): def __init__(self, frames, pos, speed): Object.__init__(self, frames, pos, speed) def shoot(self): """ create a bullet and add it to the global bullet list""" bullet = globals.bulletfactory.create(0, self.rect.midright) globals.bullets_list.add(bullet) def init_gfx(): """graphics initialisation""" globals.screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y)) globals.screen.fill(0) pygame.display.flip() def init_sound(): """sound initialisation""" pass def init(): """ general initialisation""" pygame.init() init_gfx() globals.clock = pygame.time.Clock() #globals.event_type = {'create_enemeny' : create_enemey, 'vanish_object': vanish_object()} def create_enemy(type): """ create an enemy """ pass def create_level(): """ phony level one""" # create events # dict events string => function # for e in keys(globals.events_type): pass def wait_keypress(): """ wait for a keypress and then return """ next = 0 while next != 1: for event in pygame.event.get(): if event.type == KEYDOWN: next = 1 def process_key(event, player): """ process keyboard input """ if event.key == K_ESCAPE: globals.die = True elif event.key == K_n: player.move(0, 10) pass elif event.key == K_UP: player.move(0, -10) elif event.key == K_DOWN: player.move(0, 10) elif event.key == K_LEFT: player.move(-10, 0) elif event.key == K_RIGHT: player.move(10, 0) elif event.key == K_x: player.shoot() ## # Main ## globals = Globals() init() wait_keypress() nbframe = 0 # create_player player = Player(['player1_1.bmp'], (50, 50), (0, 0)) # background globals.background = pygame.image.load(os.path.join('data', 'background.bmp')).convert() background_rect = globals.background.get_rect() globals.screen.blit(globals.background, background_rect) pygame.display.flip() # draw initial sprite globals.moving_objects.add(player) while not globals.die: # erase player #globals.screen.blit(background, player.rect, player.rect) # draw player #globals.screen.blit(player.image, player.pos) #globals.moving_objects.clear(globals.screen, globals.background) #globals.bullets_list.clear(globals.screen, globals.background) for event in pygame.event.get(): if event.type == KEYDOWN: process_key(event, player) # erase moving objects globals.moving_objects.update() rectlist = globals.moving_objects.draw(globals.screen) # bullets handling globals.bullets_list.update() rectlist.extend(globals.bullets_list.draw(globals.screen)) pygame.display.update(rectlist) # pygame.display.flip() nbframe = nbframe + 1 globals.clock.tick(60) globals.moving_objects.clear(globals.screen, globals.background) globals.bullets_list.clear(globals.screen, globals.background) # reinitializing moving objects list globals.moving_objects = pygame.sprite.RenderUpdates() print "frames : %d" % nbframe # TODO: # + Objects / Sprites # --> convert my Objects to pygame.sprite.Sprite # --> convert moving_objects list to Group # + Input # --> handle continuous keypress # + Movving Sprites # --> handle continous player move (related to Input) # --> Sprites (non related to player input) movement vector, etc...