我正在尝试在 PyGame 中制作我的第一款游戏。目标是点击一个罐子(标题为“jar.png”)以获得更多沙子。还有文字不断告诉你你有多少沙子。
不过我有一个问题,我需要一个脚本来确保当您单击“jar.png”时它会将变量 sand 增加 1。此外,如果我确实找到了解决此问题的方法,我不确定文本是否将正确显示变量。
我当前的代码如下所示:
import sys, pygame
from pygame.locals import *
pygame.init()
size = width, height = 800,500
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Sand Maker (Created by - Not Really Working Lamp Productions:)")
CORNBLUE = (100,149,237)
WHITE = (255,255,255)
sand = 0
liams = 0
screen.fill (CORNBLUE)
jar = pygame.image.load("jar.png")
screen.blit(jar,(90,50))
liambutton = pygame.image.load("liam button.png")
screen.blit(liambutton,(450,50))
liamdesc = pygame.image.load("liam desc.png")
screen.blit(liamdesc,(350,50))
myfont = pygame.font.SysFont("monospace", 16)
scoretext = myfont.render("Number of sand = "+str(sand), 1, (255,255,255))
screen.blit(scoretext, (5, 10))
liamscoretext = myfont.render("Number of Liams = "+str(liams), 1, (255,255,255))
screen.blit(liamscoretext, (350, 10))
import time
while 1:
for event in pygame.event.get():
pygame.display.flip()
if event.type == pygame.QUIT:sys.exit()
while 1:
time.sleep(5)
sand = sand + 1 * liams
那么,您能否向我提供一段经过编辑的代码,其中包含我想要的功能。谢谢你。