I'm trying to make a snake game in python (I'm quite bad at python, just starting with pygame so my code isn't the most effective) and the game is not responding to any keyboard inputs, I don't remember what I changed (shouldn't have been anything important enough to cut off keyboard inputs) but before I did this change, the code would run through the for statement 3 times before registering the keyboard input Another problem I an having with the code which I haven't looked into much (so it's probably an easy fix which I'll find if I scan through my code) is that the snake spawns on the screen in the wrong pattern, it should be 3 blocks in a row, but is instead 2 blocks, 1 block space and 1 block
I realise that I have not implemented the fact that the snake tail should follow the head so please do not make a note of that in the comments, thanks
Here is my code (Just the snake class, I don't want to clutter up the screen with too much over-complicated, terrible code)
Under the code is a small explanation of some of the variables as they may not be too obvious
Thank you to anyone who helps out
class Snake:
def __init__():
Snake.Movement = "R"
Snake.Front = 5
Snake.Back = 3
Apple.newPos()
Snake.Move()
def Move():
while True:
for event in p.event.get():
if event.type == p.KEYDOWN:
#print("aaaaaaAAAAAAAAAAAAA")
#if len(Snake.Movement) == 0:
if event.key == p.K_UP or event.key == p.K_w:
if Snake.Movement != "D":
print("DIRECTION UP")
Snake.Movement = "U"
elif event.key == p.K_DOWN or event.key == p.K_s:
if Snake.Movement != "U":
print("DIRECTION DOWN")
Snake.Movement = "D"
elif event.key == p.K_LEFT or event.key == p.K_a:
if Snake.Movement != "R":
print("DIRECTION LEFT")
Snake.Movement = "L"
elif event.key == p.K_RIGHT or event.key == p.K_d:
if Snake.Movement != "L":
print("DIRECTION RIGHT")
Snake.Movement = "R"
t.sleep(1)
for x in range(1,17):
for y in range(1,17):
if Setup.Screen[y][x] == 2:
p.draw.rect(Bg,(255,0,0),p.Rect(x*25,y*25,25,25),0,0)
elif Setup.Screen[y][x] != 0 and Setup.Screen[y][x] != 2:
if Setup.Screen[y][x] == Snake.Back:
p.draw.rect(Bg,(0,0,0),p.Rect(x*25,y*25,25,25),0,0)
elif Setup.Screen[y][x] == Snake.Front:
Snake.Front += 1
print(Snake.Movement)
if Snake.Movement == "U":
if Setup.Screen[y+1][x] == 1:
exit()
else:
p.draw.rect(Bg,(0,255,100),p.Rect(x*25,(y-1)*25,25,25),0,0)
headX = x
headY = y-1
elif Snake.Movement == "D":
if Setup.Screen[y-1][x] == 1:
exit()
else:
p.draw.rect(Bg,(0,255,100),p.Rect(x*25,(y+1)*25,25,25),0,0)
headX = x
headY = y+1
elif Snake.Movement == "L":
if Setup.Screen[y][x-1] == 1:
exit()
else:
p.draw.rect(Bg,(0,255,100),p.Rect((x-1)*25,y*25,25,25),0,0)
headX = x-1
headY = y
else:
if Setup.Screen[y][x+1] == 1:
exit()
else:
p.draw.rect(Bg,(0,255,100),p.Rect((x+1)*25,y*25,25,25),0,0)
headY = y
headX = x+1
else:
p.draw.rect(Bg,(0,255,100),p.Rect(x*25,y*25,25,25),0,0)
Snake.Back += 1
p.display.flip()
try:
Setup.Screen[headY][headX] = Snake.Front
except:
pass
- Setup.Screen[][] is a 2 dimensional array with numbers in the lists to associate with the different objects on screen (1 is a wall, 2 is an apple, 3+ is the snake).
- Snake.Front is an integer showing which number in Setup.Screen[][] the front of the snake is, as it changes each iteration.
- Snake.Back is the same as Snake.Front but for the back of the snake and isn't fully implemented yet.
- All of the print lines are from me trying to locate errors, they aren't needed