如何添加每次单击我的cookie精灵时添加1的分数文本列表? (蟒蛇)

时间:2017-07-04 14:09:48

标签: python pygame scoreloop

游戏代码处理Cookie和Shop类。我怎么做到这样我有一个文本框,上面写着{Cookie Number} Cookies?是否有一个命令,如果event.click然后cookie +1?

game_loop的内部是一个简单的文本框,只是说了一些cookie编号,最后是cookie。我想我必须创建一个列表,但我不知道该怎么做。我会使用{}吗?另外,感谢大家的帮助。

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()

1 个答案:

答案 0 :(得分:0)

在事件循环中,检查用户是否按下鼠标按钮,然后查看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)

要将文字显示在屏幕上,您需要借助score方法.format'{} Cookies'.format(score)放入字符串中:my_font.render。然后将其传递给text_surface = my_font.render('{} Cookies'.format(score), True, BROWN) screen.blit(text_surface, (30, 30)) 并对返回的表面进行blit。

all_sprites

此外,您不应创建并填充while循环内的game_loop组。这是更新的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. 函数:

"clicked"