Pygame window not opening until all processes are complete macOS 12.2 - python

macOS Monterey 12.2 M1 Pro
Version: pygame 2.1.2 (SDL 2.0.18, Python 3.10.7)
Pygame will not display a window until all my code has run and the program is waiting for input to exit. Once program is complete, Pygame window shows. If I didn't use the following code:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
then the window wouldn't even display at all.
Tried uninstalling and reinstalling pygame. Tried installing older version of pygame (which doesn't work, keep getting errors). Tried installing most recent development version. (2.1.3.dev8)
Having to transfer files to old windows laptop to check if program even works properly. Any ideas why this is happening? Is there an older dev version of pygame that will work instead?
example:
import pygame
w_in_squares = 8
h_in_squares = 8
square_dim = 50
drawing_window = pygame.display.set_mode((w_in_squares*square_dim, h_in_squares*square_dim))
drawing_window.fill((127, 127, 127))
the_colour_white = (255, 255, 255)
the_colour_black = ( 0, 0, 0)
current_colour = the_colour_white
for i in range(0, h_in_squares):
for j in range(0, w_in_squares):
pygame.draw.rect(drawing_window, current_colour, (i * square_dim, j * square_dim, square_dim, square_dim))
pygame.display.update()
pygame.time.delay(100)
if current_colour == the_colour_white:
current_colour = the_colour_black
else:
current_colour = the_colour_white
if current_colour == the_colour_white:
current_colour = the_colour_black
else:
current_colour = the_colour_white
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
The point of this program is that you are supposed to be able to watch each square of a checkerboard being drawn one by one. Though, only after every square is drawn does the window display.

I am not sure on your entire code, but try adding a pygame.display.update() in your while loop, this should make pygame keep the screen refreshed.
Again this could not solve your solution because you do not show your full program.

Related

Import "pygame" could not be resolved

I installed pygame using the pip in command prompt but it still shows an error. I have already attempted changing the path so pip could properly be accessed and using the line "pygame.init()" but the error still appears.
Here's the code:
import pygame, sys
pygame.init()
w, h = 600, 600
r = (255, 0, 0)
s = pygame.display.set_mode( (w, h) )
s.fill(r)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.update()
by the way if it still isn't working you could try using Pycharm. If you can't do that you could try using replit and downloading the pygame package, then exporting it with Github.

Pygame Window refuses to start after importing * from pygame.local

So I started programming a game for a school project and currently face a problem with event.type KEYDOWN.
This is my Code. When running it a window opens but it stays completely black without showing either the background color or the sprite. I tried pinpointing the issue with the print() commands to see in what line exactly the code stops working. Every, except the last print command is executed, which leaves me to believe the While Loop is the cause of my problem.
import pygame
from pygame.locals import *
print("Import complete")
WIDTH = 640
HEIGHT = 480
TITLE = "Tales of Tesbold"
print("Window complete")
Tesbold = Actor("tesbold.png") #Sprites vordefinition
Tesbold.x = 200
Tesbold.y = 100
print("Tesbold complete")
wechsel = True
def draw(): #Hintergrund und alle Sprites
screen.clear()
screen.fill((200,200,200))
Tesbold.draw()
print("Draw Screen complete")
while True:
for event in pygame.event.get():
if event.type == quit:
sys.exit()
if event.type == KEYDOWN:
if(event.key == K_RIGHT):
Tesbold.x += 10
print("While Loop complete")
This is the console output:
pygame 2.0.1 (SDL 2.0.14, Python 3.8.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
Import complete
Window complete
Tesbold complete
Draw Screen complete
---------- FINISHED ----------
exit code: 2 status: 0
Can someone tell me what I did wrong for it to not function anymore?
Python is case sensitive. The event type enumerators are all written in capital letters. See pygame.event module:
if event.type == quit:
if event.type == QUIT:
pygame.quit() is a function and uninitialize all pygame modules.

Window freeze after pygame program runs

I am learning pygame and have been working on examples given in 'Python Crash Course' book by Eric Matthews (https://g.co/kgs/WP5fay). I have installed pygame version 1.9.3 on macOS High Sierra.
When I run the following program, a window opens and when I click 'X' to close it, the window freezes and the curses keeps circling and I get a python not responding error in activity monitor. I have tried a number of option to fix the problem but it is not going away.
import pygame
import sys
def run_game():
pygame.init()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Alien Invasion')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
run_game() #when I run this function, a window opens and freezes a couple of seconds after
You have to terminate the application loop and uninitialize all pygame modules with pygame.quit():
def run_game():
pygame.init()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Alien Invasion')
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.flip()
pygame.quit()
sys.exit()

pygame.error: file not a windows.bmp file (have looked at other similar questions but unsuccessful so far)

I'm very new to pygame, and am using the book "Beginning Game developement with Python and Pygame". I have pretty much typed the example code and keep getting the same
"pygame error: file not a windows.bmp file"
and would like to be able to load jpg/png as in the example in the book. I'm pretty sure I'm in the right directory for mine to work and the images I wanted to use are the same format as in the example. I have also searched for solutions but none of the answers seemed to work for me.
The code from the book is as follows (I have python 2.7.4, Ubuntu 13.04 and (I think) pygame 1.2.15):
background_image_filename = 'sushiplate.jpg'
mouse_image_filename = 'fugu.png'
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("Hello, World!")
background = pygame.image.load(background_image_filename).convert()
mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
screen.blit(background, (0,0))
x, y = pygame.mouse.get_pos()
x-= mouse_cursor.get_width() / 2
y-= mouse_cursor.get_height() / 2
screen.blit(mouse_cursor, (x, y))
pygame.display.update()
my version of the code so far:
import os.path
background = os.path.join('Documents/Python/Pygame','Background.jpg')
cursor_image = os.path.join('Documents/Python/Pygame','mouse.png')
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("Hello, World!")
background = pygame.image.load(background).convert()
mouse_cursor = pygame.image.load(cursor_image).convert_alpha()
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
screen.blit(background, (0,0))
x, y = pygame.mouse.get_pos()
x-= mouse_cursor.get_width() / 2
y-= mouse_cursor.get_height() / 2
screen.blit(mouse_cursor, (x, y))
pygame.display.update()
Thanks for your help :)
I can almost guarantee that you're getting your paths wrong. In your code, you've put in relative paths, meaning that pygame is looking for your assets in subfolders of the working directory (the directory where you execute your code).
A demo of how I think you would have to have things laid out and where your code is looking is below - in this example you would have a command prompt open in /home/your_username/Documents/my_games (or ~/Documents/my_games) and you'd be running python your_game_script.py.
|---home
|---your_username
|---Documents
|---some_subfolder
|---my_games
|---your_game_script.py
|---Documents
|---Python
|---Pygame
|---Background.jpg
|---mouse.png
This would work, but I suspect you don't have your folders set up this way, and that's the reason it's not working. If you run an interactive python prompt in the same folder as your game script, try the following:
import os
os.path.isfile('Documents/Python/Pygame/Background.jpg')
os.path.isfile('Documents/Python/Pygame/mouse.png')
I suspect the result will be false for both - meaning the files couldn't be found at that subfolder location. I would recommend that you have the following structure for your game files:
|---my_game
|---your_game_script.py
|---images
|---Background.jpg
|---mouse.png
Then in your_game_script.py you can load the files in the following way:
background = 'images/Background.jpg' #relative path from current working dir
cursor_image = 'images/mouse.png' #relative path from current working dir
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
pygame.display.set_caption("Hello, World!")
background = pygame.image.load(background).convert()
mouse_cursor = pygame.image.load(cursor_image).convert_alpha()

Pygame.error: file is not a WINDOWS BMP file (mac osx)

Hi after much research I cant find the answer.
running mac osx 10.8.4 python 2.7.5 and pygame 1.9.2.
all modules were found in the build of pygame and reinstalling doesnt fix the issue
while running:
import pygame
import math
import random
black = (0,0,0)
red = (255,0,0)
white = (255,255,255)
blue = (0,0,255)
green = (0,255,0)
pygame.init()
print pygame.image.get_extended()
size = (1000,700)
screen = pygame.display .set_mode(size)
pygame.display.set_caption("My game")
done = False
clock = pygame.time.Clock()
background_image = pygame.image.load("red_x.png").convert()
while done == False:
# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# ALL EVENT PROCESSING SHOULD GO ABOVE THIS COMMENT
# ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT
# ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
screen.fill(black)
screen.blit(background_image,[0,0])
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
pygame.display.flip()
clock.tick(20)
pygame.quit()
I get a file is not a valid windows BMP error.
pygame.images.get_extended() returns 0
and
try:
import SDL_image
print "Loaded SDL_image"
except:
print "Failed to import SDL_image"
try:
import libpng
print "Loaded libpng"
except:
print "Failed to import libpng"
returns both failed import messages. I think thats all the tests i saw while searching for this and all of their solutions didnt work.
I had the same problem, but I managed to solve it installing the following version of Pygame: http://pygame.org/ftp/pygame-1.9.1release-python.org-32bit-py2.7-macosx10.3.dmg

Categories