Pygame couldnt load picture - python

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.

Related

How do I fix pygame.error: video system not initialized in replit

I'm playing around with Pygame, and when I clicked Run for the first time, Pygame gives me the error
pygame.error: video system not initialized
How do I fix this?
The code I have at the moment is
import pygame
pygame.init()
white = (255, 255, 255)
green = (0, 255, 0)
blue = (0, 0, 128)
X = 400
Y = 400
display_surface = pygame.display.set_mode((X, Y))
pygame.display.set_caption('Epic SAVER')
font = pygame.font.Font('freesansbold.ttf', 32)
text = font.render('Epic SAVER', True, green, blue)
textRect = text.get_rect()
textRect.center = (X // 2, Y // 2)
while True:
display_surface.fill(white)
display_surface.blit(text, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.display.update()
I'm running this in Replit, and how would I fix this?
I got this off of the internet because I don't know Pygame that well
replit.com allows creating Python/PyGame scripts and multiplayer coding. When creating a new repl, select the Pygame template:

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

Pygame Screen.Blit image overlap

I'm relatively new to programming, and even newer to pygame. As a high school IT project, I've decided to create a simple rage platformer game in pygame with a goal to improve my Python skills. I've just started the code after watching a few Clear Code videos (lots of inspiration from him). My main issue I'm running into, regardless of any other bad programming practices or mistakes elsewhere, is that when I blit the surfaces player_surf and background_surf onto the screen, only the bottom one appears when I run the code. I orginally thought it was something wrong with the transparency of the images I was importing, but that wasn't the issue after I fixed that. Then, I thought it might be bliting it off the screen, but if I do them seperately, they work. As I'm extrememly new, I'm not sure what went wrong. I would appreciate any advice.
import pygame
from sys import exit
class GameState():
def __init__(self):
self.state = "cutscene"
def cutscene(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(background_surf, (0, 0))
def main_game(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(player_surf, (screen_x / 2, screen_y / 2))
screen.blit(background_surf, (0, 0))
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
pygame.init()
screen_x = 800
screen_y = 400
screen = pygame.display.set_mode((screen_x, screen_y))
pygame.display.set_caption("Game")
fps = pygame.time.Clock()
game_state = GameState()
# Background
background_surf = pygame.image.load("Game/Background.png").convert()
background_surf = pygame.transform.smoothscale(background_surf, (screen_x, screen_y))
# Player
player_surf = pygame.image.load("Game/Red_Square.png").convert_alpha()
player_rect = player_surf.get_rect(center=(screen_x / 2, screen_y / 2))
while True:
game_state.main_game()
# game_state.cutscene()
pygame.display.update()
fps.tick(60)
You need to draw the background, then the player otherwise the player will be hidden:
def main_game(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# draw
screen.blit(background_surf, (0, 0))
screen.blit(player_surf, (screen_x // 2, screen_y // 2))

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

Python Pygame not showing image

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 ...

Categories