Pygame script freezing on windows (Program not responding) - python

I am new to stackoverflow, I decided to join because I sometimes have problems with programming. This one is really annoying, I cant figure out why it doesn't work. Any help would be appreciated!
I get the windows "Program not responding" error message
A simple display:
import pygame
pygame.init()
BLUE = pygame.Color(0, 0, 255)
size = [1280, 720]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Hangman")
done = False
clock = pygame.time.Clock()
while not done:
# Leaves the fps at 30
clock.tick(30)
screen.fill(BLUE)
pygame.display.flip()
The expected result is a blue screen, instead I get a blue screen that crashes

In your game loop handling events will prevent freezing.
import pygame
pygame.init()
BLUE = pygame.Color(0, 0, 255)
size = [1280, 720]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Hangman")
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
else:
print event
# Leaves the fps at 30
clock.tick(30)
screen.fill(BLUE)
pygame.display.flip()

Related

I'm having trouble with pygame

I was trying to make the display a diferent colour in pygame but the screen just stays black
this is the code i had for it:
import pygame
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("space invaders")
icon = pygame.image.load('ufo(1).png')
pygame.display.set_icon(icon)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 255))
pygame.display.update()
I was following a tutorial on youtube but when i tried to make any colour it just stayed black.
I am following the same tutorial and you have to make sure that your screen.fill and display.udpate command are in your while loop at the right position under the for event like this:
import pygame
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("space invaders")
icon = pygame.image.load('ufo(1).png')
pygame.display.set_icon(icon)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 255))
pygame.display.update()
Output:
The output should give you a blue background like above.
Make sure the screen.fill((0, 0, 255)) and the pygame.display.update() are in the game loop (the while running loop).
import pygame
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 600))
icon = pygame.image.load('ufo(1).png')
pygame.display.set_icon(icon)
pygame.display.set_caption("space invaders")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 255))
pygame.display.update()
Also, if no action or animation is being display that would require the computer to do anything other than just loop through like normal, you can use the pygame.event.wait() method.
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 600))
icon = pygame.image.load('ufo(1).png')
pygame.display.set_icon(icon)
pygame.display.set_caption("space invaders")
running = True
while running:
event = pygame.event.wait()
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 255))
pygame.display.update()

Pygame window will not open

I'm completely new to Python and I just started learning Pygame. I'm trying to work with drawing shapes right now, but I can't get the window to open. Idk what the problem is, here's my code:
`#import libraries
import pygame
import math
#init pygame
pygame.init()
#clock
clock = pygame.time.Clock()
#window screen
screen_height_x = 500
screen_height_y = 400
screen = pygame.display.set_mode((screen_height_x, screen_height_y))
#RGBA colors
Black = (0, 0, 0)
White = (255, 255, 255)
Blue = (0, 0, 255)
Red = (255, 0, 0)
Green = (0, 255, 0)
#program
running = True
while running:
for event in pygame.event.get():
running = False
screen.fill(Black)
pygame.display.update()
clock.tick(30)
#Quit
pygame.quit()
quit()`
You're immediately quitting the main loop and you also aren't drawing anything. Try this instead:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(Black)
pygame.display.update()
clock.tick(30)
#Quit
pygame.quit()
quit()
Disclaimer: I cannot try this out at the moment but it should get you started in the right direction.
Is this a class part of the projet or the main class ? If it is the main class, you it should contain a main function.
If this is the main function, you should declare it by adding def main(): before the pygame.init()
And add the module to exectute this as the main script under Quit
if __name__ == "__main__":
main()
Sorry, im french my english is not the best aha !

Pygame: Display not updating until after delay

I have a bug in the program I am writing where I first call:
pygame.display.update()
Then I call:
pygame.time.wait(5000)
I want the program to update the display and then wait for a set period of time before continuing. However for some reason the display only updates after the waiting time, not before.
I have attached some example code to demonstrate what is happening:
import pygame
pygame.init()
white = (255,255,255)
black = (0,0,0)
green = (0,255,0)
screenSize = screenWidth, screenHeight = 200, 200
screen = pygame.display.set_mode(screenSize)
screen.fill(white)
pygame.draw.rect(screen, black,((50,50),(50,50)))
pygame.display.update()
pygame.time.wait(5000)
pygame.quit()
raise SystemExit
This should create a white window with black box, then wait 5 seconds and then quit.
However what it actually does is make a window, wait 5 seconds, then for a split second the box will appear, then it immediately quits.
Does anyone know how to fix this problem?
Your window's content will only be drawn if your window manager tells your window to actually paint something. So if your code works depends on your window manager.
What you should do to make it work is to let pygame process all events, and you do it usually by calling pygame.event.get().
Also, you should avoid calling blocking functions like pygame.time.wait(), since while they block, you can't handle events or let pygame process system events. That means pygame won't repaint the window, and you can't close the window.
Consider changing your code to this:
import pygame
pygame.init()
white = (255,255,255)
black = (0,0,0)
green = (0,255,0)
screenSize = screenWidth, screenHeight = 200, 200
screen = pygame.display.set_mode(screenSize)
screen.fill(white)
pygame.draw.rect(screen, black,((50,50),(50,50)))
run = True
clock = pygame.time.Clock()
dt = 0
while run:
dt += clock.tick()
for e in pygame.event.get():
if e.type == pygame.QUIT:
run = False
if dt >= 5000:
run = False
pygame.display.update()

Python 3.4 Pygame My sprite does not appear

I have written simple code to get a green block which is my sprite to scroll across the screen. When the game starts the sprite is meant to appear in the centre of the screen, however when I run my code the screen is just black and the green block does not appear unless I click on the x cross on the window to exit the screen, then it appears for a second when the window is closing. Any ideas how I can resolve this.
import pygame, random
WIDTH = 800 #Size of window
HEIGHT = 600 #size of window
FPS = 30
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
class Player(pygame.sprite.Sprite):
#sprite for the player
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 50))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH/2, HEIGHT/2)
def update(self):
self.rect.x += 5
#initialize pygame and create window
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
#Game loop
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
#check for closing window
if event.type == pygame.QUIT:
running = False
#update
all_sprites.update()
#Render/Draw
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()
All your code to updat the sprites, fill the screens and draw the sprites is outside your main loop (while running)
You must have in mind that identation Python's syntax: your commands are just outside your mainloop.
Moreover, I'd strongly advise to put that mainloop inside a proper function, instead of just leaving it on the module root.
...
#Game loop
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
#check for closing window
if event.type == pygame.QUIT:
running = False
#update
all_sprites.update()
#Render/Draw
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()

When i make a pygame game and hover over it, the cursor turns into the loading thing

When i make a pygame game and hover over it, the cursor turns into the loading thing, that means I can't resize the window, or if i were to make buttons i wouldn't be able to use them... Help? Here's my code.
import pygame
import random
import time
import os
from pygame.locals import *
red = (255,0,0)
white = (255,255,255)
black = (0,0,0)
green = (0,255,0)
blue = (0,0,255)
pygame.init()
background_colour = (white)
(width, height) = (800, 600)
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
screen.fill(background_colour)
while True:
pygame.draw.circle(screen,
(random.randint(0,255),
random.randint(0,255),random.randint(0,255)),
(random.randint(0,800),random.randint(0,600)), 20)
pygame.display.flip()
So yeah, it always happens with pygame.
Add this in top of your while.
This for runs throught events and if
event.type == pygame.QUIT
This means that you presed cross for closing the window and window will be closed. You can catch all pygame events like this.
while True: #your wile
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#your code

Categories

Resources