Pygame screen loosing click ability when resized - python

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.

Related

Adding a vertical and horizontal scroll bar to the display surface in Pygame

I'm using pygame and I've been working so far in a large monitor, but now I need to work on a small laptop, whose screen is much smaller than the mentioned monitor. The dimensions I gave to the display surface and the items blitted to it work well on the previous monitor but it is too large on the laptop and therefore I cannot reach all buttons. Since I can't seem to find a way to resize everything proportionaly, is there an easy way to add a vertical and horizontal scroll bar in order to navigate around the display surface?
Thank you
I don't think that you can add a scroll bar. What you can do is shift the x or y of everything when you press the corresponding arrow keys. But as said by Ted Klein Bergman, it is better to simply resize everything.

screensize of turtle in python

I am trying to work with the screen in turtle and am confused about the dimensions of the screen. So in this example:
import turtle
screen = turtle.Screen()
print(screen.screensize())
turtle.done()
Python prints the dimension of the turtle window to be (400,300). However, the screen looks much bigger on the desktop and when I move the turtle by 640 pixels to the right (from the center) or 540 pixels downwards then the edge of the screen is reached. This would indicate that the screensize is 1280 * 1080 pixels.
So my specific questions are:
What information do I get from calling screen.screensize()
When the turtle is moved, is it moved in pixels or is another metric used?
So many thanks in advance!
Let's clear up some misconceptions about turtle window size:
First, the default window you get in standalone turtle is 50% of your display width and 75% of your display height. Which means that not everyone gets the same default window. Something to consider when writing turtle software for others.
You can set the window's size using the setup() method or function. You can get the current window size using the window_width() and window_height() methods or functions.
The screensize() method or function gets/sets the size of the backing store for the window. Generally, the return value is of no use to you, as the area the turtle can travel is the size of the window, so no backing store needed. It's there for folks who, for example, want a 500x500 window onto a 2000x2000 plane that the turtle can wander. Then scrollbars appear to allow you to move that peephole of a window about the larger plane.
You can modify many of turtle's default behaviors with a turtle.cfg file.
You can also find this in the turtle documentation: https://docs.python.org/3/library/turtle.html#screenspecific

Overlay all screens and draw rectangle with a mouse

I am working on tiny program to capture screen print, I want to do it in a similar fashion that Win Snipping Tool is working. First I need to overlay all screens with a 50% opacity layer and then, using the mouse, draw a rectangle and read vertices coordinates. Honestly, I have no idea how to bite this. I tried with win32api / gui and it is great to get mouse coordinates, but still was unable to draw a rectangle. My idea (one of many) is to (using PIL / ImageGrab) take shots of both displays, put an overlay and print them as a full screen on all windows, but I failed while doing this. Other idea is to take img grab and create two new windows using BeeWare / Toga (that is GUI framework I am using) in full screen, but I was unable to find any method to open window on second display. Any ideas and hints will be greatly appreciated, I am really counting on you, as I feel I reached dead end.
Well,It is very easy to use tkinter.
Ok,It is the principle when I make my screenshot application:
User presses the button to start.
Make a new window whose width and height should full cover all the screens,and hide the title bar(If it is had to achieve,maybe use width=9999 and height=9999).
Take a screenshot of all the desktop(You can use ImageGrab.grab((),all_screens=True)) to do that.
Make the screenshot showed in a Canvas(I know that toga have this widget).
Start your mouse listener thread and save the position of pressed.
When user moves his mouse,create a rectangle(toga's Canvas have a function rect()).Maybe use this rect(pressed_x,pressed_y,move_x,move_y).And delete the last rectangle(Then it will always show only one rectangle).
When user released his mouse,save the position of released.And use ImageGrab.grab((pressed_x,pressed_y,released_x,released_y),all_screens=True) to crop the selected area.
If you want to show it in application interface.toga has a widget called ImageView.You can put the image in it.

Failing to update pygame surfaces

I'm making a pygame game. I have 3 surfaces: gameDisplay (where the character and background is directly rendered to), guiSurf and invSurf
I have a clock made in core pyhon which displays the game time with the pygame font. I blit the clock to guiSurf and then in my gameloop I blit guiSurf and invSurf to gameDisplay. My problem is that the clock leaves a mark from where it was. IE when it changes from '07:00' to '07:01', the '01' is ontop of the '00' which shouldn't be there. I would post the code but theres like 400 lines. Does anyone have any idea what I may of done wrong. Link to a picture of the clock
Make sure you 'clear' the area of where the time is being printed by blitting another image over the spot where the text is. When you draw your surfaces to the screen it simply becomes one 'surface' that is always drawn until overwritten by something else. You need to clear this surface before you blit something else to it, otherwise you get the effect you see. By simple calling display_name.fill((0,0,0)) at the beginning of every game tick you will 'clear' the screen and then redraw your text onto it without the spillover effect. Of course you will have to reblit everything to the screen every tick, but this should not be a problem unless you need to blit thousands of items. If you do not wish to redraw everything, then blit a small rectangle over the text and then redraw it, and your issues should be solved.
I hope this helps you with your issue and happy coding!
Visibly, the clock is blitted twice on the guiSurf. And I guess it keeps stacking the previous image of time (7:00, then 7:01, then 7.02 and so on). You need to clear the surface holding the clock before drawing time into it : clock_surf.fill(clearcolor, clock_surf.get_rect()).

Pygame: Blitting a moving background creates too much blur

What I am trying to do is create a viewport to view a small portion of a background. (And later put sprites in).
However the problem I have noticed is there seems to be an issue of the background blurring when it starts moving. I was not sure if this is because blitting is slow or because of a problem in the code. I was looking for examples on how others blit or create scrolling backgrounds and found this article: Scrolling Games
I used their simple example and sure enough the background appears blurry as you scroll (aka blit the background with an offset). I also thought it might be the FPS dropping for whatever reason however it doesn't deviate at all. I can't recall an issue like this with other 2D games. I understand there may be some motion blur due to it constantly shifting. Just wondering if I can do anything to alleviate this. Can someone chime in on anything I may be missing? I would appreciate any feedback or help. Thank you
I couldn't know what caused the problem you faced, but I guess it is related to double buffering.
Did you use at least two surfaces?
# preparing two surfaces in __init__()
screen = pygame.display.set_mode((800,600))
background = pygame.Surface(screen.get_size())
background.fill((250, 250, 250))
# called at every step in main loop
# draw images on the background surface
background.blit(image, position)
....
# blit background to screen
screen.blit(background, (0, 0))
pygame.display.flip()
If images are drawn on the screen surface directly, flicking occurs.
By "blurry" do you mean that the background appears "doubled"? Do you get the same effect when moving a normal-sized (e.g., 64x64) sprite?
If you are seeing double, then it's probably a refresh rate problem. Turning on vsync may help.
What frame rate are you getting?
If you slow down the animation to around 10 FPS, do you have the same problem?

Categories