Pygame window opens and the closes immediately.
I have copied the code from a youtube channel and it still opens and closes immediately
import pygame
pygame.init()
class Game():
def __init__(self):
self.width = 800
self.height = 600
self.win = pygame.display.set_mode((self.width, self.height))
def run(self):
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
Game = Game()
The while loop should be waiting for me to quit but its auto executing the quit function
You need to call your run method as follows:
game = Game()
game.run()
By calling using game = Game() and then game.run(), the code should work.
Related
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()
simple code but still not able to figure out where things are going wrong,
it says module pygame has no member QUIT,quit
import pygame
WIDTH = 900
HEIGHT = 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run=False
pygame.quit()
if __name__=="__main__":
main()
Since you already imported pygame you can just do "QUIT" instead of "pygame.QUIT"
So I started programming a game for a school project and currently face a problem with event.type KEYDOWN.
This is my Code. When running it a window opens but it stays completely black without showing either the background color or the sprite. I tried pinpointing the issue with the print() commands to see in what line exactly the code stops working. Every, except the last print command is executed, which leaves me to believe the While Loop is the cause of my problem.
import pygame
from pygame.locals import *
print("Import complete")
WIDTH = 640
HEIGHT = 480
TITLE = "Tales of Tesbold"
print("Window complete")
Tesbold = Actor("tesbold.png") #Sprites vordefinition
Tesbold.x = 200
Tesbold.y = 100
print("Tesbold complete")
wechsel = True
def draw(): #Hintergrund und alle Sprites
screen.clear()
screen.fill((200,200,200))
Tesbold.draw()
print("Draw Screen complete")
while True:
for event in pygame.event.get():
if event.type == quit:
sys.exit()
if event.type == KEYDOWN:
if(event.key == K_RIGHT):
Tesbold.x += 10
print("While Loop complete")
This is the console output:
pygame 2.0.1 (SDL 2.0.14, Python 3.8.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
Import complete
Window complete
Tesbold complete
Draw Screen complete
---------- FINISHED ----------
exit code: 2 status: 0
Can someone tell me what I did wrong for it to not function anymore?
Python is case sensitive. The event type enumerators are all written in capital letters. See pygame.event module:
if event.type == quit:
if event.type == QUIT:
pygame.quit() is a function and uninitialize all pygame modules.
I use device files to read entries with the open() and f.read() functions, but the problem is that read() doesn't stop until there is a new entry. So I can't let my program do anything else...
running = True
with open("/dev/input/js0", "rb") as f:
while running:
event = f.read(8)
pygame.display.flip()
clock.tick(30)
pygame.quit()
For example, here I can only draw the window if the device is used.
Thanks for reading.
One option is to use Python's select.poll method, which lets you check to see if a file descriptor has any input available. We could rewrite your code something like this:
import select
import pygame
pygame.init()
pygame.display.init()
pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
running = True
poll = select.poll()
with open("/dev/input/js0", "rb") as f:
poll.register(f, select.POLLIN)
while running:
events = poll.poll(0)
if events:
event = f.read(8)
print('FLIP!')
pygame.display.flip()
clock.tick(30)
pygame.quit()
For more reading, look for articles on "non blocking io" with Python.
Alternately, you could use Pygame's event handling code rather than reading directly from /dev/input/js0 yourself. For example:
import pygame
pygame.init()
pygame.joystick.init()
pygame.display.init()
pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
js = pygame.joystick.Joystick(0)
js.init()
running = True
while running:
event = pygame.event.poll()
if event.type == pygame.QUIT:
break
if event.type == pygame.JOYHATMOTION:
print('FLIP!')
pygame.display.flip()
clock.tick(30)
pygame.quit()
The above code will only react to hat pressed, but you could easily extend it to listen to axis and button actions as well.
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()