Convert to python3

This commit is contained in:
kleph 2022-01-18 02:08:18 +01:00
parent fb8a00a1da
commit 4d8cccd288

View file

@ -14,6 +14,7 @@ from math import sqrt,pow,ceil,floor
SCREEN_X = 640
SCREEN_Y = 480
class Globals:
def __init__(self):
self.screen = None
@ -32,7 +33,8 @@ class Globals:
self.player = None
self.players_list = pygame.sprite.RenderUpdates()
class Event_Spawn:
class EventSpawn:
def __init__(self, enemy_type, pos):
self.time = 0
self.enemy_type = 0
@ -43,6 +45,7 @@ class Event_Spawn:
globals.enemies_list.add(e)
globals.enemies_active_list.add(e)
class Object(pygame.sprite.Sprite):
def __init__(self, frames, pos, speed):
pygame.sprite.Sprite.__init__(self)
@ -67,8 +70,7 @@ class Object(pygame.sprite.Sprite):
self.rect.move_ip(self.speed)
class BulletFactory():
class BulletFactory:
""" BulletFactory, create bullets """
def create(self, type, pos, wavepos):
@ -90,14 +92,14 @@ class BulletFactory():
bullet.speed = (10,0)
bullet.damage = 10
return bullet
def create_type1(self, pos, wavepos):
""" create a bullet of type 1
temporary laser type """
filename = 'laser' + str(wavepos + 1) + '.bmp'
#print "filename: " + filename
#filename = 'laser1.bmp'
# print "filename: " + filename
# filename = 'laser1.bmp'
bullet = Bullet([filename], 0, pos)
bullet.speed = (10,0)
bullet.damage = 10
@ -141,6 +143,7 @@ class Bullet(Object):
""" 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)
@ -200,8 +203,8 @@ class EnemyFactory:
def __init__(self):
pass
def create(self, type, pos):
if type == 0:
def create(self, enemy_type, pos):
if enemy_type == 0:
return self.create_type0(pos)
def create_type0(self, pos):
@ -255,10 +258,10 @@ class Enemy(Object):
norm = sqrt(pow(direction[0], 2) + pow(direction[1], 2))
speed = (direction[0] / norm, direction[1] / norm)
speed2 = (ceil(speed[0] * localspeed), floor(speed[1]) * localspeed)
print "direction: " + str(direction)
print "norm: " + str(norm)
print "speed: " + str(speed)
print "speed2: " + str(speed2)
print("direction: " + str(direction))
print("norm: " + str(norm))
print( "speed: " + str(speed))
print("speed2: " + str(speed2))
print
return speed2
@ -267,7 +270,6 @@ class Enemy(Object):
return self.aim_player()
# return (-10, 0)
def die(self):
""" make the enemy dying """
self.dying = True
@ -284,21 +286,24 @@ def init_gfx():
globals.screen.fill(0)
pygame.display.flip()
def init_sound():
"""sound initialisation"""
pass
def init():
""" general initialisation"""
pygame.init()
pygame.display.set_caption('Pyshoot')
pygame.mouse.set_visible(0)
pygame.mouse.set_visible(False)
init_gfx()
globals.clock = pygame.time.Clock()
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 """
def __init__(self):
self.event_list = []
@ -312,14 +317,14 @@ class Level():
# create events
# dict events string => function
# for e in keys(globals.events_type):
self.event_list.insert(0, (50, Event_Spawn(0, (500,200)) ) )
self.event_list.insert(0, (150, Event_Spawn(0, (500,100)) ) )
self.event_list.insert(0, (150, Event_Spawn(0, (500,200)) ) )
self.event_list.insert(0, (250, Event_Spawn(0, (500,100)) ) )
self.event_list.insert(0, (250, Event_Spawn(0, (500,200)) ) )
self.event_list.insert(0, (250, Event_Spawn(0, (500,300)) ) )
self.event_list.insert(0, (50, EventSpawn(0, (500, 200))))
self.event_list.insert(0, (150, EventSpawn(0, (500, 100))))
self.event_list.insert(0, (150, EventSpawn(0, (500, 200))))
self.event_list.insert(0, (250, EventSpawn(0, (500, 100))))
self.event_list.insert(0, (250, EventSpawn(0, (500, 200))))
self.event_list.insert(0, (250, EventSpawn(0, (500, 300))))
self.next_event, event = self.event_list[-1]
print "next : " + str(self.next_event)
print("next : " + str(self.next_event))
def update(self, nbframe):
# print "(next, frame) : (" + str(self.next_event) + ", " + str(self.next_event) + ")"
@ -328,7 +333,7 @@ class Level():
frame, event = event_tuple
event.activate()
self.past_event_list.append(event)
#self.event_list.remove(event_tuple)
# self.event_list.remove(event_tuple)
if len(self.event_list) > 0:
self.next_event, event = self.event_list[-1]
else:
@ -339,7 +344,8 @@ class Level():
def end(self):
""" end of level """
pass
def wait_keypress():
""" wait for a keypress and then return """
next = 0
@ -348,6 +354,7 @@ def wait_keypress():
if event.type == KEYDOWN:
next = 1
def process_key(event, player):
""" process keyboard input """
if event.type == KEYDOWN:
@ -364,7 +371,8 @@ def process_key(event, player):
elif event.key == K_x:
player.start_shoot()
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_active_list.add(e)
elif event.key == K_SPACE:
@ -393,12 +401,13 @@ def transition(type):
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))
globals.screen.set_at((x, y), (0, 0, 0, 255))
if i % 1000 == 0 :
pygame.display.update()
globals.screen.fill((0, 0, 0))
@ -410,21 +419,24 @@ def transition_fade_to_black():
array = pygame.PixelArray(globals.screen)
pass
def transition_fade_to_white():
""" lighten every pixels on screen """
pass
def handle_enemies_collisions(collisions):
""" take the returned dictionnary from pygame.sprite.groupcollide()"""
for bullet, enemies in collisions.iteritems():
for bullet, enemies in collisions.items():
if enemies != []:
for e in enemies:
e.die()
globals.bullets_list_player.remove(bullet)
def handle_player_collisions(collisions):
""" take the returned dictionnary from pygame.sprite.groupcollide()"""
for bullet, enemies in collisions.iteritems():
for bullet, enemies in collisions.items():
# show some player reaction ;-)
globals.bullets_list.remove(bullet)
@ -432,6 +444,7 @@ def handle_player_collisions(collisions):
# Main
##
globals = Globals()
init()
@ -474,7 +487,6 @@ while not globals.die:
ret = pygame.sprite.groupcollide(globals.bullets_list, globals.players_list, False, False)
handle_player_collisions(ret)
# add player (if it's not already in the group). ugly, but ok for now.
globals.moving_objects.add(player)
@ -495,15 +507,15 @@ while not globals.die:
globals.clock.tick(60)
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)
# 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)