patternpythonMinor
Bunny and Badgers: A tower defense game
Viewed 0 times
defensegameandbadgerstowerbunny
Problem
I made a tower-defense game in Python with Pygame called "Bunny and Badgers". You're a bunny and must kill the badgers before they come into the castles. You can use W, A, S, D and the arrow keys to move around and press a mouse button to shoot. You can also use a joystick to control it. The game dures 1 minute and 30 seconds (or shorter if you're dead). When a badger came into a castle, the health will be increased.
The project is also on Github.
I mainly want to improve the speed. And I want to remove the hacky way of using settings.py (by using ConfigParser?). So, how can I improve my code?
Screenshot:
main.py
menu.py
```
#!/usr/bin/env python2
# Import libraries
try:
import pygame_sdl2
pygame_sdl2.import_as_pygame()
import pygame, sys, singleplayer, settings, jstest
from pygame.locals import *
except:
import pygame, sys, singleplayer, settings, jstest
from pygame.locals import *
def launch():
# Initialize pygame
pygame.init()
# Initialize the pygame font module
pygame.font.init()
# Set the width and height of the window
width, height = int(pygame.display.Info().current_w), int(pygame.display.Info().current_h)
# Create the window
screen = pygame.display.set_mode((width, height), pygame.HWSURFACE | pygame.DOUBLEBUF)
# Set title
pygame.display.set_caption("Bunny and Badgers")
# Initialize the joystick module
pygame.joystick.init()
# Check if there are any joysticks
joystick_count = pygame.joystick.get_count()
# If there are any joysticks, initialize the first one, else quit the joystick module
if joystick_count:
joystick = pygame.joystick.Joystick(0)
joystick.init()
JS = True
else:
pygame.joystick.quit()
JS = False
# Set choice
choice = 1
Schoice = 1
MorS = "main"
# Set 1/12 height
height12 = height/12
# Load i
The project is also on Github.
I mainly want to improve the speed. And I want to remove the hacky way of using settings.py (by using ConfigParser?). So, how can I improve my code?
Screenshot:
main.py
#!/usr/bin/env python2
import menu
# Launch menu
menu.launch()menu.py
```
#!/usr/bin/env python2
# Import libraries
try:
import pygame_sdl2
pygame_sdl2.import_as_pygame()
import pygame, sys, singleplayer, settings, jstest
from pygame.locals import *
except:
import pygame, sys, singleplayer, settings, jstest
from pygame.locals import *
def launch():
# Initialize pygame
pygame.init()
# Initialize the pygame font module
pygame.font.init()
# Set the width and height of the window
width, height = int(pygame.display.Info().current_w), int(pygame.display.Info().current_h)
# Create the window
screen = pygame.display.set_mode((width, height), pygame.HWSURFACE | pygame.DOUBLEBUF)
# Set title
pygame.display.set_caption("Bunny and Badgers")
# Initialize the joystick module
pygame.joystick.init()
# Check if there are any joysticks
joystick_count = pygame.joystick.get_count()
# If there are any joysticks, initialize the first one, else quit the joystick module
if joystick_count:
joystick = pygame.joystick.Joystick(0)
joystick.init()
JS = True
else:
pygame.joystick.quit()
JS = False
# Set choice
choice = 1
Schoice = 1
MorS = "main"
# Set 1/12 height
height12 = height/12
# Load i
Solution
This not a full review.
Your functions are too long for my taste. I try to keep my functions to at most 10-15 lines. Breaking your code in smaller functions is some type of documentation which improves readability.
You have a lot of
with all other types of conditions mixed in. It makes for a very hard read. There are probably ways you can "fix" that, which will probably involve using more and shorter functions.
is just
Your functions are too long for my taste. I try to keep my functions to at most 10-15 lines. Breaking your code in smaller functions is some type of documentation which improves readability.
You have a lot of
if choice == 1:
choice = 2
elif choice == 2:
choice = 3
elif choice == 3:
choice = 1with all other types of conditions mixed in. It makes for a very hard read. There are probably ways you can "fix" that, which will probably involve using more and shorter functions.
if settings.getFullscreen() == True:
elif settings.getFullscreen() == False:is just
if settings.getFullscreen():
else:Code Snippets
if choice == 1:
choice = 2
elif choice == 2:
choice = 3
elif choice == 3:
choice = 1if settings.getFullscreen() == True:
elif settings.getFullscreen() == False:if settings.getFullscreen():
else:Context
StackExchange Code Review Q#103942, answer score: 2
Revisions (0)
No revisions yet.