0% found this document useful (0 votes)
24 views3 pages

S

The document describes a snake game created with Pygame. It defines variables for the screen, snake, food and controls. The main game loop handles movement, collisions, scoring and resetting the food position.

Uploaded by

steven.rengifo01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

S

The document describes a snake game created with Pygame. It defines variables for the screen, snake, food and controls. The main game loop handles movement, collisions, scoring and resetting the food position.

Uploaded by

steven.rengifo01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pygame

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

#windows size and name

dis = pygame.display.set_mode((diswidth, disheigth))


pygame.display.set_caption("Snake")

#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])

#funtion to add tail segments


def oursnake(snakeblock, snakelist):
for x in snakelist:
pygame.draw.rect(dis, black, [x[0], x[1], snakeblock, snakeblock])

#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

direction = (" ")


x1 = diswidth / 2
y1 = disheigth / 2

x1_change = 0
y1_change = 0

snakelist = []
snakelenght = 1

foodx = round(random.randrange(0, diswidth - snakeblock) / 10.0) * 10.0


foody = round(random.randrange(0, disheigth - snakeblock) / 10.0) * 10.0

#the main while loop


while not game_over:
#this is what happens if the snake collides with itself or with the walls
while game_close == True:
dis.fill(blue)
pygame.HWSURFACE
message("You lost! Press Q to quit or C to play again", red)

pygame.display.update()

#button maping for game over screen


for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameloop()

#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()

sankehead = [x1, y1]


snakelist.append(sankehead)
if len(snakelist) > snakelenght:
del snakelist[0]
for x in snakelist[:-1]:
if x == sankehead:
game_close = True

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()

You might also like