Improve coding style

This commit is contained in:
Nguyễn Gia Phong 2018-10-09 21:20:38 +07:00
parent eb088c0cf1
commit bad902d02e
3 changed files with 36 additions and 32 deletions

View File

@ -94,7 +94,7 @@ class Hero:
self.next_beat -= 1000.0 / fps self.next_beat -= 1000.0 / fps
self.next_strike -= 1000.0 / fps self.next_strike -= 1000.0 / fps
full_spin = pi * 2 / self.get_sides() full_spin = pi * 2 / self.sides
if self.slashing and self.next_strike <= 0: if self.slashing and self.next_strike <= 0:
self.next_strike = ATTACK_SPEED self.next_strike = ATTACK_SPEED
self.spin_queue = randsign() * self.spin_speed self.spin_queue = randsign() * self.spin_speed
@ -105,9 +105,10 @@ class Hero:
else: else:
self.spin_queue = 0.0 self.spin_queue = 0.0
def get_sides(self): @property
"""Return the number of sides the hero has. While the hero is def sides(self):
generally a trigon, Agent Orange may turn him into a square. """Number of sides the hero has. While the hero is generally
a trigon, Agent Orange may turn him into a square.
""" """
return 3 if self.next_heal < 0 else 4 return 3 if self.next_heal < 0 else 4
@ -115,21 +116,22 @@ class Hero:
"""Turn to the given angle if the hero is not busy slashing.""" """Turn to the given angle if the hero is not busy slashing."""
if round(self.spin_queue) != 0: return if round(self.spin_queue) != 0: return
delta = (angle - self.angle + pi) % (pi * 2) - pi delta = (angle - self.angle + pi) % (pi * 2) - pi
unit = pi * 2 / self.get_sides() / self.spin_speed unit = pi * 2 / self.sides / self.spin_speed
if abs(delta) < unit: if abs(delta) < unit:
self.angle, self.spin_queue = angle, 0.0 self.angle, self.spin_queue = angle, 0.0
else: else:
self.spin_queue = delta / unit self.spin_queue = delta / unit
def get_shots(self): @property
"""Return list of Bullet shot by the hero.""" def shots(self):
"""List of Bullet the hero has just shot."""
if not self.firing or self.slashing or self.next_strike > 0: return [] if not self.firing or self.slashing or self.next_strike > 0: return []
self.next_strike = ATTACK_SPEED self.next_strike = ATTACK_SPEED
if not randrange(int(self.highness + 1)): if not randrange(int(self.highness + 1)):
return [Bullet(self.surface, self.x, self.y, return [Bullet(self.surface, self.x, self.y,
self.angle, 'Aluminium')] self.angle, 'Aluminium')]
self.highness -= 1.0 self.highness -= 1.0
n = self.get_sides() n = self.sides
corners = {randrange(n) for _ in range(n)} corners = {randrange(n) for _ in range(n)}
angles = (self.angle + pi*2*corner/n for corner in corners) angles = (self.angle + pi*2*corner/n for corner in corners)
return [Bullet(self.surface, self.x, self.y, angle, 'Aluminium') return [Bullet(self.surface, self.x, self.y, angle, 'Aluminium')
@ -141,7 +143,7 @@ class Hero:
def draw(self): def draw(self):
"""Draw the hero.""" """Draw the hero."""
trigon = regpoly(self.get_sides(), self.R, self.angle, self.x, self.y) trigon = regpoly(self.sides, self.R, self.angle, self.x, self.y)
fill_aapolygon(self.surface, trigon, self.get_color()) fill_aapolygon(self.surface, trigon, self.get_color())
def resize(self, maze_size): def resize(self, maze_size):
@ -182,17 +184,19 @@ class Enemy:
self.sfx_slash = SFX_SLASH_HERO self.sfx_slash = SFX_SLASH_HERO
def get_pos(self): @property
"""Return coordinate of the center of the enemy.""" def pos(self):
"""Coordinates (in pixels) of the center of the enemy."""
x, y = self.maze.get_pos(self.x, self.y) x, y = self.maze.get_pos(self.x, self.y)
step = self.maze.distance * ENEMY_SPEED / self.maze.fps step = self.maze.distance * ENEMY_SPEED / self.maze.fps
return x + self.offsetx*step, y + self.offsety*step return x + self.offsetx*step, y + self.offsety*step
def get_distance(self): @property
"""Return the distance from the center of the enemy to def distance(self):
the center of the maze. """Distance from the center of the enemy
to the center of the maze.
""" """
return self.maze.get_distance(*self.get_pos()) return self.maze.get_distance(*self.pos)
def place(self, x=0, y=0): def place(self, x=0, y=0):
"""Move the enemy by (x, y) (in grids).""" """Move the enemy by (x, y) (in grids)."""
@ -200,6 +204,11 @@ class Enemy:
self.y += y self.y += y
if self.awake: self.maze.map[self.x][self.y] = ENEMY if self.awake: self.maze.map[self.x][self.y] = ENEMY
@property
def spawn_volumn(self):
"""Volumn of spawning sound effect."""
return 1 - self.distance / self.maze.get_distance(0, 0) / 2
def wake(self): def wake(self):
"""Wake the enemy up if it can see the hero. """Wake the enemy up if it can see the hero.
@ -224,15 +233,13 @@ class Enemy:
return False return False
self.awake = True self.awake = True
self.maze.map[self.x][self.y] = ENEMY self.maze.map[self.x][self.y] = ENEMY
play(self.maze.sfx_spawn, play(self.maze.sfx_spawn, self.spawn_volumn, self.get_angle() + pi)
1 - self.get_distance()/self.maze.get_distance(0, 0)/2,
self.get_angle() + pi)
return True return True
def fire(self): def fire(self):
"""Return True if the enemy has just fired, False otherwise.""" """Return True if the enemy has just fired, False otherwise."""
if self.maze.hero.dead: return False if self.maze.hero.dead: return False
x, y = self.get_pos() x, y = self.pos
if (self.maze.get_distance(x, y) > FIRANGE*self.maze.distance if (self.maze.get_distance(x, y) > FIRANGE*self.maze.distance
or self.next_strike > 0 or self.next_strike > 0
or (self.x, self.y) in AROUND_HERO or self.offsetx or self.offsety or (self.x, self.y) in AROUND_HERO or self.offsetx or self.offsety
@ -269,7 +276,7 @@ class Enemy:
def get_slash(self): def get_slash(self):
"""Return the enemy's close-range damage.""" """Return the enemy's close-range damage."""
wound = (self.maze.slashd - self.get_distance()) / self.maze.hero.R wound = (self.maze.slashd - self.distance) / self.maze.hero.R
return wound if wound > 0 else 0.0 return wound if wound > 0 else 0.0
def slash(self): def slash(self):
@ -283,7 +290,7 @@ class Enemy:
the center of the screen and terminal point is the center of the center of the screen and terminal point is the center of
the enemy. the enemy.
""" """
x, y = self.get_pos() x, y = self.pos
return atan2(y - self.maze.y, x - self.maze.x) return atan2(y - self.maze.y, x - self.maze.x)
def get_color(self): def get_color(self):
@ -304,7 +311,7 @@ class Enemy:
"""Draw the enemy.""" """Draw the enemy."""
if self.isunnoticeable(): return if self.isunnoticeable(): return
radius = self.maze.distance / SQRT2 radius = self.maze.distance / SQRT2
square = regpoly(4, radius, self.angle, *self.get_pos()) square = regpoly(4, radius, self.angle, *self.pos)
fill_aapolygon(self.maze.surface, square, self.get_color()) fill_aapolygon(self.maze.surface, square, self.get_color())
def update(self): def update(self):

View File

@ -52,7 +52,7 @@ MAZE_SIZE = 10 # cells
MIDDLE = MAZE_SIZE // 2 * CELL_WIDTH MIDDLE = MAZE_SIZE // 2 * CELL_WIDTH
HEAL_SPEED = 1 # HP/s HEAL_SPEED = 1 # HP/s
HERO_SPEED = 5 # grid/s HERO_SPEED = 5 # grid/s
ENEMY_SPEED = 6 # grid/s ENEMY_SPEED = 6 # grid/s
BULLET_SPEED = 15 # grid/s BULLET_SPEED = 15 # grid/s
ATTACK_SPEED = 333.333 # ms/strike ATTACK_SPEED = 333.333 # ms/strike
MAX_WOUND = 1 # per attack turn MAX_WOUND = 1 # per attack turn

View File

@ -264,7 +264,7 @@ class Maze:
# in playable FPS (24 to infinity), the difference within 2%. # in playable FPS (24 to infinity), the difference within 2%.
self.hero.next_heal = abs(self.hero.next_heal * (1 - wound)) self.hero.next_heal = abs(self.hero.next_heal * (1 - wound))
elif (uniform(0, sum(self.enemy_weights.values())) elif (uniform(0, sum(self.enemy_weights.values()))
< self.enemy_weights[color]): < self.enemy_weights[color]):
self.hero.next_heal = -1.0 # what doesn't kill you heals you self.hero.next_heal = -1.0 # what doesn't kill you heals you
if color == 'Butter' or color == 'ScarletRed': if color == 'Butter' or color == 'ScarletRed':
wound *= ENEMY_HP wound *= ENEMY_HP
@ -283,7 +283,7 @@ class Maze:
if not self.hero.spin_queue: return if not self.hero.spin_queue: return
killist = [] killist = []
for i, enemy in enumerate(self.enemies): for i, enemy in enumerate(self.enemies):
d = self.slashd - enemy.get_distance() d = self.slashd - enemy.distance
if d > 0: if d > 0:
wound = d * SQRT2 / self.distance wound = d * SQRT2 / self.distance
if self.next_slashfx <= 0: if self.next_slashfx <= 0:
@ -299,7 +299,7 @@ class Maze:
def track_bullets(self): def track_bullets(self):
"""Handle the bullets.""" """Handle the bullets."""
self.bullets.extend(self.hero.get_shots()) self.bullets.extend(self.hero.shots)
fallen = [] fallen = []
block = (self.hero.spin_queue and self.hero.next_heal < 0 block = (self.hero.spin_queue and self.hero.next_heal < 0
and self.hero.next_strike > self.hero.spin_queue / self.fps) and self.hero.next_strike > self.hero.spin_queue / self.fps)
@ -316,17 +316,14 @@ class Maze:
enemy = new_enemy(self, gridx, gridy) enemy = new_enemy(self, gridx, gridy)
enemy.awake = True enemy.awake = True
self.map[gridx][gridy] = ENEMY self.map[gridx][gridy] = ENEMY
play(self.sfx_spawn, play(self.sfx_spawn, enemy.spawn_volumn, enemy.get_angle())
1 - enemy.get_distance()/self.get_distance(0, 0)/2,
enemy.get_angle())
enemy.hit(wound) enemy.hit(wound)
self.enemies.append(enemy) self.enemies.append(enemy)
fallen.append(i) fallen.append(i)
continue continue
for j, enemy in enumerate(self.enemies): for j, enemy in enumerate(self.enemies):
if not enemy.awake: continue if not enemy.awake: continue
x, y = enemy.get_pos() if bullet.get_distance(*enemy.pos) < self.distance:
if bullet.get_distance(x, y) < self.distance:
enemy.hit(wound) enemy.hit(wound)
if enemy.wound >= ENEMY_HP: if enemy.wound >= ENEMY_HP:
self.score += enemy.wound self.score += enemy.wound
@ -389,7 +386,7 @@ class Maze:
for enemy in self.enemies: for enemy in self.enemies:
if enemy.isunnoticeable(): continue if enemy.isunnoticeable(): continue
x, y = self.expos(*enemy.get_pos()) x, y = self.expos(*enemy.pos)
color, angle = COLORS[enemy.get_color()], deg(enemy.angle) color, angle = COLORS[enemy.get_color()], deg(enemy.angle)
export['e'].append([color, x, y, angle]) export['e'].append([color, x, y, angle])