I followed a turtorial where I was lerning to create games using pygame. I just started and I already have a problem. It says it has trouble with finding the image. Here is what it says:
C:\Users\Patryk\AppData\Local\Programs\Python\Python39\python.exe:
can't open file ''
C:\Users\Patryk\PycharmProjects\PePeSza-Game\main.py: [Errno 2]
I tried looking for someone with same problem, but couldn't find the anwser to my question. Here is whole code:
import pygame
pygame.init()
# Game window
screen_width = 800
screen_height = 640
lower_margin = 100
side_margin = 300
screen = pygame.display.set_mode((screen_width,screen_height))
screen = pygame.display.set_caption(('Level editor'))
# Importing images
bg_img = pygame.image.load('./bg.png')
# Create function for drawing background
def draw_bg():
screen.blit('bg_img', (0, 0))
# Mainloop
run = True
while run:
draw_bg()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()
There are two thing you need to do in the code
removing the ./ before image name. It might be causing issues
# Importing images
bg_img = pygame.image.load('bg.png') # removing the ./
In screen.blit in draw_bg, you were passing string but you need to pass the object made with pygame.image.load
# Create function for drawing background
def draw_bg():
screen.blit(bg_img, (0, 0)) # removing the apostrophe
Full code:
import pygame
pygame.init()
# Game window
screen_width = 800
screen_height = 640
lower_margin = 100
side_margin = 300
screen = pygame.display.set_mode((screen_width,screen_height))
screen = pygame.display.set_caption(('Level editor'))
# Importing images
bg_img = pygame.image.load('bg.png')
# Create function for drawing background
def draw_bg():
screen.blit(bg_img, (0, 0))
# Mainloop
run = True
while run:
draw_bg()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()
Related
I was trying to draw an image onto the screen but I keep getting errors. The error keeps saying "video system not initialized". I am new to python, can anyone help?
import pygame
import os
#game window
WIN = pygame.display.set_mode((1000, 800))
NAME = pygame.display.set_caption("Space War!")
#FPS limit
FPS = (60)
#image i am trying to draw onto screen
SPACE_BACKGROUND = pygame.image.load(os.path.join('space_background.png'))
pygame.init()
#allows pygame to quit
def main():
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
#updates images
pygame.display.update()
#calling def main(): function
if __name__ == "__main__":
main()
do:
def main():
clock = pygame.time.Clock()
run = True
FPS = 60
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
#updates images
WIN.blit(SPACE_BACKGROUND, (0, 0)) #you FORGOT this part
pygame.display.update()
BTW the main function cannot access the global 'FPS' variable so declare that within the 'main' function(make it local).
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)
I get this error when I try to run my pygame code pygame.error: video system not initialized. I'm using Repl.it and am attempting to create an aiming game which can track accuracy and in which you only have 3 lives.
import pygame
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False # Here we exit the Loop and execute what after
pygame.quit()
# Play Surface
width = 1080
height = 720
playSurface = pygame.display.set_mode((width, height))
pygame.display.set_caption('Aim Practice')
# Colors
red = pygame.Color(0, 0, 0)
blue = pygame.Color(255, 255, 255)
Image of most of the code
https://repl.it/join/dppwnpin-isa__paz (You can view the full code here!)
The issue is that you event loop is running before anything is initialised. As #zenofpython says in his answer, the calls to prepare the window must come before the main event loop.
Your main event loop is first, and nothing is setup to run.
Just moving the code around will fix it:
import pygame
# FIRST, HANDLE ALL THE INITIALISATION OF PYGAME, FONTS, MIXER etc.
# Play Surface
width = 1080
height = 720
playSurface = pygame.display.set_mode((width, height))
pygame.display.set_caption('Aim Practice')
# Colors
red = pygame.Color(0, 0, 0)
blue = pygame.Color(255, 255, 255)
# ... AND THE REST
# MAIN LOOP
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False # Here we exit the Loop and execute what after
playSurface.fill( blue ) # fill the screen
pygame.display.flip() # flush all the drawing operations to the window
fpsController.tick_busy_loop(60) # clamp the max-FPS
pygame.quit()
You should use pygame.display.set_mode before running your event loop. pygame.event.get won't work if you haven't created a window.
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 ...
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)