patternpythonMinor
Improvements on Python game?
Viewed 0 times
gameimprovementspython
Problem
This is a game I made in Python. While it is not my first, I am not happy with the result. I would like suggestions on ways I can make it better, more user-friendly, and more enjoyable. Also, if you have suggestions on how to get the same effect, but with cleaner, more organized code, please make them.
Please feel free to make any suggestions you feel will help me, except to use a GUI.
```
#!/usr/bin/python
import curses
import random
import time
import math
class game:
def __init__(self):
self.instuctmsg = ['use a and d to move left and right','use w to jump if you are on the bottom',
'dont fall once you get high up','bounce on platforms to get higher',
'go through platforms from the bottom to jump higher','','hit any key to play the game']
self.ratio = {'plat':30,'player':6,'update':15}
self.platlen = 5
self.numlevels = 5
def __call__(self):
self.quit = False
self.screen.nodelay(1)
curses.noecho()
self.screen.clear()
self.gameover = False
self.scorea = [self.dims[0]-1]
self.score = 0
self.upc = 0
self.oupc = 0
self.x = self.dims[1]/2
self.y = self.dims[0]-1
self.platforms = [[1,self.dims[1]/2+5]]
self.draw()
self.screen.refresh()
self.screen.getch()
self.oq = -1
self.q = -1
i = 0
for i in range(15):
self.makeplat()
self.update()
self.update()
self.update()
while 1:
if i % self.ratio['plat'] == 0:
self.makeplat()
if i % self.ratio['player'] == 0:
if self.oupc != int(bool(self.upc)):
self.oupc = (int(bool(self.upc))+1)%2
if self.upc > 0:
if i % 15 == 0:
if self.y != self.dims[0]-1:
self.y += 1
Please feel free to make any suggestions you feel will help me, except to use a GUI.
```
#!/usr/bin/python
import curses
import random
import time
import math
class game:
def __init__(self):
self.instuctmsg = ['use a and d to move left and right','use w to jump if you are on the bottom',
'dont fall once you get high up','bounce on platforms to get higher',
'go through platforms from the bottom to jump higher','','hit any key to play the game']
self.ratio = {'plat':30,'player':6,'update':15}
self.platlen = 5
self.numlevels = 5
def __call__(self):
self.quit = False
self.screen.nodelay(1)
curses.noecho()
self.screen.clear()
self.gameover = False
self.scorea = [self.dims[0]-1]
self.score = 0
self.upc = 0
self.oupc = 0
self.x = self.dims[1]/2
self.y = self.dims[0]-1
self.platforms = [[1,self.dims[1]/2+5]]
self.draw()
self.screen.refresh()
self.screen.getch()
self.oq = -1
self.q = -1
i = 0
for i in range(15):
self.makeplat()
self.update()
self.update()
self.update()
while 1:
if i % self.ratio['plat'] == 0:
self.makeplat()
if i % self.ratio['player'] == 0:
if self.oupc != int(bool(self.upc)):
self.oupc = (int(bool(self.upc))+1)%2
if self.upc > 0:
if i % 15 == 0:
if self.y != self.dims[0]-1:
self.y += 1
Solution
A few suggestions:
Hope that's useful!
- Follow PEP8's code conventions (e.g. spaces after commas, line lengths);
- Put your
start()instance method immediately after__init__(); it took me a while to work out whereself.dimshad come from! Or, even better, combine the two methods;
- Take your opportunities to make functions. For example, you repeatedly use
self.screen.addstrto put some lines of centred text on thescreen, you could make a functioncentred_text(list_of_lines)to save some duplication;
- If you call something (e.g.
self.update) repeatedly, consider refactoring it -self.update(3)orfor _ in range(3): self.update()would be neater;
- Keep going with the OOP - try refactoring to create a
Platformclass and aPlayerclass (the latter paves the way to two-player mode!);
- Try to factor out magic numbers, like the initial 15 platforms; and
- Use clearer variable names than
oqandupc.
Hope that's useful!
Context
StackExchange Code Review Q#38601, answer score: 5
Revisions (0)
No revisions yet.