Advertisement
HCTGT_scripta

Python Snakegame

Jan 14th, 2021
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.61 KB | None | 0 0
  1. import pygame
  2. import sys
  3. import time
  4. import random
  5.  
  6.  
  7. def main():
  8.     """Snake v 1.01"""
  9.     score = 0  # Initial score
  10.     fps = pygame.time.Clock()
  11.     direction = "RIGHT"  # Initial direction
  12.     snake_position = [100, 50]  # Initial snake position
  13.     snake_body = [[100, 50], [90, 50], [100, 50]]  # Initial snake body
  14.     # It places the food randomly, excluding the border
  15.     food_position = [random.randint(1, 71) * 10, random.randint(1, 45) * 10]
  16.     food_spawn = True
  17.     # Game surface
  18.     player_screen = pygame.display.set_mode((720, 460))  # Set screen size
  19.     pygame.display.set_caption("Snake v.1.01")  # Set screen title and version
  20.     # Will define the colors
  21.     red = pygame.Color("red")
  22.     green = pygame.Color("green")
  23.     black = pygame.Color("black")
  24.     orange = pygame.Color("orange")
  25.     white = pygame.Color("white")
  26.  
  27.     def bug_check():
  28.         """ Checks the mistakes, and closes the program if it does while
  29.        printing on the console how many bugs it has """
  30.         bugs = pygame.init()
  31.         if bugs[1] > 0:
  32.             print("There are", bugs[1], "bugs! quiting.....")
  33.             time.sleep(3)
  34.             sys.exit("Closing program")
  35.         else:
  36.             print("The game was initialized")
  37.  
  38.     def you_lose():
  39.         """ When the players loses, it will show a red message in times new
  40.         roman font with 44 px size in a rectangle box"""
  41.         font_game_over = pygame.font.SysFont("times new roman", 44)
  42.         game_over_surface = font_game_over.render("Game over :(", True, red)
  43.         game_over_position = game_over_surface.get_rect()
  44.         game_over_position.midtop = (360, 15)
  45.         player_screen.blit(game_over_surface, game_over_position)
  46.         scoring(1)
  47.         pygame.display.flip()  # Updates the screen, so it doesnt freeze
  48.         quiting()
  49.  
  50.     def quiting():
  51.         """ When this function is called, it will wait 4 seconds and exit"""
  52.         time.sleep(4)
  53.         pygame.quit()
  54.         sys.exit()
  55.  
  56.     def scoring(game_over=0):
  57.         """ It will show the score on the top-left side of the screen in times new
  58.        roman font with 16px size and black color in a rectangle box"""
  59.         score_font = pygame.font.SysFont("times new roman", 16)
  60.         score_surface = score_font.render("Score : {}".format(score), True, black)
  61.         score_position = score_surface.get_rect()
  62.         if game_over == 0:  # By default it puts it on the top-left
  63.             score_position.midtop = (40, 10)
  64.         else:  # Unless its game over, where it puts below the game over message
  65.             score_position.midtop = (360, 80)
  66.         player_screen.blit(score_surface, score_position)
  67.  
  68.     bug_check()
  69.     while True:
  70.         for event in pygame.event.get():
  71.             if event.type == pygame.QUIT:
  72.                 quiting()
  73.             elif event.type == pygame.KEYDOWN:
  74.                 # Choose direction by user input, block opposite directions
  75.                 key_right = event.key == pygame.K_RIGHT or event.key == ord("d")
  76.                 key_left = event.key == pygame.K_LEFT or event.key == ord("a")
  77.                 key_down = event.key == pygame.K_DOWN or event.key == ord("s")
  78.                 key_up = event.key == pygame.K_UP or event.key == ord("w")
  79.                 if key_right and direction != "LEFT":
  80.                     direction = "RIGHT"
  81.                 elif key_left and direction != "RIGHT":
  82.                     direction = "LEFT"
  83.                 elif key_down and direction != "UP":
  84.                     direction = "DOWN"
  85.                 elif key_up and direction != "DOWN":
  86.                     direction = "UP"
  87.                 elif event.key == pygame.K_ESCAPE:
  88.                     quiting()  # It will quit when esc is pressed
  89.         # Simulates the snake movement(together with snake_body_pop)
  90.         if direction == "RIGHT":
  91.             snake_position[0] += 10
  92.         elif direction == "LEFT":
  93.             snake_position[0] -= 10
  94.         elif direction == "DOWN":
  95.             snake_position[1] += 10
  96.         elif direction == "UP":
  97.             snake_position[1] -= 10
  98.         # Body mechanics
  99.         snake_body.insert(0, list(snake_position))
  100.         if snake_position == food_position:
  101.             score += 1  # Every food taken will raise the score by 1
  102.             food_spawn = False  # It removes the food from the board
  103.         else:
  104.             # If the food is taken it will not remove the last body piece(raising snakes size)
  105.             snake_body.pop()
  106.         if food_spawn is False:  # When a food is taken it will respawn randomly
  107.             food_position = [random.randint(1, 71) * 10, random.randint(1, 45) * 10]
  108.         food_spawn = True  # It will set the food to True again, to keep the cycle
  109.         # Drawing
  110.         player_screen.fill(white)  # Set the background to white
  111.         for position in snake_body:  # Snake representation on the screen
  112.             pygame.draw.rect(player_screen, green, pygame.Rect(position[0], position[1], 10, 10))
  113.         # Food representation on the screen
  114.         pygame.draw.rect(player_screen, orange, pygame.Rect(food_position[0], food_position[1], 10, 10))
  115.         if snake_position[0] not in range(0, 711) or snake_position[1] not in range(0, 451):
  116.             you_lose()  # Game over when the Snake hit a wall
  117.         for block in snake_body[1:]:
  118.             if snake_position == block:
  119.                 you_lose()  # Game over when the Snake hits itself
  120.         scoring()
  121.         pygame.display.flip()  # It constantly updates the screen
  122.         fps.tick(20)  # It sets the speed to a playable value
  123.  
  124.  
  125. if __name__ == "__main__":
  126.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement