Add fancy losing screen and keybinding for new game

This commit is contained in:
Nguyễn Gia Phong 2017-10-22 20:10:40 +07:00
parent aafa943a53
commit 3e25eb424c
6 changed files with 44 additions and 22 deletions

View File

@ -1 +1 @@
include README.rst LICENSE
include README.rst LICENSE screenshot.png

View File

@ -35,6 +35,12 @@ The installation procedure should be as simply as follow:
Control
-------
F2
New game.
Escape, ``p``
Pause.
Up, ``w``
Move up.

View File

@ -39,7 +39,7 @@ class Hero:
self.R = int((w * h / sin(pi*2/3) / 624) ** 0.5)
self.next_strike = 0
self.slashing = self.firing = False
self.slashing = self.firing = self.dead = False
self.spin_speed = fps / HERO_HP
self.spin_queue = self.wound = 0.0
@ -48,7 +48,7 @@ class Hero:
old_speed, time = self.spin_speed, pygame.time.get_ticks()
self.spin_speed = fps / (HERO_HP-self.wound**0.5)
self.spin_queue *= self.spin_speed / old_speed
self.wound -= HEAL_SPEED / self.spin_speed / HERO_HP
if not self.dead: self.wound -= HEAL_SPEED / self.spin_speed / HERO_HP
if self.wound < 0: self.wound = 0.0
if self.slashing and time >= self.next_strike:
@ -63,7 +63,15 @@ class Hero:
self.angle = atan2(y - self.y, x - self.x)
self.spin_queue = 0.0
trigon = regpoly(3, self.R, self.angle, self.x, self.y)
fill_aapolygon(self.surface, trigon, self.color[int(self.wound)])
try:
fill_aapolygon(self.surface, trigon, self.color[int(self.wound)])
except IndexError: # When the hero is wounded over his HP
self.wound = HERO_HP
def die(self):
"""Handle the hero's death."""
self.dead = True
self.slashing = self.firing = False
def resize(self):
"""Resize the hero."""
@ -87,16 +95,6 @@ class Enemy:
self.spin_speed = fps / ENEMY_HP
self.spin_queue = self.wound = 0.0
def firable(self):
"""Return True if the enemies should shoot the hero,
False otherwise.
"""
if not self.awake or self.spin_queue or self.offsetx or self.offsety:
return False
else:
self.next_move = pygame.time.get_ticks() + ATTACK_SPEED
return True
def pos(self, distance, middlex, middley):
"""Return coordinate of the center of the enemy."""
x, y = pos(self.x, self.y, distance, middlex, middley)
@ -152,6 +150,16 @@ class Enemy:
color = self.color[int(self.wound)] if self.awake else FG_COLOR
fill_aapolygon(self.surface, square, color)
def firable(self):
"""Return True if the enemies should shoot the hero,
False otherwise.
"""
if (not self.awake or self.spin_queue or self.offsetx or self.offsety
or (self.x, self.y) in SURROUND_HERO):
return False
self.next_move = pygame.time.get_ticks() + ATTACK_SPEED
return True
def die(self):
"""Kill the enemy."""
"""Handle the enemy's death."""
self.maze[self.x][self.y] = EMPTY if self.awake else WALL

View File

@ -44,6 +44,8 @@ BULLET_LIFETIME = 1000 # ms
EMPTY, WALL, HERO, ENEMY = range(4)
ADJACENT_GRIDS = (1, 0), (0, 1), (-1, 0), (0, -1)
SURROUND_HERO = set((MIDDLE + x, MIDDLE + y) for x, y in
ADJACENT_GRIDS + ((1, 1), (-1, 1), (-1, -1), (1, -1)))
TANGO = {'Butter': ((252, 233, 79), (237, 212, 0), (196, 160, 0)),
'Orange': ((252, 175, 62), (245, 121, 0), (206, 92, 0)),

View File

@ -38,8 +38,14 @@ def main():
for event in events:
if event.type == QUIT:
going = False
elif event.type == VIDEORESIZE:
maze.resize(event.w, event.h)
elif event.type == KEYDOWN:
if event.key in (K_ESCAPE, K_p):
if event.key == K_F2: # new game
maze.__init__((maze.w, maze.h), fps)
elif maze.hero.dead:
continue
elif event.key in (K_ESCAPE, K_p):
maze.paused ^= True
elif event.key in (K_UP, K_w):
maze.move(up=-1)
@ -51,6 +57,8 @@ def main():
maze.move(right=-1)
elif event.key == K_RETURN:
maze.hero.slashing = True
elif maze.hero.dead:
continue
elif event.type == KEYUP:
if event.key in (K_UP, K_w):
maze.move(up=1)
@ -72,8 +80,6 @@ def main():
maze.hero.firing = False
elif event.button == 3:
maze.hero.slashing = False
elif event.type == VIDEORESIZE:
maze.resize(event.w, event.h)
if len(flash_time) > 5:
new_fps = 5000.0 / (flash_time[-1] - flash_time[0])
flash_time.popleft()

View File

@ -262,11 +262,11 @@ class Maze:
for bullet in self.bullets: bullet.place(dx, dy, self.step)
self.draw()
self.slash()
self.track_bullets()
for enemy in self.enemies:
enemy.update(fps, self.distance, self.middlex, self.middley)
self.hero.update(fps)
self.slash()
self.track_bullets()
pygame.display.flip()
pygame.display.set_caption('Brutal Maze - Score: {}'.format(
int(self.score - INIT_SCORE)))
@ -299,5 +299,5 @@ class Maze:
def lose(self):
"""Handle loses."""
print('Your score is: {}'.format(int(self.score - INIT_SCORE)))
quit()
self.hero.die()
self.up = self.left = self.down = self.right = 0