Pygame贪吃蛇与食物碰撞检测无提示问题求助
贪吃蛇游戏碰撞检测问题修复
你的问题主要出在食物坐标生成逻辑与蛇的移动步长不匹配,导致碰撞条件永远无法触发,另外消息显示的逻辑也有问题,即使触发了也看不到。以下是具体修复方案:
核心问题拆解
- 坐标不匹配:蛇每次移动
snake_block=16像素,所以蛇的坐标是16的倍数;但食物坐标用round(xxx /10)*10生成,是10的倍数,两者几乎不可能相等,碰撞检测自然失效。 - 消息显示逻辑错误:即使碰撞条件满足,调用
message()后没有立即更新屏幕,而且下一次循环会用screen.fill(black)清空画面,消息会瞬间被覆盖。 - 语法错误:
pygame.quit缺少括号,应该是pygame.quit()。
修复后的完整代码
import pygame import random pygame.init() # 设置窗口 screen_width = 1920 screen_height = 1080 screen = pygame.display.set_mode((screen_width,screen_height)) pygame.display.set_caption("Snake Game") pygame.display.update() # 颜色定义 white = (255, 255, 255) yellow = (255, 255, 102) black = (0, 0, 0) red = (213, 50, 80) green = (0, 255, 0) blue = (50, 153, 213) # FPS控制 clock = pygame.time.Clock() # 消息显示函数 font_style = pygame.font.SysFont(None, 50) def message(msg, color): mesg = font_style.render(msg, True, color) # 修正文本居中:用get_rect获取文本矩形,再设置中心坐标 mesg_rect = mesg.get_rect(center=(screen_width/2, screen_height/2)) screen.blit(mesg, mesg_rect) # 游戏主循环 def gameLoop(): game_over = False game_close = False # 蛇初始位置 x1 = screen_width/2 y1 = screen_height/2 # 蛇的属性 snake_block = 16 snake_speed = 120 x1_change = 0 y1_change = 0 # 食物坐标生成:改为和蛇步长一致的倍数 foodX = round(random.randrange(0, screen_width - snake_block) / snake_block) * snake_block foodY = round(random.randrange(0, screen_height - snake_block) / snake_block) * snake_block while not game_over: while game_close == True: screen.fill(black) message("你输了!按ALT+F4退出或C重新开始", red) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_c: gameLoop() elif event.key == pygame.K_ESCAPE: game_over = True game_close = False for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_a and x1_change == 0: x1_change = -snake_block y1_change = 0 elif event.key == pygame.K_d and x1_change == 0: x1_change = snake_block y1_change = 0 elif event.key == pygame.K_w and y1_change == 0: x1_change = 0 y1_change = -snake_block elif event.key == pygame.K_s and y1_change == 0: x1_change = 0 y1_change = +snake_block # 边界处理(修正为蛇身完全出界才 teleport) if x1 >= screen_width: x1 = 0 elif x1 < 0: x1 = screen_width - snake_block elif y1 >= screen_height: y1 = 0 elif y1 < 0: y1 = screen_height - snake_block x1 += x1_change y1 += y1_change screen.fill(black) pygame.draw.rect(screen, blue, [foodX, foodY, snake_block, snake_block]) pygame.draw.rect(screen, white, [x1,y1,snake_block,snake_block]) # 碰撞检测与消息显示 if x1 == foodX and y1 == foodY: message("Yummy!", red) # 生成新的食物 foodX = round(random.randrange(0, screen_width - snake_block) / snake_block) * snake_block foodY = round(random.randrange(0, screen_height - snake_block) / snake_block) * snake_block pygame.display.update() clock.tick(snake_speed) pygame.quit() quit() gameLoop()
额外优化点
- 修正了消息居中的逻辑:原来的
[screen_width/2, screen_height/2]会让文本左上角居中,改用get_rect().center实现真正的中心对齐。 - 增加了方向锁:防止蛇反向移动(比如按A向左时不能直接按D向右)。
- 碰撞后生成新的食物,符合贪吃蛇逻辑。
- 修复了边界teleport的逻辑,让蛇完全出界后才回到对面。
内容的提问来源于stack exchange,提问作者ubersmurf
相关产品推荐
相关产品推荐

