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
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've been learning python and decided to play around with PyGame. The PyGame window randomly stops showing up when I run the script. I don't get any errors or anything, the PyGame window just won't appear.
The scripts used to work, now they don't. I had this issue before and restarted my computer a few times and eventually it started working again. But it's happening again.
So, I've got no clue what's causing it and no clue how to fix it. I've tried it with two different PyGame scripts, neither work. I'll post a sample script below.
Im using:
PyGame v2.0.1
SDL v2.0.14
Python 3.7.3
OS: Windows 10
IDE: PyCharm
I know PyGame version 2 is new, is this a bug with the new version? Thanks for your help.
Edit: restarting the computer once again fixed the issue in the short-term. I'm still looking for a long-term solution to the problem so I don't have to the restart the computer every time this happens.
Here's some sample code:
import pygame, sys
pygame.init()
windowSize = sizeW, sizeH = 1920, 1080
window = pygame.display.set_mode(windowSize)
pygame.display.set_caption('Test')
black = 0, 0, 0
speed = 0.2
x, y = 50, 50
h, w = 50, 50
blue = 0, 0, 255
guy = pygame.image.load('tile032.png')
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x - speed > 0:
x -= speed
if keys[pygame.K_RIGHT] and x + w + speed < sizeW:
x += speed
if keys[pygame.K_UP] and y - speed > 0:
y -= speed
if keys[pygame.K_DOWN] and y + speed + h < sizeH:
y += speed
window.fill(black)
window.blit(guy, (x,y))
pygame.display.update()
Handle the closing window properly , use pygame.quit().
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
This issue usually comes when you are exiting pygame window just after calling it. It can be resolved by exiting the pygame only after the run variable goes as False. Either make the logic when your program ends or after the run function is defined as false.
After the Pygame window closes, look at the terminal to see if there is an error. If you don't use Linux, look at the place where the error usually comes up.
How I usually do it is have pygame.quit() at the very end of the code, outside of every loop, class, and method. I don't usually use sys.exit() since I am a very new pygame user, but you should also set run to false in the event loop so then it hits that pygame.quit() method at the end. And like others suggest, maybe have sys.exit() after that too.
Instead of using sys.exit(), use pygame.QUIT().I think that should do it.
I think you have to be using either pygame.quit() or just quit() or say run is False than using sys.exit() because for me personally it just broke my code while running it with pygame. So try out this for killing your pygame window:
for event in pygame.event.get():
if event.type == pygame.QUIT:
# loop will run for last time then the game will stop
run = False
Hope this helps you!
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'm working on a game with Pygame and it was going well...until it decided to not respond. Here's my code:(sorry if the formatting doesn't work im new to stackoverflow)
#MODULES USED (use from to make calling functions easier)
from random import *
from pygame import *
import pygame
from pygame.locals import *
pygame.init()
from time import *
#INITIALISE THE PYGAME WINDOW
pygame.event.pump()
screen = display.set_mode([500, 500])
blue = [230, 242, 255]
screen.fill(blue)
pygame.display.update()
default = pygame.image.load("default.jpg")
screen.blit(default, (0,0))
pygame.display.update()
click = pygame.mouse.get_pressed()
#BASIC HEXAPAWN
#ALL POSSIBLE COMPUTER'S MOVE BOARDS AS ARRAY HERE
#TThe moves from the board images are left to right
class Board:
def __init__(self, board, moves):
self.board = board
self.moves = moves
boards1 = [pygame.image.load("a1.jpg"), pygame.image.load("a2.jpg"), pygame.image.load("a3.jpg")] #move1 boards
#irrelevant stuff removed, just initialising the other boards.
#GAME MAIN LOOP
while True:
#START GAME - 1st move
print("You play as O, computer is X")
currentboard = "O O O\n# # #\nX X X"
print(currentboard)
#PLAYER MOVE 1
screen.blit(boards1[0], (0, 250))
pygame.display.update()
if boards1[0].get_rect().collidepoint(pygame.mouse.get_pos()) and click:
screen.blit(boards1[0], (0,0))
pmove = 0
#note: I haven't added the other board options yet.
currentboard = boards1[pmove]
#[insert more unnecessary code here]
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
break
pygame.quit()
Basically whenever I run the code, the Pygame window looks alright, but when I try to click on the image it just stops responding. Also the window is always stuck on a loading cursor, idk why.
I've tried everything I could find but, nope, not working.
If anyone can help then I'd appreciate it.
Thanks :)
Eleeza
edit:i didnt know others could edit my posts too
Clearing some stuff up, when I ran my code, there were no errors, no Traceback the only problem is the unresponsive thing.
Also sorry im really bad at explaining things :/
There are a few things going on, but the main one is that you have you click variable set only once when the program starts.
Move click = pygame.mouse.get_pressed() inside of the main loop.
If you do that and it still hangs, you should show the rest of your code.
Also, the full code is not there, so I can't be 100% sure, but I don't think that break should be there.
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.