【问题标题】:I am trying to make two buttons for my main menu我正在尝试为我的主菜单制作两个按钮
【发布时间】:2020-05-17 10:15:05
【问题描述】:

我使用了 Tech with Tim 提供的关于如何在我的程序中制作按钮的教程。我用第一个做得很好,把它变成了“开始”按钮,但我也需要一个“退出”按钮,但它不起作用。有人可以告诉我我的代码有什么问题吗?我已经尝试了我所知道的一切。代码如下:

import pygame
from pygame.locals import *

pygame.init()

win = pygame.display.set_mode((800, 600))
win.fill((0, 180, 210))

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, win, outline=None):
        # Call this method to draw the button on the screen
        if outline:
            pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)

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

        if self.text != '':
            font = pygame.font.SysFont('comicsans', 60)
            text = font.render(self.text, 1, (0, 0, 0))
            win.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

def redrawWindow():
    win.fill((0, 180, 210))
    greenButton.draw(win, (0, 0, 0))

run = True
greenButton = button((0, 255, 0), 280, 255, 250, 100, "Start")
while run:
    redrawWindow()
    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 the button")

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

class button2():
    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, win, outline=None):
        # Call this method to draw the button on the screen
        if outline:
            pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)

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

        if self.text != '':
            font = pygame.font.SysFont('arial', 60)
            text = font.render(self.text, 1, (0, 0, 0))
            win.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

def redrawWindow():
    win.fill((0, 180, 210))
    greenButton.draw(win, (0, 0, 0))
    redButton.draw(win, (0, 0, 0))

run = True
redButton = button((0, 255, 0), 280, 300, 250, 100, "Quit")
while run:
    redrawWindow()
    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 the 2button")

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

【问题讨论】:

    标签: python button pygame


    【解决方案1】:

    您不需要 button2 类。类的诀窍在于它们可以用于多个实例。见Instance Objects

    只需创建 button 的 2 个实例:

    greenButton = button((0, 255, 0), 280, 190, 250, 100, "Start")
    redButton = button((255, 0, 0), 280, 310, 250, 100, "Quit")
    

    并处理两个按钮的事件:

    while run:
        redrawWindow()
        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 the button")
                if redButton.isOver(pos):
                    print("clicked the 2button")
    
            if event.type == pygame.MOUSEMOTION:
                if greenButton.isOver(pos):
                    greenButton.color = (105, 105, 105)
                else:
                    greenButton.color = (0, 255, 0)
                if redButton.isOver(pos):
                    redButton.color = (105, 105, 105)
                else:
                    redButton.color = (255, 0, 0)
    

    看例子:

    import pygame
    from pygame.locals import *
    
    pygame.init()
    
    win = pygame.display.set_mode((800, 600))
    win.fill((0, 180, 210))
    
    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, win, outline=None):
            # Call this method to draw the button on the screen
            if outline:
                pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)
    
            pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)
    
            if self.text != '':
                font = pygame.font.SysFont('comicsans', 60)
                text = font.render(self.text, 1, (0, 0, 0))
                win.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
    
    def redrawWindow():
        win.fill((0, 180, 210))
        greenButton.draw(win, (0, 0, 0))
        redButton.draw(win, (0, 0, 0))
    
    run = True
    greenButton = button((0, 255, 0), 280, 190, 250, 100, "Start")
    redButton = button((255, 0, 0), 280, 310, 250, 100, "Quit")
    while run:
        redrawWindow()
        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 the button")
                if redButton.isOver(pos):
                    print("clicked the 2button")
    
            if event.type == pygame.MOUSEMOTION:
                if greenButton.isOver(pos):
                    greenButton.color = (105, 105, 105)
                else:
                    greenButton.color = (0, 255, 0)
                if redButton.isOver(pos):
                    redButton.color = (105, 105, 105)
                else:
                    redButton.color = (255, 0, 0)
    

    【讨论】:

      【解决方案2】:

      您不必总是为按钮使用类,也可以使用函数。 这是我的样子:

      def button(x, y, w, h, inactive, active, action=None):
          mouse = pygame.mouse.get_pos()
          click = pygame.mouse.get_pressed()
      
          if x + w > mouse[0] > x and y + h > mouse[1] > y:
              gameDisplay.blit(active, (x, y))
              if click[0] == 1 and action is not None:
                  action()
          else:
              gameDisplay.blit(inactive, (x, y))
      

      这是每个参数的含义:

      x - 按钮的 x 坐标

      y - 按钮的 y 坐标

      w - 按钮的宽度

      h - 按钮的高度

      active - 鼠标悬停在按钮上时的图片

      inactive - 按钮空闲时的图片

      action - 点击按钮时执行的函数

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-24
        • 2011-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-25
        • 2017-02-16
        相关资源
        最近更新 更多