如何添加分数文本列表,每次单击cookie sprite时都会添加1?(python)

如何添加分数文本列表,每次单击cookie sprite时都会添加1?(python),python,pygame,scoreloop,Python,Pygame,Scoreloop,游戏代码处理Cookie和Shop类。我如何使它成为一个显示{Cookie Number}Cookies的文本框?是否有一个命令显示如果event.click然后cookie+1 在游戏循环中有一个简单的文本框,上面只写着“一些cookie编号,末尾有cookies这个词。我想我必须创建一个列表,但我不知道怎么做。我会使用{}?另外,谢谢大家的帮助 import pygame as pg import os WIDTH = 1024 HEIGHT = 768 CLOCK = pg.time.

游戏代码处理Cookie和Shop类。我如何使它成为一个显示{Cookie Number}Cookies的文本框?是否有一个命令显示如果event.click然后cookie+1

游戏循环
中有一个简单的文本框,上面只写着“一些cookie编号,末尾有cookies这个词。我想我必须创建一个列表,但我不知道怎么做。我会使用
{}
?另外,谢谢大家的帮助

import pygame as pg
import os

WIDTH = 1024
HEIGHT = 768

CLOCK = pg.time.Clock()
FPS = 60

WHITE = (255, 255, 255)
BROWN = (153, 51, 51)
DARKGRAY = (40, 40, 40)
BGCOLOR = DARKGRAY

game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, "img")

screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption("Cookie Clicker")

class Cookie(pg.sprite.Sprite):

    def __init__(self):
        pg.sprite.Sprite.__init__(self)
        self.image = pg.image.load(os.path.join(img_folder, "2.png")).convert()
        self.rect = self.image.get_rect()
        self.rect.center = ((WIDTH - 670, HEIGHT / 2))

cookie = Cookie()

class Shop(pg.sprite.Sprite):

    def __init__(self):
        pg.sprite.Sprite.__init__(self)
        self.image = pg.Surface((300, 768))
        self.image.fill(WHITE)
        self.rect = self.image.get_rect()
        self.rect.center = ((WIDTH - 150, HEIGHT / 2))


def game_loop():
    pg.init()
    pg.font.init()

    my_font = pg.font.SysFont("Comic Sans MS", 30)

    text_surface = my_font.render('SOME NUMBER - Cookies', False, (0, 255, 0))

    running = True
    while running:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False

        all_sprites = pg.sprite.Group()
        all_sprites.add(cookie, shop)
        all_sprites.update()

        screen.fill(BGCOLOR)
        all_sprites.draw(screen)
        screen.blit(text_surface, (175, 100))
        pg.display.flip()

        CLOCK.tick(FPS)

    pg.quit()


game_loop()

在事件循环中,检查用户是否按下鼠标按钮,然后查看
cookie.rect
是否与
event.pos
(或使用
pg.mouse.get_pos()
)冲突,如果冲突,则增加一个分数变量:

elif event.type == pg.MOUSEBUTTONDOWN:
    # Use the collidepoint method of this `pygame.Rect`.
    if cookie.rect.collidepoint(event.pos):
        score += 1
        print(score)
要在屏幕上blit文本,您需要借助
.format
方法:
'{}Cookies.format(score)
,将
分数
放入字符串中。然后将其传递到
我的字体。渲染
并blit返回的曲面

text_surface = my_font.render('{} Cookies'.format(score), True, BROWN)
screen.blit(text_surface, (30, 30))
此外,您不应在
while
循环中创建和填充
所有精灵
组。以下是更新的
游戏循环
功能:

def game_loop():
    pg.init()

    my_font = pg.font.SysFont("Comic Sans MS", 30)
    cookie = Cookie()
    shop = Shop()
    all_sprites = pg.sprite.Group()
    all_sprites.add(cookie, shop)
    score = 0

    running = True
    while running:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False
            elif event.type == pg.MOUSEBUTTONDOWN:
                if cookie.rect.collidepoint(event.pos):
                    score += 1
                    print(score)

        all_sprites.update()

        screen.fill(BGCOLOR)
        all_sprites.draw(screen)
        screen.blit(text_surface, (175, 100))
        text_surface = my_font.render('{} Cookies'.format(score), True, BROWN)
        screen.blit(text_surface, (30, 30))
        pg.display.flip()

        CLOCK.tick(FPS)

    pg.quit()
    sys.exit()  # import sys at the top of the file.

您只是想记录并显示点击次数,还是想在每次点击时生成一个新的cookie精灵并显示cookie的总数?