0% found this document useful (0 votes)
41 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)
41 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

import pygame

import random
import queue

[Link]()

#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 = [Link].set_mode((diswidth, disheigth))


[Link].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 = [Link]()

Q = [Link](maxsize=10)

#fonts
font_style = [Link]("bahnschrift", 25)
score_font = [Link]("comicsansms", 35)

#score function
def yourscore(score):
value = score_font.render(f"Score: {score}", True, yellow)
[Link](value, [0, 0])

#funtion to add tail segments


def oursnake(snakeblock, snakelist):
for x in snakelist:
[Link](dis, black, [x[0], x[1], snakeblock, snakeblock])

#message funtion
def message(msg, color):
mesg = font_style.render(msg, True, color)
[Link](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([Link](0, diswidth - snakeblock) / 10.0) * 10.0


foody = round([Link](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:
[Link](blue)
[Link]
message("You lost! Press Q to quit or C to play again", red)

[Link]()

#button maping for game over screen


for event in [Link]():
if [Link] == [Link]:
if [Link] == pygame.K_q:
game_over = True
game_close = False
if [Link] == pygame.K_c:
gameloop()

#keypresses
for event in [Link]():
if [Link] == [Link]:
game_over = True
if [Link] == [Link]:
if [Link] == pygame.K_LEFT and direction != "right":
[Link]("left")
elif [Link] == pygame.K_RIGHT and direction != "left":
[Link]("right")
elif [Link] == pygame.K_UP and direction != "down":
[Link]("up")
elif [Link] == pygame.K_DOWN and direction != "up":
[Link]("down")
elif [Link] == pygame.K_a and direction != "right" :
[Link]("left")
elif [Link] == pygame.K_d and direction != "left":
[Link]("right")
elif [Link] == pygame.K_w and direction != "down":
[Link]("up")
elif [Link] == pygame.K_s and direction != "up":
[Link]("down")

if [Link]()is False:
next_command = [Link]()
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"]
[Link]()

x1 += x1_change
y1 += y1_change
[Link](blue)
[Link](dis, black, [x1, y1, snakeblock, snakeblock])
[Link](dis, green, [foodx, foody, snakeblock, snakeblock])
[Link]()

sankehead = [x1, y1]


[Link](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)
[Link]()

#Eats food
if x1 == foodx and y1 == foody:
foodx = round([Link](0, diswidth - snakeblock) / 10.0) * 10.0
foodx = round([Link](0, disheigth - snakeblock) / 10.0) *
10.0
snakelenght += 1
[Link](snakespeed)
[Link]()
quit()
gameloop()

You might also like