From ef70806a48a6917f64110716ec08d9174626079a Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Wed, 9 Oct 2019 11:15:55 +0700 Subject: Fix typos and optimizations --- brutalmaze/characters.py | 32 +++++++++++++------------------- brutalmaze/maze.py | 12 ++++++++---- brutalmaze/misc.py | 9 ++------- 3 files changed, 23 insertions(+), 30 deletions(-) diff --git a/brutalmaze/characters.py b/brutalmaze/characters.py index 1febf00..3fdc78e 100644 --- a/brutalmaze/characters.py +++ b/brutalmaze/characters.py @@ -20,7 +20,7 @@ __doc__ = 'Brutal Maze module for hero and enemy classes' from collections import deque -from math import atan, atan2, sin, pi +from math import atan, atan2, gcd, sin, pi from random import choice, randrange, shuffle from sys import modules @@ -28,7 +28,7 @@ from .constants import ( TANGO, HERO_HP, SFX_HEART, HEAL_SPEED, MIN_BEAT, ATTACK_SPEED, ENEMY, ENEMY_SPEED, ENEMY_HP, SFX_SLASH_HERO, MIDDLE, WALL, FIRANGE, AROUND_HERO, ADJACENTS, EMPTY, SQRT2, MINW) -from .misc import sign, cosin, randsign, regpoly, fill_aapolygon, choices, play +from .misc import sign, randsign, regpoly, fill_aapolygon, choices, play from .weapons import Bullet @@ -206,7 +206,7 @@ class Enemy: if self.awake: self.maze.map[self.x][self.y] = ENEMY @property - def spawn_volumn(self): + def spawn_volume(self): """Volumn of spawning sound effect.""" return 1 - self.distance / self.maze.get_distance(0, 0) / 2 @@ -217,24 +217,18 @@ class Enemy: has just woken it, False otherwise. """ if self.awake: return None - startx = starty = MIDDLE - stopx, stopy, distance = self.x, self.y, self.maze.distance - if startx > stopx: startx, stopx = stopx, startx - if starty > stopy: starty, stopy = stopy, starty - dx = (self.x-MIDDLE)*distance + self.maze.centerx - self.maze.x - dy = (self.y-MIDDLE)*distance + self.maze.centery - self.maze.y - mind = cosin(abs(atan(dy / dx)) if dx else 0) * distance - def get_distance(x, y): return abs(dy*x - dx*y) / (dy**2 + dx**2)**0.5 - for i in range(startx, stopx + 1): - for j in range(starty, stopy + 1): - if self.maze.map[i][j] != WALL or i == stopx and j == stopy: - continue - x, y = self.maze.get_pos(i, j) - if get_distance(x - self.maze.x, y - self.maze.y) <= mind: - return False + srcx, destx = self.x, MIDDLE + if abs(destx - srcx) != 1: srcx += sign(destx - srcx) or 1 + srcy, desty = self.y, MIDDLE + if abs(desty - srcy) != 1: srcy += sign(desty - srcy) or 1 + m, n = destx - srcx, desty - srcy + lcm = abs(m * n // gcd(m, n)) + w, u = lcm // m, lcm // n + for i in range(lcm): + if self.maze.map[srcx+i//w][srcy+i//u] == WALL: return False self.awake = True self.maze.map[self.x][self.y] = ENEMY - play(self.maze.sfx_spawn, self.spawn_volumn, self.get_angle() + pi) + play(self.maze.sfx_spawn, self.spawn_volume, self.get_angle()+pi) return True def fire(self): diff --git a/brutalmaze/maze.py b/brutalmaze/maze.py index 119eab1..d5ac3ff 100644 --- a/brutalmaze/maze.py +++ b/brutalmaze/maze.py @@ -314,7 +314,7 @@ class Maze: enemy = new_enemy(self, gridx, gridy) enemy.awake = True self.map[gridx][gridy] = ENEMY - play(self.sfx_spawn, enemy.spawn_volumn, enemy.get_angle()) + play(self.sfx_spawn, enemy.spawn_volume, enemy.get_angle()) enemy.hit(wound) self.enemies.append(enemy) continue @@ -466,7 +466,7 @@ class Maze: return True # Forest Fire algorithm with step count - queue = defaultdict(list, {0: [(self.destx, self.desty)]}) + queue = [[(self.destx, self.desty)]] visited, count, distance = set(), 1, 0 while count: if not queue[distance]: distance += 1 @@ -483,8 +483,12 @@ class Maze: return False for i, j in around(x, y): if self.map[i][j] == EMPTY and check(i, j): - queue[distance + 1].append((i, j)) - count += 1 + try: + queue[distance + 1].append((i, j)) + except IndexError: + queue.append([(i, j)]) + finally: + count += 1 # Failed to find way to move to target self.stepx = self.stepy = 0 diff --git a/brutalmaze/misc.py b/brutalmaze/misc.py index 008d7cf..a5cb78a 100644 --- a/brutalmaze/misc.py +++ b/brutalmaze/misc.py @@ -63,17 +63,12 @@ def sign(n): def deg(x): - """Convert angle x from radians to degrees casted to a nonnegative - integer. + """Convert angle x from radians to degrees, + casted to a nonnegative integer. """ return round2((lambda a: a if a > 0 else a + 360)(degrees(x))) -def cosin(x): - """Return the sum of cosine and sine of x (measured in radians).""" - return cos(x) + sin(x) - - def join(iterable, sep=' ', end='\n'): """Return a string which is the concatenation of string representations of objects in the iterable, separated by sep. -- cgit 1.4.1