Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- # Initialize pygame
- pygame.init()
- # Set the window size
- window_size = (600, 600)
- # Create the window
- screen = pygame.display.set_mode(window_size)
- # Set the title of the window
- pygame.display.set_caption("Snake")
- # Set the dimensions of the snake block
- block_size = 20
- # Set the rate of the game (in milliseconds)
- refresh_rate = 150
- # Set the font for rendering text
- font = pygame.font.Font(None, 36)
- # Set the colors
- black = (0, 0, 0)
- white = (255, 255, 255)
- green = (0, 255, 0)
- red = (255, 0, 0)
- # Set the initial position and velocity of the snake
- x_position = window_size[0] // 2
- y_position = window_size[1] // 2
- x_velocity = 0
- y_velocity = 0
- # Set the initial length of the snake
- length = 1
- # Create an empty list for storing the blocks of the snake
- snake = []
- # Set the initial position of the food
- food_x = random.randrange(0, window_size[0] - block_size, block_size)
- food_y = random.randrange(0, window_size[1] - block_size, block_size)
- # Set the initial game over flag to False
- game_over = False
- # Set the initial score to 0
- score = 0
- # Set the initial high score to 0
- high_score = 0
- # Main game loop
- while not game_over:
- # Check for user input
- for event in pygame.event.get():
- # Quit the game if the user closes the window
- if event.type == pygame.QUIT:
- game_over = True
- # Change the direction of the snake based on the arrow keys
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT and x_velocity != block_size:
- x_velocity = -block_size
- y_velocity = 0
- elif event.key == pygame.K_RIGHT and x_velocity != -block_size:
- x_velocity = block_size
- y_velocity = 0
- elif event.key == pygame.K_UP and y_velocity != block_size:
- x_velocity = 0
- y_velocity = -block_size
- elif event.key == pygame.K_DOWN and y_velocity != -block_size:
- x_velocity = 0
- y_velocity = block_size
- # Update the position of the snake
- x_position += x_velocity
- y_position += y_velocity
- # Check if the snake has collided with the edges of the screen
- if x_position < 0 or x_position > window_size[0] - block_size or y_position < 0 or y_position > window_size[1] - block_size:
- game_over = True
- # Check if the snake has collided with itself
- for block in snake:
- if block[0] == x_position and block[1] == y_position and len(snake) > 1:
- game_over = True
- # Update the snake list
- snake.append((x_position, y_position))
- # Remove the first block of the snake if the length of the snake is greater than the current length
- if len(snake) > length:
- snake.pop(0)
- # Check if the snake has eaten the food
- if x_position == food_x and y_position == food_y:
- # Increase the length of the snake
- length += 1
- # Increase the score
- score += 1
- # Check if the score is higher than the high score
- if score > high_score:
- high_score = score
- # Generate new food
- food_x = random.randrange(0, window_size[0] - block_size, block_size)
- food_y = random.randrange(0, window_size[1] - block_size, block_size)
- # Clear the screen
- screen.fill(black)
- # Draw the food
- pygame.draw.rect(screen, green, (food_x, food_y, block_size, block_size))
- # Draw the snake
- for block in snake:
- pygame.draw.rect(screen, white, (block[0], block[1], block_size, block_size))
- # Render the score text
- text = font.render("Score: " + str(score), True, white)
- screen.blit(text, (10, 10))
- # Render the high score text
- text = font.render("High Score: " + str(high_score), True, white)
- screen.blit(text, (10, 40))
- # Update the display
- pygame.display.flip()
- # Wait for the refresh rate
- pygame.time.delay(refresh_rate)
- # Game over message
- text = font.render("Game Over", True, red)
- text_rect = text.get_rect()
- text_rect.center = (window_size[0] // 2, window_size[1] // 2)
- screen.blit(text, text_rect)
- pygame.display.flip()
- # Wait for the user to quit
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement