How do I stop the pygame window freezing up? [duplicate] - python

This question already has answers here:
Pygame unresponsive display
(1 answer)
Pygame window not responding after a few seconds
(3 answers)
Closed 2 years ago.
When I run the code a blue rect is displayed in its proper location however the entire window freezes and eventually crashes. How can I fix this?
import pygame
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")
run = True
while run:
pygame.time.delay(100)
filled_rect = pygame.Rect(100, 100, 25, 25)
pygame.draw.rect(win, (0,0,255), filled_rect)
pygame.display.update()

You have to add an event loop. Handle the events, by either pygame.event.pump() or pygame.event.get(). Thus the IO and internal events are handled and the window keeps responding. e.g.:
import pygame
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")
run = True
while run:
pygame.time.delay(100)
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# clear the disaply
win.fill(0)
# draw the scene
filled_rect = pygame.Rect(100, 100, 25, 25)
pygame.draw.rect(win, (0,0,255), filled_rect)
# update the dispaly
pygame.display.update()
pygame.quit()

Related

cannot blit anything to screen. why not? [duplicate]

This question already has answers here:
Python Pygame Newbie Code Showing Blank Screen
(4 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 10 months ago.
so im trying to blit tonk but its not working. can you help? all it shows is black
import pygame,pynput
pygame.init()
width = 700
height = 700
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption('generic shooter pew pew')
Tonk = pygame.image.load("TONK.png")
screen.blit(Tonk,(0,0))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
exit()
clock = pygame.time.Clock()
clock.tick(60)
You need to blit in the while loop and add a display update too :
import pygame,pynput
pygame.init()
width = 700
height = 700
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption('generic shooter pew pew')
Tonk = pygame.image.load("TONK.png")
clock = pygame.time.Clock()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
exit()
screen.blit(Tonk,(0,0))
pygame.display.flip()
clock.tick(60)

Why is my Pygame script not correctly filling the screen? [duplicate]

This question already has answers here:
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 1 year ago.
My code is below, when it is run the Pygame screen stays black and is not filled with the correct color at all, (87, 160, 211)
import pygame
pygame.init()
window_size = (800,600)
screen = pygame.display.set_mode((800,600))
car_image = pygame.image.load('car.png')
def display_car(x,y):
screen.blit(car_image, (x,y))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((87, 160, 211))
display_car(window_size[0]/2, window_size[1]/2)
You must update the display by calling either pygame.display.update() or pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((87, 160, 211))
display_car(window_size[0]/2, window_size[1]/2)
pygame.display.flip()

Rectangle not being drawn [duplicate]

This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Closed 1 year ago.
When running this code:
import pygame,time
GREEN = (30, 156, 38)
WHITE = (255,255,255)
pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (0,0,100,100))
time.sleep(3)
Pygame shows a black screen for 3 seconds, but doesn't draw a rectangle.
I am using Atom using script package to run code
You have to implement an application loop. The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
limit frames per second to limit CPU usage
import pygame
GREEN = (30, 156, 38)
WHITE = (255,255,255)
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
# applicaition loop
run = True
while run:
# limit frames per second
clock.tick(60)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# clear display
screen.fill(WHITE)
# draw objects
pygame.draw.rect(screen, GREEN, (0, 0, 100, 100))
# update display
pygame.display.flip()
pygame.quit()
exit()
Note, you must do the event handling. 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.
Update the screen with:
pygame.display.update()
at the end of your code you have posted.
You have to update the screen like that:
pygame.display.flip()
to render what you just drew.
Your code should look like this:
import pygame
import time
pygame.init()
GREEN = (30, 156, 38)
WHITE = (255,255,255)
screen = pygame.display.set_mode((640, 480))
# draw on screen
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (0,0,100,100))
# show that to the user
pygame.display.flip()
time.sleep(3)
Off-topic: You should also get the events to allow the user to close the window:
import pygame
from pygame.locals import QUIT
import time
pygame.init()
GREEN = (30, 156, 38)
WHITE = (255, 255, 255)
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock() # to slow down the code to a given FPS
# draw on screen
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (0, 0, 100, 100))
# show that to the user
pygame.display.flip()
start_counter = time.time()
while time.time() - start_counter < 3: # wait for 3 seconds to elapse
for event in pygame.event.get(): # get the events
if event.type == QUIT: # if the user clicks "X"
exit() # quit pygame and exit the program
clock.tick(10) # limit to 10 FPS
# (no animation, you don't have to have a great framerate)
Note that you must put all of this into a game loop if you want to repeat it like a classic game.

