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.
Related
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 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.
I'm working on a game with Pygame and it was going well...until it decided to not respond. Here's my code:(sorry if the formatting doesn't work im new to stackoverflow)
#MODULES USED (use from to make calling functions easier)
from random import *
from pygame import *
import pygame
from pygame.locals import *
pygame.init()
from time import *
#INITIALISE THE PYGAME WINDOW
pygame.event.pump()
screen = display.set_mode([500, 500])
blue = [230, 242, 255]
screen.fill(blue)
pygame.display.update()
default = pygame.image.load("default.jpg")
screen.blit(default, (0,0))
pygame.display.update()
click = pygame.mouse.get_pressed()
#BASIC HEXAPAWN
#ALL POSSIBLE COMPUTER'S MOVE BOARDS AS ARRAY HERE
#TThe moves from the board images are left to right
class Board:
def __init__(self, board, moves):
self.board = board
self.moves = moves
boards1 = [pygame.image.load("a1.jpg"), pygame.image.load("a2.jpg"), pygame.image.load("a3.jpg")] #move1 boards
#irrelevant stuff removed, just initialising the other boards.
#GAME MAIN LOOP
while True:
#START GAME - 1st move
print("You play as O, computer is X")
currentboard = "O O O\n# # #\nX X X"
print(currentboard)
#PLAYER MOVE 1
screen.blit(boards1[0], (0, 250))
pygame.display.update()
if boards1[0].get_rect().collidepoint(pygame.mouse.get_pos()) and click:
screen.blit(boards1[0], (0,0))
pmove = 0
#note: I haven't added the other board options yet.
currentboard = boards1[pmove]
#[insert more unnecessary code here]
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
break
pygame.quit()
Basically whenever I run the code, the Pygame window looks alright, but when I try to click on the image it just stops responding. Also the window is always stuck on a loading cursor, idk why.
I've tried everything I could find but, nope, not working.
If anyone can help then I'd appreciate it.
Thanks :)
Eleeza
edit:i didnt know others could edit my posts too
Clearing some stuff up, when I ran my code, there were no errors, no Traceback the only problem is the unresponsive thing.
Also sorry im really bad at explaining things :/
There are a few things going on, but the main one is that you have you click variable set only once when the program starts.
Move click = pygame.mouse.get_pressed() inside of the main loop.
If you do that and it still hangs, you should show the rest of your code.
Also, the full code is not there, so I can't be 100% sure, but I don't think that break should be there.
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!
The image is a playing card. We are using pygame 4.5 community edition and pycharm 2.6.9 because 2.7 does not support pygame (this is a school). Here is the code:
import pygame
pygame.init()
picture=pygame.image.load("cards/S01.png")
pygame.display.set_mode(picture.get_size())
main_surface = pygame.display.get_surface()
main_surface.blit(picture, (0,0))
pygame.display.update()
Why does the window disappear?
Try this:
import pygame
pygame.init()
picture=pygame.image.load("cards/S01.png")
pygame.display.set_mode(picture.get_size())
main_surface = pygame.display.get_surface()
main_surface.blit(picture, (0,0))
while True:
main_surface.blit(picture, (0,0))
pygame.display.update()
pygame.display.update() updates a frame. There are multiple frames per second depending on what what you draw onto the surface.
The problem is, after you update the screen with pygame.display.update(), you do nothing, and your program simply ends. pygame.display.update() does not block.
You need what is usually called a main loop. Here's a simple example with event handling:
import pygame
pygame.init()
picture = pygame.image.load("cards/S01.png")
# display.set_mode already returns the screen surface
screen = pygame.display.set_mode(picture.get_size())
# a simple flag to show if the application is running
# there are other ways to do this, of course
running = True
while running:
# it's important to get all events from the
# event queue; otherwise it may get stuck
for e in pygame.event.get():
# if there's a QUIT event (someone wants to close the window)
# then set the running flag to False so the while loop ends
if e.type == pygame.QUIT:
running = False
# draw stuff
screen.blit(picture, (0,0))
pygame.display.update()
This way, your application does not, only when someone closes the window.