I wanted to make a game using pygame and I started my code making the pygame window
import pygame
pygame.init()
pygame.display.set_mode((1500, 1000))
But every time I run the program, my pygame window does not respond.
Does anyone know what is happening, this did happen to me on previous codes too but it wasn't all the time like it is now
you need a game loop to start the game and you need events to work on.
for python code i suggest:
import pygame
pygame.init()
pygame.display.set_mode((1500, 1000))
start_game = True
while start_game:
print("Game Started!")
and for the events you can use this code in the while loop:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
print("mouse button up pressed")
the source of this code is : RipTutorial
I assume that by 'my pygame window does not respond' you mean that you believe the window is not displaying. I think that you are just missing it because it is closing too quickly.
The window does display, but because your program ends right after you create the window, the window is immediately closed again. This is likely just happening so quickly that you are not noticing it. If you want to see this, then just put a call to sleep after opening the window and you will see the window sit there until the sleep expires and then it will close.
If you do not mean that and you are seeing the window but expecting it to respond, then I do not understand what you expect it to respond to. You do not have any code that tries to make it respond to anything.
EDIT:
I just noticed that #AnassABEA suggests that in a comment under his answer, though not in his answer. #AnassABEA you should update your answer to include this since I believe that is the actual answer to his question, not the missing event loop.
Add this below to your code:
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
Lines 4 to 5 of the code are for closing pygame when the window is closed.
Related
I've tried a few examples of pygame being used with controllers and they all seem to encounter the same issue, I can't figure out if I haven't installed pygame correctly or if im doing something else wrong.
import pygame
pygame.joystick.init()
for index in range(pygame.joystick.get_count()):
joystick = pygame.joystick.Joystick(index)
print(joystick) # <- does actually return a controller
while True:
pass # <- runs a few times and then stops
See PyGame window not responding after a few seconds. You have to handle the events in the application loop. See pygame.event.get() respectively pygame.event.pump():
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
e.g.:
while True:
pygame.event.pump()
or
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
I want to completely prevent the user from closing the Pygame window except for the key x. Currently, I'm able to prevent the user from closing it, but I am unable to prevent the user from opening another window that overlaps it (press windows key -> open chrome, which overlaps the Pygame window).
import sys
import pygame
from pygame.locals import *
pygame.init()
infoObject = pygame.display.Info()
screen = pygame.display.set_mode((infoObject.current_w, infoObject.current_h), flags=pygame.NOFRAME)
while True:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
pygame.quit()
sys.exit()
if event.type == QUIT:
pass
pygame.display.flip()
We don't have complete solutions for this task.
You can make the title var vanish away by setting display mode to NOFRAME (You have already used this): pygame.display.set_mode(flags = pygame.NOFRAME), but this is overkill for just stopping close buttons
Although any user with minimum Computer Knowledge will fire up a Task manager and kill python.exe :-(
There are ways of disabling task manager through Registry in Windows, Image File Execution Options blah blah, but your game script will barely run with Administrator priviliges (With nagging of UAC)
In order to do this, you can add this piece of code to your pygame.display.set_mode():
pygame.display.set_mode(..., flags=pygame.NOFRAME). Now, the bar with the "x" button will disapear.
Notice that if you decide to make this change, you won't be able to move your game window anymore. (Unless you change it back).
I'm a new in PyGame and I was making a game. Until I discovered that when you were moving the window to another place of your desktop for example, the game temporarily stops. But ticks of the game still running when I used pygame.time.get_ticks().
So I made a completely new program with only the necessary code and it does the same thing.
Can someone explain me why it does it and if we can resolve this problem?
import pygame
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('The Test Program')
running = True
update_counter = 1
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
print(str(update_counter) + " updates")
update_counter += 1
pygame.quit()
quit()
# So try to move the window across your screen and you will see that, prints will stop and they will resume when you will release the click
So someone tell me that was normal, so I think it's only on Windows but there are no solutions. I put a script that show a pause symbol on screen when the cursor leave the window to make it normal.
After a long time trying to install pygame for 2.7 it finally installs and I now have it downloaded, but there is now the problem that it keeps not responding after a couple seconds of being open. Any answer will be appreciated, the code that I have so far is just.
import pygame
pygame.init()
pygame.display.set_mode((640,480))
so I need some help please.
So what you want to do, like what skrx said, is a while loop to continuously keep the code inside the while loop and the pygame window running, and as well as a for event loop, in order to be able to shut down the window. Here's how you can do it:
import pygame
pygame.init()
pygame.display.set_mode((640, 480)) # opens the display
while True: # the while loop that will keep your display up and running!
for event in pygame.event.get(): # the for event loop, keeping track of events,
if event.type == pygame.QUIT: # and in this case, it will be keeping track of pygame.QUIT, which is the X or the top right
pygame.quit() # stops pygame
There are other ways of stopping the while loop, and you could do this:
running = True
while running: # the while loop that will keep your display up and running!
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
Hope this helps!
When I go to close the program window, the program freezes, then I am forced to force quit the program. Why doesn't the program close when the X / Close button is clicked on.
I am also using python 2.7 if that matters.
import pygame
import os, sys
from itertools import *
from oryxsprites import *
from oryxbackground import *
running = True
while running:
backgroundmain()
pygame.display.set_caption('OryxGame')
#pygame.display.set_icon(biggrasstile)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
A quote from an article that sports code very similar to yours:
The window now persists whilst 'running' is equal to True, which it
will be until you close the window (by clicking the X). Note that if
you use an IDE for Python programming, then it may interfere with
Pygame. This isn’t normally a major problem but it can stop the Pygame
window from closing properly. If so, adding pygame.quit() should solve
the problem.
With Python 3.2, pygame 1.9 win32, sys.exit() is useless (seen on an online tuto).
pygame.quit() works perfectly