1
votes

Trying to create a simple bit of code that registers mouse position on click when in window and also when in fullscreen.

Currently the correct values will be returned when in windowed mode (providing cursor is over the window) however in fullscreen regardless of the mouse position I'm returned the value 800,480 (screen resolution). Here is my code:

import pygame

windowwidth = 800
windowheight = 480
pygame.init()
pygame.display.init()
screen = pygame.display.set_mode((windowwidth, windowheight))
pygame.display.flip()

running = 1
while running:
    LEFT = 1
    event1 = pygame.event.get()
    for event in event1:
         if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
              print(event.pos)
         if event.type == pygame.KEYDOWN:
              if event.key == pygame.K_f:
                   pygame.display.set_mode((800,480), pygame.FULLSCREEN)
              if event.key == pygame.K_g:
                   pygame.display.set_mode((800, 480))
              else:
                   break

    pygame.display.flip()
2

2 Answers

1
votes

While @Michael O'Dwyer's answer suffices, in addition to his clarification of pre-determining the canvas size of the PyGame window, you could also detect your system's width and height automatically using the pygame.display.Info() method for better clarity. Here would be your code with automatically detected width and height:

import pygame

# Initialize Pygame
pygame.init()

# Get system display information
displayInfo = pygame.display.Info()
windowwidth = displayInfo.current_w
windowheight = displayInfo.current_h

# Commented out - No need to reinitialize other Pygame modules
# after pygame.init() is called
# pygame.display.init()

screen = pygame.display.set_mode((windowwidth, windowheight))
pygame.display.flip()

running = 1
while running:
    LEFT = 1
    for event in pygame.event.get():
         if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
              print(event.pos)
         if event.type == pygame.KEYDOWN:
              if event.key == pygame.K_f:
                   pygame.display.set_mode((windowwidth, windowheight), pygame.FULLSCREEN)
              if event.key == pygame.K_g:
                   pygame.display.set_mode((800, 480))
              else:
                   break

    pygame.display.flip()

Here is the PyGame documentation for the pygame.display.Info() method.

Hope this helped!

0
votes

The problem is that pygame is getting it's mouse-click coordinates in pixels which is tied to the resolution that is passed in pygame.display.set_mode(). So because you are setting the resolution to (800, 480) when it goes fullscreen, it isn't going to give you accurate pixel coordinates.

The solution?

Pass in the real monitor resolution when going full screen. In pygame, if you pass in (0, 0) as the resolution, pygame will create a full-screen resolution for you.

So pass in this special screen size and a fullscreen flag and that's it! The code now works!

I hope this answer helped you! If you have any further questions please post a comment below!

import pygame

windowwidth = 0
windowheight = 0
pygame.init()
pygame.display.init()
screen = pygame.display.set_mode((windowwidth, windowheight))
pygame.display.flip()

running = 1
while running:
    LEFT = 1
    event1 = pygame.event.get()
    for event in event1:
         if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
              print(event.pos)
         if event.type == pygame.KEYDOWN:
              if event.key == pygame.K_f:
                   pygame.display.set_mode((0,0), pygame.FULLSCREEN)
              if event.key == pygame.K_g:
                   pygame.display.set_mode((800, 480))
              else:
                   break    

    pygame.display.flip()