屏幕上只画了一个按钮

2024-04-24 18:35:47 发布

您现在位置:Python中文网/ 问答频道 /正文

当我定义这样一个按钮

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")

。。。屏幕上只有一个按钮。你知道吗

我可以“听”的行动,但当我点击它,例如有两行“你好字”

这意味着我的代码不能使用两个或多个按钮。你知道吗

这是我的按钮类:

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)

Tags: textifpngdefdisplayitactionwidth
1条回答
网友
1楼 · 发布于 2024-04-24 18:35:47

正如@Hacketo发布的,问题是当你打电话的时候

 ButtonCPyg.butX = 0

您使用所谓的class attributes,它由类本身所拥有,并由类的所有实例所共享。你知道吗

这意味着,当您创建第二个按钮(即ButtonCPyg实例)时,您覆盖了ButtonCPyg类的所有类属性:两个按钮具有相同的参数,并且在屏幕上显示在相同的位置。你知道吗

为了避免这个问题,您应该使用instance attributes,它由类的特定实例所拥有。可以使用self.前缀声明这种类型的属性:将所有ButtonCPyg.attribute_name更改为self.attribute_name

 self.butX = 0

希望这有帮助:)

相关问题 更多 >