Pygame window closing instantly with the while run loop [duplicate] - python

This question already has answers here:
What does if __name__ == "__main__": do?
(45 answers)
Closed 6 months ago.
I am watching a tutorial on pygame (made by Tech With Tim) and after following all the stuff with the run variable and the while run function, my window is still closing immediately. I have searched stackoverflow and the pygame forums but they all say stuff similar that doesn't work.
import pygame
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First game")
BACKGROUND = (255, 255, 255)
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
WIN.fill(BACKGROUND)
pygame.display.update()
pygame.quit()
if __name__ == "__name__":
main()

Replace
if __name__ == "__name__":
with
if __name__ == "__main__":

Related

Error: video system not initialized when using pygame

I keep having the error whenever I try to run this code:
import pygame
pygame.init()
print(pygame.display.Info())
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")
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()
I've tried adding pygame.init() but it didn't help.
You have a couple of problems here. The immediate cause of the error is that your call to pygame.quit() isn't inside the main function, so it gets called immediately when the program starts. You need to fix that:
import pygame
pygame.init()
print(pygame.display.Info())
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")
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()
This will allow your program to run...but it's never going to quit. You're comparing event.type == pygame.quit, but pygame.quit is a function; this comparison will always be False. You need to compare it to pygame.QUIT, which is an integer value that will match event.type as expected:
import pygame
pygame.init()
print(pygame.display.Info())
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")
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()

How to draw a player sprite on the screen [duplicate]

This question already has answers here:
How to draw images and sprites in pygame?
(4 answers)
Closed 7 days ago.
The code sample below does not work as intended.
import pygame as pg
import time as t
import math as m
import random as r
import os # this efficiently imports the necessary modules
pg.init()
screen = pg.display.set_mode((500, 500))
pg.display.set_caption("bullet hell")
clock = pg.time.Clock()
clock.tick(60)
player = pg.image.load("player.png")
def loopkill():
if event.type == pg.QUIT:
running = False
def loop(): # this is the mainloop for pygame
running = True
while running:
for event in pg.event.get():
t.sleep(100)
if event.type == pg.QUIT:
running = False
if event.type == pg.KEYDOWN:
print("ready")
pg.surface.blit(player, (250, 250))
g.draw(screen)
g.update()
pg.display.flip()
# Quit Pygame
pg.quit()
loop()
Expected a player sprite, however got a blank screen instead.
I'm suspicious of the t.sleep(100) that would make your program stop for 100 seconds every time there is an event. Also, the functions which draw the screen are within the event loop, so they are only called when there are events.

pygame instantly quits window

Why is the window instantly closing?
I am trying to follow this tutorial so far everything is a coppy but it doesnt work
https://www.youtube.com/watch?v=jO6qQDNa2UY
there is lines under
pygame.QUIT():
run = False
import pygame
WIDTH, HEIGHT = 900, 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()
Not a python guy, but I think the last line should be
if __name__ == "__main__":
main()
BlackBeans is on the correct track about the typo in your "if" statement. Also, though, there needs to be a "pygame.init()" call at the beginning of the program as well. Plus "pygame.QUIT()" actually should be "pygame.QUIT". With those bits of information, the following code snippet presents a black screen until the window is closed.
import pygame
pygame.init() # This is needed
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
def main():
print("Entered main")
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT: # Omit the "()"
print("Quitting")
run = False
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()
Give that a go to see if you progress further in your game development.

Pygame window closes right after

import pygame
WIDTH, HEIGHT = 1100, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game")
WHITE = (255, 255, 255)
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
WIN.fill(WHITE)
pygame.display.update()
pygame.quit()
if __name__ == "__GAME!__":
main()
When I run this code, the pygame window closes right after while using Virtual Studio Code. While using Jupyter notebook it runs but goes unresponsive after 2-3 seconds. Both of them do not display any errors though.
I'm not sure why you're using
if __name__ == "__GAME!__"
But if I change this to
if __name__ == "__main__"
Then it works.

clearing the screen with a key press in pygame [duplicate]

This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Pygame level/menu states
(2 answers)
Closed 2 years ago.
this code is supposed to go to a different function which clears the screen when you hit z, and it does do that, but whenever I let go of z it switches back to the title function and redraws everything.
I'm trying to use it as a "hit this key to go to a new window" type thing and the screenfill is just a fill-in for now, so you can see why that's a problem. for the record I'm using python 3.6.
I've also already tried putting global start in the top of the title function, but then the z key does nothing. please and thanks!
import pygame
from pygame.locals import *
import random
pygame.init()
LOGO = pygame.image.load("kelogo.png")
savannah = pygame.image.load("savannah.png")
hitkey = pygame.image.load("hitkey.png")
display_width = 800
display_height = 600
white = (255, 255, 255)
game_display = pygame.display.set_mode((display_width, display_height))
start = False
def main_game():
while start:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
game_display.fill(white)
pygame.display.update()
def title():
intro = False
while not intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
game_display.blit(savannah, ((0),(0)))
game_display.blit(LOGO, ((0),(100)))
game_display.blit(hitkey, ((130),(100)))
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_z:
start = True
main_game()
pygame.display.update()
title()
main_game()
pygame.quit()
quit()

Categories

Resources