My pygame window closes without any errors right after I run the code
Here's my code:
import pygame
pygame.init() # initialises pygame
win = pygame.display.set_mode((500, 500)) # width, height
pygame.display.set_caption("Project_01")
If anyone can help, thank you in advance :)
See Why is my PyGame application not running at all? Your application will exit right after the window is created. You have to implement an application loop. You must implement an application loop to prevent the window from closing immediately:
import pygame
pygame.init() # initialises pygame
win = pygame.display.set_mode((500, 500)) # width, height
pygame.display.set_caption("Project_01")
clock = pygame.time.Clock()
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
win.fill((0, 0, 0))
# draw scene
# [...]
pygame.display.flip()
pygame.quit()
Related
I am trying to write my first game using Python3 & Pygame in the Pycharm Community IDE. I am trying to make a simple Pong game :-D lol I am a beginner so please help me if you can.
When I run my code, I am getting the following error:
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "/home/TechSlugz/PycharmProjects/Pong-Project/Pong1.py", line 7, in <module>
clock = pygame.time.clock()
AttributeError: module 'pygame.time' has no attribute 'clock'
Process finished with exit code 1
The code is just the basic set up for a game window, check it out
import pygame
import sys
# General Setup
pygame.init()
clock = pygame.time.clock()
# Setting Up The Main Window
screen_width = 1280
screen_height = 960
screen = pygame.display.setmode((screen_width, screen_height))
pygame.display.set_caption("Pong")
while True:
#handling input
for event in pygame.event.get():
if event.type == pygame.quit:
pygame.quit()
sys.exit()
#Updating The Window
pygame.display.flip()
clock.tick(60)
Can you notice anything that I am doing wrong to get this issue? None of my IDEs seem to recognise time as a pygame module but I am pretty damn sure it is...
The clock is a class, so you should use C instead of c.
clock = pygame.time.Clock()
and you have another problem, setmode is not an available function. You meant to write set_mode
The code that works fine for me:
import pygame
import sys
# General Setup
pygame.init()
clock = pygame.time.Clock()
# Setting Up The Main Window
screen_width = 1280
screen_height = 960
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pong")
while True:
#handling input
for event in pygame.event.get():
if event.type == pygame.quit:
pygame.quit()
sys.exit()
#Updating The Window
pygame.display.flip()
clock.tick(60)
This question already has answers here:
What is the difference between .quit and .QUIT in pygame
(2 answers)
pygame.error: video system not initialized
(5 answers)
Closed 1 year ago.
I'm trying to do one of the projects in the Python Crash Course book: Alien Invasion Chapter 12. I just started and for some reason the error: pygame.error: video system not initialized keeps popping up. I'm pretty sure I followed the directions appropriately so I don't know what I have possibly done wrong...?
import sys
import pygame
from settings import Settings
def run_game():
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption('Alien Invasion')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
screen.fill(ai_settings.bg_color)
pygame.quit()
sys.exit()
pygame.display.flip()
run_game()
The error occurs because pygame.event.get is called before you initialize the display with pygame.display.set_mode.
It's very important in Python to indent your code correctly. Your program should most likely look similar to this version:
import sys
import pygame
from settings import Settings
def run_game():
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption('Alien Invasion')
# This block should be in the `run_game` function.
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Fill the screen in the while loop not in the event
# loop. It makes no sense to fill the screen only when
# the user quits.
screen.fill(ai_settings.bg_color)
pygame.display.flip()
run_game()
I am learning pygame and have been working on examples given in 'Python Crash Course' book by Eric Matthews (https://g.co/kgs/WP5fay). I have installed pygame version 1.9.3 on macOS High Sierra.
When I run the following program, a window opens and when I click 'X' to close it, the window freezes and the curses keeps circling and I get a python not responding error in activity monitor. I have tried a number of option to fix the problem but it is not going away.
import pygame
import sys
def run_game():
pygame.init()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Alien Invasion')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
run_game() #when I run this function, a window opens and freezes a couple of seconds after
You have to terminate the application loop and uninitialize all pygame modules with pygame.quit():
def run_game():
pygame.init()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Alien Invasion')
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.flip()
pygame.quit()
sys.exit()
I am new to Python programming and I recently started working with the PyGame module. Here is a simple piece of code to initialize a display screen. My question is : Currently, the maximize button is disabled and I cannot resize the screen. How do I enable it to switch between full screen and back?
Thanks
import pygame, sys
from pygame.locals import *
pygame.init()
#Create a displace surface object
DISPLAYSURF = pygame.display.set_mode((400, 300))
mainLoop = True
while mainLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainLoop = False
pygame.display.update()
pygame.quit()
To become fullscreen at native resolution, do
DISPLAYSURF = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
To make the window resizeable, add the pygame.RESIZABLE parameter when seting the mode. You can set the mode of the screen surface multiple times but you might have to do pygame.display.quit() followed by pygame.display.init()
You should also check the pygame documentation here http://www.pygame.org/docs/ref/display.html#pygame.display.set_mode
The method you are looking for is pygame.display.toggle_fullscreen
Or, as the guide recommends in most situations, calling pygame.display.set_mode() with the FULLSCREEN tag.
In your case this would look like
DISPLAYSURF = pygame.display.set_mode((400, 300), pygame.FULLSCREEN)
(Please use the pygame.FULLSCREEN instead of just FULLSCREEN because upon testing with my own system FULLSCREEN just maximized the window without fitting the resolution while pygame.FULLSCREEN fit my resolution as well as maximizing.)
import pygame, os
os.environ['SDL_VIDEO_CENTERED'] = '1' # You have to call this before pygame.init()
pygame.init()
info = pygame.display.Info() # You have to call this before pygame.display.set_mode()
screen_width,screen_height = info.current_w,info.current_h
These are the dimensions of your screen/monitor. You can use these or reduce them to exclude borders and title bar:
window_width,window_height = screen_width-10,screen_height-50
window = pygame.display.set_mode((window_width,window_height))
pygame.display.update()
This will let you toggle from maximize to the initial size
import pygame, sys
from pygame.locals import *
pygame.init()
#Create a displace surface object
#Below line will let you toggle from maximize to the initial size
DISPLAYSURF = pygame.display.set_mode((400, 300), RESIZABLE)
mainLoop = True
while mainLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainLoop = False
pygame.display.update()
pygame.quit()
You'd have to add the full screen parameter onto the display declaration like this:
import pygame, sys
from pygame.locals import *
pygame.init()
#Create a displace surface object
DISPLAYSURF = pygame.display.set_mode((400, 300), FULLSCREEN)
mainLoop = True
while mainLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainLoop = False
pygame.display.update()
pygame.quit()
Just use pygame.RESIZABLE as an easier option.
When i run the code it play only the .wav file
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((500, 400), 0, 32)
pygame.mixer.music.load('background.ogg')
pygame.mixer.music.play()
soundObj = pygame.mixer.Sound('bird.wav')
soundObj.play()
pygame.mixer.music.stop()
someone know what i should do for the background.ogg to be played too?
Remove this line:
pygame.mixer.music.stop()
Basically, you're loading and playing the background noise, and then immediately stopping it. I also suggest you create a main loop, like so:
import pygame
from pygame.locals import *
# This makes sure that you're not importing the module.
if __name__ == "__main__":
# Create surface
size = width, height = (500, 400)
# You don't need the extra arguments
window = pygame.display.set_mode(size)
# Do sound stuff
pygame.mixer.music.load('background.ogg')
pygame.mixer.music.play()
soundObj = pygame.mixer.Sound('bird.wav')
soundObj.play()
# This is your main loop.
running = True
while running:
# Check if escape was pressed
Most of the time people just check if Escape was pressed in their main loop (if it was pressed, they set running = False and exit the program).