-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspace.py
More file actions
157 lines (122 loc) · 4.65 KB
/
space.py
File metadata and controls
157 lines (122 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#
# The main game script - render the intro, and start the main game loop
#
#
import pygame
import sys
from modules.celestial import Celestial
from modules.game import Player, Game
from pygame.locals import *
from modules.ship import Ship
from modules.settings import screen, width,height, black
pygame.init()
pygame.mixer.set_num_channels(3)
clock = pygame.time.Clock()
#
# Draw the intro, and wait for a key press to start the game
#
def intro():
# the text to render
text_lines = [
[80, "Astro Battles!!!!"],
[30, "The second and seventh planets are in the midst"],
[30, "of a terrible religious war"],
[20, "(tabs or spaces)"],
[40, "When it's your turn, use the direction buttons"],
[40, "to guide your rocket to its target"],
[30, "The less fuel you use, the more damage you do"],
[40, "Press any key to start"],
]
# now we scan through the text, and render it centered on the screen
# get the total height, we move up half this value from the centre
# so that the overall text will be centered
total_height = sum([t[0] for t in text_lines])
current_height = height / 2 - total_height / 2
# render the texts
for i in xrange(len(text_lines)):
t = text_lines[i]
basicfont = pygame.font.SysFont(None, t[0])
current_height += t[0]
text = basicfont.render(t[1], True, (255, 0, 0), (0, 0, 0))
textrect = text.get_rect()
textrect.center = (width / 2, current_height)
screen.blit(text, textrect)
pygame.display.flip()
# now, wait for a key to be pressed
while True:
event = pygame.event.wait()
if event.type == KEYDOWN:
break
#
# The main game loop
# - build the celestial elements (this includes the two players), the ship and the game
# - until the game is over, offer each player a chance to kill the other one ;)
#
def playGame():
# build the solar system
sun = Celestial("resources/images/sun.jpg", 75, 0, 1)
player1 = Player(1, Celestial("resources/images/player1.png", mass=20, radius=85, eccentricity=0.95))
player2 = Player(2, Celestial("resources/images/player2.png", mass=20, radius=340, eccentricity=0.95))
# this collection is everything that can attract the ship
planets = [
sun,
player1.celestial,
Celestial("resources/images/planet.png", mass=15, radius=123, eccentricity=1.05),
Celestial("resources/images/planet.png", mass=10, radius=175, eccentricity=0.90),
Celestial("resources/images/planet.png", mass=10, radius=250, eccentricity=0.85),
Celestial("resources/images/planet.png", mass=20, radius=300, eccentricity=0.95),
player2.celestial
]
ship = Ship()
game = Game([player1, player2], ship, planets, screen)
# the game is over when all but one player is dead
while not game.is_over():
# this is listening for someone to close the window
event = pygame.event.poll()
if event.type == pygame.QUIT:
sys.exit(0)
# clear the screen
screen.fill(black)
# let the player know whose turn it is, and how
# much fuel is left on the spaceship
game.render_state()
# move the planets in their orbits
for p in planets:
p.move()
p.blit(screen)
# move the ship by
# - dragging it towards the sun and each planet
# - if the player is pressing the direction keys, then apply that thrust
if ship.is_launched:
# while the ship is launching we ignore the home planet
attracting_planets = planets
if ship.is_launching:
attracting_planets = [e for e in planets if e != game.current_player().celestial ]
ship.apply_acceleration(attracting_planets)
ship.move()
# if the ship has hit something, or has run out of fuel, then
# then it's the next player's turn
# : if the ship hit a player and killed them then game.is_over()
# will say so
game.check_collisions()
if ship.is_ship_dead():
game.next_player()
else:
# the ship isn't launched, so check if we've been asked
# to do so
game.check_launch_trigger()
game.heal_other_players()
# redraw the frame, but we don't want a framerate > 40
pygame.display.flip()
clock.tick(40)
# if we're here, then the game is over
game.render_game_over()
#
# Ok,so now we do something ;)
#
# Draw the intro, and then keep playing games until someone closes
# the window
#
intro()
while 1:
playGame()