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

Python snake game

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

Problem

If anyone has the patience to take a look at my first python snake game, I'd be very grateful for any feedback. I am fairly new to programming and Python, but am looking to improve so any constructive feedback is appreciated.

```
import Tkinter
import time
import random

UP = u'\uf700'
DOWN = u'\uf701'
LEFT = u"\uf702"
RIGHT = u'\uf703'
TIMESTEP = 50
WIDTH = 300
HEIGHT = 300
colours = ['blue','red','pink','yellow','cyan','magenta','green']

class game(object):
def __init__(self):
self.window = Tkinter.Tk()
self.canvas = Tkinter.Canvas(self.window, width = WIDTH, height = HEIGHT)
self.canvas.pack()
self.leader = Rect(self.canvas,50,50,60,60,'red',10,0)
self.rect2 = Rect(self.canvas,40,50,50,60,'blue',10,0)
self.test1 = Rect(self.canvas,100,100,110,110,'pink',0,0)
self.followers = [self.rect2,self.test1,Rect(self.canvas,200,200,210,210,'yellow',0,0)]

def moveit(self):
# moves the leader and the followers follow!
prevpos = (self.leader.coords()[0],self.leader.coords()[1])
self.leader.move(self.leader.getvel()[0],self.leader.getvel()[1])

for rect in self.followers:
x = (rect.coords()[0],rect.coords()[1])
self.moveto(rect,prevpos)
prevpos = x

self.eatapple()
self.selfcollision()
self.throughwalls()

self.window.after(TIMESTEP,self.moveit)

def moveto(self,box1,pos):
#moves box1 to pos
box1.move(pos[0]-box1.coords()[0],pos[1]-box1.coords()[1])

def makeapple(self):
#This randomly puts an apple on the canvas
x1 = random.randrange(0,WIDTH,10)
y1 = random.randrange(0,HEIGHT,10)
self.apple = Rect(self.canvas,x1,y1,x1+10,y1+10, 'purple',0,0)

def eatapple(self):
#This check for eating apple, if eaten, creates new apple and grows the snake
if self.leader.coords() == self.apple.coords():
self.canvas.delete(self.apple.id)

Solution

Coding style

Please follow PEP8, the style guide of Python.
The code will become so much easier to read.

More tuple-assignments please

Use tuple-assignments more aggressively. For example, instead of this:

x1 = self.followers[-1].coords()[0]
    y1 = self.followers[-1].coords()[1]
    x2 = self.followers[-1].coords()[2]
    y2 = self.followers[-1].coords()[3]


This is simpler:

x1, y1, x2, y2 = self.followers[-1].coords()


Avoid repeated calls

Use tuple/list slicing more aggressively.
For example, these expressions are long and tedious:

prevpos = (self.leader.coords()[0],self.leader.coords()[1])
self.leader.move(self.leader.getvel()[0],self.leader.getvel()[1])


This is simpler:

prevpos = self.leader.coords()[:2]
self.leader.move(*self.leader.getvel()[:2])


Not only this is simpler, but it avoids making repeated method calls.

Use elif

These conditions are mutually exclusive:

if x1>=WIDTH:
        self.moveto(self.leader,(0,HEIGHT-y1))
    if x1<0:
        self.moveto(self.leader,(WIDTH,HEIGHT-y1))


As they cannot happen at the same time, you should chain them with an elif.

if x1>=WIDTH:
        self.moveto(self.leader,(0,HEIGHT-y1))
    elif x1<0:
        self.moveto(self.leader,(WIDTH,HEIGHT-y1))


Strange API

Most of the functions of game are not natural:

  • moveto



  • eatapple



  • grow



  • selfcollision



  • throughwalls



A game that moves? And eats apple?

Methods should represent actions that can be performed on the class.
Since "selfcollision" and "throughwalls" are not verbs,
it's not trivial what they represent.
All these methods make for a very strange API.

Code Snippets

x1 = self.followers[-1].coords()[0]
    y1 = self.followers[-1].coords()[1]
    x2 = self.followers[-1].coords()[2]
    y2 = self.followers[-1].coords()[3]
x1, y1, x2, y2 = self.followers[-1].coords()
prevpos = (self.leader.coords()[0],self.leader.coords()[1])
self.leader.move(self.leader.getvel()[0],self.leader.getvel()[1])
prevpos = self.leader.coords()[:2]
self.leader.move(*self.leader.getvel()[:2])
if x1>=WIDTH:
        self.moveto(self.leader,(0,HEIGHT-y1))
    if x1<0:
        self.moveto(self.leader,(WIDTH,HEIGHT-y1))

Context

StackExchange Code Review Q#101912, answer score: 9

Revisions (0)

No revisions yet.