HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Small text-based fight

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
textsmallfightbased

Problem

I plan to flesh this out into a hopefully full 2-3 minute text adventure game, and the most fun part to start with is the combat.

The fight function down at the bottom repeats itself quite a bit, the help() function call is in multiple places and I was wondering if there's any way to reduce that? Also general tips on how I can improve would be appreciated.

```
# -- coding: utf-8 --

import random
from colorama import Fore, Back, Style
from colorama import init
from Tkinter import *

init()

'''
import pygame, sys
from pygame.locals import *

# set up pygame
pygame.init()

# set up the window
windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption('Hello world!')

# set up the colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# set up fonts
basicFont = pygame.font.SysFont(None, 48)

# set up the text
text = basicFont.render('Hello world!', True, WHITE, BLUE)
textRect = text.get_rect()
textRect.centerx = windowSurface.get_rect().centerx
textRect.centery = windowSurface.get_rect().centery

# draw the white background onto the surface
windowSurface.fill(WHITE)

# draw a green polygon onto the surface
pygame.draw.polygon(windowSurface, GREEN, ((146, 0), (291, 106), (236, 277), (56, 277), (0, 106)))

Attack: The amount of damage you do.
Health: Amount of damage you can take.
Armor: (not in yet)
Accuracy: The percentage something will hit.

'''

class Player(object):
def __init__(self, name):
self.name = name
self.attack = 3
self.health = 10
#not used
self.magic_attack = 2
self.armor = 1

def description(self):
print "%s the mighty hero!" % (self.name)

#A class full of the character's fighting abilities
class Ability(object):
def __init__(self, damage, accuracy):
self.damage = damage
self.accuracy = accuracy

def description(self, name):
#None of this is used yet, either
if name

Solution

There are multiple things you can improve/simplify.

Code Style

  • remove the extra newlines between the parts of the code, keeping 2 blank lines between the top-level class and function definitions, 1 blank line between the class methods (PEP8 reference)



  • the docstrings should be put into triple double-quotes. The module level docstring should be on top, before the import statements.



  • put the main execution code block into the if __name__ == '__main__':



  • use print() as a function instead of a statement for Python-3.x compatibility



  • when you put an inline comment, start with a space (PEP8 reference)



Code Simplifications

You can replace these multiple if/elif/elses:

def description(self, name):
    #None of this is used yet, either
    if name == "slash":
        print "A very accurate attack with low damage."

    elif name == "stab":
        print "A high damaging attack with low accuracy."

    elif name == "normal":
        print "A normal attack."


with a dictionary lookup:

ABILITY_DESCRIPTIONS = {
    "slash": "A very accurate attack with low damage.",
    "stab": "A high damaging attack with low accuracy.",
    "normal": "A normal attack."
}

def description(self, name):
    print(ABILITY_DESCRIPTIONS.get(name, "Ability description not found"))


You can replace expressions like if is_hit == True: with if is_hit: - there is no need to explicitly check for equality with True. Same applies for other places when you compare with True or False.

accuracy_calc() function can be rewritten as:

def accuracy_calc(accuracy):
    return random.randint(0, 100) <= accuracy

Code Snippets

def description(self, name):
    #None of this is used yet, either
    if name == "slash":
        print "A very accurate attack with low damage."

    elif name == "stab":
        print "A high damaging attack with low accuracy."

    elif name == "normal":
        print "A normal attack."
ABILITY_DESCRIPTIONS = {
    "slash": "A very accurate attack with low damage.",
    "stab": "A high damaging attack with low accuracy.",
    "normal": "A normal attack."
}

def description(self, name):
    print(ABILITY_DESCRIPTIONS.get(name, "Ability description not found"))
def accuracy_calc(accuracy):
    return random.randint(0, 100) <= accuracy

Context

StackExchange Code Review Q#159687, answer score: 3

Revisions (0)

No revisions yet.