Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
import sys
import pygame
def run_game():
pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Alien game")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
run_game()
I’m starting playing with the pygame module and I want to display a window but when I run the program I face this problem pygame error: video system not initalized even I have put pygame.init() in a function and I call it later in the program, I have tried to put pygame.init() after the importing statements but I face another problem in pygame.display.flip(), the error say pygame.error: Display mode not set
it's due to indentations.
the main loop isn't in your run_game function, but in the main part of the code, because it miss an indent.
All of your while loop need 1 more indent.
Here you start your infinite loop before calling run_game, and so without initializing pygame.
Here is the correct code
import sys
import pygame
def run_game():
pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Alien game")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
run_game()
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
So,This was my code,i was coding normally by watching a tutorial but suddenly when i used the fill attribution, an error popped up saying the following :
line 15, in display.fill((25, 25, 29))
AttributeError: 'NoneType' object has no attribute 'fill'
And under is the code,that i wrote,if anybody willingly helped me then,i would be very happy!
Under Bellow is my code
import pygame
pygame.init()
pygame.display.set_mode((800, 600))
display = pygame.display.set_caption("Space Invaders!")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
display.fill((25, 25, 29))
pygame.display.update()
While I haven't got pygame so I can't test the code, I strongly suspect your issue is related to these three lines and how they relate to each other:
pygame.display.set_mode((800, 600))
display = pygame.display.set_caption("Space Invaders!")
display.fill((25, 25, 29))
You have set the display mode and now you want to fill it. However, you haven't actually assigned the output of display.set_mode() to display, you've assigned the output of display.set_caption() - which, as someone else has already commented, is nothing as display.set_caption() doesn't return a value.
So, when you try to use display, it doesn't contain anything.
Consider trying the following code instead (though I don't know if the order is important):
display = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Space Invaders!")
I suspect that pygame failed to initialize. This propogated to:
display = pygame.display.set_caption("Space Invaders!")
Returning 'NoneType' object which finally fails when you run:
display.fill((25, 25, 29))
use a breakpoint at "display =..." to see the return value.
after looking further....it's syntax/formatting related. Here are my corrections to get it going:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
display = pygame.display.set_caption("Space Invaders!")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((25, 25, 29))
pygame.display.update()
This question already has answers here:
Pygame window not responding after a few seconds
(3 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 2 years ago.
MacOSX 10.15.2, Python 3.8 and Pygame 2.0.0.
Hello! I am currently trying to make a dark purple (87,61,122) background in Pygame, but it only appears as a black background, seemingly frozen and loading forever.
Here is my code:
import pygame
bgcolor = [87,61,122]
pygame.init()
screen = pygame.display.set_mode((1600,900))
screen.fill(bgcolor)
pygame.display.flip
Is there anything wrong with the code or is Pygame just refusing to co-operate?
pygame.display.flip is not a function call. You missed the parentheses.
However, you have to handle the events in the application loop. See pygame.event.get() respectively pygame.event.pump():
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system. This will update the contents of the entire display.
The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (draw all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
import pygame
bgcolor = [87,61,122]
pygame.init()
screen = pygame.display.set_mode((1600,900))
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
# handle events
for event in pygame.event.get():
if event.type == QUIT:
run = False
# clear display
screen.fill(bgcolor)
# draw scene
# [...]
# update display
pygame.display.flip()
pygame.quit()
exit()
change pygame.display.flip by pygame.display.flip()
or pygame.display.update()
not that without IDLE, this script will display a window and directly destroy it.
To add like loop:
import pygame
bgcolor = [87,61,122]
pygame.init()
screen = pygame.display.set_mode((1600,900))
loop = True
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False #Quiting
screen.fill(bgcolor)
pygame.display.update()
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
import pygame
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 600))
running = True
while running:
for event pygame.event.get():
if event.type == pygame.QUIT:
running = False
you are missing an "in" in your for loop
import pygame
pygame.init()
pygame.display.set_mode((800, 600))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
also, check out the Stackoverflow guide in order to use proper font for your code, have a great day
I wanted to make a game using pygame and I started my code making the pygame window
import pygame
pygame.init()
pygame.display.set_mode((1500, 1000))
But every time I run the program, my pygame window does not respond.
Does anyone know what is happening, this did happen to me on previous codes too but it wasn't all the time like it is now
you need a game loop to start the game and you need events to work on.
for python code i suggest:
import pygame
pygame.init()
pygame.display.set_mode((1500, 1000))
start_game = True
while start_game:
print("Game Started!")
and for the events you can use this code in the while loop:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
print("mouse button up pressed")
the source of this code is : RipTutorial
I assume that by 'my pygame window does not respond' you mean that you believe the window is not displaying. I think that you are just missing it because it is closing too quickly.
The window does display, but because your program ends right after you create the window, the window is immediately closed again. This is likely just happening so quickly that you are not noticing it. If you want to see this, then just put a call to sleep after opening the window and you will see the window sit there until the sleep expires and then it will close.
If you do not mean that and you are seeing the window but expecting it to respond, then I do not understand what you expect it to respond to. You do not have any code that tries to make it respond to anything.
EDIT:
I just noticed that #AnassABEA suggests that in a comment under his answer, though not in his answer. #AnassABEA you should update your answer to include this since I believe that is the actual answer to his question, not the missing event loop.
Add this below to your code:
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
Lines 4 to 5 of the code are for closing pygame when the window is closed.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I've been trying to figure this out all day without success:
How do you set up a playlist of songs to play in pygame? The queue command doesn't seem to work for me. All that happens is that the first song I load will play all the way through and then the second won't start. The pygame documentation says this is all I need. Am I making an obvious mistake?
import pygame
pygame.init()
pygame.mixer.init
screen=pygame.display.set_mode((800,450))
pygame.mixer.music.load('mimages\\sounds\\droll.wav')
pygame.mixer.music.play(0,0.0)
pygame.mixer.music.queue('mimages\\sounds\\hip2014.wav')
running=True
clock=pygame.time.Clock()
while running:
clock.tick(2)
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
holdon=False
if event.type==pygame.KEYDOWN and event.key==pygame.K_ESCAPE:
running=False
holdon=False
print pygame.mixer.music.get_busy()
The queue () function in the Pygame module does not work correctly.
I created a function that works without queue () function.
I don't know why it doesn't work. :)
But luckily there's a code that works.
try it:
import pygame
import time
from tkinter import *
root = Tk()
c = ["music1.mp3","music2.mp3","music3.mp3"] #you_can_add_more
x= 0
def music():
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(c[x])
pygame.mixer.music.play(0)
que()
def que():
global x, c
pos = pygame.mixer.music.get_pos()
if int(pos) == -1:
x += 1
pygame.mixer.music.load(c[x])
pygame.mixer.music.play(0)
root.after(1, que)
music()
root.mainloop()
Try this instead:
pygame.mixer.music.load('mimages\\sounds\\droll.wav')
pygame.mixer.music.queue('mimages\\sounds\\hip2014.wav')
pygame.mixer.music.play()
This allows you to build up the queue, then play through it all at once. If you want to play different files later, try running pygame.mixer.music.stop() (if the queue hasn't finished yet) then use load() (optionally followed by one or more queue() statements) then play() again.