屏幕上只绘制了一个按钮
Only one Button is drawn on screen
当我这样定义一个按钮时
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")
...屏幕上只有一个按钮
我可以 "listen" 进行操作,但是当我点击它时,例如有两行 "Hello Word"
这意味着我的代码无法使用两个或更多按钮。
有我的按钮 class :
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)
正如@Hacketo 发布的那样,问题是当您调用实例时
ButtonCPyg.butX = 0
您使用所谓的 class attributes,它们 属于 class 本身 并且 由 共享class.
的所有 个实例
这意味着,当您创建第二个 Button(即 ButtonCPyg
实例)时,您将覆盖所有 class 属性 的 ButtonCPyg
class:两个按钮具有相同的参数,并且显示在屏幕上的相同位置。
要避免此问题,您应该使用 instance attributes,它们 由 class 的特定实例拥有。您可以使用 self.
前缀来声明此类属性:将所有 ButtonCPyg.attribute_name
更改为 self.attribute_name
:
self.butX = 0
希望这对您有所帮助:)
当我这样定义一个按钮时
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")
...屏幕上只有一个按钮
我可以 "listen" 进行操作,但是当我点击它时,例如有两行 "Hello Word"
这意味着我的代码无法使用两个或更多按钮。
有我的按钮 class :
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)
正如@Hacketo 发布的那样,问题是当您调用实例时
ButtonCPyg.butX = 0
您使用所谓的 class attributes,它们 属于 class 本身 并且 由 共享class.
的所有 个实例这意味着,当您创建第二个 Button(即 ButtonCPyg
实例)时,您将覆盖所有 class 属性 的 ButtonCPyg
class:两个按钮具有相同的参数,并且显示在屏幕上的相同位置。
要避免此问题,您应该使用 instance attributes,它们 由 class 的特定实例拥有。您可以使用 self.
前缀来声明此类属性:将所有 ButtonCPyg.attribute_name
更改为 self.attribute_name
:
self.butX = 0
希望这对您有所帮助:)