# Having trouble with making the Snake game

Hey this is my first time posting anything here or asking for help but I’m kinda stumped. I coded the game “Snake” and despite being very basic as I’m a beginner, it turned out pretty alright. The only real problem I’m having is that the key inputs are kinda messed up. I’ve already added code that doesn’t allow the snake to turn around directly into itself, but if, as an example, the snake is going up and I press the left button followed quickly by the down button it will glitch and do it anyways. My current theory was that it was because my key press code was out side of the timer that told the snake when to move, but I’ve now moved the key press stuff into the timer and yet the bug persists, perhaps even worse now. So yeah, I’m out of ideas on how to fix this and any help would be much much appreciated

btw, here is the (very messy) code:

``````rectWidth = 16
rectX = []
rectY = []
rectColor = []
body = [0]
bodyDir = [0]
gridSetup1 = True
timer = 0
left = False
right = False
up = False
down = False
apple=-1
turnCord=[]
turnDir=[]
list1 = 0
score=0
game = False
time1=0
def setup():
size(500,510)
def draw():
background(255)
if gridSetup1 == True:
gridSetup()
elif game == True:
for i in range(len(rectX)):
if i == head:
fill(0,255,0)
elif i == apple:
fill(255,0,0)
else:
fill(255,255,255)
for f in range(len(body)):
if i == body[f]:
fill(0,255,0)
rect(rectX[i],rectY[i],500/rectWidth,500/rectWidth)
timer += 1
if timer >= 7:
timer = 0
if left == True:
turnDir.append('l')
elif right == True:
turnDir.append('r')
elif up == True:
turnDir.append('u')
elif down == True:
turnDir.append('d')
for i in range(len(body)):
if i>1 and body[i]==head:
death()
game = False
for i in range(50):
bodyDir.append(500)
body.append(500)
for f in range(len(turnCord)):
if body[i] == turnCord[f]:
if turnDir[f] == 'l':
bodyDir[i] = 'l'
elif turnDir[f] == 'r':
bodyDir[i] = 'r'
elif turnDir[f] == 'u':
bodyDir[i] = 'u'
elif turnDir[f] == 'd':
bodyDir[i] = 'd'
if bodyDir[i] == 'l':
body[i] -= 1
elif bodyDir[i] == 'r':
body[i] += 1
elif bodyDir[i] == 'u':
body[i] -= rectWidth+2
elif bodyDir[i] == 'd':
body[i] += rectWidth+2
if offScreen(head) == True:
death()
game = False
while offScreen(apple) == True or apple == head:
if apple == head:
score+=1
if bodyDir[len(bodyDir)-1]=='r':
body.append(body[len(body)-1]-1)
bodyDir.append('r')
elif bodyDir[len(bodyDir)-1]=='l':
body.append(body[len(body)-1]+1)
bodyDir.append('l')
if bodyDir[len(bodyDir)-1]=='u':
body.append(body[len(body)-1]+(rectWidth+2))
bodyDir.append('u')
elif bodyDir[len(bodyDir)-1]=='d':
body.append(body[len(body)-1]-(rectWidth+2))
bodyDir.append('d')
apple = int(random(sq(rectWidth)))
for i in range(len(body)):
while apple == body[i]:
apple = int(random(sq(rectWidth)))
for i in range(len(turnCord)):
for f in range(len(turnCord)):
if turnCord[f]==turnCord[i] and f!=i:
list1 = f
if list1 > 0:
turnCord.pop(list1)
turnDir.pop(list1)
list1=-1
if key=='a' or keyCode==LEFT and (right == False or len(body) < 2):
left = True
right = False
up = False
down = False
if key=='d'or keyCode==RIGHT and (left == False or len(body) < 2):
right = True
left = False
up = False
down = False
if key == 's' or keyCode==DOWN and (up == False or len(body) < 2):
left = False
right = False
up = False
down = True
t=0
if key=='w' or keyCode==UP and (down == False or len(body) < 2):
right = False
left = False
up = True
down = False
rect(0,494,510,510)
fill(0)
textSize(100)
text(score,0,490)
def gridSetup():
x=-499/rectWidth
y=-499/rectWidth
for i in range(rectWidth+2):
for i in range(rectWidth+2):
rectColor.append(255)
rectX.append(x)
rectY.append(y)
x+=500/rectWidth
y+=500/rectWidth
x=0
apple = int(random(sq(rectWidth)))
while offScreen(head) == True:
while offScreen(apple) == True or apple == head:
apple = int(random(sq(rectWidth)))
game = True
score = 0
gridSetup1 = False
def offScreen(pos):
global rectWidth
for i in range(rectWidth*2):
if pos == ((rectWidth+2)*i)-2 or pos == ((rectWidth+2)*i)-1 or pos <= rectWidth or pos >= sq(rectWidth+2)-rectWidth+2:
return True
else:
return False
def death():
rectWidth = 16
rectX = []
rectY = []
rectColor = []
body = [0]
bodyDir = [0]
gridSetup1 = True
timer = 0
left = False
right = False
up = False
down = False
apple=-1
turnCord=[]
turnDir=[]
list1 = 0
``````

That’s a bunch of code to go through, but the easiest way I can think of would be to add a boolean value that’s made false when a button is pressed, then true once the snake moves. then you only allow inputs if the boolean is true.

So basically in pseudocode it would be;

``````boolean moveable = true;
if (buttonpressed and moveable){
set direction;
moveable = false;
}
move(){
do movement;
moveable = true;
}
``````
1 Like

I did something similar to that and it seemed to fix the problem. Thanks for the help!

1 Like