贪吃蛇visual studio code程序代码
时间: 2023-10-29 09:05:29 浏览: 131
贪吃蛇程序代码
以下是一个简单的贪吃蛇游戏的VSCode程序代码(使用Python语言):
```python
import pygame, sys, random, time
difficulty = 25
# Easy -> 50
# Medium -> 25
# Hard -> 10
# Window size
frame_size_x = 720
frame_size_y = 480
# Checks for errors encountered
check_errors = pygame.init()
if check_errors[1] > 0:
print(f"[!] Had {check_errors[1]} errors when initialising game, exiting...")
sys.exit(-1)
else:
print("[+] Game successfully initialised")
# Initialise game window
pygame.display.set_caption('Snake Game @ Python')
game_window = pygame.display.set_mode((frame_size_x, frame_size_y))
# Colors (R, G, B)
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(200, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)
# FPS (Frames Per Second) controller
fps_controller = pygame.time.Clock()
# Game variables
snake_position = [100, 50]
snake_body = [[100, 50], [90, 50], [80, 50]]
food_position = [random.randrange(1, (frame_size_x//10)) * 10,
random.randrange(1, (frame_size_y//10)) * 10]
food_spawn = True
direction = 'RIGHT'
change_to = direction
score = 0
# Game Over
def game_over():
my_font = pygame.font.SysFont('times new roman', 90)
game_over_surface = my_font.render('YOU DIED', True, red)
game_over_rect = game_over_surface.get_rect()
game_over_rect.midtop = (frame_size_x/2, frame_size_y/4)
game_window.blit(game_over_surface, game_over_rect)
pygame.display.flip()
time.sleep(3)
pygame.quit()
sys.exit()
# Main Function
while True:
# Handling key events
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
change
阅读全文