Python Pygame not showing image - python

I have problem, image "floor.png" is not showing on screen, blue color is showing. I was searching my old codes and I do everything same as in old working game.
But still image is invisible.
import pygame
pygame.init()
display_width = 600
display_height = 900
gameDisplay = pygame.display.set_mode((display_height, display_width))
pygame.display.set_caption("Parkour")
clock = pygame.time.Clock()
lightblue = (102, 255, 255)
floorImg = pygame.image.load("floor.png")
floorX = 300
floorY = 300
crashed = False
def DrawPictures(floorX, floorY):
gameDisplay.blit(floorImg, (floorX, floorY))
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
DrawPictures(floorX, floorY)
gameDisplay.fill(lightblue)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
Any help?

You blit the image on the screen surface correctly, but you just fill the screen afterwards. Just switch
DrawPictures(floorX, floorY)
gameDisplay.fill(lightblue)
to
gameDisplay.fill(lightblue)
DrawPictures(floorX, floorY)

FIX:
Problem is in the image not in code, I tried another image and the image worked ...

Related

Why does my window keep flashing in pygame?

I made a window in pygame, tried to make the window white and even though I only call pygame.display.update() once it keeps flashing between white and black.
This is my code:
import pygame
running = True
pygame.init()
White = (255, 255, 255)
while running == True:
screen = pygame.display.set_mode((1400, 800))
screen.fill(White)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
I tried changing it to pygame.display.flip() but it still stayed the same.
I also tried to put pygame.display.update() out of the loop but then the screen just stayed black.
You need to create the Pygame window (pygame.display.set_mode) once before the application loop instead of constantly creating a new window in the application loop:
import pygame
running = True
pygame.init()
screen = pygame.display.set_mode((1400, 800))
White = (255, 255, 255)
while running == True:
screen.fill(White)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()

Transparent background not disappearing when displayed on pygame window

I am creating a car game and have come across a problem. I've added a car image with a transparent background into my code but the car appears on the screen with the transparent background visible. I want to make the background to disappear. This is a link to the image: https://www.pngfind.com/mpng/bboJib_car-top-view-png-audi-transparent-png/
Here is the code:
import pygame
pygame.init()
import pygame
pygame.init()
black = (0, 0, 0)
#screen
screen_width = 600
screen_length = 800
screen = pygame.display.set_mode((screen_length, screen_width))
clock = pygame.time.Clock()
FPS = 60
#the car i am having trouble with
car2Img = pygame.image.load('car2.png')
car2_X = 0
car2_Y = 0
carX_change = 0
def car2(x, y):
screen.blit(car2Img, (x, y))
running = True
while running:
screen.fill((119, 118, 110))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
car2(car2_X, car2_Y)
clock.tick(FPS)
pygame.display.update()
pygame.quit()
In order to remove the background from a png image you must use the convert_alpha() function when loading it:
car2Img = pygame.image.load('car2.png').convert_alpha()
If your image doesn't contain alpha then you can use a color key to remove the background:
car2Img.set_colorkey(colorOfBackground)

Python- Pygame won't display images or captions

My code runs with no errors, but the background image does not display and neither does the caption at the top of the window. I have checked that my script is importing the correct pygame module, and tried different images. I have Python 3.6.2 on a Mac.
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((400, 433))
pygame.display.set_caption('Pac Man')
clock = pygame.time.Clock()
background = pygame.image.load('/Users/MyMacbook/Desktop/pac-man/background.png')
gameDisplay.blit(background, (100, 0))
run = True
while run:
gameDisplay.blit(background, (0, 0))
pygame.display.update()
pygame.quit()
quit()
Yeah it appears that #skrx is correct, there's something blocking by not clearing the event buffer. This code works for me:
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((400, 433))
pygame.display.set_caption('Pac Man')
clock = pygame.time.Clock()
background = pygame.image.load('/Path/To/Background.png')
gameDisplay.blit(background, (100, 0))
run = True
while run:
for e in pygame.event.get():
if e.type == pygame.QUIT:
run = False
gameDisplay.blit(background, (0, 0))
pygame.display.update()
pygame.quit()
quit()

Pygame couldnt load picture

I'm making a game and when i try to load the picture it says "Cannot find reference 'load' in 'image.py'"
That's my code:
import pygame
pygame.init()
display_width = 800
display_height = 600
black = (0, 0, 0,)
white = (255, 255, 255)
red = (255, 0, 0)
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Boat Race')
clock = pygame.time.Clock()
carImg = pygame.image.load('boat.png')
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
pygame.display.update()
clock.tick(30)
pygame.quit()
quit()
It seems like there is an error with the PyGame installation, but it will be helpful if you post the traceback error. It seems the code is fine, at least to me.
This should be a comment, but i don't have enough reputation.
After like 1 hour of thinking i found that the whole problem was with the PyCharm because when i runned the program only with python the image was load and i was able to display it. Ty for your help.

My Pygame image not loading

I found a tutorial online (on Youtube) and it showed me the basics of creating a game with pygame.
I saved a png image in the same folder as my py script. When I run the script it shows no error but my image does not shows in pygame window. Kindly advice
Here's the script
import pygame,sys
pygame.init()
WIDTH,HEIGHT = 640,360
screen = pygame.display.set_mode((WIDTH,HEIGHT),0,32)
clock = pygame.time.Clock()
FPS = 24
dog_img = pygame.image.load("dog.png")
#PROCESS
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#PROCESS
#LOGIC
#LOGIC
#DRAW
screen.blit(dog_img,(0,0))
pygame.display.flip()
#DRAW
clock.tick(FPS)
Fix the indentation. screen.blit(dog_img, (0, 0)) and the two lines below should be inside of the while loop (indented with 4 spaces).
import pygame,sys
pygame.init()
WIDTH, HEIGHT = 640, 360
screen = pygame.display.set_mode((WIDTH,HEIGHT),0,32)
clock = pygame.time.Clock()
FPS = 24
# Always use `.convert()` or `.convert_alpha()`. It'll improve the performance.
dog_img = pygame.image.load("dog.png").convert_alpha()
#PROCESS
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#LOGIC
#DRAW
screen.blit(dog_img, (0, 0))
pygame.display.flip()
clock.tick(FPS)

Categories