【问题标题】:Pygame Snake Eating ItselfPygame 蛇吃自己
【发布时间】:2018-08-03 15:13:47
【问题描述】:

我最近开始使用 pygame,并且正在关注 TheNewBostons 的 youtube 教程。这是我的主要游戏循环:

def game_loop():
    global direction
    global tdirection

    lead_x=display_width/2
    lead_y=display_height/2

    block_size=10
    change_x=10
    change_y=0

    game_exit=False
    GameOver=False
    main_menu=False


    snakelist=[]
    snakelength=1

    applethickness=20

    RandAppleX=round(random.randrange(0, display_width-applethickness )/10.0)*10.0
    RandAppleY=round(random.randrange(0, display_height-applethickness )/10.0)*10.0



    while not game_exit:


        while GameOver==True:
            game_display.fill(white)
            message_screen('You lost, Press Q to quit or C to retry.', red)
            pygame.display.update()
            for event in pygame.event.get():
                if event.type==pygame.KEYDOWN:
                    if event.key==pygame.K_q:
                        loss.play()
                        game_exit=True
                        GameOver= False


                    elif event.key==pygame.K_c:
                        direction='right'
                        game_loop()


                if event.type==pygame.QUIT:
                    game_exit=True
                    GameOver= False




        for event in pygame.event.get():


            if event.type==pygame.QUIT:
                game_exit=True

            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_a:

                    change_x=-block_size
                    direction='left'

                    change_x=-block_size                        
                    change_y=0
                elif event.key==pygame.K_d:
                    direction='right'

                    change_x=block_size
                    change_y=0  
                elif event.key==pygame.K_w:
                    direction='up'

                    change_y=-block_size
                    change_x=0
                elif event.key==pygame.K_s:
                    direction='down'

                    change_y=block_size
                    change_x=0

                elif event.key==pygame.K_ESCAPE:
                    pause()





        if lead_x>=display_width or lead_x<0 or lead_y>=display_height or lead_y<0:
            loss.play()
            GameOver=True



        lead_x+=change_x
        lead_y+=change_y




        clock.tick(FPS)





        snakehead=[]

        snakehead.append(lead_x)
        snakehead.append(lead_y)

        snakelist.append(snakehead)


        if len(snakelist)>snakelength:
            del snakelist[0]

        for segment in snakelist[:-1]:
            if segment==snakehead:
                loss.play()
                GameOver=True






        game_display.fill(white)
        game_display.blit(aimg, (RandAppleX, RandAppleY))   

        snake(snakelist, block_size)
        score(snakelength-1)




        if lead_x>= RandAppleX and lead_x<= RandAppleX+applethickness and lead_y<= RandAppleY+applethickness and lead_y>= RandAppleY:


            RandAppleX=round(random.randrange(0, display_width-applethickness )/10.0)*10.0
            RandAppleY=round(random.randrange(0, display_height-applethickness )/10.0)*10.0
            snakelength+=1
            effect.play()





        pygame.display.update()




    pygame.quit()
    quit()

现在我需要防止蛇在向某个方向移动时吃掉自己。例如,然后它向右移动,按下我键盘上的 A 按钮会立即让它吃掉自己,如果它向上移动,按下 S 按钮会做同样的事情。有没有办法防止这种情况发生?

【问题讨论】:

  • 您是否尝试过跟踪方向,并根据方向禁用某些输入?

标签: python python-3.x pygame


【解决方案1】:

一个简单的解决方案可能是通过检查它的移动方向是否与请求的方向相反来防止蛇返回自身。这将涉及一些额外的检查。像这样的:

        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_a and direction != 'right':

                change_x=-block_size
                direction='left'

                change_x=-block_size                        
                change_y=0
            elif event.key==pygame.K_d and direction != 'left':
                direction='right'

                change_x=block_size
                change_y=0  
            elif event.key==pygame.K_w and direction != 'down':
                direction='up'

                change_y=-block_size
                change_x=0
            elif event.key==pygame.K_s and direction != 'right':
                direction='down'

                change_y=block_size
                change_x=0

【讨论】:

  • 哇!我不敢相信我错过了这么简单的事情!非常感谢!
  • @Shell1500 没问题!它一直发生在我身上xD
【解决方案2】:

在改变方向时,您可以只检查方向是否与当前方向相反,如果是,则不要设置新方向。这解决了问题。

【讨论】:

  • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review
  • @GalSilberman 为什么它没有提供答案?它包含有关如何解决问题的说明。
  • @MegaMan 这是一个有效的答案,尽管有些人可能认为答案应该更加详尽和具体。您收到的评论是从审核队列中自动生成的,标记时可能会出现错误。
猜你喜欢
  • 2011-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-19
  • 2014-01-31
相关资源
最近更新 更多