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')
Related
i try to load backgroud.png using pygame.image.load(),but i get nothing.here is my code,please help me ,thanks.
import pygame
pygame.init()
# screen
screen = pygame.display.set_mode((480, 700))
# 1.load_image
bg = pygame.image.load("./images/background.png")
# 2.blit
screen.blit(bg, (0, 0))
# 3.update
pygame.display.update()
while True:
pass
pygame.quit()
here is my sreen:it get nothing
With a game you're making all stuff that needs to be refreshed needs to be in the main game loop, your problem is, is you are drawing the image outside that game loop, meaning it gets drawn once then cleared and never drawn again.
To fix your code this is how you would write it:
import pygame
pygame.init()
# screen
screen = pygame.display.set_mode((480, 700))
# 1.load_image
bg = pygame.image.load("./images/background.png")
while True:
# 2.blit
screen.blit(bg, (0, 0))
# 3.update
pygame.display.update()
pygame.quit()
But notice how the bg=pygame.image... is outside the loop, this is because if it was inside the loop it would create a new instance of that image every time the loop happens.
The main game loop works by looping through all your functions and other stuff and then doing again and again, and again.
A game loop is how fps works, basically it is the measurement of how many times per second that game loop happens.
Make sure whenever you are doing anything in the loop it actually has a place there, for example loading an image doesn't, but updating where the player is on the screen does.
If you want to have a look at a good game loop that can be applied to most game engines this website will help you. But don't look at the most complex one when using pygame as it isn't built for that. Fix Your Timestep!
But your original problem of the image not loading isn't the case, it was loading but you were drawing your image in the wrong way, if you want a basic tutorial on pygame watch these videos: Game Development in Python 3 With PyGame - 1 - Intro
A better system to ease development
import pygame
bg = None
def load_resources():
bg = pygame.image.load("./images/background.png")
# all other resources
def render():
screen.blit(bg, (0,0))
def update():
# all logic updates for example movement of entities.
### start of game
load_resources()
while True:
update()
render()
pygame.display.update()
pygame.quit()
I think you should not write pass inside while loop because of it the output window will stop responding. Also you should write the screen blit and display.update inside while loop and you should write the correct image extension in the path. You can also write the full path of the file like ==> pygame.image.load(r"C:\Users\Desktop\back_ground.jpg")
import pygame
pygame.init()
# screen
screen = pygame.display.set_mode((480, 700))
# 1.load_image
bg = pygame.image.load("back_ground.jpg")
while True:
# 2.blit
screen.blit(bg, (0, 0))
# 3.update
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
I recently downloaded pygame on my school pc(I have experience with it before), but when I try to open a window it does not work and are black even though I colored the window in the code.
I even copied this code:
import pygame
background_colour = (255,255,255)
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Tutorial 1')
screen.fill(background_colour)
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
But it does the same thing.
Extra:
I use Spyder with python 3.6
Thanks in advance:)
Your code work in my mac. If you are in Windows, try to restart the PC. Also, try to execute your example from a command line.
The image is a playing card. We are using pygame 4.5 community edition and pycharm 2.6.9 because 2.7 does not support pygame (this is a school). Here is the code:
import pygame
pygame.init()
picture=pygame.image.load("cards/S01.png")
pygame.display.set_mode(picture.get_size())
main_surface = pygame.display.get_surface()
main_surface.blit(picture, (0,0))
pygame.display.update()
Why does the window disappear?
Try this:
import pygame
pygame.init()
picture=pygame.image.load("cards/S01.png")
pygame.display.set_mode(picture.get_size())
main_surface = pygame.display.get_surface()
main_surface.blit(picture, (0,0))
while True:
main_surface.blit(picture, (0,0))
pygame.display.update()
pygame.display.update() updates a frame. There are multiple frames per second depending on what what you draw onto the surface.
The problem is, after you update the screen with pygame.display.update(), you do nothing, and your program simply ends. pygame.display.update() does not block.
You need what is usually called a main loop. Here's a simple example with event handling:
import pygame
pygame.init()
picture = pygame.image.load("cards/S01.png")
# display.set_mode already returns the screen surface
screen = pygame.display.set_mode(picture.get_size())
# a simple flag to show if the application is running
# there are other ways to do this, of course
running = True
while running:
# it's important to get all events from the
# event queue; otherwise it may get stuck
for e in pygame.event.get():
# if there's a QUIT event (someone wants to close the window)
# then set the running flag to False so the while loop ends
if e.type == pygame.QUIT:
running = False
# draw stuff
screen.blit(picture, (0,0))
pygame.display.update()
This way, your application does not, only when someone closes the window.
Whenever I run my code the Python Window that shows up does not respond.
Is there something wrong with my code or do I have to re-install pygame and python?
I get a black pygame window and then it turns white and says not responding?
Also I am new to this so please make this as simple as possible. I tried looking everywhere for the answer but could not get it in a way that I could understand.
Please help me out. Thanks :)
1 - Import library
import pygame
from pygame.locals import *
2 - Initialize the game
pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
3 - Load Images
player = pygame.images.load("resources/images/dude.png")
4 - keep looping through
while 1:
# 5 - clear the screen before drawing it again
screen.fill(0)
# 6 - draw the screen elements
screen.blit(player, (100,100))
# 7 - update the screen
pygame.display.flip()
# 8 - loop through the events
for event in pygame.event.get():
# check if the event is the X button
if event.type==pygame.QUIT:
# if it is quit the game
pygame.quit()
exit(0)
Don't import pygame.locals. It is actually unnecessary, since you are already importing pygame.
Also, as #furas said, it should be:
player = pygame.image.load("resources/images/dude.png")
Not:
player = pygame.images.load("resources/images/dude.png")
This will clear up some of the problems in your code.
From my personal experience,if you run pygame code from IDLE it often does not respond at all.Try saving your project as a .py file and then run it with python.exe.It always works for me.
And as furas said use
player = pygame.image.load("resources/images/dude.png")
instead of
player = pygame.images.load("resources/images/dude.png")
Replace that for loop with this
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
I am doing an assignment with pyGame, a very simple one, we have to load an image in a window, but it does not load! And the IDLE does not show any errors..I tried relative paths to my image (room.png) also absolute paths (C:...\room.png) but nothing. Here is my code.
import pygame, sys #import pygame and system library
from pygame import * #import all pygame sublibs
pygame.init()
screen = display.set_mode((385,384)) #set screen size
display.set_caption("Blit example")
#this one gets the current working directory
background_file_name = "room.png" #import bg
background_surface = pygame.image.load(background_file_name) #use bg
while True:
for e in pygame.event.get():
if e.type==QUIT: #break the loop and quit
pygame.quit()
sys.exit()
break
screen.fill((255,0,255)) #fill the screen with magenta
screen.blit(background_surface, (0,0))
display.update()
Your indentation is messed up.
You only start drawing after the application has ended.
Try putting the drawing code into your main loop.
Since you said in a comment you had no idea about programming, you probably don't know that indenting is important in python. Your while loop is running continuously, waiting for an exit signal. But the display is only performed after that...
while True:
for e in pygame.event.get():
if e.type==QUIT: #break the loop and quit
pygame.quit()
sys.exit()
break
screen.fill((255,0,255)) #fill the screen with magenta
screen.blit(background_surface, (0,0))
display.update()
now the display code is inside the while loop