2009-08-15 02:27:58 +02:00
|
|
|
#!/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
|
2009-08-17 00:30:20 +02:00
|
|
|
import random
|
2009-08-15 02:27:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
# 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()
|
2009-08-19 00:35:01 +02:00
|
|
|
self.enemies_list = pygame.sprite.RenderUpdates()
|
2009-10-08 01:10:24 +02:00
|
|
|
self.enemies_active_list = pygame.sprite.RenderUpdates()
|
2009-08-19 00:35:01 +02:00
|
|
|
self.player = None
|
|
|
|
|
2009-08-15 02:27:58 +02:00
|
|
|
|
|
|
|
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
|
2009-10-08 01:10:24 +02:00
|
|
|
self.frames_index = 0
|
2009-08-15 02:27:58 +02:00
|
|
|
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
|
2009-10-08 01:10:24 +02:00
|
|
|
self.image = self.frames[self.frames_index]
|
2009-08-15 02:27:58 +02:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
self.rect.move_ip(self.speed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BulletFactory():
|
|
|
|
""" BulletFactory, create bullets """
|
|
|
|
|
2009-08-18 00:38:38 +02:00
|
|
|
def create(self, type, pos, wavepos):
|
|
|
|
""" create bullt of type type
|
|
|
|
wave paremeter indicate the position of the bullet in the wave :
|
|
|
|
starting the wave (0), middle (1) or ending the wave (2 )"""
|
|
|
|
|
2009-08-15 02:27:58 +02:00
|
|
|
if type == 0:
|
|
|
|
return self.create_type0(pos)
|
|
|
|
|
2009-08-18 00:38:38 +02:00
|
|
|
if type == 1:
|
|
|
|
return self.create_type1(pos, wavepos)
|
|
|
|
|
2009-08-15 02:27:58 +02:00
|
|
|
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
|
|
|
|
|
2009-08-18 00:38:38 +02:00
|
|
|
def create_type1(self, pos, wavepos):
|
2009-08-17 23:53:40 +02:00
|
|
|
""" create a bullet of type 1
|
|
|
|
temporary laser type """
|
|
|
|
|
2009-08-18 00:38:38 +02:00
|
|
|
filename = 'laser' + str(wavepos + 1) + '.bmp'
|
|
|
|
#print "filename: " + filename
|
|
|
|
#filename = 'laser1.bmp'
|
|
|
|
bullet = Bullet([filename], 0, pos)
|
2009-08-17 23:53:40 +02:00
|
|
|
bullet.speed = (10,0)
|
|
|
|
bullet.damage = 10
|
|
|
|
return bullet
|
2009-08-15 02:27:58 +02:00
|
|
|
|
|
|
|
class Bullet(Object):
|
|
|
|
def __init__(self, frames, type, pos):
|
|
|
|
Object.__init__(self, frames, pos, (0, 0))
|
|
|
|
self.type = type
|
2009-08-17 01:47:25 +02:00
|
|
|
# recenter the sprite
|
|
|
|
# self.rect = self.rect.move((0, - self.rect.height / 2))
|
|
|
|
self.rect = self.rect.move((-32, - self.rect.height / 2))
|
2009-08-17 01:06:52 +02:00
|
|
|
|
|
|
|
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())
|
2009-08-15 02:27:58 +02:00
|
|
|
|
2009-08-15 02:31:57 +02:00
|
|
|
class Player(Object):
|
|
|
|
def __init__(self, frames, pos, speed):
|
|
|
|
Object.__init__(self, frames, pos, speed)
|
2009-08-17 01:06:52 +02:00
|
|
|
self.shooting = False
|
2009-08-17 23:53:40 +02:00
|
|
|
self.bullet_type = 0
|
2009-08-18 00:38:38 +02:00
|
|
|
self.bullet_counter = 0
|
|
|
|
self.bullet_threshold = 5
|
2009-08-15 02:31:57 +02:00
|
|
|
|
2009-08-15 02:33:14 +02:00
|
|
|
def shoot(self):
|
|
|
|
""" create a bullet and add it to the global bullet list"""
|
2009-08-18 00:38:38 +02:00
|
|
|
bullet = globals.bulletfactory.create(self.bullet_type, self.rect.midright, self.shoot_wavepos)
|
|
|
|
if self.shoot_wavepos == 0:
|
|
|
|
self.shoot_wavepos = 1
|
|
|
|
if self.shoot_wavepos == 2:
|
|
|
|
self.shoot_wavepos = 0
|
|
|
|
self.shooting = False
|
|
|
|
|
2009-08-15 02:33:14 +02:00
|
|
|
globals.bullets_list.add(bullet)
|
2009-08-15 02:31:57 +02:00
|
|
|
|
2009-08-15 03:06:18 +02:00
|
|
|
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])
|
2009-08-17 01:47:25 +02:00
|
|
|
self.image = self.frames[1]
|
2009-08-15 03:06:18 +02:00
|
|
|
|
2009-08-17 00:30:20 +02:00
|
|
|
def stop_move(self, vector):
|
2009-08-15 03:06:18 +02:00
|
|
|
""" stop moving sequence """
|
2009-08-17 00:30:20 +02:00
|
|
|
self.speed = (self.speed[0] - vector[0], self.speed[1] - vector[1])
|
|
|
|
|
|
|
|
if self.speed == (0, 0):
|
|
|
|
globals.moving_objects.remove(self)
|
2009-08-17 01:47:25 +02:00
|
|
|
self.image = self.frames[0]
|
2009-08-17 00:30:20 +02:00
|
|
|
|
|
|
|
def start_shoot(self):
|
2009-08-17 01:06:52 +02:00
|
|
|
self.shooting = True
|
2009-08-18 00:38:38 +02:00
|
|
|
self.shoot_wavepos = 0
|
2009-08-17 00:30:20 +02:00
|
|
|
|
|
|
|
def stop_shoot(self):
|
2009-08-18 00:38:38 +02:00
|
|
|
self.shoot_wavepos = 2
|
|
|
|
|
|
|
|
def change_bullet_type(self):
|
|
|
|
self.bullet_type ^= 1
|
2009-08-17 01:06:52 +02:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
self.rect.move_ip(self.speed)
|
|
|
|
if self.shooting:
|
2009-08-18 00:38:38 +02:00
|
|
|
if self.bullet_counter == self.bullet_threshold:
|
|
|
|
self.shoot()
|
|
|
|
self.bullet_counter = 0
|
2009-08-19 00:35:01 +02:00
|
|
|
else:
|
2009-08-18 00:38:38 +02:00
|
|
|
self.bullet_counter += 1
|
2009-08-15 03:06:18 +02:00
|
|
|
|
2009-08-19 00:35:01 +02:00
|
|
|
class Enemy(Object):
|
|
|
|
def __init__(self, frames, type, pos):
|
|
|
|
Object.__init__(self, frames, pos, (-1, 0))
|
|
|
|
self.counter = 0
|
2009-10-08 01:10:24 +02:00
|
|
|
self.threshold = 2
|
|
|
|
self.dying = False
|
2009-08-19 00:35:01 +02:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
if self.counter == self.threshold:
|
2009-10-08 01:10:24 +02:00
|
|
|
if self.dying:
|
|
|
|
self.frames_index += 1
|
|
|
|
if self.frames_index == 6:
|
|
|
|
globals.enemies_list.remove(self)
|
|
|
|
return
|
|
|
|
self.image = self.frames[self.frames_index]
|
|
|
|
else:
|
|
|
|
self.rect.move_ip(self.speed)
|
|
|
|
self.counter = 0
|
2009-08-19 00:35:01 +02:00
|
|
|
else:
|
|
|
|
self.counter += 1
|
|
|
|
|
|
|
|
def aim_player(self):
|
|
|
|
""" compute speed vector to aim at current player position """
|
2009-09-07 22:25:46 +02:00
|
|
|
|
|
|
|
def die(self):
|
|
|
|
""" make the enemy dying """
|
2009-10-08 01:10:24 +02:00
|
|
|
self.dying = True
|
|
|
|
self.threshold = 5
|
|
|
|
self.frames_index = 2
|
|
|
|
self.image = self.frames[self.frames_index]
|
|
|
|
globals.enemies_active_list.remove(self)
|
|
|
|
|
2009-08-19 00:35:01 +02:00
|
|
|
|
2009-08-15 02:27:58 +02:00
|
|
|
def init_gfx():
|
|
|
|
"""graphics initialisation"""
|
|
|
|
|
2009-08-17 00:30:20 +02:00
|
|
|
globals.screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y), HWSURFACE|DOUBLEBUF)
|
2009-08-15 02:27:58 +02:00
|
|
|
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()
|
2009-08-17 00:30:20 +02:00
|
|
|
random.seed()
|
2009-08-15 02:27:58 +02:00
|
|
|
#globals.event_type = {'create_enemeny' : create_enemey, 'vanish_object': vanish_object()}
|
|
|
|
|
|
|
|
|
|
|
|
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 """
|
2009-08-15 03:06:18 +02:00
|
|
|
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()
|
2009-08-19 00:35:01 +02:00
|
|
|
elif event.key == K_e:
|
2009-10-08 01:10:24 +02:00
|
|
|
e = Enemy(['enemy1_1.bmp', 'enemy1_2.bmp', 'enemy1_3.bmp', 'enemy1_4.bmp', 'enemy1_5.bmp', 'enemy1_6.bmp'], 0, (500, 200))
|
|
|
|
globals.enemies_list.add(e)
|
|
|
|
globals.enemies_active_list.add(e)
|
2009-08-18 00:38:38 +02:00
|
|
|
elif event.key == K_SPACE:
|
|
|
|
player.change_bullet_type()
|
|
|
|
|
2009-08-15 03:06:18 +02:00
|
|
|
if event.type == KEYUP:
|
|
|
|
if event.key == K_UP:
|
2009-08-17 00:30:20 +02:00
|
|
|
player.stop_move((0, -1))
|
2009-08-15 03:06:18 +02:00
|
|
|
elif event.key == K_DOWN:
|
2009-08-17 00:30:20 +02:00
|
|
|
player.stop_move((0, 1))
|
2009-08-15 03:06:18 +02:00
|
|
|
elif event.key == K_LEFT:
|
2009-08-17 00:30:20 +02:00
|
|
|
player.stop_move((-1, 0))
|
2009-08-15 03:06:18 +02:00
|
|
|
elif event.key == K_RIGHT:
|
2009-08-17 00:30:20 +02:00
|
|
|
player.stop_move((1,0))
|
2009-08-15 03:06:18 +02:00
|
|
|
elif event.key == K_x:
|
|
|
|
player.stop_shoot()
|
|
|
|
|
2009-08-15 02:27:58 +02:00
|
|
|
|
2009-08-17 00:30:20 +02:00
|
|
|
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
|
|
|
|
|
2009-09-07 22:25:46 +02:00
|
|
|
def handle_enemies_collisions(collisions):
|
|
|
|
""" take the returned dictionnary from pygame.sprite.groupcollide()"""
|
|
|
|
for bullet, enemies in collisions.iteritems():
|
|
|
|
if enemies != []:
|
|
|
|
for e in enemies:
|
|
|
|
e.die()
|
|
|
|
globals.bullets_list.remove(bullet)
|
|
|
|
|
2009-08-15 02:27:58 +02:00
|
|
|
##
|
|
|
|
# Main
|
|
|
|
##
|
|
|
|
|
|
|
|
globals = Globals()
|
|
|
|
init()
|
|
|
|
|
|
|
|
wait_keypress()
|
|
|
|
|
|
|
|
nbframe = 0
|
|
|
|
|
|
|
|
# create_player
|
2009-08-17 01:47:25 +02:00
|
|
|
player = Player(['player1_1.bmp', 'player1_2.bmp'], (50, 50), (0, 0))
|
2009-08-19 00:35:01 +02:00
|
|
|
globals.player = player
|
2009-08-15 02:27:58 +02:00
|
|
|
|
|
|
|
# background
|
2009-09-07 22:25:46 +02:00
|
|
|
globals.background = pygame.image.load(os.path.join('data', 'background1.bmp')).convert()
|
2009-08-15 02:27:58 +02:00
|
|
|
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:
|
|
|
|
|
2009-08-17 00:47:59 +02:00
|
|
|
# erase previously moved objects
|
2009-08-15 03:06:18 +02:00
|
|
|
globals.moving_objects.clear(globals.screen, globals.background)
|
|
|
|
globals.bullets_list.clear(globals.screen, globals.background)
|
2009-08-19 00:35:01 +02:00
|
|
|
globals.enemies_list.clear(globals.screen, globals.background)
|
2009-08-15 02:27:58 +02:00
|
|
|
|
2009-08-17 00:43:59 +02:00
|
|
|
# update game logic
|
2009-08-15 02:27:58 +02:00
|
|
|
globals.moving_objects.update()
|
|
|
|
globals.bullets_list.update()
|
2009-08-19 00:35:01 +02:00
|
|
|
globals.enemies_list.update()
|
2009-09-07 22:25:46 +02:00
|
|
|
|
|
|
|
# collisions
|
2009-10-08 01:10:24 +02:00
|
|
|
ret = pygame.sprite.groupcollide(globals.bullets_list, globals.enemies_active_list, False, False)
|
2009-09-07 22:25:46 +02:00
|
|
|
handle_enemies_collisions(ret)
|
|
|
|
|
2009-08-17 00:47:59 +02:00
|
|
|
|
|
|
|
# add player (if it's not already in the group). ugly, but ok for now.
|
|
|
|
globals.moving_objects.add(player)
|
2009-08-17 00:43:59 +02:00
|
|
|
|
|
|
|
# drawing
|
|
|
|
rectlist = globals.moving_objects.draw(globals.screen)
|
2009-08-15 02:27:58 +02:00
|
|
|
rectlist.extend(globals.bullets_list.draw(globals.screen))
|
2009-08-19 00:35:01 +02:00
|
|
|
rectlist.extend(globals.enemies_list.draw(globals.screen))
|
2009-08-15 02:27:58 +02:00
|
|
|
|
|
|
|
pygame.display.update(rectlist)
|
|
|
|
|
|
|
|
# pygame.display.flip()
|
|
|
|
nbframe = nbframe + 1
|
|
|
|
|
|
|
|
globals.clock.tick(60)
|
|
|
|
|
2009-08-15 03:06:18 +02:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == KEYDOWN or event.type == KEYUP:
|
|
|
|
process_key(event, player)
|
2009-08-15 02:27:58 +02:00
|
|
|
|
|
|
|
# reinitializing moving objects list
|
2009-08-15 03:06:18 +02:00
|
|
|
#globals.moving_objects = pygame.sprite.RenderUpdates()
|
2009-08-15 02:27:58 +02:00
|
|
|
|
2009-08-17 00:30:20 +02:00
|
|
|
# transition(0)
|
|
|
|
|
2009-08-15 02:27:58 +02:00
|
|
|
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...
|