Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lock mouse in window Pygame

I want to make a FPS game in Pygame in windowed mode.

I need to be able to move my camera around for 360 degrees and more without limitation and with the hidden cursor.

I used Pygame's set_visible and set_pos but it doesn't prevent my mouse going out of the window and blocking on the screen borders.

import pygame
pygame.init()
game_display = pygame.display.set_mode((800,600))
pygame.mouse.set_visible(False)

exit = False

while (not exit):
    pygame.mouse.set_pos = (400, 300)
    mouse_move = (0,0)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit = True
        if event.type == pygame.MOUSEMOTION:
            mouse_move = event.rel 
    if mouse_move != (0,0):
        print(mouse_move)

pygame.quit()
like image 220
Elgirhath Avatar asked Dec 23 '22 11:12

Elgirhath


1 Answers

You have to call pygame.event.set_grab(True) as well.

Better allow the users to exit with the Esc or another key, because they won't be able to click the x button anymore to close the window.

elif event.type == pygame.KEYDOWN:
    if event.key == pygame.K_ESCAPE:
        exit = True
like image 135
skrx Avatar answered Dec 27 '22 10:12

skrx