对Python来说真的很新,我被困住了。我不知道如何让 HP 和 DMG 在单击我创建的按钮时调用它时随机化。
这是我目前拥有的:
# find your fav character images and pass it here
Char1 = Character('Snart.png','CAPTAIN COLD',DISPLAYSURF,(100,300),200)
Char2 = Character('Flash.png','FLASH',DISPLAYSURF,(700,300),200)
def displayButtons(bList):
for x in bList:
x.display()
def main():
B1.active = True
clickCount = 1
B2.active = False
while True:
DISPLAYSURF.fill(BGCOLOR)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
## MOUSE EVENTS
elif event.type == MOUSEBUTTONDOWN:
mouse = pygame.mouse.get_pos()
if B1.clicked(mouse):
B1.highlight = True
print("Hello") ## Just to see if it actually get's pressed
clickCount = 2
elif B2.clicked(mouse):
B2.highlight = True
print("Bye") ## Just to see if it actually get's pressed
clickCount = 1
elif event.type == MOUSEBUTTONUP:
if B1.clicked(mouse):
Char1.Randomize() ## Randomize the HP DMG
B1.highlight = False
B1.active = False
B2.active = True
elif B2.clicked(mouse):
Char2.Randomize() ## Randomize the HP DMG
B2.highlight = False
B2.active = False
B1.active = True
Char1.display()
Char2.display()
displayButtons(BUTTONLIST)
pygame.display.update()
main()
对于它正在创建的类:
class Character(object):
def __init__(self, imagefile,charname,surf,pos,scalesize):
self.SURF = surf
self.POS = pos
self.IMAGESURF = pygame.image.load(imagefile)
self.IMAGESURF = pygame.transform.scale(self.IMAGESURF, (scalesize,scalesize))
self.HP = (0, 300) # should range from (0 - 300) ## randint: return a random integer(start,stop)
self.DMG = (0, 100) # should range from (0 - 100)
self.GameFont = pygame.font.SysFont("Sylfaen", 50)
# this text has a black background. Can you make it transparent ?. DONE
self.NAME = self.GameFont.render(charname, True,(255,255,255),None)
self.Randomize()
self.__drawText()
self.__displayText()
# complete this function
# this function should randomize HP, DMG and should display on the screen
# this function should be called on a button press
def Randomize(self):
#pass
self.HP = randint(0, 300)
self.DMG = randint(0, 300)
## DON'T UNCOMMENT UNLESS YOU WANT IT TO RANDOMLY GENERATE NON-STOP
## self.HPText = self.GameFont.render('HP : ' +str(self.HPrand), True,(255,255,255),None)
## self.DMGText = self.GameFont.render('DMG: ' +str(self.DMGrand), True,(255,255,255),None)
def __displayText(self):
self.SURF.blit(self.HPText,(self.POS[0]+200,self.POS[1]+50))
self.SURF.blit(self.DMGText,(self.POS[0]+200,self.POS[1]+150))
self.SURF.blit(self.NAME,(self.POS[0]+20,self.POS[1]-100))
# fix the error in this function, DONE
def __drawText(self):
# this text has a black background. Can you make it transparent ?.
self.HPText = self.GameFont.render('HP : ' +str(self.HP), True,(255,255,255),None)
self.DMGText = self.GameFont.render('DMG: ' +str(self.DMG), True,(255,255,255),None)
# fix the errors in this function, DONE
def display(self):
self.Randomize()
self.__displayText()
self.SURF.blit(self.IMAGESURF,self.POS)
随机化HP
值和DMG
值后,需要重新呈现每个值的文本值。您有一个名为 __drawText
的函数来执行此操作,但是在按下按钮时不会调用它。这就是为什么即使在调用Randomize
后仍继续绘制旧值的原因。
我不确定您希望您的课程如何工作,但也许应该从Randomize
调用__drawText
?你不能依赖运行Randomize
的外部代码来调用__drawText
,因为你给它起了一个以两个下划线开头的名字(这调用了 Python 的名称重整系统)。如果它应该是类 API 的一部分,你当然不想这样做。外部代码仍然可以调用__drawText
,但只能通过手动进行重整(并调用例如 Char1._Character__drawText
)。
最后一件事,与您当前的问题无关。您的变量的命名方式对于 Python 代码来说有点不寻常。更常见的 Python 风格是对大多数变量和函数(包括方法)使用lowercase_with_underscores
名称,并为类保留TitleCase
名称。 ALL_CAPS
偶尔用于名义上常量的变量,但常规变量样式即使对于常量也很常见(例如 math.pi
)。
使用不同的命名约定不会使您的代码出错,但其他程序员可能比遵循标准约定更难遵循。请参阅 PEP 8 了解用于官方 Python 解释器的样式。许多其他 Python 代码都遵循该指南(在行长度上可能会有更多的回旋余地)。Google还有一个Python风格指南,据我所知,它与PEP 8非常兼容。