This is my first time posting on Stack Overflow so please let me know if I could be doing anything better for asking my question!
Here is the tutorial I am following timestamped to about where I was. I am using VSCode Python, and Pygame. https://youtu.be/UZg49z76cLw?t=1138
I have the file in the directory specified and I have made sure it is named correctly by copy-pasting it.
Whenever I run my code, the bg_surface does not show up on my screen. Before it said that it didn't exist, but I have put the full file address in and it stopped doing that, but still won't display the actual image. I would also like to get away from using the full file path as I won't be able to send it...
import pygame, sys
pygame.init()#initiating pygame
screen = pygame.display.set_mode((576,1024))#Set canvas size
clock = pygame.time.Clock()
bg_surface = pygame.image.load('C:\\Users\\{my_name}\\MyPythonScripts\\Flapp\\assets\\background-day.png')
bg_surface = pygame.transform.scale2x(bg_surface)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()#Ends the game
sys.exit()#ends the program
screen.blit(bg_surface,(0,0))#Blit puts a surface on another surface
pygame.display.update#Update screen
clock.tick(120)#Set framerate
Things I have tried:
bg_surface = pygame.image.load('C:\\Users\\{my_name}\\MyPythonScripts\\Flapp\\assets\\background-day.png')
bg_surface = pygame.image.load('assets\\background-day.png')
bg_surface = pygame.image.load('assets/background-day.png')
bg_surface = pygame.image.load(r'assets/background-day.png')
bg_surface = pygame.image.load(r'assets\background-day.png')
bg_surface = pygame.image.load('assets\background-day.png')
And a lot of variations like those. Bonus points if you can tell me why I get:
Exception has occurred: SystemExit
File "C:\Users\Corbin\MyPythonScripts\Flapp\flapp.py", line 14, in <module>
sys.exit()#ends the program
When I close the program... Thanks!
Corbin, you haven't put parenthesis at the end of pygame.display.update. It should rather be pygame.display.update(). That's when you'll be able to get your background image.
Related
So, I'm trying to change the PyGame icon for a game I am working on. Whenever I run the code, it hits me with pygame.error: Unsupported image format
The pygame window also opens and closes with the code under # Setting the game icon, and it did not do that when I did not have those lines in the code.
I've searched for a good answer, but I can't find a good one. If anyone's got any suggestions I would appreciate them.
I am programming on Visual Studio Code with Python 3.10
Here is my code:
import time
import pygame
# Initializes Pygame
pygame.init()
# Game Screen Variables
background_colour = (255,255,255)
# Sets up the playscreen
screen = pygame.display.set_mode((1100,750),0,32)
pygame.display.set_caption("Dusco's Game")
screen.fill(background_colour)
pygame.display.flip()
# Setting the game icon
img = pygame.image.load('gameicon.png')
pygame.display.set_icon(img)
# Game Loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit ()
Make sure that it is a PNG and that you do the path so since it is erroring try this:
img = pygame.image.load('c:/users/USERNAME/PATH_TO_IMAGE/gameicon.png')
I'm not able to add in images to my programme in Pycharm, I've imported Pygame and os and it wouldn't work. The image is in .png format 64 bit from flaticon.com
Is there something else I need to do to be able to add it in, I was following a PyGame tutorial online since I'm a beginner in programming
The python launcher just glitches when I add anything to do with an image. everything else works perfectly fine.
Here is the entire code of the project so far:
import pygame
import os
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Space Invaders - GAME ONE (PyGame)")
playerImg = pygame.image.load('space-invaders.png')
playerX = 370
playerY = 480
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 0))
pygame.display.update()
this is the error message:
playerImg = pygame.image.load('space-invaders.png')
pygame.error: Couldn't open space-invaders.png
Process finished with exit code 1
it just glitches with any addition of an image. If I remove all image related code, it works fine
Also, import os just greys out in the editor
First check if the image is in the same folder as your .py. Next, make sure the image is really .png not just renamed for example, check this by trying to load another image. Last but not the least, check the spelling, maybe the name of the picture is misspelled. Since you are not using paths I can't remember anything else that could cause the issue.
Also, I do not know if it is the mistake during the copy process but last 2 lines of code need to be inside the for loop. And why you have imported os?
EDIT: Answer to the OP question in the comment
By importing os you can now use this module, nothing more. Here is how you can locate your .png file:
current_dir = os.path.dirname(__file__) #The location of your `.py`
img_dir = os.path.join(current_dir, "img_dir") #Path to the folder where your `png` is
player_img = pygame.image.load(os.path.join(img_dir, "space-invaders.png"))
Instead of "img_dir" you need to put the name of your folder, or the path, depending on the folder location.
I have two files, one to generate a world, and another to run the main code. However, the main screen keeps crashing for no reason. I think the world gen may also be broken, but it does at least pass on valid data to the main code.
# Main loop.
while RUNNING:
# Fill the screen.
screen.fill((0,0,0))
# Event handling.
for eventa in event.get():
if eventa.type == QUIT:
RUNNING = f
screen.fill(SCREENCOLOR)
# Draw the world.
for tile in WORLD:
if tile.surface == None:
pass
else:
screen.blit(tile.surface,tile.location)
# Draw the character
screen.blit(PLAYER["image"],PLAYER["loc"])
# Pygame commands clear up.
clock.tick(FPS)
screen.flip()
This code doesn't even fill the screen with white. This may just be too much data to handle, sorry if it is.
World generator
Main code
Previous question
I'm fairly sure that you aren't inserting too many things onto the screen. I believe that the problem is far more simple. You have said screen.flip() However, a surface object has no attribute called flip. You must be confused with the function pygame.display.flip() If you use this instead, the game shall display its visual output.
So, I wrote this code, and it really should work. The main problem is that the code just stops responding when it opens a window and runs.
bif = "back.jpeg"
mif = "image2.png"
import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640,360),0,32)
background = pygame.image.load(bif).convert()
mouse_c = pygame.image.load(mif).convert_alpha()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit
sys.exit()
screen.blit(background,(0,0))
x,y = pygame.mouse.get_pos()
x -= mouse_c.get_width()/2
y -= mouse_c.get_height()/2
screen.blit(mouse_c, (x,y))
pygame.display.update()
Also, I am getting this error in IDLE.
Traceback (most recent call last):
File "C:\Users\Liam\Documents\game\Game", line 10, in <module>
background = pygame.image.load(bif).convert()
error: Couldn't open back.jpeg
When you're running your code, you have to make sure it runs in the right working directory. That would be why it doesn't find the image.
Since you're running your code in IDLE, the terminal will probably keep the process alive, so that the window won't close after the error (it would if the program terminated). Since it doesn't run event.get regularly, windows will notice that it isn't responding and that's what you're getting.
To find out what directory your script is running from, print the output of os.getcwd().
If you don't want to fiddle with paths yet and just have it run, why not set an absolute path for the image for now, like "C:\Users\My Name\Projects\Python\back.jpeg".
pygame.quit
sys.exit
should be
pygame.quit()
sys.exit()
and
screen.blit(mouse_c(x,y))
should be
screen.blit(mouse_c, (x,y))
also you may want to look into limiting it to a certain max frames a second (tutorial covers this)
The following bit of code throws an error:
while True:
event = pygame.event.wait()
if (event.type == ENDSONG):
queue_song()
This it what it reads:
File "pygametest.py", line 22, in <module>
event = pygame.event.wait()
pygame.error: video system not initialized
Most of the digging I've done says that the error stems from pygame.init() not being run, but that's what starts my code.
Am I missing something else?
Edit: Added Code.
import pygame
import time
def queue_song():
print "Queueing New Song"
pygame.init()
pygame.mixer.init()
#rest of code
Even if you initialize Pygame, you need to call to display.set_mode to avoid this error:
pygame.init()
pygame.display.set_mode((width, height))
# rest of the code
This happens because the event queue needs the video mode to be set to work properly. From the documentation:
The input queue is heavily dependent on the pygame display module. If the display has not been initialized and a video mode not set, the event queue will not really work.