当我定义这样的按钮时
onebut = ButtonCPyg()
onebut.giveOpt(x=0,y=0,width=400,height=200,action = helloW) # hello word
onebut.changeText("Hello","coolFont.otf",25,(0,0,0))
onebut.changePic("thepic.png","thepicOver.png")
它工作正常。我可以.display()
显示它,.listen()
- 方法也可以显示。
但问题是,当我定义第二个按钮时......
onebutt2 = ButtonCPyg()
onebutt2.giveOpt(x=200,y=600,width=400,height=200,action = helloWW)
onebutt2.changeText(newText="Hello",newFont="coolFont.otf",newSize=25,newColor= (255,255,255))
onebutt2.changePic("thebutt.png","thebuttO.png")
...屏幕上只有一个按钮。
我可以"听"但是当我点击它时,有两行" Hello Word"
这意味着我的代码不能使用两个或更多按钮。
有我的按钮类:
class ButtonCPyg:
#so Main class
def __init__(self):
#init method
###BUTTON'S parameters
ButtonCPyg.butX = 0
ButtonCPyg.butY = 0
#
ButtonCPyg.width = 0
ButtonCPyg.height = 0
##
#TEXTin
ButtonCPyg.text = ""
ButtonCPyg.textFontL = "coolFont.otf"
ButtonCPyg.textSize = 0
ButtonCPyg.textColor = (0,0,0)
#
ButtonCPyg.pic = None
ButtonCPyg.picOver = None
##
ButtonCPyg.action = None
#TEXT
def changeText(self,newText="",newFont="",newSize=0,newColor=(0,0,0)):
#change text things
ButtonCPyg.text = newText
ButtonCPyg.textSize = newSize
ButtonCPyg.textColor = newColor
ButtonCPyg.textFontL = newFont
def text_objects(self,text ,font,color):
#text objects
textSurface = font.render(text,True,color)
##returns
return textSurface , textSurface.get_rect()
def message_display(this_object,text,x,y,n,color):
#text types
largeText = pygame.font.Font(ButtonCPyg.textFontL,n)
##Surfafce and text
TextSurf , TextRect = this_object.text_objects(text ,largeText,color)
##specifies
TextRect.center = (x,y)
#Blitting
mainc.gameDisplay.blit(TextSurf,TextRect)
##give things
def giveOpt(self,x=0,y=0,width = 0,height = 0,action = None):
#givit
ButtonCPyg.butX = x
ButtonCPyg.butY = y
##
#
ButtonCPyg.width = width
ButtonCPyg.height = height
##ACTION
ButtonCPyg.action = action
##Button things
def changePic(self,picString,picOString):
#change options
#try and str for safe
try:
#
ButtonCPyg.pic = pygame.image.load(str(picString))
ButtonCPyg.picOver = pygame.image.load(str(picOString))
#
except Exception, e:
#if error do nothing
pass
##blitt it and read actions
def display(self):
#display it overly things
mousePos = pygame.mouse.get_pos()
#if over mouse
if ButtonCPyg.butX + ButtonCPyg.width > mousePos[0] > ButtonCPyg.butX and ButtonCPyg.butY + ButtonCPyg.height > mousePos[1] > ButtonCPyg.butY:
#then blit it 'overly'
mainc.gameDisplay.blit(ButtonCPyg.picOver,(ButtonCPyg.butX,ButtonCPyg.butY))
#text on it
design.message_display(ButtonCPyg.text,ButtonCPyg.butX-ButtonCPyg.width/2,ButtonCPyg.butY-ButtonCPyg.height/2,ButtonCPyg.textSize,ButtonCPyg.textColor)
else: #if not
#blit it normaly
mainc.gameDisplay.blit(ButtonCPyg.pic,(ButtonCPyg.butX,ButtonCPyg.butY))
#text on it
design.message_display(ButtonCPyg.text,ButtonCPyg.butX-ButtonCPyg.width/2,ButtonCPyg.butY-ButtonCPyg.height/2,ButtonCPyg.textSize,ButtonCPyg.textColor)
##action listen
def listen(thing):
#listen if click
mousePos = pygame.mouse.get_pos()
#if over mouse
if ButtonCPyg.butX + ButtonCPyg.width > mousePos[0] > ButtonCPyg.butX and ButtonCPyg.butY + ButtonCPyg.height > mousePos[1] > ButtonCPyg.butY:
#then listen it 'overly'
thing.action()
##
def helloW(anarg):
print("Clicked"))
我有完整的游戏循环:
##GAME loops
def mainLoop():
#Main game loops . Set run True
mainc.isRunningMain = True
##Widgets
oneba = ButtonCPyg()
oneba.giveOpt(x=0,y=0,width=400,height=200,action = helloW)
oneba.changeText("Hello","coolFont.otf",25,(0,0,0))
oneba.changePic("thepic.png","thepic.png")
onebb = ButtonCPyg()
onebb.giveOpt(x=200,y=600,width=400,height=200,action = helloWW)
onebb.changeText(newText="Hello",newFont="coolFont.otf",newSize=25,newColor=(255,255,255))
onebb.changePic("thebutt.png","thebuttO.png")
#while run run
while mainc.isRunningMain:
#event handle
for event in pygame.event.get():
##qquit
if event.type == pygame.QUIT:
##then quit
mainc.isRunningMain = False
if event.type == pygame.MOUSEBUTTONDOWN:
#then list mousbdownact
oneba.listen()
onebb.listen()
##displays
#bg
mainc.gameDisplay.fill((0,0,0))
##
design.headEr()
oneba.display()
onebb.display()
####UPDATE
pygame.display.update()
#tick the cloclk
mainc.fpsEl += 1
##TIME
if mainc.fpsEl == mainc.fpsCur:
#then add to time
mainc.timeEl += 1
##
if point.point + point.income <= point.storage:
#then add points
point.point += point.income
##else
else:
#then not add it
point.macSt = True
#print(mainc.timeEl)
#set it back
mainc.fpsEl = 0
#
mainc.clock.tick(mainc.fpsCur)
答案 0 :(得分:0)
正如@Hacketo发布的那样,问题是当你调用实例
时 ButtonCPyg.butX = 0
您使用所谓的class attributes,由类本身拥有,由所有类实例共享
这意味着当您创建第二个按钮(即ButtonCPyg
个实例)时,会覆盖ButtonCPyg
的所有类属性 class:两个按钮具有相同的参数,并显示在屏幕上的相同位置。
要避免此问题,您应使用instance attributes,由类的特定实例拥有。您可以使用self.
前缀声明此类属性:将所有ButtonCPyg.attribute_name
更改为self.attribute_name
:
self.butX = 0
希望这会有所帮助:)