I am loading an image in python what I think is proper but it still doesn't display. I don't know if the image is too big or what.
import pygame
import math
pygame.display.init()
window = pygame.display.set_mode((600, 500))
mapImg = pygame.image.load("mapoftheusa.bmp")
done = False
while not done:
window.fill((0,0,0))
evtList = pygame.event.get()
for evt in evtList:
if evt.type == pygame.QUIT:
done = True
window.blit(mapImg, (0,0)) #<<will not blit
pygame.quit()
You forgot to add a call to pygame.display.update() right after window.blit(mapImg, (0,0)).
So, your complete code should be:
import pygame
import math
pygame.display.init()
window = pygame.display.set_mode((600, 500))
mapImg = pygame.image.load("mapoftheusa.bmp")
done = False
while not done:
window.fill((0,0,0))
evtList = pygame.event.get()
for evt in evtList:
if evt.type == pygame.QUIT:
done = True
window.blit(mapImg, (0,0)) #<<will not blit
pygame.display.update() # solution: you forgot this...
pygame.quit()
pygame.display.update() updates the window (screen) with your drawings. If you dont call it you will not see anything at all. pygame.display.flip() also works, but it should be used when double buffering or hardware surfaces are being used.
Also, I think it is better to initialize pygame by calling pygame.init() as this will initialize all of its modules including the display.
Related
I'm currently trying to follow the Introduction to Pygame tutorial and I'm stuck on one step where the speaker makes the surface. His surface is bright red while my surface isn't visible at all.
Here's the code:
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption('Runner')
clock = pygame.time.Clock()
test_surface = pygame.Surface((100, 200))
test_surface.fill('Red')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(test_surface, (0, 0))
pygame.display.update()
clock.tick(60)
I've already tried starting from the very beginning, my code and the steps I make are identical to his. I've also tried deleting python and pygame. I've installed Python version that the speaker has (3.9), but nothing helps.
It is a matter of indentation. You have draw the scene and update the display in the application loop:
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption('Runner')
clock = pygame.time.Clock()
test_surface = pygame.Surface((100, 200))
test_surface.fill('Red')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# INDENTATION
#<------|
screen.blit(test_surface, (0, 0))
pygame.display.update()
clock.tick(60)
(i followed a yt tut so idk) my code: idk if i put code in wrong place
import pygame
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0)
player_sprite = pygame.image.load("will smith.png") .convert_alpha
pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
exit = False
while not exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
print(event)
display.fill(white)
pygame.display.update()
pygame.quit()
quit()
not really sure why it does this, im new to python so idk
There are multiple things that needs to fixed. First of all to you actually need to blit the image on the screen.
while not exit:
for event in pygame.event.get():
#...[block of code]
display.fill(white)
display.blit(player_sprite, (100,100)) # BLIT IMAGE TO SCREEN
.convert_alpha() is a method and you need to call it after you initialize pygame and set the video mode.
You can read more about convert_alpha() here
pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
#converting it after init and setting game mode
player_sprite = pygame.image.load("will smith.png").convert_alpha()
Here's the final working code
import pygame
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0)
pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
player_sprite = pygame.image.load("will smith.png").convert_alpha()
exit = False
while not exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
# print(event)
display.fill(white)
display.blit(player_sprite, (100,100))
pygame.display.update()
pygame.quit()
quit()
You have to blit the image in the application loop, after clearing the display and before updating the display:
display.blit(player_sprite, (100, 100))
convert_alpha is function. You missed the parentheses
player_sprite = pygame.image.load("will smith.png").convert_alpha
player_sprite = pygame.image.load("will smith.png").convert_alpha()
Also, you cannot call a pygame function before you initialize pygame. pygame.init() should be the very first pygame function called.
Complete example:
import pygame
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0)
pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
player_sprite = pygame.image.load("will smith.png").convert_alpha()
exit = False
while not exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
print(event)
display.fill(white)
display.blit(player_sprite, (100, 100))
pygame.display.update()
pygame.quit()
quit()
The typical PyGame application loop has to:
limit the frames per second to limit CPU usage with pygame.time.Clock.tick
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()
I have tried putting the fill color before blit but it won't work.
im using python 3.8.2, windows 10, and pygame 1.9.6.
please help
btw im following this tutorial (im new to pygame)
and here's my code
import pygame
#initialize
pygame.init()
#screen
screen = pygame.display.set_mode((800, 600))
#things
pygame.display.set_caption("space invaders 1.0")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
#player settings
playerimg = pygame.image.load('space-invaders.png')
playerx = 370
playery = 430
#define player
def player():
screen.blit(playerimg, (playerx, playery))
#loop
running = True
while running:
#rgb
screen.fill((10, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT():
running = False
player()
pygame.display.update()
#end process
pygame.quit()
exit()
ill answer any questions
(side note it crashes when I press "x" and exits when I press '-' idk why)
pygame.QUIT is not a function - it's a constant, simply an integer representing a QUIT event. You can't do if event.type == pygame.QUIT(), you need to do if event.type == pygame.QUIT, without parenthesis.
You also don't really need to call pygame.quit() and exit() at the end of the program - both pygame and the program itself will be terminated when the program reaches its end.
Apart from that your program works for me, images are showing. Make sure your image is visible over a black background and if it is and you are still having problems I'll try to help you with them.
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()
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((800,600))
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.draw.rect(screen, (255,0,0),(400,300,50,50))
pygame.display.flip()
As #Furas writes, the screen update code is not being called from within the loop. Python uses indentation to designate code blocks, so if the function call (or other code section) is not indented to the correct column, it is literally a completely different set of operations.
Since a piece of sample code is worth a thousand words:
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((800,600))
game_over = False
while not game_over:
# Handle user-events
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
# Re-draw the screen
pygame.draw.rect(screen, (255,0,0), (400,300,50,50))
pygame.display.flip()
pygame.quit()
sys.exit()