pygame.display.update() doesn't work well [duplicate] - python

This question already has an answer here:
Pygame: Display not updating until after delay
(1 answer)
Closed 2 years ago.
I was programming pygame in Mac. Then, there was a problem.
I wrote the code like below, but pygame.display.update() doesn't work.
It should update and wait 3 seconds, but it wait 3 seconds first, and it updates after pygame.quit().
Can anyone tell me how to solve this problem?
this doesn't work:
import pygame
import sys
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("hello")
font = pygame.font.SysFont("comicsans", 100)
text = font.render("hello", 1, (255, 255, 255))
win.blit(text, (200, 200))
pygame.display.update()
pygame.time.delay(3000)
pygame.quit()
sys.exit(0)
this works normaly:
import pygame
import sys
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("hello")
font = pygame.font.SysFont("comicsans", 100)
text = font.render("hello", 1, (255, 255, 255))
win.blit(text, (200, 200))
pygame.display.update()
pygame.quit()
pygame.time.delay(3000)
sys.exit(0)
OS: Mac
Python Version 3.8.3
Pygame Version 1.9.6
Editor: Jupyter Notebook

The issue is on MacOS (and potentially other OS's), the screen only updates after pygame events are checked.
Normally, this is done by calling pygame.event.get() or pygame.event.poll().
However, if your program doesn't care about checking events, then a call to pygame.event.pump() will work as well.
This issue is briefly mentioned in the documentation for pygame.event.pump(), link here.
There are important things that must be dealt with internally in the
event queue. The main window may need to be repainted or respond to
the system. If you fail to make a call to the event queue for too
long, the system may decide your program has locked up.
So the solution is to call pygame.event.pump() (or another event function) after pygame.display.update().
That the corrected code would then be:
import pygame
import sys
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("hello")
font = pygame.font.SysFont("comicsans", 100)
text = font.render("hello", 1, (255, 255, 255))
win.blit(text, (200, 200))
pygame.display.update()
pygame.event.pump() # Make sure events are handled
pygame.time.delay(3000)
pygame.quit()
sys.exit(0)

Related

When I run my code I get a green screen with multiple small balls that says Fps counter. My picture's don't show up [duplicate]

I have a simple Pygame program:
#!/usr/bin/env python
import pygame
from pygame.locals import *
pygame.init()
win = pygame.display.set_mode((400,400))
pygame.display.set_caption("My first game")
But every time I try to run it, I get this:
pygame 2.0.0 (SDL 2.0.12, python 3.8.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
And then nothing happens.
Why I can't run this program?
Your application works well. However, you haven't implemented an application loop:
import pygame
from pygame.locals import *
pygame.init()
win = pygame.display.set_mode((400,400))
pygame.display.set_caption("My first game")
clock = pygame.time.Clock()
run = True
while run:
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update game objects
# [...]
# clear display
win.fill((0, 0, 0))
# draw game objects
# [...]
# update display
pygame.display.flip()
# limit frames per second
clock.tick(60)
pygame.quit()
The typical PyGame application loop has to:
handle the events by calling 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 calling either pygame.display.update() or pygame.display.flip()
limit frames per second to limit CPU usage with pygame.time.Clock.tick
repl.it/#Rabbid76/PyGame-MinimalApplicationLoop See also Event and application loop
This is interesting. Computer read your program line by line[python]. When all the line are interpreted, the program closed. To solve this problem you need to add a while loop to make sure the program will continue until you close the program.
import pygame,sys
from pygame.locals import *
pygame.init()
pygame.display.set_caption("My first game")
win = pygame.display.set_mode((400,400))
#game loop keeps the game running until you exit the game.
game_running=True
while game_running:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
win.fill((0, 250, 154)) #Fill the pygame window with specific color. You can use hex or rgb color
pygame.display.update() #Refresh the pygame window
You can check more pygame Examples.
https://github.com/01one/Pygame-Examples
I think this will be helpful.

pygame window continuously crashing

import pygame
pygame.init()
white=(255,255,255)
black=(0,0,0)
red=(255,0,0)
green=(0,255,0)
blue=(0,0,255)
gameDisplay=pygame.display.set_mode((800,600))
gameDisplay.fill(black)
Displayer=pygame.PixelArray(gameDisplay)
pygame.draw.line(gameDisplay, blue,(100,200),(300,450),5)
pygame.draw.rect(gameDisplay, red, (400,400,50,25))
pygame.draw.circle(gameDisplay, white,(150,150,),75)
I used the code to make some shapes but I am unable to export it.
The pygame window always stops responding. How would I stop this behaviour?
Remove the line:
Displayer=pygame.PixelArray(gameDisplay)
This will lock the gameDisplay and the following drawing operations will fail. You don't need that line of code at all. pygame.PixelArray locks the surface:
During its lifetime, the PixelArray locks the surface, thus you explicitly have to close() it once its not used any more and the surface should perform operations in the same scope.
You can remove Displayer=pygame.PixelArray(gameDisplay)
And also you will need to write pygame.display.flip()
This will actually display all the drawings on the screen.
The last thing you need is a while loop that runs until the program ends to make sure you are drawing the shapes on every frame:
pygame.init()
white=(255,255,255)
black=(0,0,0)
red=(255,0,0)
green=(0,255,0)
blue=(0,0,255)
gameDisplay=pygame.display.set_mode((800,600))
gameDisplay.fill(black)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
gameDisplay.fill((255, 255, 255))
pygame.draw.line(gameDisplay, blue,(100,200),(300,450),5)
pygame.draw.rect(gameDisplay, red, (400,400,50,25))
pygame.draw.circle(gameDisplay, white,(150,150,),75)
pygame.display.flip()
pygame.quit()
exit()
This is the most common way to do it, you check we didn't close the window, and then draw on every frame each shape, then use pygame.display.flip() to actually display everything.
Hope this helps!

Python Can't load background images in pygame

i try to load backgroud.png using pygame.image.load(),but i get nothing.here is my code,please help me ,thanks.
import pygame
pygame.init()
# screen
screen = pygame.display.set_mode((480, 700))
# 1.load_image
bg = pygame.image.load("./images/background.png")
# 2.blit
screen.blit(bg, (0, 0))
# 3.update
pygame.display.update()
while True:
pass
pygame.quit()
here is my sreen:it get nothing
With a game you're making all stuff that needs to be refreshed needs to be in the main game loop, your problem is, is you are drawing the image outside that game loop, meaning it gets drawn once then cleared and never drawn again.
To fix your code this is how you would write it:
import pygame
pygame.init()
# screen
screen = pygame.display.set_mode((480, 700))
# 1.load_image
bg = pygame.image.load("./images/background.png")
while True:
# 2.blit
screen.blit(bg, (0, 0))
# 3.update
pygame.display.update()
pygame.quit()
But notice how the bg=pygame.image... is outside the loop, this is because if it was inside the loop it would create a new instance of that image every time the loop happens.
The main game loop works by looping through all your functions and other stuff and then doing again and again, and again.
A game loop is how fps works, basically it is the measurement of how many times per second that game loop happens.
Make sure whenever you are doing anything in the loop it actually has a place there, for example loading an image doesn't, but updating where the player is on the screen does.
If you want to have a look at a good game loop that can be applied to most game engines this website will help you. But don't look at the most complex one when using pygame as it isn't built for that. Fix Your Timestep!
But your original problem of the image not loading isn't the case, it was loading but you were drawing your image in the wrong way, if you want a basic tutorial on pygame watch these videos: Game Development in Python 3 With PyGame - 1 - Intro
A better system to ease development
import pygame
bg = None
def load_resources():
bg = pygame.image.load("./images/background.png")
# all other resources
def render():
screen.blit(bg, (0,0))
def update():
# all logic updates for example movement of entities.
### start of game
load_resources()
while True:
update()
render()
pygame.display.update()
pygame.quit()
I think you should not write pass inside while loop because of it the output window will stop responding. Also you should write the screen blit and display.update inside while loop and you should write the correct image extension in the path. You can also write the full path of the file like ==> pygame.image.load(r"C:\Users\Desktop\back_ground.jpg")
import pygame
pygame.init()
# screen
screen = pygame.display.set_mode((480, 700))
# 1.load_image
bg = pygame.image.load("back_ground.jpg")
while True:
# 2.blit
screen.blit(bg, (0, 0))
# 3.update
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

why wont python pygame wont work in spyder?

I recently downloaded pygame on my school pc(I have experience with it before), but when I try to open a window it does not work and are black even though I colored the window in the code.
I even copied this code:
import pygame
background_colour = (255,255,255)
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Tutorial 1')
screen.fill(background_colour)
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
But it does the same thing.
Extra:
I use Spyder with python 3.6
Thanks in advance:)
Your code work in my mac. If you are in Windows, try to restart the PC. Also, try to execute your example from a command line.

Segmentation fault: 11 when trying to run Pygame

I am using OSX 10.11.6, Python 2.17.12 and Pygame 1.9.1.
I made this simple program that should display a black rectangle in the middle of a white field. However, when I try to run it I get an error saying:
Segmentation fault: 11
I have tried several things, but nothing seems to work. Here is my code:
import pygame
pygame.init()
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Slither')
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [400, 300, 20, 20])
pygame.display.update()
pygame.quit()
quit()
Does someone know how I can solve this issue? Thanks in advance!
Note: I am writing my code in Atom, and running it in Terminal using this command:
$ python2.7-32 slither.py
This is due to a flaw in the built-in SDL library that Pygame depends on. Pygame can create a screen, but attempting to touch it will immediately crash it with Segmentation error 11.
From the official SDL website, go to the download page and get the runtime library 1.2.15 for Mac. Open the .dmg you downloaded and you'll be given an SDL.framework file. Open /Library/Frameworks in Finder and move the framework file there. You may need to select Replace.

Categories

Resources