
import pygame
import sys
import random
import math
from pygame.math import Vector2
pygame.init()
CELL_SIZE = 30
GRID_WIDTH = 20
GRID_HEIGHT = 15
SCREEN_WIDTH = CELL_SIZE * GRID_WIDTH
SCREEN_HEIGHT = CELL_SIZE * GRID_HEIGHT
FPS = 60
BACKGROUND = (15, 15, 30)
GRID_COLOR = (30, 30, 50)
SNAKE_HEAD = (50, 205, 50)
SNAKE_BODY = (40, 180, 40)
FOOD_COLOR = (220, 60, 60)
TEXT_COLOR = (220, 220, 220)
UI_BACKGROUND = (20, 20, 40, 200)
UI_BORDER = (70, 130, 200)
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Python貪吃蛇")
clock = pygame.time.Clock()
try:
font_large = pygame.font.Font(None, 48)
font_medium = pygame.font.Font(None, 36)
font_small = pygame.font.Font(None, 28)
except:
font_large = pygame.font.SysFont(None, 48)
font_medium = pygame.font.SysFont(None, 36)
font_small = pygame.font.SysFont(None, 28)
class Snake:
def init(self):
self.reset()
def reset(self):
self.body = [Vector2(5, 7), Vector2(4, 7), Vector2(3, 7)]
self.direction = Vector2(1, 0)
self.new_direction = Vector2(1, 0)
self.grow = False
self.speed = 8
self.move_counter = 0
def update(self):
if self.new_direction != -self.direction:
self.direction = self.new_direction
self.move_counter += self.speed / FPS
if self.move_counter >= 1:
self.move_counter -= 1
head = self.body[0] + self.direction
self.body.insert(0, head)
if not self.grow:
self.body.pop()
else:
self.grow = False
def draw(self, surface):
for i, segment in enumerate(self.body):
if i == 0:
color = SNAKE_HEAD
else:
color = SNAKE_BODY
rect = pygame.Rect(segment.x * CELL_SIZE, segment.y * CELL_SIZE, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(surface, color, rect, 0, 7)
if i > 0:
prev = self.body[i-1]
if prev.x == segment.x:
if prev.y < segment.y:
pygame.draw.rect(surface, color,
(segment.x * CELL_SIZE + 3,
segment.y * CELL_SIZE - 3,
CELL_SIZE - 6, 6))
else:
pygame.draw.rect(surface, color,
(segment.x * CELL_SIZE + 3,
segment.y * CELL_SIZE + CELL_SIZE - 3,
CELL_SIZE - 6, 6))
else:
if prev.x < segment.x:
pygame.draw.rect(surface, color,
(segment.x * CELL_SIZE - 3,
segment.y * CELL_SIZE + 3,
6, CELL_SIZE - 6))
else:
pygame.draw.rect(surface, color,
(segment.x * CELL_SIZE + CELL_SIZE - 3,
segment.y * CELL_SIZE + 3,
6, CELL_SIZE - 6))
eye_size = CELL_SIZE // 5
if self.direction == Vector2(1, 0):
left_eye = (self.body[0].x * CELL_SIZE + CELL_SIZE - eye_size - 2,
self.body[0].y * CELL_SIZE + eye_size + 2)
right_eye = (self.body[0].x * CELL_SIZE + CELL_SIZE - eye_size - 2,
self.body[0].y * CELL_SIZE + CELL_SIZE - eye_size*2 - 2)
elif self.direction == Vector2(-1, 0):
left_eye = (self.body[0].x * CELL_SIZE + 2,
self.body[0].y * CELL_SIZE + eye_size + 2)
right_eye = (self.body[0].x * CELL_SIZE + 2,
self.body[0].y * CELL_SIZE + CELL_SIZE - eye_size*2 - 2)
elif self.direction == Vector2(0, 1):
left_eye = (self.body[0].x * CELL_SIZE + eye_size + 2,
self.body[0].y * CELL_SIZE + CELL_SIZE - eye_size - 2)
right_eye = (self.body[0].x * CELL_SIZE + CELL_SIZE - eye_size*2 - 2,
self.body[0].y * CELL_SIZE + CELL_SIZE - eye_size - 2)
else:
left_eye = (self.body[0].x * CELL_SIZE + eye_size + 2,
self.body[0].y * CELL_SIZE + 2)
right_eye = (self.body[0].x * CELL_SIZE + CELL_SIZE - eye_size*2 - 2,
self.body[0].y * CELL_SIZE + 2)
pygame.draw.circle(surface, (0, 0, 0), left_eye, eye_size)
pygame.draw.circle(surface, (0, 0, 0), right_eye, eye_size)
class Food:
def init(self, snake):
self.position = self.generate_position(snake)
def generate_position(self, snake):
while True:
x = random.randint(0, GRID_WIDTH - 1)
y = random.randint(0, GRID_HEIGHT - 1)
position = Vector2(x, y)
if position not in snake.body:
return position
def draw(self, surface):
center = (self.position.x * CELL_SIZE + CELL_SIZE // 2,
self.position.y * CELL_SIZE + CELL_SIZE // 2)
radius = CELL_SIZE // 2 - 2
pygame.draw.circle(surface, FOOD_COLOR, center, radius)
leaf_points = [
(center[0] - radius//2, center[1] - radius),
(center[0] + radius//2, center[1] - radius),
(center[0], center[1] - radius - radius//2)
]
pygame.draw.polygon(surface, (50, 180, 50), leaf_points)
pygame.draw.line(surface, (100, 70, 30),
(center[0], center[1] - radius),
(center[0], center[1] - radius - radius//3), 2)
class Game:
def init(self):
self.snake = Snake()
self.food = Food(self.snake)
self.score = 0
self.game_over = False
self.paused = False
self.special_food_timer = 0
self.special_food = None
self.special_food_active = False
def update(self):
if self.game_over or self.paused:
return
self.snake.update()
if self.snake.body[0] == self.food.position:
self.snake.grow = True
self.food = Food(self.snake)
self.score += 10
if random.random() < 0.3 and self.snake.speed < 15:
self.snake.speed += 0.5
head = self.snake.body[0]
if (head.x < 0 or head.x >= GRID_WIDTH or
head.y < 0 or head.y >= GRID_HEIGHT or
head in self.snake.body[1:]):
self.game_over = True
if self.special_food_active:
self.special_food_timer -= 1
if self.special_food_timer <= 0:
self.special_food_active = False
if self.snake.body[0] == self.special_food:
self.snake.grow = True
self.special_food_active = False
self.score += 30
if not self.special_food_active and random.random() < 0.005:
self.special_food = Food(self.snake).position
self.special_food_active = True
self.special_food_timer = FPS * 5
def draw(self, surface):
for x in range(GRID_WIDTH):
for y in range(GRID_HEIGHT):
rect = pygame.Rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(surface, GRID_COLOR, rect, 1)
self.food.draw(surface)
if self.special_food_active:
center = (self.special_food.x * CELL_SIZE + CELL_SIZE // 2,
self.special_food.y * CELL_SIZE + CELL_SIZE // 2)
radius = CELL_SIZE // 3
if pygame.time.get_ticks() % 500 < 250:
pygame.draw.circle(surface, (255, 215, 0), center, radius)
pygame.draw.circle(surface, (255, 255, 100), center, radius - 2)
self.snake.draw(surface)
score_text = font_medium.render(f"分數: {self.score}", True, TEXT_COLOR)
surface.blit(score_text, (10, 10))
speed_text = font_small.render(f"速度: {self.snake.speed:.1f}", True, TEXT_COLOR)
surface.blit(speed_text, (SCREEN_WIDTH - 120, 10))
if self.game_over:
self.draw_game_over(surface)
if self.paused:
self.draw_paused(surface)
def draw_game_over(self, surface):
overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SRCALPHA)
overlay.fill((0, 0, 0, 180))
surface.blit(overlay, (0, 0))
game_over_text = font_large.render("游戲結束!", True, (220, 60, 60))
score_text = font_medium.render(f"最終分數: {self.score}", True, TEXT_COLOR)
restart_text = font_small.render("按R鍵重新開始", True, TEXT_COLOR)
surface.blit(game_over_text, (SCREEN_WIDTH//2 - game_over_text.get_width()//2,
SCREEN_HEIGHT//2 - 60))
surface.blit(score_text, (SCREEN_WIDTH//2 - score_text.get_width()//2,
SCREEN_HEIGHT//2))
surface.blit(restart_text, (SCREEN_WIDTH//2 - restart_text.get_width()//2,
SCREEN_HEIGHT//2 + 50))
def draw_paused(self, surface):
overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SRCALPHA)
overlay.fill((0, 0, 0, 120))
surface.blit(overlay, (0, 0))
pause_text = font_large.render("游戲暫停", True, (70, 130, 200))
continue_text = font_medium.render("按P鍵繼續", True, TEXT_COLOR)
surface.blit(pause_text, (SCREEN_WIDTH//2 - pause_text.get_width()//2,
SCREEN_HEIGHT//2 - 30))
surface.blit(continue_text, (SCREEN_WIDTH//2 - continue_text.get_width()//2,
SCREEN_HEIGHT//2 + 30))
def reset(self):
self.snake.reset()
self.food = Food(self.snake)
self.score = 0
self.game_over = False
self.paused = False
self.special_food_active = False
game = Game()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if not game.game_over:
if event.key == pygame.K_UP:
game.snake.new_direction = Vector2(0, -1)
elif event.key == pygame.K_DOWN:
game.snake.new_direction = Vector2(0, 1)
elif event.key == pygame.K_LEFT:
game.snake.new_direction = Vector2(-1, 0)
elif event.key == pygame.K_RIGHT:
game.snake.new_direction = Vector2(1, 0)
elif event.key == pygame.K_p:
game.paused = not game.paused
if event.key == pygame.K_r:
game.reset()
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
game.update()
screen.fill(BACKGROUND)
game.draw(screen)
pygame.draw.rect(screen, UI_BORDER, (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), 3)
title_text = font_small.render("Python貪吃蛇", True, (100, 180, 255))
screen.blit(title_text, (SCREEN_WIDTH // 2 - title_text.get_width() // 2,
SCREEN_HEIGHT - 30))
if not game.game_over and not game.paused:
controls_text = font_small.render("方向鍵移動 | P暫停 | R重開 | ESC退出", True, (180, 180, 200))
screen.blit(controls_text, (SCREEN_WIDTH // 2 - controls_text.get_width() // 2,
SCREEN_HEIGHT - 60))
pygame.display.flip()
clock.tick(FPS)
浙公網安備 33010602011771號