贪吃蛇小游戏
import pygame
import random
import sys
初始化
pygame.init()
WIDTH, HEIGHT = 800, 600
GRID_SIZE = 20
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('精美贪吃蛇')
clock = pygame.time.Clock()
颜色
BG_COLOR = (20, 25, 30)
GRID_COLOR = (40, 45, 50)
SNAKE_HEAD = (100, 255, 100)
SNAKE_BODY = (80, 220, 80)
FOOD_COLORS = [(255,50,50), (255,150,50), (255,255,50), (50,255,50)]
蛇类
class Snake:
def init(self):
self.reset()
def reset(self):
self.positions = [(WIDTH//2//GRID_SIZE, HEIGHT//2//GRID_SIZE)]
self.direction = (1, 0)
self.score = 0
self.speed = 8
def move(self):
head_x, head_y = self.positions[0]
dir_x, dir_y = self.direction
new_pos = ((head_x + dir_x) % (WIDTH//GRID_SIZE),
(head_y + dir_y) % (HEIGHT//GRID_SIZE))
if new_pos in self.positions:
return False
self.positions.insert(0, new_pos)
self.positions.pop()
return True
def grow(self):
self.positions.append(self.positions[-1])
self.score += 10
if self.score % 50 == 0:
self.speed += 1
def draw(self):
for i, (x, y) in enumerate(self.positions):
rect = pygame.Rect(x*GRID_SIZE, y*GRID_SIZE, GRID_SIZE, GRID_SIZE)
color = SNAKE_HEAD if i == 0 else SNAKE_BODY
pygame.draw.rect(screen, color, rect, border_radius=3)
pygame.draw.rect(screen, (50,180,50), rect, 1, border_radius=3)
食物类
class Food:
def init(self):
self.position = self.new_position()
self.color = random.choice(FOOD_COLORS)
self.size = 0
def new_position(self):
return (random.randint(0, WIDTH//GRID_SIZE-1),
random.randint(0, HEIGHT//GRID_SIZE-1))
def draw(self):
x, y = self.position
center = (x*GRID_SIZE + GRID_SIZE//2, y*GRID_SIZE + GRID_SIZE//2)
self.size = (self.size + 0.1) % 1.5
radius = int(GRID_SIZE * 0.4 * (1 + 0.3 * abs(self.size - 0.75)))
# 发光效果
glow = pygame.Surface((GRID_SIZE*2, GRID_SIZE*2), pygame.SRCALPHA)
pygame.draw.circle(glow, (*self.color, 50), (GRID_SIZE, GRID_SIZE), radius+5)
screen.blit(glow, (center[0]-GRID_SIZE, center[1]-GRID_SIZE))
# 食物主体
pygame.draw.circle(screen, self.color, center, radius)
游戏主循环
def main():
snake = Snake()
food = Food()
font = pygame.font.SysFont('Arial', 30)
running = True
while running:
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and snake.direction != (0, 1):
snake.direction = (0, -1)
elif event.key == pygame.K_DOWN and snake.direction != (0, -1):
snake.direction = (0, 1)
elif event.key == pygame.K_LEFT and snake.direction != (1, 0):
snake.direction = (-1, 0)
elif event.key == pygame.K_RIGHT and snake.direction != (-1, 0):
snake.direction = (1, 0)
elif event.key == pygame.K_r:
snake.reset()
# 游戏逻辑
if not snake.move():
snake.reset()
if snake.positions[0] == food.position:
snake.grow()
food.position = food.new_position()
while food.position in snake.positions:
food.position = food.new_position()
food.color = random.choice(FOOD_COLORS)
# 绘制
screen.fill(BG_COLOR)
# 绘制网格
for x in range(0, WIDTH, GRID_SIZE):
pygame.draw.line(screen, GRID_COLOR, (x, 0), (x, HEIGHT))
for y in range(0, HEIGHT, GRID_SIZE):
pygame.draw.line(screen, GRID_COLOR, (0, y), (WIDTH, y))
food.draw()
snake.draw()
# 显示分数
score_text = font.render(f'分数: {snake.score}', True, (200, 200, 200))
screen.blit(score_text, (10, 10))
pygame.display.flip()
clock.tick(snake.speed)
pygame.quit()
sys.exit()
if name == "main":
main()

浙公网安备 33010602011771号