Get screen orientation with Python - python

I did a simple game using Python and Pygame.
The game works on both orientations (Portrait and Landscape) since that I play in the same orientation that I used to open the game.
If I rotate the device changing the orientation with the game running, everything appears at the wrong place.
The problem is that I don't know how to detect that the device was rotated to redraw the screen correctly.
Then, my question is:
How I can detect that the user rotated the device using only a Python or Pygame code?
IMPORTANT: Suggestions should not be related to any particular OS, because the same code must run on notebooks with touch screen (running Linux, Windows or OS X) and mobile devices (running Android or iOS).
OBSERVATION: I tried to create another screen to get its size and compare with the actual screen using pygame.display.set_mode() but this new screen returned the same size of the actual screen instead of have its width equals to actual screen height.
Thanks for your help.

You have to set the RESIZABLE flag when you generate the display surface by set_mode(). When the logical display resolution changes, then you get a VIDEORESIZE event:
screen = pygame.set_mode((0, 0), pygame.FULLSCREEN | pygame.pygame.RESIZABLE)
width, height = screen.get_size()
while run:
for event in pygame.event.get():
if event.type == pygame.VIDEORESIZE:
width, height = event.w, event.h

Related

How do I delete the previous possible moves when I click on a different sprite in my Pygame chess game? [duplicate]

Hi i want to ask a question which has been answered a lot of times on this site but i never found an appreciable answer.How to remove the images in Pygame if there is already a background image in game window.
In most of the answers,they say to use screen.fill(color) but it makes the area black.It works fine if there is no background image but with background image,it looks odd when only a certain region is colored black.How can i get rid of it?Is there an alternative way to remove the image in this particular situation.By the way I'm not adding any specific code here because i deal with this issue a lot of time while building games.
You can not remove an image. You have to redraw the entire scene (including the background) in the application loop. Note the images are just a buch of pixel drawn on top of the background. The background "under" the images is overwritten and the imformation is lost:
The main application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
A minimum application is
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
# main application loop
run = True
while run:
clock.tick(60)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update game states and move objects
# [...]
# clear the display
window.fill(0) # or `blit` the back ground image instead
# draw the scene - draw all the objects
# [...]
# update the display
pygame.display.flip()

Python 3.9.6: How to unblit images after timer reaches 0 [duplicate]

Hi i want to ask a question which has been answered a lot of times on this site but i never found an appreciable answer.How to remove the images in Pygame if there is already a background image in game window.
In most of the answers,they say to use screen.fill(color) but it makes the area black.It works fine if there is no background image but with background image,it looks odd when only a certain region is colored black.How can i get rid of it?Is there an alternative way to remove the image in this particular situation.By the way I'm not adding any specific code here because i deal with this issue a lot of time while building games.
You can not remove an image. You have to redraw the entire scene (including the background) in the application loop. Note the images are just a buch of pixel drawn on top of the background. The background "under" the images is overwritten and the imformation is lost:
The main application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
A minimum application is
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
# main application loop
run = True
while run:
clock.tick(60)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update game states and move objects
# [...]
# clear the display
window.fill(0) # or `blit` the back ground image instead
# draw the scene - draw all the objects
# [...]
# update the display
pygame.display.flip()

Pygame screen loosing click ability when resized

I am making a game using python and pygame. I had a problem a few days ago that I needed to give my games a functionality of being resized and maintain the aspect ratio. Also everything on the screen is resized proportionately. And luckily I got a quick solution to create two different pygame surfaces. One is the screen visible to the user and the other is to manage the blitting functionality. Actually, fake screen has everything blitted and then it itself is blitted to the main screen by using
main_screen.blit((pygame.transform.scale(fake_screen, main_screen.get_rect().size), [0, 0]).
The main problem is that now since the MOUSEBUTTONDOWN events are getting triggered on the main screen and not on fake screen, But
the clicks are getting processed according to the fake screen. This means that when I click on a button after resizing, the button appears to be their but actually its at its respective position on the fake screen. This makes all the buttons loose their functionality after the VIDEORESIZE event. Can anyone help me out with this? I hope that I was able to explain.
Easy answer: use the pygame.SCALED display flag.
It resizes the main screen for you and the mouse events too, without your program needing to know anything about it. Documented on this page: https://www.pygame.org/docs/ref/mixer.html
Using this means you wouldn’t need to use a fake screen at all, or do anything at all with scaling on your end.
DIY answer:
If you still want to control the scaling yourself, you just have to scale the mouse events along with the screen. Like scale then the opposite way you scale the fake screen.
In your case it looks like that would involve dividing the mouse event x by the ratio between fakescreen width and screen width, and same with y (with heights ofc).
I got a very easy solution to this myself. I just after getting mouse x and y coordinates, changed them to proportionately corresponding points. With a simple math. I mean, if x coordinate is 15% of main screen width, then convert it to 15% of fake screen width. This way, the fake screen will get properly scaled coordinates. The mathematical equation can be as follows:-
mouse_x = mouse_x/(xd/100)
mouse_x *= 10
mouse_y = mouse_y/(yd/100)
mouse_y *= 6
Here xd and yd are width and height of the resizable main screen respectively. And 10 and 6 are 1% of 1000 and 600 which are the width and height of the fake screen.
This solved my problem and game is now working perfectly.
Thank You.

How can I make an image disappear on pygame? [duplicate]

Hi i want to ask a question which has been answered a lot of times on this site but i never found an appreciable answer.How to remove the images in Pygame if there is already a background image in game window.
In most of the answers,they say to use screen.fill(color) but it makes the area black.It works fine if there is no background image but with background image,it looks odd when only a certain region is colored black.How can i get rid of it?Is there an alternative way to remove the image in this particular situation.By the way I'm not adding any specific code here because i deal with this issue a lot of time while building games.
You can not remove an image. You have to redraw the entire scene (including the background) in the application loop. Note the images are just a buch of pixel drawn on top of the background. The background "under" the images is overwritten and the imformation is lost:
The main application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
A minimum application is
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
# main application loop
run = True
while run:
clock.tick(60)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update game states and move objects
# [...]
# clear the display
window.fill(0) # or `blit` the back ground image instead
# draw the scene - draw all the objects
# [...]
# update the display
pygame.display.flip()

How to remove a previous rectangle created with pygame.draw.rect and having a picture as background [duplicate]

Hi i want to ask a question which has been answered a lot of times on this site but i never found an appreciable answer.How to remove the images in Pygame if there is already a background image in game window.
In most of the answers,they say to use screen.fill(color) but it makes the area black.It works fine if there is no background image but with background image,it looks odd when only a certain region is colored black.How can i get rid of it?Is there an alternative way to remove the image in this particular situation.By the way I'm not adding any specific code here because i deal with this issue a lot of time while building games.
You can not remove an image. You have to redraw the entire scene (including the background) in the application loop. Note the images are just a buch of pixel drawn on top of the background. The background "under" the images is overwritten and the imformation is lost:
The main application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
A minimum application is
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
# main application loop
run = True
while run:
clock.tick(60)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update game states and move objects
# [...]
# clear the display
window.fill(0) # or `blit` the back ground image instead
# draw the scene - draw all the objects
# [...]
# update the display
pygame.display.flip()

Categories