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.
Related
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()
I cannot find, what is wrong with background, even with debugger it is not doing nothing
#importing
from turtle import width
import pygame
#resolution
pygame.display.set_caption("Tanks")
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
#background color (my problem)
def draw_window():
WIN.fill((255, 0, 0))
pygame.display.update()
#main functions
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
#game start
if __name__ == "__main__":
main()
You have to call draw_window in the application loop:
#importing
import pygame
#resolution
pygame.display.set_caption("Tanks")
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
#background color (my problem)
def draw_window():
WIN.fill((255, 0, 0))
pygame.display.update()
#main functions
def main():
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
draw_window()
pygame.quit()
#game start
if __name__ == "__main__":
main()
The typical PyGame application loop has to:
limit the frames per second to limit CPU usage with pygame.time.Clock.tick
handle the events by calling 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 (blit all the objects)
update the display by calling either pygame.display.update() or pygame.display.flip()
When you write a function, it is just some instructions on memory, it doesn't run unless you call it. This is the case. You write a set of instructions, draw_window, but you never call it. What you need to do is before the game loop, call draw_window. So the code would be something like this.
#importing
from turtle import width
import pygame
#resolution
pygame.display.set_caption("Tanks")
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
#background color (my problem)
def draw_window():
WIN.fill((255, 0, 0))
pygame.display.update()
#main functions
def main():
run = True
while run:
draw_window()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
#game start :)
if __name__ == "__main__":
main()
This way the result will be a red window which is rgb(0, 0).
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.
I'm creating a simple pygame program (PyGame 1.9.6 on Python 3.7), but some of the code inside my while loop doesn't seem to work. When I run the program, the window opens, but the screen doesn't fill with black, nor does the window close when I press "x"
import pygame
# pygame setup
pygame.init()
# Open a window on the screen
width, height = 600, 600
screen = pygame.display.set_mode((width, height))
def main():
running = True
clock = pygame.time.Clock()
BLACK = (0,0,0)
while running:
clock.tick(5) # number of loops per second
print("tick")
screen.fill(BLACK)
for event in pygame.event.get():
print("event detected")
if event == pygame.QUIT:
running = False
pygame.display.update()
main()
In the console, "tick" appears like normal and "event detected" appears after pressing any keys or mouse click. I don't get any errors when I run it.
If event == pygame.QUIT: should be if event.type == pygame.QUIT:
I usually use that event like this:
if event.type == pygame.QUIT:
pygame.quit()
quit()
Just as #Telan said
if event==pygame.QUIT:
should be
if event.type==pygame.QUIT:
However to properly shutdown pygame, pygame.quit() is important to shutdown pygame modules which is the reverse of pygame.init()
While sys.exit() is used to properly shutdown the main python program.
from sys import exit
import pygame
if event.type == pygame.QUIT:
pygame.quit()
exit()
Full code is shown below. Enjoy!
import pygame
from sys import exit
# pygame setup
pygame.init()
# Open a window on the screen
width, height = 600, 600
screen = pygame.display.set_mode((width, height))
def main():
clock = pygame.time.Clock()
BLACK = (0, 0, 0)
while True:
clock.tick(5) # number of loops per second
print("tick")
screen.fill(BLACK)
for event in pygame.event.get():
print("event detected")
if event.type == pygame.QUIT:
pygame.quit()
exit()
pygame.display.update()
if __name__ == "__main__":
main()
I'm completely new to Python and I just started learning Pygame. I'm trying to work with drawing shapes right now, but I can't get the window to open. Idk what the problem is, here's my code:
`#import libraries
import pygame
import math
#init pygame
pygame.init()
#clock
clock = pygame.time.Clock()
#window screen
screen_height_x = 500
screen_height_y = 400
screen = pygame.display.set_mode((screen_height_x, screen_height_y))
#RGBA colors
Black = (0, 0, 0)
White = (255, 255, 255)
Blue = (0, 0, 255)
Red = (255, 0, 0)
Green = (0, 255, 0)
#program
running = True
while running:
for event in pygame.event.get():
running = False
screen.fill(Black)
pygame.display.update()
clock.tick(30)
#Quit
pygame.quit()
quit()`
You're immediately quitting the main loop and you also aren't drawing anything. Try this instead:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(Black)
pygame.display.update()
clock.tick(30)
#Quit
pygame.quit()
quit()
Disclaimer: I cannot try this out at the moment but it should get you started in the right direction.
Is this a class part of the projet or the main class ? If it is the main class, you it should contain a main function.
If this is the main function, you should declare it by adding def main(): before the pygame.init()
And add the module to exectute this as the main script under Quit
if __name__ == "__main__":
main()
Sorry, im french my english is not the best aha !