#!/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 import random # 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) 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 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 def create_type1(self, pos): """ create a bullet of type 1 temporary laser type """ bullet = Bullet(['laser1.bmp', 'laser2.bmp', 'laser3.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 # recenter the sprite # self.rect = self.rect.move((0, - self.rect.height / 2)) self.rect = self.rect.move((-32, - self.rect.height / 2)) def update(self): # destroy when collide or when exists the screen if self.in_screen(): self.rect.move_ip(self.speed) else: globals.bullets_list.remove(self) def in_screen(self): """ detect if bullet is in the screen """ return self.rect.colliderect(globals.screen.get_rect()) class Player(Object): def __init__(self, frames, pos, speed): Object.__init__(self, frames, pos, speed) self.shooting = False self.bullet_type = 0 def shoot(self): """ create a bullet and add it to the global bullet list""" bullet = globals.bulletfactory.create(self.bullet_type, self.rect.midright) globals.bullets_list.add(bullet) def start_move(self, vector): """ initiate moving sequence """ globals.moving_objects.add(self) self.speed = (self.speed[0] + vector[0], self.speed[1] + vector[1]) self.image = self.frames[1] def stop_move(self, vector): """ stop moving sequence """ self.speed = (self.speed[0] - vector[0], self.speed[1] - vector[1]) if self.speed == (0, 0): globals.moving_objects.remove(self) self.image = self.frames[0] def start_shoot(self): self.shooting = True def stop_shoot(self): self.shooting = False def update(self): self.rect.move_ip(self.speed) if self.shooting: self.shoot() def init_gfx(): """graphics initialisation""" globals.screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y), HWSURFACE|DOUBLEBUF) 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() random.seed() #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.type == KEYDOWN: if event.key == K_ESCAPE: globals.die = True elif event.key == K_UP: player.start_move((0, -1)) elif event.key == K_DOWN: player.start_move((0, 1)) elif event.key == K_LEFT: player.start_move((-1, 0)) elif event.key == K_RIGHT: player.start_move((1, 0)) elif event.key == K_x: player.start_shoot() if event.type == KEYUP: if event.key == K_UP: player.stop_move((0, -1)) elif event.key == K_DOWN: player.stop_move((0, 1)) elif event.key == K_LEFT: player.stop_move((-1, 0)) elif event.key == K_RIGHT: player.stop_move((1,0)) elif event.key == K_x: player.stop_shoot() def transition(type): """ transition selector """ if type == 0: transition_disintegrate() elif type == 1: transition_fade_to_black() elif type == 2: transition_fade_to_white() def transition_disintegrate(): """ put black pixels at randoom location""" for i in range(0, 200000): x = random.randint(0, SCREEN_X) y = random.randint(0, SCREEN_Y) globals.screen.set_at((x, y), (0,0,0, 255)) if i % 1000 == 0 : pygame.display.update() globals.screen.fill((0, 0, 0)) pygame.display.update() def transition_fade_to_black(): """ darken every pixels on screen """ array = pygame.PixelArray(globals.screen) pass def transition_fade_to_white(): """ lighten every pixels on screen """ pass ## # Main ## globals = Globals() init() wait_keypress() nbframe = 0 # create_player player = Player(['player1_1.bmp', 'player1_2.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 previously moved objects globals.moving_objects.clear(globals.screen, globals.background) globals.bullets_list.clear(globals.screen, globals.background) # update game logic globals.moving_objects.update() globals.bullets_list.update() # add player (if it's not already in the group). ugly, but ok for now. globals.moving_objects.add(player) # drawing rectlist = globals.moving_objects.draw(globals.screen) rectlist.extend(globals.bullets_list.draw(globals.screen)) pygame.display.update(rectlist) # pygame.display.flip() nbframe = nbframe + 1 globals.clock.tick(60) for event in pygame.event.get(): if event.type == KEYDOWN or event.type == KEYUP: process_key(event, player) # reinitializing moving objects list #globals.moving_objects = pygame.sprite.RenderUpdates() # transition(0) 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...