所以当我将鼠标悬停在它上面时,我试图让这个按钮改变颜色,但是在打开程序后pygame.mouse.get_pos()没有更新。
我是python和program的新手,所以任何帮助都会得到很大的赞赏。
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption('Click to Adventure')
clock = pygame.time.Clock()
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
gameDisplay.fill(white)
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
ycrd = int((y+(h/2)))
r = int(h/2)
print(mouse)
if x+(w+(h/2)) > mouse[0] > x-(h/2) and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay,ac,(x,y,w,h))
pygame.draw.circle(gameDisplay,ac,(x,ycrd),r,0)
pygame.draw.circle(gameDisplay,ac,(x+w,ycrd),r,0)
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay,ic,(x,y,w,h))
pygame.draw.circle(gameDisplay,ic,(x,ycrd),r,0)
pygame.draw.circle(gameDisplay,ic,(x+w,ycrd),r,0)
smallText = pygame.font.SysFont("comicsansms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x+(w/2)),(y+(h/2)))
gameDisplay.blit(textSurf, textRect)
button("Hi",300,200,100,50,red,green,None)
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
#print(event)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
我不确定为什么pygame.mouse.get_pos()没有更新,因为我有pygame.time.Clock()和clock.tick(60)。
答案 0 :(得分:0)
我自己不是pygame
专家,但我能看到的是,您在游戏开始时只调用一次button()
功能。您需要创建一个将在每个帧中调用的函数,等待事件发生,在您的情况下,更改按钮的颜色。
在屏幕上制作一个普通按钮。然后,创建一个函数,当鼠标位于按钮顶部并更改颜色时将调用该函数。
像,
Button = MakeButton(); #This function will create a button and show on the screen
在while not gameExit:
if Button.get_rect().collidepoint(pygame.mouse.get_pos()): #This line will wait for the mouse to hover over the button
ChangeButtonColor(); #This function will change the button color
这些伪代码可以帮助您入门。
答案 1 :(得分:0)
pygame使用游戏开发框架中常见的“更新/绘制循环”模式。此模式由一个主要是非终止的循环组成,您可以在代码中将其作为while not gameExit:
。在此循环中,您必须通过使用事件标志来处理所有用户输入。例如,您的代码处理QUIT
事件。
但是,您的按钮代码并非专门针对此模式而定制。相反,它希望在屏幕上创建一个“按钮”作为某种静态对象,比如基于事件的GUI。
首先,您应该将更新步骤和绘制步骤分开。更新会更改游戏状态,绘制步骤只会检查状态并在屏幕上绘制内容。在您的情况下,更新步骤应检查鼠标是否在按钮上,并定义使用的颜色。
让我们进行分离:
# to be used in the draw step
def draw_button(msg, x, y, width, height, color):
ycrd = int((y + (h / 2)))
r = int(h / 2)
pygame.draw.rect(gameDisplay, color, (x, y, width, height))
pygame.draw.circle(gameDisplay, color, (x, ycrd), r, 0)
pygame.draw.circle(gameDisplay, color, (x + w, ycrd), r, 0)
smallText = pygame.font.SysFont("comicsansms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x + (w / 2)), (y + (h / 2)))
# to be used in the update step
def check_mouse_state_over_box(x, y, width, height):
# I'm returning strings here, but you should use something else like an enum
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + (w + (h / 2)) > mouse[0] > x - (h / 2) and y + h > mouse[1] > y:
if click[0] == 1:
return 'click'
else:
return 'hover'
else:
return 'no-relation'
现在button()
函数分离了更新和绘制步骤,您可以在游戏循环中正确使用它:
while not game_exit:
# UPDATE STEP
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit() # this function is undefined it seems?
# you can also use 'elif event.type == pygame.MOUSEMOTION:' if you wish, but then the logic is a bit different
button_state = check_mouse_over_box(300, 200, 100, 50)
if button_state = 'no-relation':
button_color = red
else:
button_color = green
if button_state = 'click':
action() # define whatever action you want
# DRAW STEP
draw_button('Hi', 300, 200, 100, 50, button_color)
gameDisplay.blit() # I think this is mandatory in pygame
现在,这应该有效。但是如果你知道面向对象编程(OOP),你可以定义一个保持其位置和颜色的类Button,一个检查鼠标并正确更改按钮状态的更新方法,以及一个绘制按钮的绘制方法。如果您不了解OOP,这是学习它的好机会:)