PROBLEM:
if my mouse cursor is outside the window my game runs but if i have my cursor inside console gives me this error
Traceback (most recent call last):
File "c:/Users/jackw/Desktop/New folder/main.py", line 36, in <module>
if event.type == pg.QUIT():
TypeError: 'int' object is not callable
heres my code
import pygame as pg
from Config import *
from bin import *
# initialising pygame
pg.init()
class Game():
def background(self,background):
window.blit(background, (0,0))
# defining classes for use
g = Game()
# game loop
while isrunning:
# making sure the game is running on a constant clock
time.tick(fps)
# add background
g.background(gameback)
# setting up events
for event in pg.event.get():
# closing window event
if event.type == pg.QUIT():
isrunning = False
# input events
# show finished frame
pg.display.flip()
# Last code before closing the window
# closing the window
pg.quit()
most variables are defined in different files
config file
gamevars file
this program works fine on macOS I'm only getting this error on windows 10.
here's a video of the bug
QUIT is not a method or function, it is an enumerator constant, which specifies the type of the event (See pygame.event.Event()).
Remove the brackets to solve the issue:
if event.type == pg.QUIT():
if event.type == pg.QUIT:
pg.QUIT is an enumeration value. It's basically an integer. Your code added parentheses for some reason; this is invalid syntax. Use merely
if event.type == pg.QUIT:
What you coded is vaguely like
if event.type == 4():
Related
I'm new to pygame. I have written the following code, but the generated window doesn't allow me to close it.
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Looping and the game running are two different things. If you want to close the game after the loop ends, you should do so with pygame.quit(). Importing sys and adding sys.exit() as afterwards lets you exit the Python script altogether. Depending on which IDE you use, this may not happen automatically.
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((800,600))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
Like the title says I am confused why I get the error: pygame.error: video system not initialized
As far as i understand this error is raised if you forget to initialize your code with pygame.init() but I did, here's my code and thanks in advance:
import pygame
from pygame.locals import *
pygame.init()
screen_height = 700
screen_width = 1000
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("platformer")
# load images
sun_img = pygame.image.load("img/sun.png")
backround_img = pygame.image.load("img/sky.png")
run = True
while run:
screen.blit (backround_img,(0,0))
screen.blit(sun_img, (100, 50))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
pygame.display.update()
the error does not crash the window or anything and it works as intended
its just somewhat annoying.
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
pygame.display.update()
When the event loop happens, if you close the window, you call pygame.quit(), which quits pygame. Then it tries to do:
pygame.display.update()
But pygame has quit, which is why you are getting the message.
separate the logic out. Events in one function or method, updating in another, and drawing and updating the display in another to avoid this.
This question already has answers here:
pygame window closes immediatly after opening up
(1 answer)
What is the difference between .quit and .QUIT in pygame
(2 answers)
Closed 1 year ago.
Having researched this for hours, I cannot figure out why this error is being triggered. Here is the entire message:
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "snake.py", line 37, in <module>
redraw_window()
File "snake.py", line 23, in redraw_window
win.fill((0, 0, 0))
pygame.error: display Surface quit
When I run the program, the window opens and closes instantly. I'm running Python v3.7 via a conda virtual environment. And here is my code:
import pygame
pygame.init()
#----------------------------
# CONSTANTS
#----------------------------
window_width = 256
window_height = 256
win = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('Snake Game')
#----------------------------
# CLASSES
#----------------------------
#----------------------------
# REDRAW WINDOW
#----------------------------
def redraw_window():
win.fill((0, 0, 0))
pygame.display.update()
#----------------------------
# MAIN GAME LOOP
#----------------------------
running = True
while running:
# listen for window closure
for event in pygame.event.get():
if event.type == pygame.quit():
run = False
redraw_window()
pygame.quit()
I even tried passing 'win' into the redraw_window function and that changed nothing.
pygame.quit() is a function and uninitialize all PyGame modules. When you do
if event.type == pygame.quit():
the function is called and all PyGame modules are uninitialized.
The type attribute of pygame.event.Event() object indicates the type of event. You need to compare the event type to the enumeration constant that identifies the event. The quit event is identified by pygame.QUIT (see pygame.event module):
Hence, you have to compete with pygame.QUIT instead of pygame.quit():
running = True
while running:
# listen for window closure
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
redraw_window()
pygame.quit()
I have only today started using pygame, and am just getting familiar with how to use it. I want to start out by simply creating a blank window. I have attempted using the code below:
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
while not done:
for event in pygame.display.event.get():
if event.type == pygame.QUIT():
done = True
pygame.display.flip()
However, the window appears for only a second before disappearing, followed by the error message:
line 8, in
for event in pygame.display.event.get():
AttributeError: module 'pygame.display' has no attribute 'event'
Can anyone help?
Thanks
The event queue is accessed via the function pygame.event.get(). You are trying to use the pygame display. Also QUIT is not a function.
Please try:
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
while not done:
for event in pygame.event.get(): # <-- HERE
if event.type == pygame.QUIT: # <-- AND HERE
done = True
pygame.display.flip()
The documentation shows event as an attribute of the pygame module. I think you should change the line to pygame.event.get().
This makes sense given the error.
After running this code, I got an error:
pygame.error: video system not initialized
My code:
import sys
import pygame
def run_game():
# Initialize game and create a screen object.
pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Alien Invasion")
# Start the main loop for the game.
while True:
# Watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Make the most recently drawn screen visible.
pygame.display.flip()
run_game()
Can anyone help me and explain what this error means and how to rectify it?
The error is raised because pygame.event.get is called without an initialized display (pygame.display.set_mode). The problem is that your while loop is not indented correctly, so it is executed before the run_game function is called. The loop should be inside of the run_game function.
import sys
import pygame
def run_game():
# Initialize game and create a screen object.
pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Alien Invasion")
# Start the main loop for the game.
while True:
# Watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Make the most recently drawn screen visible.
pygame.display.flip()
run_game()
put your code:
run_game()
before while statment`