[英]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. 游戏代码处理Cookie和Shop类。 How do I make it so that I have a text box which says {Cookie Number} Cookies?
如何制作一个文本框,上面写着“ {Cookie Number} Cookies”? Is there a command that says if event.click then cookie +1?
是否有一条命令说如果event.click然后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. 在
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()
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: 在事件循环中,检查用户是否按下了鼠标按钮,然后查看
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)
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)
. 要将文本显示在屏幕上,您需要借助
.format
方法将score
放入字符串中: '{} Cookies'.format(score)
。 Then pass it to my_font.render
and blit the returned surface. 然后将其传递到
my_font.render
并对返回的表面进行平化处理。
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. 另外,您不应该在
while
循环内创建并填充all_sprites
组。 Here's the updated game_loop
function: 这是更新的
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.