How to fix flickering of objects and figures in pygame? [duplicate]

This question already has an answer here:
Why is the PyGame animation is flickering
(1 answer)
Closed 1 year ago.
Here I, have am tying to simply make 2 rectangles using pygame module. I have made them using 2 methods but they don't seem to work. Please suggest some that could work.
(I am using Python 3.8.3)
Here is the code:
import pygame
pygame.init()
screen = pygame.display.set_mode((100, 100))
box_color = (255, 0, 0)
color = (0, 150, 100)
ybox = 20
xbox = 20
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
ybox += 5
if (ybox >= 45):
ybox = 45
pygame.draw.rect(screen, box_color, pygame.Rect(xbox, ybox, 50, 50), 2)
pygame.display.flip()
pygame.draw.rect(screen, box_color, pygame.Rect(20, 95, 50, 50), 2)
pygame.display.flip()
screen.fill(color)
pygame.display.update()
Your game is flickering because of the multiple calls of pygame.display.flip() and pygame.display.update(). One update of the display at the end of the application loop is sufficient. Multiple calls to pygame.display.update() or pygame.display.flip() cause flickering.
Remove all calls of pygame.display.update() and pygame.display.flip() from your code, but call it once at the end of the application loop. However, clear the display before drawing the objects:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
ybox += 5
if (ybox >= 45):
ybox = 45
# clear disaply
screen.fill(color)
# draw objects
pygame.draw.rect(screen, box_color, pygame.Rect(xbox, ybox, 50, 50), 2)
pygame.draw.rect(screen, box_color, pygame.Rect(20, 95, 50, 50), 2)
# update dispaly
pygame.display.update()
The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()

Python + Pygame; screen.fill() crashing window

Whenever I try to include "screen.fill(insert value here)" to my pygame code, the window instantly crashes. I can comment it out and my code works just fine. I'm really not sure why.
I've tried moving the line around, using other people's code in part - no luck.
import pygame
pygame.init()
win = pygame.display.set_mode((1000, 800))
pygame.display.set_caption("Tetris")
img=pygame.image.load("C:\\Users\\Charlotte\\Desktop\\tetris_grid.png")
win.blit(img,(450, 150))
running = True
while running:
screen.fill(0, 0, 0)
pygame.display.update()
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
When I run the code with that line in it, the window opens and then it instantly closes.
If you are trying to fill the screen with black, the color must be passed as a tuple:
win.fill((0, 0, 0))
Also, you never assign screen, maybe you intended to use win?
The doc for Surface.fill.
The color parameter to fill() has to be either a single grayscale value or a tuple of 3 RGB or 4 RGBA components. So it has to be either:
win.fill(0)
or
win.fill((0, 0, 0))
Further you've to blit() the image in the main loop. To continuously draw the scene in the main application loop, you've to:
handle the events
clear the window
draw the scene (blit the image)
update the display
Furthermore I recommend to use tick() of pygame.time.Clock rather than pygame.time.delay() tpo control the frames per second.
import pygame
pygame.init()
win = pygame.display.set_mode((1000, 800))
pygame.display.set_caption("Tetris")
clock = pygame.time.Clock()
img=pygame.image.load("C:\\Users\\Charlotte\\Desktop\\tetris_grid.png")
running = True
while running:
clock.tick(60)
# handle the events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# clear the display
win.fill(0)
# draw the scene
win.blit(img,(450, 150))
# update the display
pygame.display.update()
pygame.quit()

Categories