Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, sys, time, random # Necessary libraries
- def bug_check(): # Checking the mistakes, and closing the program if it does(while printing how many bugs)
- bugs = pygame.init()
- if bugs[1] > 0:
- print("There are", bugs[1], "bugs! quiting.....")
- time.sleep(3)
- sys.exit("Closing program")
- else:
- print("The game was initialized")
- def you_lose(): # When you lose, it will show a message
- font_game_over = pygame.font.SysFont("times new roman", 44) # Choosing the font and size of the message
- game_over_surface = font_game_over.render("Game over :(", True, red) # It chooses the message and the color
- game_over_position = game_over_surface.get_rect() # Chooses the geometry of the message box(rectangle in this case)
- game_over_position.midtop = (360, 15) # Position of the message inside the screen
- player_screen.blit(game_over_surface, game_over_position) # Put the message in the said position
- scoring(0) # Calls the scoring function and shows it at the end
- pygame.display.flip() # Updates the screen
- quiting() # Calls the exit function
- def quiting(): # When you want to quit, it will wait 4 seconds and exit
- time.sleep(4)
- pygame.quit()
- sys.exit()
- def scoring(choice=1): # It will show the score on the top side of the screen, it updates automaticaly
- score_font = pygame.font.SysFont("times new roman", 16) # Choosing the font and size of the message
- score_surface = score_font.render("Score : {}".format(score), True, black) # It chooses the message and the color
- score_position = score_surface.get_rect() # Chooses the geometry of the message box(rectangle in this case)
- if choice == 1: # By default it puts it on the top
- score_position.midtop = (40, 10)
- else: # Unless its game over, where it puts right below the game over message
- score_position.midtop = (360, 80)
- player_screen.blit(score_surface, score_position) # Put the message in the specified position
- # Scoring
- score = 0 # Inicial score
- # FPS control
- fps = pygame.time.Clock() # It controls the frames per second
- # Directions
- direction = "RIGHT" # Initial direction
- # Game objects
- 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 = [random.randint(1, 71) * 10, random.randint(1, 45) * 10] # randomizes the food pos, excludes the border
- food_spawn = True # It will be only false when the Snakes eats it, and will be replaced immediately
- # Game surface
- player_screen = pygame.display.set_mode((720, 460)) # Set screen size
- pygame.display.set_caption("Snake v.1.0") # Set screen title
- # Colors
- red = pygame.Color("red") # Defines red color (you can use rgb code instead)
- green = pygame.Color("green") # Defines green color (you can use rgb code instead)
- black = pygame.Color("black") # Defines black color (you can use rgb code instead)
- orange = pygame.Color("orange") # Defines orange color (you can use rgb code instead)
- white = pygame.Color("white") # Defines white color (you can use rgb code instead)
- # Bug checking
- bug_check() # Calls bug_check function
- # Main game
- while True:
- for event in pygame.event.get(): # Here it enters all pre-defined events
- if event.type == pygame.QUIT: # If the event is one for exiting(alt+f4) etc
- quiting() # It will call the exiting function
- elif event.type == pygame.KEYDOWN: # If the event is from the keyboard typing
- if (event.key == pygame.K_RIGHT or event.key == ord("d")) \
- and (direction != "LEFT"): # It will define the direction blocking a revert direction ex: R-L, U-D
- direction = "RIGHT"
- if (event.key == pygame.K_LEFT or event.key == ord("a")) \
- and (direction != "RIGHT"): # It will define the direction blocking a revert direction ex: R-L, U-D
- direction = "LEFT"
- if (event.key == pygame.K_DOWN or event.key == ord("s")) \
- and (direction != "UP"): # It will define the direction blocking a revert direction ex: R-L, U-D
- direction = "DOWN"
- if (event.key == pygame.K_UP or event.key == ord("w")) \
- and (direction != "DOWN"): # It will define the direction blocking a revert direction ex: R-L, U-D
- direction = "UP"
- if event.key == pygame.K_ESCAPE:
- quiting() # It will quit when esc is pressed
- # Snake movement and position
- if direction == "RIGHT": # The defined direction will change the snake position
- snake_position[0] += 10
- if direction == "LEFT": # The defined direction will change the snake position
- snake_position[0] -= 10
- if direction == "DOWN": # The defined direction will change the snake position
- snake_position[1] += 10
- if direction == "UP": # The defined direction will change the snake position
- snake_position[1] -= 10
- # Snake body
- snake_body.insert(0, list(snake_position)) # It will make the snake grow when the food is taken
- 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:
- snake_body.pop() # It will keep the snake together
- if food_spawn is False: # When a food is taken it will respawn randomly
- food_position = [random.randint(1, 71) * 10, random.randint(1, 45) * 10]
- food_spawn = True # It will set the food to True again, to keep the cycle until the game ends
- # Drawing
- player_screen.fill(white) # Set the background to white
- for position in snake_body: # It draws the Snake on the board
- pygame.draw.rect(player_screen, green, pygame.Rect(position[0], position[1], 10, 10))
- # It draws the food on the board
- pygame.draw.rect(player_screen, orange, pygame.Rect(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() # If the Snake hits a wall it will show the game over screen and close the game
- for block in snake_body[1:]:
- if snake_position == block:
- you_lose() # If the Snake hits itself it will show the game over screen and close the game
- scoring() # It shows the score at the top of the screen
- pygame.display.flip() # It constantly updates the screen
- fps.tick(20) # It sets the fps to a reasonable value
Advertisement
Add Comment
Please, Sign In to add comment