贪吃蛇
忘了还有之前做的贪吃蛇游戏import pygame
import random
import sys
from collections import deque
# 初始化Pygame
pygame.init()
# 设置屏幕大小和标题
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 480
SIZE = 20
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('贪吃蛇')
# 定义颜色
LIGHT = (100, 100, 100) # 蛇的颜色
DARK = (200, 200, 200) # 食物颜色
BLACK = (0, 0, 0) # 网格线颜色
BGCOLOR = (40, 40, 60) # 背景色
# 初始化字体
font1 = pygame.font.SysFont('SimHei', 24)
font2 = pygame.font.Font(None, 72)
RED = (200, 30, 30) # GAME OVER 的字体颜色
# 初始化方向和范围
pos_x = 1
pos_y = 0
scope_x = (0, SCREEN_WIDTH // SIZE - 1)
scope_y = (2, SCREEN_HEIGHT // SIZE - 1)
# 初始化蛇和食物的位置
snake = deque()
food_x = 0
food_y = 0
def _init_snake():
global snake
snake.clear()
snake.append((2, scope_y))
snake.append((1, scope_y))
snake.append((0, scope_y))
def _create_food():
global food_x, food_y
food_x = random.randint(scope_x, scope_x)
food_y = random.randint(scope_y, scope_y)
while (food_x, food_y) in snake:
food_x = random.randint(scope_x, scope_x)
food_y = random.randint(scope_y, scope_y)
def main():
global pos_x, pos_y, food_x, food_y
_init_snake()
_create_food()
game_over = True
start = False
score = 0
orispeed = 0.5
speed = orispeed
last_move_time = None
pause = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
if game_over:
start = True
game_over = False
_init_snake()
_create_food()
pos_x = 1
pos_y = 0
score = 0
last_move_time = time.time()
elif event.key == pygame.K_SPACE:
if not game_over:
pause = not pause
elif event.key in (pygame.K_w, pygame.K_UP):
if not pos_y:
pos_x = 0
pos_y = -1
elif event.key in (pygame.K_s, pygame.K_DOWN):
if not pos_y:
pos_x = 0
pos_y = 1
elif event.key in (pygame.K_a, pygame.K_LEFT):
if not pos_x:
pos_x = -1
pos_y = 0
elif event.key in (pygame.K_d, pygame.K_RIGHT):
if not pos_x:
pos_x = 1
pos_y = 0
screen.fill(BGCOLOR)
for x in range(SIZE, SCREEN_WIDTH, SIZE):
pygame.draw.line(screen, BLACK, (x, scope_y * SIZE), (x, SCREEN_HEIGHT), 1)
for y in range(scope_y * SIZE, SCREEN_HEIGHT, SIZE):
pygame.draw.line(screen, BLACK, (0, y), (SCREEN_WIDTH, y), 1)
if game_over:
if start:
imgText = font2.render('GAME OVER', True, RED)
screen.blit(imgText, ((SCREEN_WIDTH - imgText.get_width()) // 2, (SCREEN_HEIGHT - imgText.get_height()) // 2))
else:
curTime = time.time()
if curTime - last_move_time > speed:
last_move_time = curTime
next_s = (snake + pos_x, snake + pos_y)
if next_s == (food_x, food_y):
_create_food()
snake.appendleft(next_s)
score += 10
speed = orispeed - 0.03 * (score // 100)
else:
if scope_x <= next_s <= scope_x and scope_y <= next_s <= scope_y and next_s not in snake:
snake.appendleft(next_s)
snake.pop()
else:
game_over = True
if not game_over:
pygame.draw.rect(screen, LIGHT, (food_x * SIZE, food_y * SIZE, SIZE, SIZE), 0)
for s in snake:
pygame.draw.rect(screen, DARK, (s * SIZE + 1, s * SIZE + 1, SIZE - 2, SIZE - 2), 0)
imgText = font1.render(f'得分: {score}', True, (255, 255, 255))
screen.blit(imgText, (450, 7))
pygame.display.update()
if __name__ == '__main__':
main() 贪吃蛇代码问题分析与修复建议
你提供的贪吃蛇代码存在一些明显的语法和逻辑错误,下面我将指出关键问题并给出修复建议。
[*]1. 缩进错误:Python 对缩进非常敏感,像 global 后的代码块必须正确缩进。
[*]2. 范围使用错误:你在 random.randint(scope_x, scope_x) 中传入的是元组,应该是 scope_x, scope_x。
[*]3. 蛇初始化位置错误:你写的是 snake.append((2, scope_y)),但 scope_y 是一个元组,应该是 scope_y 或中间值。
[*]4. 缺少 import time:你使用了 time.time(),但没有导入 time 模块。
[*]5. 蛇移动逻辑错误:你写的是 next_s = (snake + pos_x, snake + pos_y),这不对,应该是蛇头位置加方向。
[*]6. 游戏边界判断和碰撞检测缺失或不完整。
修复建议代码片段(仅供参考)
import time# 补充导入
def _init_snake():
global snake
snake.clear()
mid_y = (scope_y + scope_y) // 2
snake.append((2, mid_y))
snake.append((1, mid_y))
snake.append((0, mid_y))
def _create_food():
global food_x, food_y
while True:
food_x = random.randint(scope_x, scope_x)
food_y = random.randint(scope_y, scope_y)
if (food_x, food_y) not in snake:
break
# 在主循环中更新蛇头位置
head_x, head_y = snake
next_s = (head_x + pos_x, head_y + pos_y)
# 判断是否撞墙或撞到自己
if (next_s < scope_x or next_s > scope_x or
next_s < scope_y or next_s > scope_y or
next_s in snake):
game_over = True
continue
如果你需要我帮你整理出完整可运行的贪吃蛇代码,请告诉我,我可以为你生成完整版本。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 import time 是什么意思? 艺小卤 发表于 2025-03-30 15:59
import time 是什么意思?
关于 import time 的解释
在 Python 中,import time 的作用是导入 Python 的 time 模块,这个模块提供了处理时间相关任务的函数。
常用功能包括:
[*]time.time():返回当前时间的时间戳(从1970年1月1日到现在的秒数,浮点数)。
[*]time.sleep(x):让程序暂停执行 x 秒。
[*]time.strftime():格式化时间为字符串。
在你的贪吃蛇游戏中,使用 time.time() 是为了控制蛇移动的时间间隔,实现“定时移动”的效果。
如果没有 import time,程序在调用 time.time() 时会报错:NameError: name 'time' is not defined。所以必须先导入这个模块。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 很想知道你的代码为什么用[ code][ /code]了还会让缩进消失 BUG我已经发在投诉建议专区了
页:
[1]