【问题标题】:How to add score tracker in PyGame Snake game? [duplicate]如何在 PyGame Snake 游戏中添加分数跟踪器? [复制]
【发布时间】:2020-01-17 22:30:20
【问题描述】:

我一直在使用 Python 3.8 开发一个 Snake 游戏,其中 PyGame 作为一个副项目。我已经掌握了所有基础知识,唯一遇到的问题是添加了一个分数跟踪器。

def main():
    global width, rows, s, snack
    title()
    width = 500
    rows = 20
    win = pygame.display.set_mode((width, width)) # Creates the screen, sets resolution
    s = snake((0,255,0), (10,10))    # Sets snake colour to green and starting position
    snack = cube(randomSnack(rows, s), colour=(255,0,0))
    score = 0
    flag = True

    white = (255, 255, 255)

    message = score
    font = pygame.font.Font('Fonts/PressStart2P-vaV7.ttf', 30)
    text = font.render(str(message), True, (255, 255, 255))
    win.blit(text, (120,30))
    pygame.display.flip()

    clock = pygame.time.Clock()    # Creates clock object that can change refresh rate

    while flag:
        pygame.time.delay(50)    # Delays by 50 ms, makes it so snake cannot move 10 blocks a second
        clock.tick(10)    # Sets max refresh rate
        s.move()
        if s.body[0].position == snack.position:
            s.addCube()
            score += 1
            print(score)
            snack = cube(randomSnack(rows, s), colour=(255,0,0))    # Randomizes snack position and sets colour as red

        for x in range(len(s.body)):    
            if s.body[x].position in list(map(lambda z:z.position,s.body[x+1:])):   # This checks if you die
                print('Score: ', len(s.body))
                s.reset((10,10))
                break

分数在while flag部分,它在开始时会弹出一会儿然后消失。间距在问题上有点混乱,但实际代码工作正常。分数只是一闪而逝

【问题讨论】:

  • 显示分数的代码在 while 循环之外 - text = font.render(str(message), True, (255, 255, 255)); win.blit(text, (120,30))。它需要在while循环之前和内部调用,也许我把它放在一个单独的函数中并从两个地方调用它。或者调整代码中显示蛇的其他部分,以便它们也显示更新后的分数。
  • while 循环内,您必须绘制/blit 所有元素 - 蛇、零食、得分 - 并稍后使用 pygame.display.flip() 在屏幕上发送缓冲区

标签: python python-3.x oop pygame


【解决方案1】:

您必须在主应用程序循环中进行绘图。主应用程序循环不断地重绘整个场景。主应用程序循环必须执行以下操作:

  • 根据事件和时间处理事件并更改游戏状态
  • 清除显示
  • 绘制场景
  • 更新显示

在主应用程序循环之前创建初始分数 Surface。当分数发生变化时,创建一个新的分数 Surface。在主应用程序循环中绘制疮:

def main():
    global width, rows, s, snack
    title()
    width = 500
    rows = 20
    win = pygame.display.set_mode((width, width)) # Creates the screen, sets resolution
    s = snake((0,255,0), (10,10))    # Sets snake colour to green and starting position
    snack = cube(randomSnack(rows, s), colour=(255,0,0))
    score = 0
    flag = True
    white = (255, 255, 255)

    # create score surface
    message = score
    font = pygame.font.Font('Fonts/PressStart2P-vaV7.ttf', 30)
    text = font.render(str(message), True, (255, 255, 255))

    clock = pygame.time.Clock()    # Creates clock object that can change refresh rate

    while flag:
        pygame.time.delay(50)    # Delays by 50 ms, makes it so snake cannot move 10 blocks a second
        clock.tick(10)    # Sets max refresh rate

        # handle the events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                flag = False

        s.move()
        if s.body[0].position == snack.position:
            s.addCube()
            score += 1

            # update score surface
            message = score
            text = font.render(str(message), True, (255, 255, 255))

            print(score)
            snack = cube(randomSnack(rows, s), colour=(255,0,0))    # Randomizes snack position and sets colour as red

        for x in range(len(s.body)):    
            if s.body[x].position in list(map(lambda z:z.position,s.body[x+1:])):   # This checks if you die
                print('Score: ', len(s.body))
                s.reset((10,10))
                break

        # clear the display
        # [...]

        # draw the score
        win.blit(text, (120,30))

        # draw the snake etc.
        # [...]

        pygame.display.flip()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 2011-05-24
    相关资源
    最近更新 更多