This question already has answers here:
Making the background move sideways in pygame
(2 answers)
How to scroll the background surface in PyGame?
(1 answer)
How to move the background image with keys in pygame?
(1 answer)
Closed 2 years ago.
I'm learning Python and trying to improve my skills by creating a little game using Pygame while learning through videos.
Therefore I would like to implements the basics menus, which include a pause menu and a couple more fonctions.
My goal is to have something like this:
Main Menu and level selection:
Game itself
Pause menu with restart/resume/back to main menu.
The problem is that, the more I use the pause menus and the more time passes, the more the main menu become visually glitched.
The visual glitch also don't seems to have a regular patter, sometimes it's the ship that stick, sometime the enemies or the lasers, or all of them at once.
The main title become also less and less clear with time.
I took some screenshots that you can find here.
I posted most of the useful code here so that you can try it.
The assets that I used can be download with this link if needed:https://techwithtim.net/wp-content/uploads/2020/04/assets.zip
EDIT:
It seems like the problem comes from this part of the code that manage the infinite auto-scrolling of the background image of the in-game loop.
global y
rel_y = y % BACKGROUND.get_rect().height
screen.blit(BACKGROUND, (0,rel_y - BACKGROUND.get_rect().height))
if rel_y < HEIGHT:
screen.blit(BACKGROUND, (0, rel_y))
y += 1
However, if the background is scrolling from the start in the main menu, it doesn't seems like there is any glitch anymore.
I don't really see why it glitched in the first place and I don't really know why making it loop from the start fixed the problem.
What could have made it happend?
(I edited the pastebin with the minimum needed for the code to work, it just need a background image that I can't add, but is on the asset download link if needed)
Related
This question already has answers here:
Can I detect if a window is partly hidden?
(2 answers)
How to determine if any part of the window is visible to Screen?
(1 answer)
How to get only the visible part of a window (Windows, gdi32, user32, etc)
(2 answers)
Closed 8 months ago.
I want to get a window's clip box so I can detect if the whole window is displayed.
package: win32gui
I know that IsWindowVisible() from the Windows API will return true if a window is NOT minimized, but it won't detect if the window is hidden behind other windows.
I also know that in C++, I could use GetWindowDC(); GetClipBox(); ReleaseDC(); to get a RECT struct with the coordinates of the smallest bounding rectangle of the window (I got this from another post on StackOverflow, but I haven't tested it).
In Python, I can use:
# That just returns a window by the title
handle = getWindowByTitle("Skype") # "Skype" is just an example
winDC = win32gui.GetWindowDC(handle)
# this function doesn't seem to exist/be implemented in win32gui
win32gui.GetClipBox(winDC, '''This also needs a RECT struct''')
win32gui.ReleaseDC(winDC)
How can I use GetClipBox() in Python? Or, is there any other way to check it?
I would like to not use any other package, because I want my app to have as few dependencies as possible.
I have also heard of ctypes, but I have never used them. I am not sure how it works, and/or if it can help here.
Firstly, there are a couple of similar questions posted on this. But they are not complete questions, and no one has really answered them all. I am using Python to send input to a game active in widow modes using "ctypes" package. The original idea is mentioned here
In particular, my codes are:
x=500
y=400
for i in range(10):
time.sleep(4)
if i > 0:
x=x+17
pos=(x,y)
#ctypes.windll.user32.SetCursorPos(x,y) # this does not work in game but
#outside the game window
ctypes.windll.user32.mouse_event(0,x,y,0,0)
print i
In order for the "mouse_event" to work, I tried inputting hexadecimal values but can't get it to move consistently. I tried setting up separate class & functions to import, then calling the function. The function works outside the game window but not inside the game window.
The separate class & functions are based on this question
Thank you for your help in advance.
I am using python to move the cursor along multiple screens (using extended display).
The method pyautogui.moveTo(_row_, _col_) moves the cursor to the correct position on the main screen, but does not move it outside of it. In windows "Personalize" I set the second screen to the right of the first one, but when I call pyautogui.moveTo(2000,400) the mouse simply moves to location (1366, 400) [the edge of the main screen].
So, how can I use python to move the cursor from one screen to another? I'll be using 4 different monitors in my project
Sorry that this is late, but since it comes up in the top Google results, I figured I'd answer for others who are ending up here.
There is currently no solution to this problem (As of 8/13/2018). It is being looked into, but not very hard.
In the meantime, I've made things work on my Windows PC with the GhostMouse freeware, which has no problem with multiple monitors:
This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 2 years ago.
Looking at my code there are no errors. I have tried all sorts to try and get my panes to display but they still won't appear. I have retraced all my steps and tried to look for where I may have gone wrong, but to me everything seems fines and should work yet it doesn't.
I currently have 3 different states. State 1 being the main screen which is "10 main boxes" on screen. State 2 being a "virtual keyboard" and state 3 being "set phrases". State 3 is the one that doesn't display the pane/s and I can't understand why.
Here's the main screen:
(The keyboard also works)
Here is what happens when you click set phrases:
A pane/rectangle is meant to appear on the set phrases screen in these co-ordinates sPhraseLoc = [(400, 100, 150, 50)]. It is also meant to have "Hi my name is Dave." written in it. (Look at code below for reference)
Can someone please explain to me why "Hi my name is Dave." isn't appearing on the screen in a pane/rectangle? All help is much appreciated.
Here is the code (It is rather long so I thought that I'd just link it)
I am also suspicious of a part of the code and think the problem lies their but I am not too sure. (Here is the link)
Like I said earlier any help is much appreciated.
Is there any way way to create multiple screens in Pygame without redrawing to the screen every time. For example, if we want to create a splash screen, then a main menu with buttons. On clicking the 'Start Game' button, it would go to a new screen which is the actual game. What I mean is anything similar to the 'Form' on Visual Studio or the Activites on Android.
Depends on what you mean.
If you mean making multiple windows at the same time, no, you can't do that--it's a limitation of SDL (although you CAN fake it by using multiprocessing (not multithreading)).
If you mean changing one screen around, yes, you can do that, with multiple calls to pygame.display.set_mode(...). You can change the resolution, arguments, etc. If you're doing OpenGL stuff, that will remake the context too.
If you just mean drawing different things to the same window, of course! That's sorta the point of PyGame.
Other than that, you'll have to clarify.
Just fill the screen with white and then draw the second screen onto the main screen. Then when you need the other screen, just refill the screen with black and then continue. It would help if both of your screens a function and you used a key like tab to "switch" between screens.