简体   繁体   English

pygame.mouse.get_pos()没有更新

[英]pygame.mouse.get_pos() is not updating

So I'm trying to make this button change color when I hover over it, but pygame.mouse.get_pos() is not updating after I open the program. 因此,当我将鼠标悬停在按钮上时,我试图使其颜色更改,但是打开程序后pygame.mouse.get_pos()不会更新。

I'm a novice to both python and program so any assistance would be a appreciated greatly. 我是python和程序的新手,因此非常感谢您的协助。

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()

I'm not really sure why pygame.mouse.get_pos() isn't updating as I have both pygame.time.Clock() and clock.tick(60). 我不太确定为什么pygame.mouse.get_pos()没有更新,因为我同时拥有pygame.time.Clock()和clock.tick(60)。

I'm not a pygame expert myself but what i can see is that you're calling the button() function only once when the game is starting. 我自己不是pygame专家,但是我可以看到的是,在游戏开始时,您只调用一次button()函数。 You need to make a function that will be called in every frame which will wait for an event to happen, in your case, changing the button's color. 您需要制作一个在每一帧中都会被调用的函数,该函数将等待事件发生(在您的情况下),以更改按钮的颜色。 Make a normal button on the screen. 在屏幕上制作一个普通按钮。 Then, make a function which will be called when the mouse is on top of the button and change color. 然后,创建一个函数,当鼠标位于按钮上方时将被调用并更改颜色。 Something like, 就像是,

Button = MakeButton(); #This function will create a button and show on the screen

In while not gameExit: 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

These are pseudo codes to get you started. 这些是伪代码,可以帮助您入门。

pygame uses the "update/draw loop" pattern, commonly found in game development frameworks. pygame使用游戏开发框架中常见的“更新/绘制循环”模式。 This pattern consists of a loop that is mostly non-terminating, and you have it in your code as while not gameExit: . 此模式由一个循环组成,该循环几乎是不终止的,并且您将其包含在代码中, while not gameExit: Inside this loop, you must handle all of the user input, through the usage of event flags. 在此循环内,您必须通过使用事件标志来处理所有用户输入。 For example, your code deals with the QUIT event. 例如,您的代码处理QUIT事件。

However, your button code is not particularly tailored for this pattern. 但是,您的按钮代码并非专门针对此模式而设计。 Instead, it hopes to create a "button" as some sort of static object on the screen, like event-based GUIs. 相反,它希望在屏幕上创建“按钮”作为某种静态对象,例如基于事件的GUI。

First, you should separate the update step and the draw step. 首先,您应该将更新步骤和绘制步骤分开。 The update changes the game state, and the draw step simply checks the state and draw things on screen. 更新会更改游戏状态,并且绘制步骤仅检查状态并在屏幕上绘制内容。 In your case, the update step should check if the mouse is over the button or not, and define which color is used. 在您的情况下,更新步骤应检查鼠标是否在按钮上方,并定义使用哪种颜色。

Let's do that separation: 让我们进行分离:

# 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'

Now that the button() function separates the update and draw steps, you can properly use it in the game loop: 现在, 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

Now, this should work. 现在,这应该工作。 But if you know object-oriented programming (OOP), you could define a class Button that holds its position and color, an update method that checks the mouse and change the button state properly, and a draw method that draws the button. 但是,如果您了解面向对象的编程(OOP),则可以定义一个Button类,该类保留其位置和颜色,一个update方法检查鼠标并正确更改按钮状态,而draw方法绘制按钮。 If you don't know OOP, this is a great opportunity to learn it :) 如果您不了解OOP,这是学习它的绝好机会:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM