From bb3d4158ca78ac53c68f1c5bca26fcef9e4010e5 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Wed, 9 Oct 2019 12:43:22 +0700 Subject: Remove useless enemy weights and clean up --- brutalmaze/characters.py | 15 +++++---------- brutalmaze/constants.py | 1 - brutalmaze/game.py | 2 +- brutalmaze/maze.py | 12 +++--------- brutalmaze/misc.py | 14 +------------- client-examples/hit-and-run.py | 5 ++--- 6 files changed, 12 insertions(+), 37 deletions(-) diff --git a/brutalmaze/characters.py b/brutalmaze/characters.py index 3fdc78e..eb07845 100644 --- a/brutalmaze/characters.py +++ b/brutalmaze/characters.py @@ -20,15 +20,15 @@ __doc__ = 'Brutal Maze module for hero and enemy classes' from collections import deque -from math import atan, atan2, gcd, sin, pi +from math import atan2, gcd, sin, pi from random import choice, randrange, shuffle from sys import modules 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, randsign, regpoly, fill_aapolygon, choices, play + ADJACENTS, EMPTY, SQRT2, ENEMIES) +from .misc import sign, randsign, regpoly, fill_aapolygon, play from .weapons import Bullet @@ -343,12 +343,7 @@ class Enemy: def die(self): """Handle the enemy's death.""" - if self.awake: - self.maze.map[self.x][self.y] = EMPTY - if self.maze.enemy_weights[self.color] > MINW + 1.5: - self.maze.enemy_weights[self.color] -= 1.5 - else: - self.maze.map[self.x][self.y] = WALL + self.maze.map[self.x][self.y] = EMPTY if self.wake else WALL self.alive = False @@ -425,7 +420,7 @@ class ScarletRed(Enemy): def new_enemy(maze, x, y): """Return an enemy of a random type in the grid (x, y).""" - color = choices(maze.enemy_weights) + color = choice(ENEMIES) try: return getattr(modules[__name__], color)(maze, x, y) except AttributeError: diff --git a/brutalmaze/constants.py b/brutalmaze/constants.py index 23fa040..4c10843 100644 --- a/brutalmaze/constants.py +++ b/brutalmaze/constants.py @@ -77,7 +77,6 @@ ENEMIES = ['Butter', 'Orange', 'Chocolate', 'Chameleon', COLOR_CODE = ascii_lowercase + '0' COLORS = {c: COLOR_CODE[i] for i, c in enumerate( color for code in ENEMIES + ['Aluminium'] for color in TANGO[code])} -MINW, MAXW = 24, 36 ENEMY_HP = 3 HERO_HP = 5 MIN_BEAT = 420 diff --git a/brutalmaze/game.py b/brutalmaze/game.py index 181346f..dc5e6d9 100644 --- a/brutalmaze/game.py +++ b/brutalmaze/game.py @@ -165,7 +165,7 @@ class Game: if event.key == self.key['mute']: if pygame.mixer.get_init() is None: pygame.mixer.init(frequency=44100) - pygame.mixer.music.load(MUSIC) + pygame.mixer.music.load(NOISE) pygame.mixer.music.set_volume(self.musicvol) pygame.mixer.music.play(-1) else: diff --git a/brutalmaze/maze.py b/brutalmaze/maze.py index d5ac3ff..e140753 100644 --- a/brutalmaze/maze.py +++ b/brutalmaze/maze.py @@ -23,14 +23,14 @@ from collections import defaultdict, deque import json from math import pi, log from os import path -from random import choice, sample, uniform +from random import choice, sample import pygame from .characters import Hero, new_enemy from .constants import ( EMPTY, WALL, HERO, ENEMY, ROAD_WIDTH, WALL_WIDTH, CELL_WIDTH, CELL_NODES, - MAZE_SIZE, MIDDLE, INIT_SCORE, ENEMIES, MINW, MAXW, SQRT2, SFX_SPAWN, + MAZE_SIZE, MIDDLE, INIT_SCORE, ENEMIES, SQRT2, SFX_SPAWN, SFX_SLASH_ENEMY, SFX_LOSE, ADJACENTS, TANGO_VALUES, BG_COLOR, FG_COLOR, COLORS, HERO_HP, ENEMY_HP, ATTACK_SPEED, MAX_WOUND, HERO_SPEED, BULLET_LIFETIME, JSON_SEPARATORS) @@ -55,7 +55,6 @@ class Maze: vx, vy (float): velocity of the maze movement (in pixels per frame) rotatex, rotatey (int): grids rotated bullets (list of .weapons.Bullet): flying bullets - enemy_weights (dict): probabilities of enemies to be created enemies (list of Enemy): alive enemies hero (Hero): the hero destx, desty (int): the grid the hero is moving to @@ -98,7 +97,6 @@ class Maze: self.vx = self.vy = 0.0 self.rotatex = self.rotatey = 0 self.bullets, self.enemies = [], [] - self.enemy_weights = {color: MINW for color in ENEMIES} self.add_enemy() self.hero = Hero(self.surface, fps, size) self.map[MIDDLE][MIDDLE] = HERO @@ -255,14 +253,11 @@ class Maze: def hit_hero(self, wound, color): """Handle the hero when he loses HP.""" - if self.enemy_weights[color] + wound < MAXW: - self.enemy_weights[color] += wound if color == 'Orange': # If called by close-range attack, this is FPS-dependant, although # in playable FPS (24 to infinity), the difference within 2%. self.hero.next_heal = abs(self.hero.next_heal * (1 - wound)) - elif (uniform(0, sum(self.enemy_weights.values())) - < self.enemy_weights[color]): + elif choice(ENEMIES) == color: self.hero.next_heal = -1.0 # what doesn't kill you heals you if color == 'Butter' or color == 'ScarletRed': wound *= ENEMY_HP @@ -529,7 +524,6 @@ class Maze: self.vx = self.vy = 0.0 self.rotatex = self.rotatey = 0 self.bullets, self.enemies = [], [] - self.enemy_weights = {color: MINW for color in ENEMIES} self.add_enemy() self.next_move = self.next_slashfx = self.hero.next_strike = 0.0 diff --git a/brutalmaze/misc.py b/brutalmaze/misc.py index a5cb78a..fe09035 100644 --- a/brutalmaze/misc.py +++ b/brutalmaze/misc.py @@ -23,7 +23,7 @@ from datetime import datetime from itertools import chain from math import degrees, cos, sin, pi from os import path -from random import shuffle, uniform +from random import shuffle import pygame from pygame.gfxdraw import filled_polygon, aapolygon @@ -87,18 +87,6 @@ def around(x, y): return chain(a, c) -def choices(d): - """Choose a random key from a dict which has values being relative - weights of the coresponding keys. - """ - population, weights = tuple(d.keys()), tuple(d.values()) - cum_weights = [weights[0]] - for weight in weights[1:]: cum_weights.append(cum_weights[-1] + weight) - num = uniform(0, cum_weights[-1]) - for i, w in enumerate(cum_weights): - if num <= w: return population[i] - - def play(sound, volume=1.0, angle=None): """Play a pygame.mixer.Sound at the given volume.""" if pygame.mixer.get_init() is None: return diff --git a/client-examples/hit-and-run.py b/client-examples/hit-and-run.py index 7bbf5a1..ef90276 100755 --- a/client-examples/hit-and-run.py +++ b/client-examples/hit-and-run.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from math import inf, atan2, degrees -from random import randrange, shuffle +from random import randrange from socket import socket AROUND = [5, 2, 1, 0, 3, 6, 7, 8] @@ -25,8 +25,7 @@ around, move = [0, 1, 2, 3, 5, 6, 7, 8], 4 while True: length = clientsocket.recv(7).decode() if length in ('', '0000000'): break # connection closed or game over - l = clientsocket.recv(int(length)).decode().split() - data = iter(l) + data = iter(clientsocket.recv(int(length)).decode().split()) nh, ne, nb, score = (int(next(data)) for _ in range(4)) maze = [list(next(data)) for _ in range(nh)] hp = (lambda c: 0 if c == 48 else 123 - c)(ord(next(data))) -- cgit 1.4.1