我是pygame的新手,一直在尝试创建带有一些按钮的简单界面。当鼠标悬停在按钮上时,我无法更改按钮的颜色。
我已经成功创建了按钮,但是无法使其与鼠标交互。该代码使用一个绿色按钮实例创建一个按钮对象。当鼠标悬停在鼠标上时,应该将按钮从绿色更改为红色。
import pygame
pygame.init()
display_width = 1200
display_height = 600
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
StartScreen = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Log In')
clock = pygame.time.Clock()
StartScreen.fill(white)
class Buttons():
def __init__(self, color, x, y, width, height, text=''):
self.color = color
self.x = int(x)
self.y = int(y)
self.w = int(width)
self.h = int(height)
self.text = text
def Draw(self, StartScreen, outline=None):
if outline:
pygame.draw.rect(StartScreen, outline, (float(self.x-2), float(self.y-2), float(self.w+4), …Run Code Online (Sandbox Code Playgroud)