# V 1.
0 - Functional game
# V 1.01 - Improves the readability, improves slightly the performance
import pygame
import sys
import time
import random
def main():
"""Snake v 1.01"""
score = 0 # Initial score
fps = [Link]()
direction = "RIGHT" # Initial direction
snake_position = [100, 50] # Initial snake position
snake_body = [[100, 50], [90, 50], [100, 50]] # Initial snake body
# It places the food randomly, excluding the border
food_position = [[Link](1, 71) * 10, [Link](1, 45) * 10]
food_spawn = True
# Game surface
player_screen = [Link].set_mode((720, 460)) # Set screen size
[Link].set_caption("Snake v.1.01") # Set screen title and version
# Will define the colors
red = [Link]("red")
green = [Link]("green")
black = [Link]("black")
orange = [Link]("orange")
white = [Link]("white")
def bug_check():
""" Checks the mistakes, and closes the program if it does while
printing on the console how many bugs it has """
bugs = [Link]()
if bugs[1] > 0:
print("There are", bugs[1], "bugs! quiting.....")
[Link](3)
[Link]("Closing program")
else:
print("The game was initialized")
def you_lose():
""" When the players loses, it will show a red message in times new
roman font with 44 px size in a rectangle box"""
font_game_over = [Link]("times new roman", 44)
game_over_surface = font_game_over.render("Game over :(", True, red)
game_over_position = game_over_surface.get_rect()
game_over_position.midtop = (360, 15)
player_screen.blit(game_over_surface, game_over_position)
scoring(1)
[Link]() # Updates the screen, so it doesnt freeze
quiting()
def quiting():
""" When this function is called, it will wait 4 seconds and exit"""
[Link](4)
[Link]()
[Link]()
def scoring(game_over=0):
""" It will show the score on the top-left side of the screen in times new
roman font with 16px size and black color in a rectangle box"""
score_font = [Link]("times new roman", 16)
score_surface = score_font.render("Score : {}".format(score), True,
black)
score_position = score_surface.get_rect()
if game_over == 0: # By default it puts it on the top-left
score_position.midtop = (40, 10)
else: # Unless its game over, where it puts below the game over message
score_position.midtop = (360, 80)
player_screen.blit(score_surface, score_position)
bug_check()
while True:
for event in [Link]():
if [Link] == [Link]:
quiting()
elif [Link] == [Link]:
# Choose direction by user input, block opposite directions
key_right = [Link] == pygame.K_RIGHT or [Link] ==
ord("d")
key_left = [Link] == pygame.K_LEFT or [Link] == ord("a")
key_down = [Link] == pygame.K_DOWN or [Link] ==
ord("s")
key_up = [Link] == pygame.K_UP or [Link] == ord("w")
if key_right and direction != "LEFT":
direction = "RIGHT"
elif key_left and direction != "RIGHT":
direction = "LEFT"
elif key_down and direction != "UP":
direction = "DOWN"
elif key_up and direction != "DOWN":
direction = "UP"
elif [Link] == pygame.K_ESCAPE:
quiting() # It will quit when esc is pressed
# Simulates the snake movement(together with snake_body_pop)
if direction == "RIGHT":
snake_position[0] += 10
elif direction == "LEFT":
snake_position[0] -= 10
elif direction == "DOWN":
snake_position[1] += 10
elif direction == "UP":
snake_position[1] -= 10
# Body mechanics
snake_body.insert(0, list(snake_position))
if snake_position == food_position:
score += 1 # Every food taken will raise the score by 1
food_spawn = False # It removes the food from the board
else:
# If the food is taken it will not remove the last body piece(raising
snakes size)
snake_body.pop()
if food_spawn is False: # When a food is taken it will respawn randomly
food_position = [[Link](1, 71) * 10, [Link](1, 45) *
10]
food_spawn = True # It will set the food to True again, to keep the cycle
# Drawing
player_screen.fill(white) # Set the background to white
for position in snake_body: # Snake representation on the screen
[Link](player_screen, green, [Link](position[0],
position[1], 10, 10))
# Food representation on the screen
[Link](player_screen, orange, [Link](food_position[0],
food_position[1], 10, 10))
if snake_position[0] not in range(0, 711) or snake_position[1] not in
range(0, 451):
you_lose() # Game over when the Snake hit a wall
for block in snake_body[1:]:
if snake_position == block:
you_lose() # Game over when the Snake hits itself
scoring()
[Link]() # It constantly updates the screen
[Link](20) # It sets the speed to a playable value
if __name__ == "__main__":
main()