0

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.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
R_C
  • 35
  • 5
  • this [Question](https://stackoverflow.com/questions/70771877/pygame-not-updating-x-and-y/70772584#70772584) might help you – gerda die gandalfziege Jan 23 '22 at 16:04
  • You need to regain the mouse position every frame. `pygame.mouse.get_pos()` does not return a dynamic view, it's just a tuple that does not change. – The_spider Jan 23 '22 at 16:19

3 Answers3

1

So, the problem is that you are not refreshing the mouse position, because is not in the loop. All you need to do is to put the pos variable inside the loop.

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()
bad_coder
  • 11,289
  • 20
  • 44
  • 72
B. Bogdan
  • 56
  • 5
  • @The_spider & B. Bogdan: Thanks, it worked! – R_C Jan 23 '22 at 19:46
  • However, is it possible to get a certain value from the tuple? I'm attempting to set a variable to the x value and another variable to the y value. – R_C Jan 23 '22 at 19:53
  • Yes, for example you can set x = pos[0] and y = pos[1], because pos is a list and at index zero is the x coord. and at index one is the y coord. – B. Bogdan Jan 23 '22 at 23:02
1

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.

B. Bogdan
  • 56
  • 5
0

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. 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.

R_C
  • 35
  • 5