How can I change the resolution of my screen in pygame - python

I want to create a game in pygame but want it to have pixelated graphics, so instead of resizing a pixelated image, i was hoping to just change the resolution of the pygame screen .
screen = pygame.display.set_mode((width, height))
Thanks.

Make two screens. One with your desired resolution( let's call screen) and the other with your desired screen size(let's call window). Then blit screen into window while scaling it to the size of the window.
window.blit(pygame.transform.scale(screen,(windoWidth,windowHeight)),(0,0))
That should work.
EDIT: As the Ted's comments suggests it will be more easy to understand like this.
resized_screen = pygame.transform.scale(screen, (windoWidth,windowHeight))
window.blit(resized_screen, (0, 0))

Related

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.

Maximize the pygame window without making it fullscreen

Is there a way to maximize the pygame window when intializing the window with set_mode without making it completely fulsscreen. I tried to get the required window_size by printing event.size, when VIDEORESIZE is called, which is (1920, 1017). But if I use this value for my window_size when setting mode I just get a window at the same size of a maximized window. If I press maximize in the top right corner it just switches between a thicker and a thinner border.
Obviously i want the thinner border, is that possible from the start?
You should try to use the
RESIZEABLE
When using set_mode in pygame like
DISPLAYSURF = pygame.display.set_mode((1920,1017), RESIZABLE)

Get screen orientation with 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

How do I make my screen zoom in and out in pygame?

The code: https://pastebin.pl/view/6f60a32d
I have tried using opengl and tried putting all of the stuff into a surface, and none of them worked
if anyone have any ideas on how to zoom in my game pls help me
Use pygame.transform.smoothscale() to generate a scaled copy of the display pygame.Surface after drawing the scene:
zoomed_screen = pygame.transform.smoothscale(screen, (new_width, new_height))
Blit the zoomed Surface on the screen
screen.blit(zoomed_screen, ....)

Pygame Black Box

Code:
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 400))
What happens:
The pygame screen pulls up, but there is a black box covering the top right-hand corner of the screen. I would post a picture but stack overflow wouldn't let me.
Yes, so what are you trying to make happen?
pygame.display.set_mode((0, 0)) should make that box full screen. Is that your objective?

Categories