S
S
import random
import queue
pygame.init()
#color values
black = (0,0,0)
white = (255, 255, 255)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
yellow = (255, 255, 0)
#resollution
diswidth = 600
disheigth = 400
#snake variables
snakeblock = 10
snakespeed = 15
compass =- {"left":[-snakeblock, 0, "left"], "right":[snakeblock, 0, "right"],
"up":[0, -snakeblock, "up"], "down":[0, snakeblock, "down"]}
clock = pygame.time.Clock()
Q = queue.Queue(maxsize=10)
#fonts
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
#score function
def yourscore(score):
value = score_font.render(f"Score: {score}", True, yellow)
dis.blit(value, [0, 0])
#message funtion
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [diswidth/10, disheigth/1.2])
def gameloop():
game_over = False
game_close = False
x1_change = 0
y1_change = 0
snakelist = []
snakelenght = 1
pygame.display.update()
#keypresses
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and direction != "right":
Q.put("left")
elif event.key == pygame.K_RIGHT and direction != "left":
Q.put("right")
elif event.key == pygame.K_UP and direction != "down":
Q.put("up")
elif event.key == pygame.K_DOWN and direction != "up":
Q.put("down")
elif event.key == pygame.K_a and direction != "right" :
Q.put("left")
elif event.key == pygame.K_d and direction != "left":
Q.put("right")
elif event.key == pygame.K_w and direction != "down":
Q.put("up")
elif event.key == pygame.K_s and direction != "up":
Q.put("down")
if Q.empty()is False:
next_command = Q.get()
if next_command == "left" and direction != "right":
x1_change, y1_change, direction = compass["left"]
elif next_command == "right" and direction != "left":
x1_change, y1_change, direction = compass["right"]
elif next_command == "up" and direction != "down":
x1_change, y1_change, direction = compass["up"]
elif next_command == "down" and direction != "up":
x1_change, y1_change, direction = compass["down"]
pygame.display.update()
x1 += x1_change
y1 += y1_change
dis.fill(blue)
pygame.draw.rect(dis, black, [x1, y1, snakeblock, snakeblock])
pygame.draw.rect(dis, green, [foodx, foody, snakeblock, snakeblock])
pygame.display.update()
oursnake(snakeblock, snakelist)
yourscore(snakelenght - 1)
pygame.display.update()
#Eats food
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, diswidth - snakeblock) / 10.0) * 10.0
foodx = round(random.randrange(0, disheigth - snakeblock) / 10.0) *
10.0
snakelenght += 1
clock.tick(snakespeed)
pygame.quit()
quit()
gameloop()