Discover your screen orientation using Python

I recently developed a Python game using Pygame which operates flawlessly in both Portrait and Landscape orientations. However, I encountered an issue when the user rotates their device while the game is running, causing everything to appear jumbled up on the screen.

My dilemma lies in the fact that I am unsure of how to detect when the user has rotated the device in order to properly redraw the game screen.

So my question is:

Is there a way to detect if the user has rotated the device solely through Python or Pygame code?

It is important to note that any suggestions should be OS-agnostic, as the same code needs to work seamlessly across devices ranging from touch-screen laptops (Linux, Windows, macOS) to mobile devices (Android, iOS).

As an attempt to solve this issue, I tried creating a new screen to compare sizes with the actual screen using pygame.display.set_mode(). Unfortunately, the newly created screen returned the same size as the current screen instead of matching its width with the actual screen height.

Thank you in advance for your assistance.

Answer №1

Make sure to include the RESIZABLE flag when creating the display surface using the set_mode() function in Pygame. This flag allows for handling a VIDEORESIZE event when the logical display resolution changes:

screen = pygame.set_mode((0, 0), pygame.FULLSCREEN | pygame.pygame.RESIZABLE)
width, height = screen.get_size()
while running:

    for event in pygame.event.get():
    
        if event.type == pygame.VIDEORESIZE:
             width, height = event.w, event.h

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps are needed to set up PyCharm for running py.test tests?

As I embark on the journey of writing unit tests for my Python code, I have decided to go with the py.test framework instead of Python's bundled unittest. After adding a "tests" directory to my project and including test_sample.py in it, my next step ...

Creating a process to build a fresh dataframe by selecting certain columns from an existing dataframe without the need for any user-defined variables

Solving the Problem: I am working on creating a method that can connect specific columns in a dataframe to generate a new dataframe without specifying the exact columns initially. The intention is to stack the columns side by side for easier data analysis. ...

What is the best way to retrieve files from a specific folder using file names saved in a python list?

I have a large collection of excel files, close to 10k in total, stored in a folder. Additionally, I have a python list containing nearly 8k filenames. My goal is to extract the files from the folder that match the filenames in the list, and then transfer ...

Guide to Making a Next Button in Tkinter for Easy Frame Switching

In my tkinter quiz program, each question has its own frame. After the user selects the correct answer from a radiobutton list, a window pops up allowing them to navigate between questions. Here's an example: https://i.stack.imgur.com/Hij8g.png Inste ...

Creating a Python API for JSON data reading

Update for 2020: Unfortunately, the API is currently not functioning properly and is no longer accessible. I am trying to utilize a JSON api in order to retrieve a random color and store it in a variable. Here's the code I have attempted so far: The ...

Merge a series of rows into a single row when the indexes are consecutive

It seems like I need to merge multiple rows into a single row in the animal column. However, this should only happen if they are in sequential order and contain lowercase alphabet characters. Once that condition is met, the index should restart to maintain ...

Splitting arrays at their edges

Query: Given an ndarray: In [2]: a Out[2]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) I am searching for a method that would result in: array([7, 8, 9, 0, 1]) Example: Starting at index 8, crossing the array boundary and stopping at index 2 (included). When ...

Refreshing all tabs in Selenium after a specific amount of time

I have a rotating dashboard set up using selenium webdriver, and I've noticed that the pages need to be refreshed every few hours. How can I incorporate this into my existing code? There are 8 tabs open, cycling through each tab every 10 seconds. I&a ...

What is the best way to create a Phrases model using a vast collection of articles (such as those from Wikipedia)?

To enhance the results in topic detection and text similarity, I am eager to create a comprehensive gensim dictionary for the French language. My strategy involves leveraging a Wikipedia dump with the following approach: Extracting each article from frwi ...

When using Python with Selenium, the value of a CSS property may appear differently in the output compared to

I am trying to figure out why all of the properties are returning as normal instead of bold for some text. I was hoping someone could explain to me what is causing this issue and how to fix it. This is my Python code: from selenium import webdriver urls ...

Troubleshooting issues with loading scripts in a scrapy (python) web scraper for a react/typescript application

I've been facing challenges with web scraping a specific webpage (beachvolleyball.nrw). In the past couple of days, I've experimented with various libraries but have struggled to get the script-tags to load properly. Although using the developer ...

Issue with running Selenium Webdriver on Google Colab using Mac and Google Chrome

Currently, I am following a web scraping tutorial on GeeksForGeeks. The tutorial can be found at the link below: I am using a Macbook Pro and working in Google Colab through Chrome browser. However, I encountered an issue when running the 4th command blo ...

What is the best technique for scrolling multiple times to locate a specific element?

I am trying to develop a script that will continuously scroll until it finds the specific element using: from appium.webdriver.common.touch_action import TouchAction The webpage in question is an Events page featuring various game titles. Currently, I ha ...

"Exploring Optimization with Python-mip and Enhancing Code with

Currently, I am utilizing Python with VSCode on Ubuntu. Despite having installed Python-mip and successfully running it in the terminal, VSCode seems to be unable to detect the mip library from Python-mip. Specifically, when I attempt to import necessary ...

Using Flask to send and receive JSON requests with boolean values

How can I include a boolean value in the request body using the Python requests library? I attempted: request_body = {'someBooleanValue': true}. => NameError: name 'true' is not defined request_body = {'someBooleanValue': ...

How to store a pointer to a pointer in Python?

Is it possible to store a reference to a reference in Python, allowing for the ability to change what that reference points to in another context? Consider the following scenario: class Foo: def __init__(self): self.standalone = 3 self.lst ...

Obtain information and receive alerts from Chrome console logs using Selenium through customized commands

Currently, I am attempting to extract logs from Chrome's console using Selenium with Python, but I am encountering some confusion as this is my first time working with it. While the code generally functions well and displays logs, I specifically need ...

Unable to store form information in Django's database

I'm currently working on form creation for my Django project and I've encountered an issue. I am unable to save form data to the Django database specifically for one of my forms. While I can successfully add data for the Wish_list model, I'm ...

Playing TIC TAC TOE with a fun twist of skipping turns

I've been tackling a python tic-tac-toe program lately. Everything seems to be working smoothly for the Human player's turn, but once the AI takes its first turn, it stops making any further moves. I've carefully gone through the code multip ...

What is the method for merging multiple Django querysets without combining them?

Having recently started working with the Django framework, I'm facing an interesting challenge that may be of interest to more experienced developers here. Here is the django model in question: STATUS_CHOICES = ( ('CL', 'CLOSED&apo ...