蛇游戏2D移动功能失效求助:代码无法实现蛇身移动
蛇游戏移动功能无法实现的问题求助
我在开发基于Pygame的蛇游戏时,完全无法实现蛇的移动功能,尝试过调整变量等多种方法均未解决,恳请提供技术帮助。
我的代码如下:
import pygame, sys, random #Functions def player_animation(): global player_speed_x, player_speed_y player.x += player_speed_x player.y += player_speed_y if player.top or player.left or player.right or player.bottom <=15: restart() player_speed_x, player_speed_y = 0, 0 if player.top or player.left or player.right or player.bottom >=screen_width: restart() player_speed_x, player_speed_y = 0, 0 if player.top or player.left or player.right or player.bottom >= screen_height: restart() player_speed_x, player_speed_y = 0, 0 if player.colliderect(player): restart() player_speed_x, player_speed_y = 0, 0 def restart(): global player_score player.center =(screen_width/2 -15, screen_height/2 -15) player_score = 0 def apple_function(): global player_score if apple.colliderect(player): player_score +=1 apple.random_x =(random.randint(15, screen_width -15)) apple.random_y =(random.randint(15, screen_width -15)) pygame.init() pygame.font.init() #Text Variables font = pygame.font.Font("freesansbold.ttf", 32) #Clock clock = pygame.time.Clock() #Game Variables vel = 5 player_speed_x = 0 player_speed_y = 0 player_score = 0 #Screen Setup screen_width = 800 screen_height = 800 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Snake") #Apple Variables apple_x = (random.randint(15, screen_width -15)) apple_y = (random.randint(15, screen_width -15)) #Game Rectangles player = pygame.Rect(15, 15, 30, 30) apple = pygame.Rect(apple_x, apple_y, 30, 30) #Colours bg_colour = pygame.Color("chartreuse2") white = (255, 255, 255) player_colour = (92, 130, 255) apple_colour = (255, 46, 46) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: player_speed_y -= vel player_speed_x = 0 if event.key == pygame.K_DOWN: player_speed_y +=vel player_speed_x = 0 if event.key == pygame.K_LEFT: player_speed_x -= vel player_speed_y = 0 if event.key == pygame.K_RIGHT: player_speed_x += vel player_speed_y = 0 #Drawn Elements screen.fill(bg_colour) pygame.draw.rect(screen, apple_colour, apple) pygame.draw.rect(screen, player_colour, player) #Functions #player_animation() apple_function() #Score Text score_text = font.render("Score: " + f"{player_score}", True, white) screen.blit(score_text, (15, 15)) pygame.display.flip() clock.tick(60)
问题分析与修复方案
- 核心问题:移动函数未调用:主循环里
player_animation()被注释掉了,这是处理蛇位置更新的关键函数,必须取消注释并执行。 - 边界判断逻辑错误:原代码中
player.top or player.left or player.right or player.bottom <=15的写法逻辑混乱,应该对每个边界单独做比较,否则会导致游戏一开始就触发重启。正确的边界判断应为:if player.top <= 15 or player.left <= 15 or player.right >= screen_width or player.bottom >= screen_height: restart() player_speed_x, player_speed_y = 0, 0 - 苹果位置更新错误:
apple.random_x不是Pygame Rect的合法属性,应该用apple.x和apple.y来更新苹果位置:apple.x = random.randint(15, screen_width - 15) apple.y = random.randint(15, screen_height - 15) - 自碰撞判断无效:
player.colliderect(player)永远为真,因为矩形和自身必然碰撞,后续实现蛇身增长后,需要用蛇身列表来检测碰撞,目前可以先注释掉这行避免游戏异常。
修复后的完整代码
import pygame, sys, random #Functions def player_animation(): global player_speed_x, player_speed_y player.x += player_speed_x player.y += player_speed_y # 修正边界判断逻辑 if player.top <= 15 or player.left <= 15 or player.right >= screen_width or player.bottom >= screen_height: restart() player_speed_x, player_speed_y = 0, 0 # 自碰撞逻辑后续实现蛇身增长后再完善,暂时注释 # if player.colliderect(player): # restart() # player_speed_x, player_speed_y = 0, 0 def restart(): global player_score player.center =(screen_width/2 -15, screen_height/2 -15) player_score = 0 def apple_function(): global player_score if apple.colliderect(player): player_score +=1 # 修正苹果位置更新方式 apple.x = random.randint(15, screen_width -15) apple.y = random.randint(15, screen_height -15) pygame.init() pygame.font.init() #Text Variables font = pygame.font.Font("freesansbold.ttf", 32) #Clock clock = pygame.time.Clock() #Game Variables vel = 5 player_speed_x = 0 player_speed_y = 0 player_score = 0 #Screen Setup screen_width = 800 screen_height = 800 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Snake") #Apple Variables apple_x = (random.randint(15, screen_width -15)) apple_y = (random.randint(15, screen_height -15)) #Game Rectangles player = pygame.Rect(15, 15, 30, 30) apple = pygame.Rect(apple_x, apple_y, 30, 30) #Colours bg_colour = pygame.Color("chartreuse2") white = (255, 255, 255) player_colour = (92, 130, 255) apple_colour = (255, 46, 46) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: player_speed_y -= vel player_speed_x = 0 if event.key == pygame.K_DOWN: player_speed_y +=vel player_speed_x = 0 if event.key == pygame.K_LEFT: player_speed_x -= vel player_speed_y = 0 if event.key == pygame.K_RIGHT: player_speed_x += vel player_speed_y = 0 #Drawn Elements screen.fill(bg_colour) pygame.draw.rect(screen, apple_colour, apple) pygame.draw.rect(screen, player_colour, player) #Functions player_animation() # 取消注释,调用移动函数 apple_function() #Score Text score_text = font.render("Score: " + f"{player_score}", True, white) screen.blit(score_text, (15, 15)) pygame.display.flip() clock.tick(60)
内容的提问来源于stack exchange,提问作者podgecodes
相关产品推荐
相关产品推荐

