After poking around in some areas of python, I've decided to try Pygame. After about two hours of coding, this is what I came up with:
import pygame
import time
import random
pygame.init()
pygame.font.init()
WINDOW = pygame.display.set_mode((500, 500))
pygame.display.set_caption('snake')
FOOD_COORS = []
TICK = 15
RUN = True
SNAKE_COMP = [[50, 50, 2], [40, 50, 2], [30, 50, 2], [20, 50, 2], [10, 50, 2]]
f = [random.randint(0, 50)*10, random.randint(0, 50)*10]
d = 2
CLOCK = pygame.time.Clock()
def hit():
time.sleep(3)
pygame.quit()
class snake():
def __init__(self, SNAKE_COMP):
self.x, self.y = SNAKE_COMP[0][0:2]
def draw(self, SNAKE_COMP):
self.SNAKE_COMP = SNAKE_COMP
for i in range(0, len(SNAKE_COMP)):
pygame.draw.rect(WINDOW, (255, 255, 255), (SNAKE_COMP[i][0], SNAKE_COMP[i][1], 10, 10))
def hit_check(self, SNAKE_COMP):
self.SNAKE_COMP = SNAKE_COMP
if SNAKE_COMP[0][0] >= 500 or SNAKE_COMP[0][0] < 0:
hit()
if SNAKE_COMP[0][1] >= 500 or SNAKE_COMP[0][1] < 0:
hit()
test_l = [[]]
for i in range(0, len(SNAKE_COMP)):
test_l.append(tuple(SNAKE_COMP[i][0:2]))
for i in range(0, len(test_l)):
if test_l.count(test_l[i]) > 1:
hit()
class food():
global FOOD_COORS
def draw(self):
x, y = self.x, self.y
pygame.draw.rect(WINDOW, (255, 255, 255), (x, y, 10, 10))
def spawn(self, SNAKE_COMP):
global FOOD_COORS
self.SNAKE_COMP = SNAKE_COMP
test_l = [[]]
for i in range(0, len(SNAKE_COMP)):
test_l.append(SNAKE_COMP[i][0:2])
g = True
while g:
x = random.randint(0, 49)*10
y = random.randint(0, 49)*10
if [x, y] not in test_l:
g = False
FOOD_COORS = [x, y]
self.x, self.y = x, y
snek = snake(SNAKE_COMP)
apple = food()
apple.spawn(SNAKE_COMP)
s = False
g = False
while RUN:
CLOCK.tick(TICK)
for event in pygame.event.get():
if event.type == pygame.QUIT:
RUN = False
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and d != 3:
d = 1
elif keys[pygame.K_RIGHT] and d != 4:
d = 2
elif keys[pygame.K_DOWN] and d != 1:
d = 3
elif keys[pygame.K_LEFT] and d != 2:
d = 4
if g != True and SNAKE_COMP[0][0:2] != FOOD_COORS:
last = len(SNAKE_COMP) - 1
for i in range(1, len(SNAKE_COMP)):
SNAKE_COMP[len(SNAKE_COMP)-i][2] = SNAKE_COMP[len(SNAKE_COMP)-i-1][2]
SNAKE_COMP[0][2] = d
for i in range(0, len(SNAKE_COMP)):
if SNAKE_COMP[i][2] == 1:
SNAKE_COMP[i][1] -= 10
elif SNAKE_COMP[i][2] == 2:
SNAKE_COMP[i][0] += 10
elif SNAKE_COMP[i][2] == 3:
SNAKE_COMP[i][1] += 10
elif SNAKE_COMP[i][2] == 4:
SNAKE_COMP[i][0] -= 10
else:
k = SNAKE_COMP[0][2]
FOOD_COORS.append(k)
if k == 1:
FOOD_COORS[1] -= 10
elif k == 2:
FOOD_COORS[0] += 10
elif k == 3:
FOOD_COORS[1] += 10
elif k == 4:
FOOD_COORS[0] -= 10
SNAKE_COMP.insert(0, FOOD_COORS)
apple.spawn(SNAKE_COMP)
snek.hit_check(SNAKE_COMP)
apple.draw()
snek.draw(SNAKE_COMP)
pygame.display.update()
WINDOW.fill((0, 0, 0))
pygame.quit()
I definitely thinks this can be improved, just not sure how.
3 Answers 3
This isn't really todo with the code, but, it's always a good idea to add comments to your code to make it more readable and understandable.
The names of classes should be in CamelCase instead of snake_case.
The class namessnake
andfood
should be capitalized.Argument names should be in lowercase.
SNAKE_COMP
can be changed tosnake_comp
or something similar to that.You should not instance attributes outside of
__init__
.
self.SNAKE_COMP
should be declared inside__init__
or should be removed completely, since it's not being used at all in either of the classes.You don't need parenthesis after a class name unless you are inheriting from another class.
Snake()
andFood()
should just beSnake
andFood
respectivelyAs @JamesRobinson correctly said so, add comments to the ambiguous parts of your code so the readers will be able to understand what it is supposed to do.
The variable names are not good.
What is f
? What is d
? What does SNAKE_COMP
even mean? I understand the first two values in it to be coordinates, but what is the third, and since it's always 2, why is it not a constant?
What does hit
mean? Hitting the wall? Hitting food? Hitting another snake? Collision is another common term which is a bit more clear.
What is FOOD_COORS
? Is it supposed to be COORDS ?
I guess d
is direction. Then name it direction
. And use names for the directions, don't call them 1, 2, 3, 4.
If you make the code readable, it increases the chance that others will take time to read it and give feedback.
Explore related questions
See similar questions with these tags.