Python 正在尝试获取“a”的文本;游戏“;它应该刷新/随机化HP和DMG值,但不是
对Python来说真的很陌生,我被卡住了。我不知道如何让HP和DMG在单击我创建的按钮时进行随机调用 以下是我目前拥有的:Python 正在尝试获取“a”的文本;游戏“;它应该刷新/随机化HP和DMG值,但不是,python,object,Python,Object,对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)
# 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
值随机化后,需要重新呈现每个值的文本值。您可以使用一个名为\uuuuDrawText
的函数来执行此操作,但按下按钮时您不会调用它。这就是为什么即使在调用了Randomize
之后,仍要继续绘制旧值
我不确定您希望您的类如何工作,但也许应该从随机化调用\u drawText
?您不能依赖运行的外部代码来同时调用\uuuu drawText
,因为您已经给了它一个以两个下划线开头的名称(它调用Python的名称混乱系统)。如果它应该是类API的一部分,那么您肯定不想这样做。外部代码仍然可以调用\uuuuDrawText
,但只能通过手动进行修改(并调用例如Char1.\uCharacter\uuuDrawText
)
最后一件事,与您当前的问题无关。变量的命名方式对于Python代码来说有点不同寻常。更常见的Python风格是对大多数变量和函数(包括方法)使用带有下划线的lowercase\u,并为类保留TitleCase
名称ALL_CAPS
偶尔用于概念上恒定的变量,但正则变量样式也非常常见,即使是常量(例如math.pi
)
使用不同的命名约定不会使您的代码出错,但是对于其他程序员来说,如果您遵循标准约定,则可能更难遵循。有关官方Python解释器使用的样式,请参见。许多其他Python代码都遵循这一指南(在行长度方面可能会有更多的回旋余地)。谷歌也有一个,这是(据我所知)几乎兼容PEP8