简体   繁体   English

鼠标坐标不变 - python

[英]Mouse coordinates not changing - python

I am trying to build a simple game, but I'm having trouble with the mouse as it's doesn't show that it's coordinates are changing.我正在尝试构建一个简单的游戏,但是我遇到了鼠标问题,因为它没有显示它的坐标正在改变。

Here's my code:这是我的代码:

import pygame

pygame.init()

# Colors
white = (255, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)

# Game Screen Dimensions
game_layout_length = 500
game_layout_width = 500

# Mouse Positions
pos = pygame.mouse.get_pos()

# Character Attributes
character_length = 10
character_width = 10

game_screen = pygame.display.set_mode((game_layout_width, game_layout_length))

game_close = False
game_lost = False

while not game_close:

    while not game_lost:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                game_close = True
                game_lost = True

            if event.type == pygame.MOUSEBUTTONDOWN:
                print(pos)
                game_screen.fill(black)
                pygame.display.update()

pygame.quit()
quit()

This is the result after clicking on multiple different parts of the screen:这是单击屏幕的多个不同部分后的结果:

pygame 2.1.2 (SDL 2.0.18, Python 3.8.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)

Process finished with exit code 0

Also, is there a way to get a rectangle to follow my mouse?另外,有没有办法让一个矩形跟随我的鼠标? I tried doing pygame.mouse.rect(game_screen, blue, [pos, character_length, character_width]) , but that just crashed my program.我尝试做pygame.mouse.rect(game_screen, blue, [pos, character_length, character_width]) ,但这只是让我的程序崩溃。

So, the problem is that you are not refreshing the mouse position, because is not in the loop.所以,问题是你没有刷新鼠标position,因为不在循环中。 All you need to do is to put the pos variable inside the loop.您需要做的就是将pos变量放入循环中。

Here is how should you do it:这是您应该如何做的:

if event.type == pygame.MOUSEBUTTONDOWN:
        pos = pygame.mouse.get_pos()
        print(pos)
        game_screen.fill(black)
        pygame.display.update()

I hope I helped you!我希望我对你有帮助!

Thanks, I successfully set a variable to my x and y coordinates but when I try to make something (like a rectangle) follow my mouse, it doesn't work or even show up.谢谢,我成功地为我的 x 和 y 坐标设置了一个变量,但是当我尝试让一些东西(如矩形)跟随我的鼠标时,它不起作用甚至显示出来。 The program just executes a plain black screen.该程序只是执行一个纯黑屏。

Here's my code:这是我的代码:

import pygame

pygame.init()

# Colors
white = (255, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)

# Game Screen Dimensions
game_layout_length = 500
game_layout_width = 500

# Character Attributes
character_length = 10
character_width = 10

game_screen = pygame.display.set_mode((game_layout_width, game_layout_length))

game_close = False
game_lost = False

while not game_close:

    while not game_lost:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                game_close = True
                game_lost = True

            if event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                character_x = pos[0]
                character_y = pos[1]
                pygame.draw.rect(game_screen, blue, [character_x, character_y, character_length, character_width])
                game_screen.fill(black)
                pygame.display.update()

pygame.quit()
quit()

I used a debug line to see if it was an issue with the variables, but those seemed to be working just fine, so I'm unsure where the issue is.我使用调试行来查看变量是否存在问题,但这些似乎工作得很好,所以我不确定问题出在哪里。

This is the answer for your second question:这是您第二个问题的答案:

So, the issue there is that after you draw the rectangle, you are filling the screen with black, so, all the rectangles are covered up with the color black.所以,问题是在你绘制矩形之后,你正在用黑色填充屏幕,所以,所有的矩形都被黑色覆盖了。

All you need to do is to delete game_screen.fill(black) and it will work.您需要做的就是删除game_screen.fill(black)并且它会起作用。

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

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