gpt4 book ai didi

python - 改变一个类,当鼠标悬停在它上面时,它会改变颜色 - Pygame

转载 作者:行者123 更新时间:2023-11-28 18:15:34 25 4
gpt4 key购买 nike

import pygame as pg
import sys
import time

# All pygame stuff under here
pg.init()

# Font definitions
backFont = pg.font.SysFont("monospace", 40)
titleFont = pg.font.SysFont("garamond", 100)
cipherFont = pg.font.SysFont("garamond", 50)
buttonFont = pg.font.SysFont("garamond", 25)
bigFont = pg.font.SysFont("garamond", 100)
Font = pg.font.SysFont(None, 32)
inputFont = pg.font.SysFont('consola', 35)
errorFont = pg.font.SysFont('tahoma', 20)
diagramFont = pg.font.SysFont('courier new', 25)

# Colour definitions
BackGray = pg.Color('gray60')
screenGray = pg.Color('gray80')
buttonGray1 = pg.Color('gray40')
buttonGray2 = pg.Color('gray50')
buttonGray3 = pg.Color('gray30')
textColour = pg.Color('navy')

# Screen size set
screen = pg.display.set_mode((800, 600))

# Clock initiated to allow regular typing speeds
clock = pg.time.Clock()

# Change button class so that when the mouse pos is sent through, changes colour ======================
class Button(pg.sprite.Sprite):
def __init__(self, text, x, y, width, height, enabled):
super().__init__()
self.colour = buttonGray2
self.image = pg.Surface((width, height))
self.image.fill(buttonGray2)
self.rect = self.image.get_rect()
txt = buttonFont.render(text, True, textColour)
txtRect = txt.get_rect(center=self.rect.center)
self.image.blit(txt, txtRect)
self.rect.topleft = x, y
self.enabled = enabled

def isPressed(self, event):
if self.enabled == True:
if event.type == pg.MOUSEBUTTONDOWN:
# MOUSE... events have an event.pos attribute (the mouse position)
# which you can pass to the collidepoint method of the rect.
if self.rect.collidepoint(event.pos):
return True
return False
def HoveredOver(self,event):
print("Ocurring")
if event.type == pg.MOUSEMOTION:
if self.rect.collidepoint(event.pos):
print("Hoveredover")
self.colour = buttonGray1
else:
self.colour = buttonGray2

def FrontPage():
# Back screen
screen.fill(screenGray)

# Button definition for continuing
Continue = Button('Continue', 105, 455, 120, 50, True)
buttonsGroup = pg.sprite.Group(Continue)

# Pygane while loop for events
while True:
for event in pg.event.get():
mouseX, mouseY = pg.mouse.get_pos()
if event.type == pg.QUIT:
pg.quit()
sys.exit()
elif Continue.isPressed(event):
print("Continue")
Continue.HoveredOver(event)

buttonsGroup.draw(screen)

pg.display.flip()
clock.tick(60)

FrontPage()

我有这个类来在 Pygame 中定义和创建按钮。目前,当鼠标悬停在按钮上时,它不会改变颜色。我试图在类中添加另一个方法,以便在鼠标悬停在按钮上时改变颜色。但是,当我运行 HoveredOver 方法时,它不会更改按钮的颜色。

如何让这个方法改变按钮的颜色?

提前致谢!

最佳答案

改变 Sprite 的 colour 属性是不够的,你还必须改变 image 因为当你调用 时 pygame 会将图像 blits 到屏幕上buttonsGroup.draw(屏幕)

我会在 __init__ 方法中创建图像或将它们作为参数传递,然后如果鼠标悬停在上方,则通过将当前图像分配给 self.image 来交换它们按钮。

class Button(pg.sprite.Sprite):

def __init__(self, text, x, y, width, height, enabled):
super().__init__()
self.colour = buttonGray2
self.image = pg.Surface((width, height))
self.image.fill(buttonGray2)
self.image_normal = self.image # Store a reference to the original image.
# Create a separate hover image.
self.image_hover = pg.Surface((width, height))
self.image_hover.fill(buttonGray1)
self.rect = self.image.get_rect()
txt = buttonFont.render(text, True, textColour)
txtRect = txt.get_rect(center=self.rect.center)
self.image.blit(txt, txtRect)
self.image_hover.blit(txt, txtRect) # Blit the text onto the hover image.
self.rect.topleft = x, y
self.enabled = enabled

def isPressed(self, event):
if self.enabled == True:
if event.type == pg.MOUSEBUTTONDOWN:
# MOUSE... events have an event.pos attribute (the mouse position)
# which you can pass to the collidepoint method of the rect.
if self.rect.collidepoint(event.pos):
return True
return False

def HoveredOver(self, event):
if event.type == pg.MOUSEMOTION:
if self.rect.collidepoint(event.pos):
print("Hoveredover")
# Swap the image.
self.image = self.image_hover
else:
# Swap the image.
self.image = self.image_normal

关于python - 改变一个类,当鼠标悬停在它上面时,它会改变颜色 - Pygame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48643605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com