如何使用 pygame 使启动、停止和重置按钮在 python 中工作?

问题描述

如何使用 pygame 使启动、停止和重置按钮在 python 中工作?这是我的按钮的代码,但我不确定为什么它们没有出现在我的窗口中。每当我在单独的 Python 文件中编写这些代码时,它们都可以工作,但是当我尝试将它们全部放入一个程序时,它们却永远不会出现。

class button():
    def __init__(self,color,x,y,width,height,text=''):
        self.color = color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text

    def draw(self,GameOfLifeMain,outline=None):
        # Call this method to draw the button on the screen
        if outline:
            pygame.draw.rect(GameOfLifeMain,outline,(self.x - 2,self.y - 2,self.width + 4,self.height + 4),0)

        pygame.draw.rect(GameOfLifeMain,self.color,(self.x,self.y,self.width,self.height),0)

        if self.text != '':
            font = pygame.font.SysFont('comicsans',40)
            text = font.render(self.text,1,(0,0))
            GameOfLifeMain.blit(text,(
                self.x + (self.width / 2 - text.get_width() / 2),self.y + (self.height / 2 - text.get_height() / 2)))

    def isOver(self,pos):
        # Pos is the mouse position or a tuple of (x,y) coordinates
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True

        return False


run = True
greenButton = button((0,255,0),80,2,100,50,'Start')
while run:
    gameOfLifeMain()
    pygame.display.update()

    for event in pygame.event.get():
        pos = pygame.mouse.get_pos()

    if event.type == pygame.QUIT:
        run = False
        pygame.quit()
        quit()

    if event.type == pygame.MOUSEBUTTONDOWN:
        if greenButton.isOver(pos):
            print('clicked')

    if event.type == pygame.MOUSEMOTION:
        if greenButton.isOver(pos):
            greenButton.color = (0,0)
        else:
            greenButton.color = (0,0)


    # ExtraRequirement
    ***# STOP***

    def draw(self,self.y + (self.height / 2 - text.get_height() / 2)))


    def isOver(self,y) coordinates
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True

        return False

run = True
redButton = button((255,200,'Stop')
while run:

    pygame.display.update()

    for event in pygame.event.get():
        pos = pygame.mouse.get_pos()

    if event.type == pygame.QUIT:
        run = False
        pygame.quit()
        quit()

    if event.type == pygame.MOUSEBUTTONDOWN:
        if redButton.isOver(pos):
            print('clicked')

    if event.type == pygame.MOUSEMOTION:
        if redButton.isOver(pos):
            redButton.color = (255,127,80)
        else:
            redButton.color = (255,0)

 ***# Restart***

    def draw(self,y) coordinates
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True

        return False

run = True
yellowButton = button(("Yellow 3"),300,'Restart')
while run:

    pygame.display.update()

    for event in pygame.event.get():
        pos = pygame.mouse.get_pos()

    if event.type == pygame.QUIT:
        run = True
        pygame.quit()
        quit()

    if event.type == pygame.MOUSEBUTTONDOWN:
        if yellowButton.isOver(pos):
            print('clicked')

    if event.type == pygame.MOUSEMOTION:
        if yellowButton.isOver(pos):
            yellowButton.color = ("orange")
        else:
            yellowButton.color = ("Yellow 3")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)