Debian Codegraph @

examples/game/version5/asteroid.py

raw symbols SCIP
1 import pyglet
2 from game import asteroid, load, player
3
4 # Set up a window
5 game_window = pyglet.window.Window(800, 600)
6
7 main_batch = pyglet.graphics.Batch()
8
9 # Set up the two top labels
10 score_label = pyglet.text.Label(text="Score: 0", x=10, y=575, batch=main_batch)
11 level_label = pyglet.text.Label(text="Version 5: It's a Game!",
12 x=400, y=575, anchor_x='center', batch=main_batch)
13
14 # Set up the game over label offscreen
15 game_over_label = pyglet.text.Label(text="GAME OVER",
16 x=400, y=-300, anchor_x='center',
17 batch=main_batch, font_size=48)
18
19 counter = pyglet.window.FPSDisplay(window=game_window)
20
21 player_ship = None
22 player_lives = []
23 score = 0
24 num_asteroids = 3
25 game_objects = []
26
27 # We need to pop off as many event stack frames as we pushed on
28 # every time we reset the level.
29 event_stack_size = 0
30
31
32 def init():
33 global score, num_asteroids
34
35 score = 0
36 score_label.text = "Score: " + str(score)
37
38 num_asteroids = 3
39 reset_level(2)
40
41
42 def reset_level(num_lives=2):
43 global player_ship, player_lives, game_objects, event_stack_size
44
45 # Clear the event stack of any remaining handlers from other levels
46 while event_stack_size > 0:
47 game_window.pop_handlers()
48 event_stack_size -= 1
49
50 for life in player_lives:
51 life.delete()
52
53 # Initialize the player sprite
54 player_ship = player.Player(x=400, y=300, batch=main_batch)
55
56 # Make three sprites to represent remaining lives
57 player_lives = load.player_lives(num_lives, main_batch)
58
59 # Make some asteroids so we have something to shoot at
60 asteroids = load.asteroids(num_asteroids, player_ship.position, main_batch)
61
62 # Store all objects that update each frame in a list
63 game_objects = [player_ship] + asteroids
64
65 # Add any specified event handlers to the event handler stack
66 for obj in game_objects:
67 for handler in obj.event_handlers:
68 game_window.push_handlers(handler)
69 event_stack_size += 1
70
71
72 @game_window.event
73 def on_draw():
74 game_window.clear()
75 main_batch.draw()
76 counter.draw()
77
78
79 def update(dt):
80 global score, num_asteroids
81
82 player_dead = False
83 victory = False
84
85 # To avoid handling collisions twice, we employ nested loops of ranges.
86 # This method also avoids the problem of colliding an object with itself.
87 for i in range(len(game_objects)):
88 for j in range(i + 1, len(game_objects)):
89
90 obj_1 = game_objects[i]
91 obj_2 = game_objects[j]
92
93 # Make sure the objects haven't already been killed
94 if not obj_1.dead and not obj_2.dead:
95 if obj_1.collides_with(obj_2):
96 obj_1.handle_collision_with(obj_2)
97 obj_2.handle_collision_with(obj_1)
98
99 # Let's not modify the list while traversing it
100 to_add = []
101
102 # Check for win condition
103 asteroids_remaining = 0
104
105 for obj in game_objects:
106 obj.update(dt)
107
108 to_add.extend(obj.new_objects)
109 obj.new_objects = []
110
111 # Check for win condition
112 if isinstance(obj, asteroid.Asteroid):
113 asteroids_remaining += 1
114
115 if asteroids_remaining == 0:
116 # Don't act on victory until the end of the time step
117 victory = True
118
119 # Get rid of dead objects
120 for to_remove in [obj for obj in game_objects if obj.dead]:
121 if to_remove == player_ship:
122 player_dead = True
123 # If the dying object spawned any new objects, add those to the
124 # game_objects list later
125 to_add.extend(to_remove.new_objects)
126
127 # Remove the object from any batches it is a member of
128 to_remove.delete()
129
130 # Remove the object from our list
131 game_objects.remove(to_remove)
132
133 # Bump the score if the object to remove is an asteroid
134 if isinstance(to_remove, asteroid.Asteroid):
135 score += 1
136 score_label.text = "Score: " + str(score)
137
138 # Add new objects to the list
139 game_objects.extend(to_add)
140
141 # Check for win/lose conditions
142 if player_dead:
143 # We can just use the length of the player_lives list as the number of lives
144 if len(player_lives) > 0:
145 reset_level(len(player_lives) - 1)
146 else:
147 game_over_label.y = 300
148 elif victory:
149 num_asteroids += 1
150 player_ship.delete()
151 score += 10
152 reset_level(len(player_lives))
153
154
155 if __name__ == "__main__":
156 # Start it up!
157 init()
158
159 # Update the game 120 times per second
160 pyglet.clock.schedule_interval(update, 1 / 120.0)
161
162 # Tell pyglet to do its thing
163 pyglet.app.run()