-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
164 lines (134 loc) · 6.33 KB
/
run.py
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
158
159
160
161
162
163
164
#!/us/bin/python2
# -*- coding: utf-8 -*-
from pygame.color import Color
import screens
from audio import *
from input import *
from player import Player
from utils import Constants
from world import World
try:
import pygame._view
except ImportError:
pass
class Game(object):
def __init__(self):
# Initialize
pygame.init()
Keyboard.init()
Sound.init()
Sound.cycleMusic()
pygame.display.set_icon(pygame.image.load('LD34-small.bmp'))
# Center window
info = pygame.display.Info()
cx = info.current_w // 2 - Constants.WIDTH // 2
cy = info.current_h // 2 - Constants.HEIGHT // 2
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (cx, cy)
# Create window and buffer
self.screen = pygame.display.set_mode((Constants.WIDTH, Constants.HEIGHT))
self.buffer = pygame.surface.Surface((Constants.WIDTH, Constants.HEIGHT), pygame.SRCALPHA)
pygame.display.set_caption(Constants.TITLE)
# Create variables objects. These will be populated in reset
self.player = None
self.world = None
self.messages = []
# Initialize font map
self.fontmap = {"hud": pygame.font.Font("saxmono.ttf", 14),
"title": pygame.font.Font("saxmono.ttf", 48),
"option": pygame.font.Font("saxmono.ttf", 28),
"score": pygame.font.Font("saxmono.ttf", 28),
"msgtitle": pygame.font.Font("saxmono.ttf", 26),
"msgbody": pygame.font.Font("saxmono.ttf", 22)}
# Initialize main menu
screens.MainMenu.register_option("Play", Constants.GAME_SCREEN)
screens.MainMenu.register_option("Info", screens.Info)
screens.MainMenu.register_option("Settings", screens.SettingsScreen)
screens.MainMenu.register_option("Credits", screens.Credits)
screens.MainMenu.register_option("Exit", Constants.EXIT_SCREEN)
# Game Loop
self.gamescreen = screens.MainMenu()
self.clock = pygame.time.Clock()
self.running = True
while self.running:
delta = self.clock.tick(Constants.FPS)
map(self.event, pygame.event.get())
self.tick(delta)
def event(self, e):
if e.type == pygame.QUIT:
self.running = False
elif e.type == SONG_END:
Sound.cycleMusic()
def reset(self):
# Create the player
self.player = Player()
# Create the world
self.world = World()
# Empty messages
self.messages = []
def tick(self, delta):
# Poll input
Mouse.update()
Keyboard.update()
# Clear the buffer
self.buffer.fill(pygame.Color(0, 0, 0, 255))
# Tick the correct game screen
if self.gamescreen == Constants.GAME_SCREEN:
# Tick the world
self.world.tick(self.buffer, delta if (self.player.ready and self.player.health > 0) else 0)
# Tick the objects
msgs = self.player.tick(self.buffer, delta, self.world.platforms)
if len(msgs) > 0:
map(self.messages.append, msgs)
# Draw messages
self.messages = [msg for msg in self.messages if msg.alive]
for msg in self.messages:
msg.tick(self.buffer, delta, self.fontmap,
{"msgtitle": pygame.color.Color("#FFFFFF"),
"msggood": pygame.color.Color("#00FF00"),
"msgneutral": pygame.color.Color("#FFFF00"),
"msgbad": pygame.color.Color("#FF0000")})
# If the player is fallen in, close the "hatch" and show distance
if self.player.rect.top >= 50:
pygame.draw.rect(self.buffer, pygame.color.Color("#111111"), (0, 0, Constants.WIDTH, 50))
# Draw distance
txt = "%0.1fkm" % (self.world.fallen / 1000)
dist = self.fontmap["score"].render(txt, True, pygame.color.Color("#FFFFFF"))
(w, h) = self.fontmap["score"].size(txt)
self.buffer.blit(dist, (Constants.WIDTH // 2 - w // 2, 25 - h // 2))
else:
pygame.draw.rect(self.buffer, pygame.color.Color("#111111"), (0, 0, Constants.WIDTH // 2 - 75, 50))
pygame.draw.rect(self.buffer, pygame.color.Color("#111111"),
(Constants.WIDTH // 2 + 75, 0, Constants.WIDTH // 2 - 75, 50))
# Draw health bar and mass/force
self.player.draw_health(self.buffer, self.fontmap["hud"], Constants.WIDTH - 200, 5)
self.player.draw_healthbar(self.buffer, Constants.WIDTH - 200, 25, 180, 10)
self.player.draw_mass(self.buffer, self.fontmap["hud"], 10, 5)
self.player.draw_force(self.buffer, self.fontmap["hud"], 10, 25)
# Draw FPS
text = "FPS: %d" % self.clock.get_fps()
surf = self.fontmap["hud"].render(text, True, Color("#FFFFFF"))
(w, _) = self.fontmap["hud"].size(text)
pygame.draw.rect(self.buffer, pygame.color.Color(17, 17, 17, 30),
(Constants.WIDTH - w - 10, Constants.HEIGHT - 20, w + 10, 20))
self.buffer.blit(surf, (Constants.WIDTH - w - 5, Constants.HEIGHT - 18))
# If dead, send to game over screen
if self.player.disphealth == 0 and len(self.player.particles) == 0:
self.gamescreen = screens.GameOver((self.world.fallen // 100) / 10.0)
elif self.gamescreen == Constants.EXIT_SCREEN:
# On exit screen, set running to False so the game quits after this tick
self.running = False
elif isinstance(self.gamescreen, screens.Screen):
# Tick the game screen
gs = self.gamescreen.tick(self.buffer, delta, self.fontmap)
# If the game screen changed, update it
if gs is not None:
# Set the new game screen
self.gamescreen = gs if not isinstance(gs, type(screens.Screen)) else gs()
# If starting a new game, reset
if self.gamescreen == Constants.GAME_SCREEN:
self.reset()
# Paint buffer to screen
self.screen.blit(self.buffer, (0, 0))
pygame.display.flip()
if __name__ == "__main__":
game = Game()