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 theevent.pos
(or usepg.mouse.get_pos()
), and if they collide, increment a score variable: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 tomy_font.render
and blit the returned surface.Also, you shouldn't create and fill the
all_sprites
group inside of thewhile
loop. Here's the updatedgame_loop
function: