Refine Plum and Scarlet Red

This commit is contained in:
Nguyễn Gia Phong 2017-11-21 18:01:32 +07:00
parent 2c5cba167e
commit fffdd969ad
2 changed files with 30 additions and 16 deletions

View File

@ -186,7 +186,9 @@ class Enemy:
if self.offsety:
self.offsety -= sign(self.offsety)
return True
if self.next_strike > pygame.time.get_ticks(): return False
if (self.next_strike > pygame.time.get_ticks()
or (self.x, self.y) in AROUND_HERO):
return False
self.move_speed = self.maze.fps / speed
directions = [(sign(MIDDLE - self.x), 0), (0, sign(MIDDLE - self.y))]
@ -247,7 +249,7 @@ class Enemy:
class Chameleon(Enemy):
"""Object representing an enemy of Chameleon type.
"""Object representing an enemy of Chameleon.
Additional attributes:
visible (int): the tick until which the Chameleon is visible
@ -272,8 +274,25 @@ class Chameleon(Enemy):
self.wound += wound
class Plum(Enemy):
"""Object representing an enemy of Plum."""
def __init__(self, maze, x, y):
Enemy.__init__(self, maze, x, y, 'Plum')
def clone(self, other):
"""Turn the other enemy into a clone of this Plum and return
True if that enemy is also a Plum, otherwise return False.
"""
if other.color != 'Plum': return False
other.x, other.y, other.angle = self.x, self.y, self.angle
other.awake, other.next_strike = True, self.next_strike
other.offsetx, other.offsety = self.offsetx, self.offsety
other.spin_queue, other.wound = self.spin_queue, self.wound
return True
class ScarletRed(Enemy):
"""Object representing an enemy of Scarlet Red type."""
"""Object representing an enemy of Scarlet Red."""
def __init__(self, maze, x, y):
Enemy.__init__(self, maze, x, y, 'ScarletRed')

View File

@ -100,22 +100,17 @@ class Maze:
def add_enemy(self):
"""Add enough enemies."""
walls, plum = [], None
for i in self.rangex:
for j in self.rangey:
if self.map[i][j] == WALL: walls.append((i, j))
for enemy in self.enemies:
if enemy.color == 'Plum' and enemy.awake: plum = enemy
walls = [(i, j) for i in self.rangex for j in self.rangey
if self.map[i][j] == WALL]
plums = [e for e in self.enemies if e.color == 'Plum' and e.awake]
plum = choice(plums) if plums else None
while walls and len(self.enemies) < log(self.score, INIT_SCORE):
x, y = choice(walls)
if all(self.map[x + a][y + b] == WALL for a, b in ADJACENT_GRIDS):
continue
enemy = new_enemy(self, x, y)
self.enemies.append(enemy)
if plum is None or enemy.color != 'Plum':
walls.remove((x, y))
else:
enemy.x, enemy.y, enemy.wound = plum.x, plum.y, plum.wound
if plum is None or not plum.clone(enemy): walls.remove((x, y))
def get_pos(self, x, y):
"""Return coordinate of the center of the grid (x, y)."""
@ -171,11 +166,11 @@ class Maze:
self.rotatey = 0
for i in range(MAZE_SIZE):
b, c = getrandbits(1), (i-1)*CELL_WIDTH + self.rotatex
for j, grid in enumerate(cell(b)):
for j, grid in enumerate(new_cell(b)):
for k in range(ROAD_WIDTH):
self.map[c + k][LAST_ROW + j] = grid
c += ROAD_WIDTH
for j, grid in enumerate(cell(b, False)):
for j, grid in enumerate(new_cell(b, False)):
for k in range(ROAD_WIDTH):
self.map[c + k][LAST_ROW + j] = grid
@ -190,7 +185,7 @@ class Maze:
fx = (uniform(0, sum(self.enemy_weights.values()))
< self.enemy_weights[color])
time = pygame.time.get_ticks()
if color == 'Butter' and fx:
if (color == 'Butter' or color == 'ScarletRed') and fx:
self.hero.wound += 1.0
elif color == 'Orange' and fx:
self.hero.next_heal = max(self.hero.next_heal, time) + wound*1000