I am trying to make a game in pygame. I have Python 3.6 installed in Ubuntu, and this is how my code looks:
import pygame
pygame.init()
# on_screen
screen = pygame.display.set_mode([800, 600])
pygame.display.set_caption('Little UFO')
# actors
ufo = pygame.image.load('ufo.png')
def player():
screen.blit(ufo, (375, 591))
run = True
while run:
screen.fill([255,255,255])
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
run = False
player()
When I run it I see a black background even though I have selected white, and I can't see the ufo.
You need to call pygame.display.flip() after drawing to the screen for changes to become visible.
By default, whenever drawing to your surface it actually draws to a hidden surface. Whenever you are done drawing everything on a frame you flip the front buffers with the back buffer. This way a user can never see a half-drawn screen.
Add pygame.display.update() after the player function is called:
while run:
screen.fill([255,255,255])
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
run = False
player()
pygame.display.update()
Using pygame.display.update() can be more efficient than flip() if you specify which section of the screen to update, more on that here: https://www.pygame.org/docs/ref/display.html#pygame.display.update
If you don't pass any parameters it does the same job as pygame.display.flip()
Related
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!
How do you make a white background in pygame? I am having an issue figuring out the correct code to make the background white and where exactly to put it in my code.
How do you make a white background in pygame?
When pygame.display.set_mode() is called, a pygame.Surface is associated to the display. Everything that is drawn on this Surface is visible on the display after the update. fill the display Surface with a white color:
screen = pygame.display.set_mode((500, 500))
# [...]
screen.fill("white")
# [...]
pygame.display.flip()
where exactly to put it in my code.
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()
Example:
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
# handel events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update game
# clear screen with white color
screen.fill("white")
# draw scene
# [...]
# update diesplay
pygame.display.flip()
pygame.quit()
exit()
This question already has answers here:
Pygame window not responding after a few seconds
(3 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 2 years ago.
MacOSX 10.15.2, Python 3.8 and Pygame 2.0.0.
Hello! I am currently trying to make a dark purple (87,61,122) background in Pygame, but it only appears as a black background, seemingly frozen and loading forever.
Here is my code:
import pygame
bgcolor = [87,61,122]
pygame.init()
screen = pygame.display.set_mode((1600,900))
screen.fill(bgcolor)
pygame.display.flip
Is there anything wrong with the code or is Pygame just refusing to co-operate?
pygame.display.flip is not a function call. You missed the parentheses.
However, you have to handle the events in the application loop. 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. This will update the contents of the entire display.
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 (draw all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
import pygame
bgcolor = [87,61,122]
pygame.init()
screen = pygame.display.set_mode((1600,900))
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
# handle events
for event in pygame.event.get():
if event.type == QUIT:
run = False
# clear display
screen.fill(bgcolor)
# draw scene
# [...]
# update display
pygame.display.flip()
pygame.quit()
exit()
change pygame.display.flip by pygame.display.flip()
or pygame.display.update()
not that without IDLE, this script will display a window and directly destroy it.
To add like loop:
import pygame
bgcolor = [87,61,122]
pygame.init()
screen = pygame.display.set_mode((1600,900))
loop = True
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False #Quiting
screen.fill(bgcolor)
pygame.display.update()
def run_game():
"""Initialise the game and create a screen object."""
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
ship = Ship(screen)
pygame.display.set_caption("Alien Invasion")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#Redraw the screen for each pass through the loop. ie fill the screen with colour.
screen.fill(ai_settings.bg_colour)
#Make the most recently drawn screen visible.
pygame.display.flip()
# Will update the screen as game elements move around. Making the game look smooth.
ship.blitme() # added after background so appears ontop of it.
run_game()
Your indentation is off, or atleast the only apparent issue. I tried running and on line 5 setting is not defined. There are other classes not defined in what you gave. Here it is with correct indentation. (had to switch pygame.display.flip() and ship.blitme())
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#Redraw the screen for each pass through the loop. ie fill the screen with colour.
screen.fill(ai_settings.bg_colour)
ship.blitme() # added after background so appears ontop of it.
#Make the most recently drawn screen visible.
pygame.display.flip()
# Will update the screen as game elements move around. Making the game look smooth.
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.