why wont the image blit? - python

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.

Related

I am trying to insert a sprite image into pygame window and it wont pop up

(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()

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()

Why is my Pygame window only displaying for only a few seconds?

I'm new to PyGame (and python in general) and I am just trying to get a window to pop up. That's all I'm after for now. Here's my code:
import pygame
pygame.init()
win = pygame.display.set_mode((600, 600))
pygame.display.set_caption('First Game')
I am using Python 3.7.0 in Pycharm and PyGame 1.9.4.
Following Python's PyGame tutorial, your next step is to start a while loop to handle the game frames:
import pygame
pygame.init()
win = pygame.display.set_mode((600, 600))
pygame.display.set_caption('First Game')
clock = pygame.time.Clock() # Determine FPS (frames-per-second)
crashed = False
# Game loop
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
print(event)
pygame.display.update()
clock.tick(60)
Because you need to first put it inside a loop (so it refreshes whatever is inside it), that way it stays on until something alters the condition of that loop.
# Event loop (HERE YOU PUT IT).
while 1:
for event in pygame.event.get():
if event.type == QUIT:
return
screen.blit(background, (0, 0))
pygame.display.flip()
if __name__ == '__main__': main()

Why does the image not show up on the screen when I blit it?

Using Python (and Pygame), I have been creating a short one-screen game and I code each part in a different window. In my Home Screen, when I blit the play button onto the screen, it doesn't appear. I am new to Python and Pygame. This is my code:
import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1352,638))
pygame.display.set_caption("Termination: Part 1")
bg = True
playButton = pygame.image.load("Play Button.png")
mouse = pygame.mouse.get_pos()
def playButtonFunction():
if background == pygame.image.load("Home Screen.png"):
background.blit(playButton(533.5,278))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN and event.key == K_SPACE:
bg = False
screen.blit(background,(0,0))
if bg:
background = pygame.image.load("Intro Screen.png")
else:
background = pygame.image.load("Home Screen.png")
playButtonFunction()
pygame.display.update()
As Frédéric Hamidi already said in a comment, the line
if background == pygame.image.load("Home Screen.png")
will not work as you probably expect.
You should probabkly pass a flag to that method or don't call that function at all when you don't want to show that playButton image.
Also, the line
background.blit(playButton(533.5,278))
will throw an exception, it should look like
background.blit(playButton, (533, 278))
So, change your code to
...
if bg:
background = pygame.image.load("Intro Screen.png")
else:
background = pygame.image.load("Home Screen.png")
screen.blit(background,(0,0))
if !bg:
background.blit(playButton, (533, 278))
...
Another issue that you load the images from disk every iteration of your game loop (with pygame.image.load). It's enough to load each image once.

trying to load bitmap image in python

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.

Categories