簡體   English   中英

如何添加一個分數文本列表,該列表每次單擊Cookie精靈時加1? (蟒蛇)

[英]How do I add a score text list that adds 1 each time I click my cookie sprite? (python)

游戲代碼處理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()

在事件循環中,檢查用戶是否按下了鼠標按鈕,然后查看cookie.rect是否與event.pos cookie.rect沖突(或使用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)

要將文本顯示在屏幕上,您需要借助.format方法將score放入字符串中: '{} Cookies'.format(score) 然后將其傳遞到my_font.render並對返回的表面進行平化處理。

text_surface = my_font.render('{} Cookies'.format(score), True, BROWN)
screen.blit(text_surface, (30, 30))

另外,您不應該在while循環內創建並填充all_sprites組。 這是更新的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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM