简体   繁体   中英

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

Game code deals with classes Cookie and Shop. How do I make it so that I have a text box which says {Cookie Number} Cookies? Is there a command that says if event.click then cookie +1?

Inside of the game_loop is a simple text box that just says "some cookie number, with the word cookies at the end. I think I would have to create a list but I am not sure how to do that. Would I use {} ? Also, thank you to everyone for your help.

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

In the event loop, check if the user presses a mousebutton, then see if the cookie.rect collides with the event.pos (or use pg.mouse.get_pos() ), and if they collide, increment a score variable:

elif event.type == pg.MOUSEBUTTONDOWN:
    # Use the collidepoint method of this `pygame.Rect`.
    if cookie.rect.collidepoint(event.pos):
        score += 1
        print(score)

To blit the text onto the screen, you need to put the score into a string with the help of the .format method: '{} Cookies'.format(score) . Then pass it to my_font.render and blit the returned surface.

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

Also, you shouldn't create and fill the all_sprites group inside of the while loop. Here's the updated game_loop function:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM