patternpythonMinor
Running a version of Space Invaders
Viewed 0 times
versioninvadersrunningspace
Problem
I made a program that runs my version of Space Invaders.
I recently finished it. I just want to make it more pythonic, and streamline it so it uses less memory and performs faster.
The basic outline of the program is at this website.
```
import pygame, sys, random
from pygame.locals import *
# set up pygame
pygame.init()
mainClock = pygame.time.Clock()
# set up the window
width = 800
height = 700
screen = pygame.display.set_mode((width, height), 0, 32)
pygame.display.set_caption('caption')
# set up movement variables
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
# set up direction variables
DOWNLEFT = 1
DOWNRIGHT = 3
UPLEFT = 7
UPRIGHT = 9
LEFT = 4
RIGHT = 6
UP = 8
DOWN = 2
# set up the colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (135, 206, 250)
blue1 = (236, 237, 252)
blue2 = (195, 197, 240)
blue3 = (111, 115, 196)
blue4 = (77, 81, 167)
blue5 = (111, 115, 196)
bg = (152, 155, 221)
paddle = (195, 197, 240)
MOVESPEED = 11
MOVE = 1
SHOOT = 15
# set up counting
score = 0
# set up font
font = pygame.font.SysFont('calibri', 50)
def makeplayer():
player = pygame.Rect(370, 635, 60, 25)
return player
def makeinvaders(invaders):
y = 0
for i in invaders:
x = 0
for j in range(11):
invader = pygame.Rect(75+x, 75+y, 50, 20)
i.append(invader)
x += 60
y += 45
return invaders
def makewalls(walls):
wall1 = pygame.Rect(60, 520, 120, 30)
wall2 = pygame.Rect(246, 520, 120, 30)
wall3 = pygame.Rect(432, 520, 120, 30)
wall4 = pygame.Rect(618, 520, 120, 30)
walls = [wall1, wall2, wall3, wall4]
return walls
def movepaddle(player):
if moveLeft and player.left > 0:
player.left -= MOVESPEED
if moveRight and player.right = width:
invader.left -= MOVE
invader_dir = LEFT
elif invader_dir == LEFT and row[0].left > 0:
invader.left -= M
I recently finished it. I just want to make it more pythonic, and streamline it so it uses less memory and performs faster.
The basic outline of the program is at this website.
```
import pygame, sys, random
from pygame.locals import *
# set up pygame
pygame.init()
mainClock = pygame.time.Clock()
# set up the window
width = 800
height = 700
screen = pygame.display.set_mode((width, height), 0, 32)
pygame.display.set_caption('caption')
# set up movement variables
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
# set up direction variables
DOWNLEFT = 1
DOWNRIGHT = 3
UPLEFT = 7
UPRIGHT = 9
LEFT = 4
RIGHT = 6
UP = 8
DOWN = 2
# set up the colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (135, 206, 250)
blue1 = (236, 237, 252)
blue2 = (195, 197, 240)
blue3 = (111, 115, 196)
blue4 = (77, 81, 167)
blue5 = (111, 115, 196)
bg = (152, 155, 221)
paddle = (195, 197, 240)
MOVESPEED = 11
MOVE = 1
SHOOT = 15
# set up counting
score = 0
# set up font
font = pygame.font.SysFont('calibri', 50)
def makeplayer():
player = pygame.Rect(370, 635, 60, 25)
return player
def makeinvaders(invaders):
y = 0
for i in invaders:
x = 0
for j in range(11):
invader = pygame.Rect(75+x, 75+y, 50, 20)
i.append(invader)
x += 60
y += 45
return invaders
def makewalls(walls):
wall1 = pygame.Rect(60, 520, 120, 30)
wall2 = pygame.Rect(246, 520, 120, 30)
wall3 = pygame.Rect(432, 520, 120, 30)
wall4 = pygame.Rect(618, 520, 120, 30)
walls = [wall1, wall2, wall3, wall4]
return walls
def movepaddle(player):
if moveLeft and player.left > 0:
player.left -= MOVESPEED
if moveRight and player.right = width:
invader.left -= MOVE
invader_dir = LEFT
elif invader_dir == LEFT and row[0].left > 0:
invader.left -= M
Solution
I have a few suggestions for improvement regarding PEP8 and design improvements.
If there's anything else you'd specifically like me to cover, let me know and I will.
- You have about 40 PEP8 violations. Many of them are naming issues, or issues with your spacing. For reference, here are a few things that you can improve/change:
- There should be two blank lanes between functions and classes.
- Functions and variables should be in
camel_case, and classes should be inPascalCase.
- Variables with values (constants) that don't change should be in
UPPER_CAMEL_CASE.
- You should have spaces after commas, like this
[a, b, c].
- There should be spaces between operators, e.g,
1 + 2, or5 ** 6.
- Your design pattern, is... odd to say the least. I'd go for a more object-oriented design. For example, have an
Enemyclass that contains methods and attributes that an enemy would have. You could also have anEnemyGroupclass which manages groups of enemies. In the current state, the functional design isn't very reusable, and it's hard to maintain.
If there's anything else you'd specifically like me to cover, let me know and I will.
Context
StackExchange Code Review Q#91220, answer score: 5
Revisions (0)
No revisions yet.