Convert to python3
This commit is contained in:
parent
fb8a00a1da
commit
4d8cccd288
1 changed files with 46 additions and 34 deletions
80
pyshoot.py
80
pyshoot.py
|
@ -14,6 +14,7 @@ from math import sqrt,pow,ceil,floor
|
||||||
SCREEN_X = 640
|
SCREEN_X = 640
|
||||||
SCREEN_Y = 480
|
SCREEN_Y = 480
|
||||||
|
|
||||||
|
|
||||||
class Globals:
|
class Globals:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.screen = None
|
self.screen = None
|
||||||
|
@ -32,7 +33,8 @@ class Globals:
|
||||||
self.player = None
|
self.player = None
|
||||||
self.players_list = pygame.sprite.RenderUpdates()
|
self.players_list = pygame.sprite.RenderUpdates()
|
||||||
|
|
||||||
class Event_Spawn:
|
|
||||||
|
class EventSpawn:
|
||||||
def __init__(self, enemy_type, pos):
|
def __init__(self, enemy_type, pos):
|
||||||
self.time = 0
|
self.time = 0
|
||||||
self.enemy_type = 0
|
self.enemy_type = 0
|
||||||
|
@ -43,6 +45,7 @@ class Event_Spawn:
|
||||||
globals.enemies_list.add(e)
|
globals.enemies_list.add(e)
|
||||||
globals.enemies_active_list.add(e)
|
globals.enemies_active_list.add(e)
|
||||||
|
|
||||||
|
|
||||||
class Object(pygame.sprite.Sprite):
|
class Object(pygame.sprite.Sprite):
|
||||||
def __init__(self, frames, pos, speed):
|
def __init__(self, frames, pos, speed):
|
||||||
pygame.sprite.Sprite.__init__(self)
|
pygame.sprite.Sprite.__init__(self)
|
||||||
|
@ -67,8 +70,7 @@ class Object(pygame.sprite.Sprite):
|
||||||
self.rect.move_ip(self.speed)
|
self.rect.move_ip(self.speed)
|
||||||
|
|
||||||
|
|
||||||
|
class BulletFactory:
|
||||||
class BulletFactory():
|
|
||||||
""" BulletFactory, create bullets """
|
""" BulletFactory, create bullets """
|
||||||
|
|
||||||
def create(self, type, pos, wavepos):
|
def create(self, type, pos, wavepos):
|
||||||
|
@ -90,14 +92,14 @@ class BulletFactory():
|
||||||
bullet.speed = (10,0)
|
bullet.speed = (10,0)
|
||||||
bullet.damage = 10
|
bullet.damage = 10
|
||||||
return bullet
|
return bullet
|
||||||
|
|
||||||
def create_type1(self, pos, wavepos):
|
def create_type1(self, pos, wavepos):
|
||||||
""" create a bullet of type 1
|
""" create a bullet of type 1
|
||||||
temporary laser type """
|
temporary laser type """
|
||||||
|
|
||||||
filename = 'laser' + str(wavepos + 1) + '.bmp'
|
filename = 'laser' + str(wavepos + 1) + '.bmp'
|
||||||
#print "filename: " + filename
|
# print "filename: " + filename
|
||||||
#filename = 'laser1.bmp'
|
# filename = 'laser1.bmp'
|
||||||
bullet = Bullet([filename], 0, pos)
|
bullet = Bullet([filename], 0, pos)
|
||||||
bullet.speed = (10,0)
|
bullet.speed = (10,0)
|
||||||
bullet.damage = 10
|
bullet.damage = 10
|
||||||
|
@ -141,6 +143,7 @@ class Bullet(Object):
|
||||||
""" detect if bullet is in the screen """
|
""" detect if bullet is in the screen """
|
||||||
return self.rect.colliderect(globals.screen.get_rect())
|
return self.rect.colliderect(globals.screen.get_rect())
|
||||||
|
|
||||||
|
|
||||||
class Player(Object):
|
class Player(Object):
|
||||||
def __init__(self, frames, pos, speed):
|
def __init__(self, frames, pos, speed):
|
||||||
Object.__init__(self, frames, pos, speed)
|
Object.__init__(self, frames, pos, speed)
|
||||||
|
@ -200,8 +203,8 @@ class EnemyFactory:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def create(self, type, pos):
|
def create(self, enemy_type, pos):
|
||||||
if type == 0:
|
if enemy_type == 0:
|
||||||
return self.create_type0(pos)
|
return self.create_type0(pos)
|
||||||
|
|
||||||
def create_type0(self, pos):
|
def create_type0(self, pos):
|
||||||
|
@ -255,10 +258,10 @@ class Enemy(Object):
|
||||||
norm = sqrt(pow(direction[0], 2) + pow(direction[1], 2))
|
norm = sqrt(pow(direction[0], 2) + pow(direction[1], 2))
|
||||||
speed = (direction[0] / norm, direction[1] / norm)
|
speed = (direction[0] / norm, direction[1] / norm)
|
||||||
speed2 = (ceil(speed[0] * localspeed), floor(speed[1]) * localspeed)
|
speed2 = (ceil(speed[0] * localspeed), floor(speed[1]) * localspeed)
|
||||||
print "direction: " + str(direction)
|
print("direction: " + str(direction))
|
||||||
print "norm: " + str(norm)
|
print("norm: " + str(norm))
|
||||||
print "speed: " + str(speed)
|
print( "speed: " + str(speed))
|
||||||
print "speed2: " + str(speed2)
|
print("speed2: " + str(speed2))
|
||||||
print
|
print
|
||||||
return speed2
|
return speed2
|
||||||
|
|
||||||
|
@ -267,7 +270,6 @@ class Enemy(Object):
|
||||||
return self.aim_player()
|
return self.aim_player()
|
||||||
# return (-10, 0)
|
# return (-10, 0)
|
||||||
|
|
||||||
|
|
||||||
def die(self):
|
def die(self):
|
||||||
""" make the enemy dying """
|
""" make the enemy dying """
|
||||||
self.dying = True
|
self.dying = True
|
||||||
|
@ -284,21 +286,24 @@ def init_gfx():
|
||||||
globals.screen.fill(0)
|
globals.screen.fill(0)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
|
|
||||||
def init_sound():
|
def init_sound():
|
||||||
"""sound initialisation"""
|
"""sound initialisation"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
""" general initialisation"""
|
""" general initialisation"""
|
||||||
pygame.init()
|
pygame.init()
|
||||||
pygame.display.set_caption('Pyshoot')
|
pygame.display.set_caption('Pyshoot')
|
||||||
pygame.mouse.set_visible(0)
|
pygame.mouse.set_visible(False)
|
||||||
init_gfx()
|
init_gfx()
|
||||||
globals.clock = pygame.time.Clock()
|
globals.clock = pygame.time.Clock()
|
||||||
random.seed()
|
random.seed()
|
||||||
#globals.event_type = {'create_enemeny' : create_enemey, 'vanish_object': vanish_object()}
|
# globals.event_type = {'create_enemeny' : create_enemey, 'vanish_object': vanish_object()}
|
||||||
|
|
||||||
class Level():
|
|
||||||
|
class Level:
|
||||||
""" Level holder """
|
""" Level holder """
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.event_list = []
|
self.event_list = []
|
||||||
|
@ -312,14 +317,14 @@ class Level():
|
||||||
# create events
|
# create events
|
||||||
# dict events string => function
|
# dict events string => function
|
||||||
# for e in keys(globals.events_type):
|
# for e in keys(globals.events_type):
|
||||||
self.event_list.insert(0, (50, Event_Spawn(0, (500,200)) ) )
|
self.event_list.insert(0, (50, EventSpawn(0, (500, 200))))
|
||||||
self.event_list.insert(0, (150, Event_Spawn(0, (500,100)) ) )
|
self.event_list.insert(0, (150, EventSpawn(0, (500, 100))))
|
||||||
self.event_list.insert(0, (150, Event_Spawn(0, (500,200)) ) )
|
self.event_list.insert(0, (150, EventSpawn(0, (500, 200))))
|
||||||
self.event_list.insert(0, (250, Event_Spawn(0, (500,100)) ) )
|
self.event_list.insert(0, (250, EventSpawn(0, (500, 100))))
|
||||||
self.event_list.insert(0, (250, Event_Spawn(0, (500,200)) ) )
|
self.event_list.insert(0, (250, EventSpawn(0, (500, 200))))
|
||||||
self.event_list.insert(0, (250, Event_Spawn(0, (500,300)) ) )
|
self.event_list.insert(0, (250, EventSpawn(0, (500, 300))))
|
||||||
self.next_event, event = self.event_list[-1]
|
self.next_event, event = self.event_list[-1]
|
||||||
print "next : " + str(self.next_event)
|
print("next : " + str(self.next_event))
|
||||||
|
|
||||||
def update(self, nbframe):
|
def update(self, nbframe):
|
||||||
# print "(next, frame) : (" + str(self.next_event) + ", " + str(self.next_event) + ")"
|
# print "(next, frame) : (" + str(self.next_event) + ", " + str(self.next_event) + ")"
|
||||||
|
@ -328,7 +333,7 @@ class Level():
|
||||||
frame, event = event_tuple
|
frame, event = event_tuple
|
||||||
event.activate()
|
event.activate()
|
||||||
self.past_event_list.append(event)
|
self.past_event_list.append(event)
|
||||||
#self.event_list.remove(event_tuple)
|
# self.event_list.remove(event_tuple)
|
||||||
if len(self.event_list) > 0:
|
if len(self.event_list) > 0:
|
||||||
self.next_event, event = self.event_list[-1]
|
self.next_event, event = self.event_list[-1]
|
||||||
else:
|
else:
|
||||||
|
@ -339,7 +344,8 @@ class Level():
|
||||||
def end(self):
|
def end(self):
|
||||||
""" end of level """
|
""" end of level """
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def wait_keypress():
|
def wait_keypress():
|
||||||
""" wait for a keypress and then return """
|
""" wait for a keypress and then return """
|
||||||
next = 0
|
next = 0
|
||||||
|
@ -348,6 +354,7 @@ def wait_keypress():
|
||||||
if event.type == KEYDOWN:
|
if event.type == KEYDOWN:
|
||||||
next = 1
|
next = 1
|
||||||
|
|
||||||
|
|
||||||
def process_key(event, player):
|
def process_key(event, player):
|
||||||
""" process keyboard input """
|
""" process keyboard input """
|
||||||
if event.type == KEYDOWN:
|
if event.type == KEYDOWN:
|
||||||
|
@ -364,7 +371,8 @@ def process_key(event, player):
|
||||||
elif event.key == K_x:
|
elif event.key == K_x:
|
||||||
player.start_shoot()
|
player.start_shoot()
|
||||||
elif event.key == K_e:
|
elif event.key == K_e:
|
||||||
e = Enemy(['enemy1_1.bmp', 'enemy1_2.bmp', 'enemy1_3.bmp', 'enemy1_4.bmp', 'enemy1_5.bmp', 'enemy1_6.bmp'], 0, (500, 200))
|
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_list.add(e)
|
||||||
globals.enemies_active_list.add(e)
|
globals.enemies_active_list.add(e)
|
||||||
elif event.key == K_SPACE:
|
elif event.key == K_SPACE:
|
||||||
|
@ -393,12 +401,13 @@ def transition(type):
|
||||||
elif type == 2:
|
elif type == 2:
|
||||||
transition_fade_to_white()
|
transition_fade_to_white()
|
||||||
|
|
||||||
|
|
||||||
def transition_disintegrate():
|
def transition_disintegrate():
|
||||||
""" put black pixels at randoom location"""
|
""" put black pixels at randoom location"""
|
||||||
for i in range(0, 200000):
|
for i in range(0, 200000):
|
||||||
x = random.randint(0, SCREEN_X)
|
x = random.randint(0, SCREEN_X)
|
||||||
y = random.randint(0, SCREEN_Y)
|
y = random.randint(0, SCREEN_Y)
|
||||||
globals.screen.set_at((x, y), (0,0,0, 255))
|
globals.screen.set_at((x, y), (0, 0, 0, 255))
|
||||||
if i % 1000 == 0 :
|
if i % 1000 == 0 :
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
globals.screen.fill((0, 0, 0))
|
globals.screen.fill((0, 0, 0))
|
||||||
|
@ -410,21 +419,24 @@ def transition_fade_to_black():
|
||||||
array = pygame.PixelArray(globals.screen)
|
array = pygame.PixelArray(globals.screen)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def transition_fade_to_white():
|
def transition_fade_to_white():
|
||||||
""" lighten every pixels on screen """
|
""" lighten every pixels on screen """
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def handle_enemies_collisions(collisions):
|
def handle_enemies_collisions(collisions):
|
||||||
""" take the returned dictionnary from pygame.sprite.groupcollide()"""
|
""" take the returned dictionnary from pygame.sprite.groupcollide()"""
|
||||||
for bullet, enemies in collisions.iteritems():
|
for bullet, enemies in collisions.items():
|
||||||
if enemies != []:
|
if enemies != []:
|
||||||
for e in enemies:
|
for e in enemies:
|
||||||
e.die()
|
e.die()
|
||||||
globals.bullets_list_player.remove(bullet)
|
globals.bullets_list_player.remove(bullet)
|
||||||
|
|
||||||
|
|
||||||
def handle_player_collisions(collisions):
|
def handle_player_collisions(collisions):
|
||||||
""" take the returned dictionnary from pygame.sprite.groupcollide()"""
|
""" take the returned dictionnary from pygame.sprite.groupcollide()"""
|
||||||
for bullet, enemies in collisions.iteritems():
|
for bullet, enemies in collisions.items():
|
||||||
# show some player reaction ;-)
|
# show some player reaction ;-)
|
||||||
globals.bullets_list.remove(bullet)
|
globals.bullets_list.remove(bullet)
|
||||||
|
|
||||||
|
@ -432,6 +444,7 @@ def handle_player_collisions(collisions):
|
||||||
# Main
|
# Main
|
||||||
##
|
##
|
||||||
|
|
||||||
|
|
||||||
globals = Globals()
|
globals = Globals()
|
||||||
init()
|
init()
|
||||||
|
|
||||||
|
@ -474,7 +487,6 @@ while not globals.die:
|
||||||
ret = pygame.sprite.groupcollide(globals.bullets_list, globals.players_list, False, False)
|
ret = pygame.sprite.groupcollide(globals.bullets_list, globals.players_list, False, False)
|
||||||
handle_player_collisions(ret)
|
handle_player_collisions(ret)
|
||||||
|
|
||||||
|
|
||||||
# add player (if it's not already in the group). ugly, but ok for now.
|
# add player (if it's not already in the group). ugly, but ok for now.
|
||||||
globals.moving_objects.add(player)
|
globals.moving_objects.add(player)
|
||||||
|
|
||||||
|
@ -495,15 +507,15 @@ while not globals.die:
|
||||||
globals.clock.tick(60)
|
globals.clock.tick(60)
|
||||||
|
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == KEYDOWN or event.type == KEYUP:
|
if event.type == KEYDOWN or event.type == KEYUP:
|
||||||
process_key(event, player)
|
process_key(event, player)
|
||||||
|
|
||||||
# reinitializing moving objects list
|
# reinitializing moving objects list
|
||||||
#globals.moving_objects = pygame.sprite.RenderUpdates()
|
# globals.moving_objects = pygame.sprite.RenderUpdates()
|
||||||
|
|
||||||
#transition(0)
|
# transition(0)
|
||||||
|
|
||||||
print "frames : %d" % nbframe
|
print("frames : %d" % nbframe)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue