patternpythonMinor
Alien Assault game
Viewed 0 times
gameassaultalien
Problem
I know this code is pretty awful, but, by any chance, could someone point out all the flaws you can find and tell me them? It's Python 3.2.3, by the way.
```
# Alien Assualt
# A bullet hell alien invasion game.
# Bobby Clarke
#Imports
import pygame, sys, os, random, math, easygui
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 450))
pygame.display.set_caption("Alien Assualt")
clock = pygame.time.Clock()
# Glitchy Lazer Mode
#pygame.key.set_repeat(1, 1)
def adv_range(limit1, limit2 = None, increment = 1.):
"""
Range function that accepts floats (and integers).
Usage:
adv_range(-2, 2, 0.1)
adv_range(10)
adv_range(10, increment = 0.5)
The returned value is an iterator. Use list(adv_range) for a list.
"""
if limit2 is None:
limit2, limit1 = limit1, 0.
else:
limit1 = float(limit1)
count = int(math.ceil(limit2 - limit1)/increment)
return (limit1 + n*increment for n in range(count))
def setup(num_of_enemies, enemyclass):
enemies = []
for i in range(0, num_of_enemies):
distance = i * round(400 / num_of_enemies)
direction = random.choice((True, False)) #True is right, left is false
enemies.append(enemyclass(x = random.randint(100, 440), y = distance , right = direction))
return enemies
class player():
"""The player"""
def __init__(self, x = screen.get_width() / 2 - 35, y = screen.get_height() - 45, img = "Images/player.png", speed = 3):
self.x = x
self.y = y
self.img = pygame.image.load(img)
self.rightkeys = (K_RIGHT, K_d, K_KP6) #Keys making it move left
self.leftkeys = (K_LEFT, K_a, K_KP4) #Keys making it move right
self.left = False
self.right = False
self.speed = speed
def move(self):
if self.right and self.x = 0:
self.x -= self.speed # Move left
def fire(self, shot, shots):
shots.append(shot(self))
return sh
```
# Alien Assualt
# A bullet hell alien invasion game.
# Bobby Clarke
#Imports
import pygame, sys, os, random, math, easygui
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 450))
pygame.display.set_caption("Alien Assualt")
clock = pygame.time.Clock()
# Glitchy Lazer Mode
#pygame.key.set_repeat(1, 1)
def adv_range(limit1, limit2 = None, increment = 1.):
"""
Range function that accepts floats (and integers).
Usage:
adv_range(-2, 2, 0.1)
adv_range(10)
adv_range(10, increment = 0.5)
The returned value is an iterator. Use list(adv_range) for a list.
"""
if limit2 is None:
limit2, limit1 = limit1, 0.
else:
limit1 = float(limit1)
count = int(math.ceil(limit2 - limit1)/increment)
return (limit1 + n*increment for n in range(count))
def setup(num_of_enemies, enemyclass):
enemies = []
for i in range(0, num_of_enemies):
distance = i * round(400 / num_of_enemies)
direction = random.choice((True, False)) #True is right, left is false
enemies.append(enemyclass(x = random.randint(100, 440), y = distance , right = direction))
return enemies
class player():
"""The player"""
def __init__(self, x = screen.get_width() / 2 - 35, y = screen.get_height() - 45, img = "Images/player.png", speed = 3):
self.x = x
self.y = y
self.img = pygame.image.load(img)
self.rightkeys = (K_RIGHT, K_d, K_KP6) #Keys making it move left
self.leftkeys = (K_LEFT, K_a, K_KP4) #Keys making it move right
self.left = False
self.right = False
self.speed = speed
def move(self):
if self.right and self.x = 0:
self.x -= self.speed # Move left
def fire(self, shot, shots):
shots.append(shot(self))
return sh
Solution
Some problems:
-
Return value instead of in-place operations on lists
You code is actually not that bad, you just have to read PEP8 and use pylint or something :)
- Classes uncapitalised
- Default parameter equal signs without space (def foo(a=0))
- Too long lines (you can break them!)
- Built-in paths ("Images/player.png")
- Use of too many arguments (use **kwargs instead)
-
Return value instead of in-place operations on lists
def fire(self, shot, shots):
#You are performing this operation on the mutable object you get
shots.append(shot(self, enemy = True))
#Therefore the next line is unnecessary
return shotsYou code is actually not that bad, you just have to read PEP8 and use pylint or something :)
Code Snippets
def fire(self, shot, shots):
#You are performing this operation on the mutable object you get
shots.append(shot(self, enemy = True))
#Therefore the next line is unnecessary
return shotsContext
StackExchange Code Review Q#15025, answer score: 7
Revisions (0)
No revisions yet.