Matuiss2

Python simple snake game

Nov 28th, 2017
1,128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.56 KB | None | 0 0
  1. import pygame, sys, time, random  # Necessary libraries
  2.  
  3.  
  4. def bug_check():  # Checking the mistakes, and closing the program if it does(while printing how many bugs)
  5.     bugs = pygame.init()
  6.     if bugs[1] > 0:
  7.         print("There are", bugs[1], "bugs! quiting.....")
  8.         time.sleep(3)
  9.         sys.exit("Closing program")
  10.     else:
  11.         print("The game was initialized")
  12.  
  13.  
  14. def you_lose():  # When you lose, it will show a message
  15.     font_game_over = pygame.font.SysFont("times new roman", 44)  # Choosing the font and size of the message
  16.     game_over_surface = font_game_over.render("Game over :(", True, red)  # It chooses the message and the color
  17.     game_over_position = game_over_surface.get_rect()  # Chooses the geometry of the message box(rectangle in this case)
  18.     game_over_position.midtop = (360, 15)  # Position of the message inside the screen
  19.     player_screen.blit(game_over_surface, game_over_position)  # Put the message in the said position
  20.     scoring(0)  # Calls the scoring function and shows it at the end
  21.     pygame.display.flip()  # Updates the screen
  22.     quiting()  # Calls the exit function
  23.  
  24.  
  25. def quiting():  # When you want to quit, it will wait 4 seconds and exit
  26.     time.sleep(4)
  27.     pygame.quit()
  28.     sys.exit()
  29.  
  30.  
  31. def scoring(choice=1):  # It will show the score on the top side of the screen, it updates automaticaly
  32.     score_font = pygame.font.SysFont("times new roman", 16)  # Choosing the font and size of the message
  33.     score_surface = score_font.render("Score : {}".format(score), True, black)  # It chooses the message and the color
  34.     score_position = score_surface.get_rect()  # Chooses the geometry of the message box(rectangle in this case)
  35.     if choice == 1:  # By default it puts it on the top
  36.         score_position.midtop = (40, 10)
  37.     else:  # Unless its game over, where it puts right below the game over message
  38.         score_position.midtop = (360, 80)
  39.     player_screen.blit(score_surface, score_position)  # Put the message in the specified position
  40.  
  41.  
  42. # Scoring
  43. score = 0  # Inicial score
  44. # FPS control
  45. fps = pygame.time.Clock()  # It controls the frames per second
  46. # Directions
  47. direction = "RIGHT"  # Initial direction
  48. # Game objects
  49. snake_position = [100, 50]  # Initial snake position
  50. snake_body = [[100, 50], [90, 50], [100, 50]]  # Initial snake body
  51. # It places the food randomly, excluding the border
  52. food_position = [random.randint(1, 71) * 10, random.randint(1, 45) * 10]  # randomizes the food pos, excludes the border
  53. food_spawn = True  # It will be only false when the Snakes eats it, and will be replaced immediately
  54. # Game surface
  55. player_screen = pygame.display.set_mode((720, 460))  # Set screen size
  56. pygame.display.set_caption("Snake v.1.0")  # Set screen title
  57. # Colors
  58. red = pygame.Color("red")  # Defines red color (you can use rgb code instead)
  59. green = pygame.Color("green")  # Defines green color (you can use rgb code instead)
  60. black = pygame.Color("black")  # Defines black color (you can use rgb code instead)
  61. orange = pygame.Color("orange")  # Defines orange color (you can use rgb code instead)
  62. white = pygame.Color("white")  # Defines white color (you can use rgb code instead)
  63. # Bug checking
  64. bug_check()  # Calls bug_check function
  65. # Main game
  66. while True:
  67.     for event in pygame.event.get():  # Here it enters all pre-defined events
  68.         if event.type == pygame.QUIT:  # If the event is one for exiting(alt+f4) etc
  69.             quiting()  # It will call the exiting function
  70.         elif event.type == pygame.KEYDOWN:  # If the event is from the keyboard typing
  71.             if (event.key == pygame.K_RIGHT or event.key == ord("d")) \
  72.                     and (direction != "LEFT"):   # It will define the direction blocking a revert direction ex: R-L, U-D
  73.                 direction = "RIGHT"
  74.             if (event.key == pygame.K_LEFT or event.key == ord("a")) \
  75.                     and (direction != "RIGHT"):  # It will define the direction blocking a revert direction ex: R-L, U-D
  76.                 direction = "LEFT"
  77.             if (event.key == pygame.K_DOWN or event.key == ord("s")) \
  78.                     and (direction != "UP"):  # It will define the direction blocking a revert direction ex: R-L, U-D
  79.                 direction = "DOWN"
  80.             if (event.key == pygame.K_UP or event.key == ord("w")) \
  81.                     and (direction != "DOWN"):  # It will define the direction blocking a revert direction ex: R-L, U-D
  82.                 direction = "UP"
  83.             if event.key == pygame.K_ESCAPE:
  84.                 quiting()  # It will quit when esc is pressed
  85.     # Snake movement and position
  86.     if direction == "RIGHT":  # The defined direction will change the snake position
  87.         snake_position[0] += 10
  88.     if direction == "LEFT":  # The defined direction will change the snake position
  89.         snake_position[0] -= 10
  90.     if direction == "DOWN":  # The defined direction will change the snake position
  91.         snake_position[1] += 10
  92.     if direction == "UP":  # The defined direction will change the snake position
  93.         snake_position[1] -= 10
  94.     # Snake body
  95.     snake_body.insert(0, list(snake_position))  # It will make the snake grow when the food is taken
  96.     if snake_position == food_position:
  97.         score += 1  # Every food taken will raise the score by 1
  98.         food_spawn = False  # It removes the food from the board
  99.     else:
  100.         snake_body.pop()  # It will keep the snake together
  101.     if food_spawn is False:  # When a food is taken it will respawn randomly
  102.         food_position = [random.randint(1, 71) * 10, random.randint(1, 45) * 10]
  103.     food_spawn = True  # It will set the food to True again, to keep the cycle until the game ends
  104.     # Drawing
  105.     player_screen.fill(white)  # Set the background to white
  106.     for position in snake_body:  # It draws the Snake on the board
  107.         pygame.draw.rect(player_screen, green, pygame.Rect(position[0], position[1], 10, 10))
  108.     # It draws the food on the board
  109.     pygame.draw.rect(player_screen, orange, pygame.Rect(food_position[0], food_position[1], 10, 10))
  110.     if snake_position[0] not in range(0, 711) or snake_position[1] not in range(0, 451):
  111.         you_lose()  # If the Snake hits a wall it will show the game over screen and close the game
  112.     for block in snake_body[1:]:
  113.         if snake_position == block:
  114.             you_lose()  # If the Snake hits itself it will show the game over screen and close the game
  115.  
  116.     scoring()  # It shows the score at the top of the screen
  117.     pygame.display.flip()  # It constantly updates the screen
  118.     fps.tick(20)  # It sets the fps to a reasonable value
Advertisement
Add Comment
Please, Sign In to add comment