Ask for any more information if needed!
I am using vscode's ide and python 3. When I run script.py the display will pop-up, but a half of a second later the display will disappear and will give this error pyagame.error: Video not intiialized.
import pygame
pygame.init()
run = True
while run:
screen = pygame.display.set_mode([500, 500])
pygame.display.set_caption("TicTac")
pygame.quit()
x = 250
y = 250
width = 40
height = 60
vol = 5
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT():
run = False
run = False
pygame.QUIT()
You do pygame.quit() immediately after pygame.display.set_mode(). pygame.quit() terminates all pygame modules. Remove it:
screen = pygame.display.set_mode([500, 500])
pygame.display.set_caption("TicTac")
# pygame.quit() <--- DELETE
pygame.QUIT is not a function, it is an enumerator constant. You can't invoke pygame.QUIT:
if event.type == pygame.QUIT():
if event.type == pygame.QUIT:
You need just one application loop, not 2 of them. Furthermore you have to update the window by either pygame.display.flip() or pygame.display.update()
import pygame
pygame.init()
screen = pygame.display.set_mode([500, 500])
pygame.display.set_caption("TicTac")
x, y = 250, 250
width, height = 40, 60
vol = 5
run = True
while run:
pygame.time.delay(100)
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# clear dispaly
screen.fill((0, 0, 0))
# draw the scene
pygame.draw.rect(screen, (255, 0, 0), (x, y, width, height))
# update display
pygame.display.flip()
pygame.quit()
Related
I am trying to make a clickable image that exits pygame, but im not sure how to make it close. I have tried using the pygame.quit() and sys.exit() but that loads for a second but doesn't do anyhting. I will show the code I have here(the only relevant code is the x and y variables nad the exit button down the bottom):
import pygame, sys
clock = pygame.time.Clock()
from pygame.locals import *
pygame.init() # inititates Pygame
pygame.display.set_caption('Lightmind')
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) # initiates the window
logo = pygame.image.load("GameLogo.png").convert()
logo = pygame.transform.scale(logo, (256, 256))
start_button = pygame.image.load("StartButton.png").convert()
start_button = pygame.transform.scale(start_button, (256, 256))
exit_button = pygame.image.load("ExitButton.png").convert()
exit_button = pygame.transform.scale(exit_button, (256, 100))
x_2 = 560
y_2 = 400
fade_in = True
fade_out = True
fade_in_ball = True
fade_in_start = True
fade_in_exit = True
running = True
while running: # game loop
for event in pygame.event.get():
if event.type == QUIT:
running = False
pygame.quit()
sys.exit()
# fade in the logo
if fade_in == True:
for i in range(255):
screen.fill((0,0,0))
logo.set_alpha(i)
screen.blit(logo, (560,260))
pygame.display.flip()
clock.tick(60)
fade_in = False
# fade out the logo
if fade_out == True:
for i in range(255):
screen.fill((0,0,0))
logo.set_alpha(255-i)
screen.blit(logo, (560,260))
pygame.display.flip()
clock.tick(60)
fade_out = False
# fade in the start button
if fade_in_start == True:
for i in range(255):
start_button.set_alpha(i)
screen.blit(start_button, (560, 240))
pygame.display.flip()
clock.tick(60)
fade_in_start = False
# fade in the exit button
if fade_in_exit == True:
for i in range(255):
exit_button.set_alpha(i)
screen.blit(exit_button, (x_2, y_2))
pygame.display.flip()
clock.tick(60)
fade_in_exit = False
# make exit button exit game
if event.type == pygame.MOUSEBUTTONDOWN:
x_2, y_2 = event.pos
if exit_button.get_rect().collidepoint(x_2, y_2):
pygame.quit()
sys.exit()
pygame.display.update()
Any help is appreciated!
You're checking the event outside of your event loop. Move it up instead:
for event in pygame.event.get():
if event.type == QUIT:
running = False
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
x_2, y_2 = event.pos
if exit_button.get_rect().collidepoint(x_2, y_2):
pygame.quit()
pygame.event.get() get all the messages and remove them from the queue. See the documentation:
This will get all the messages and remove them from the queue. [...]
If pygame.event.get() is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed.
You must handle the click detection in the event loop.
pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, but it returns a rectangle that always starts at (0, 0) since a Surface object has no position.
The Surface is placed at a position on the display with the blit function.
You've to set the location of the rectangle, either by a keyword argument, e.g:
running = True
while running: # game loop
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
extit_button_rect = exit_button.get_rect(topleft = (x_2, y_2))
if extit_button_rect.collidepoint(event.pos):
running = False
# [...]
pygame.quit()
sys.exit()
Well, I was happy starting on Pygame with a bit of knowledge on python, But while i was following some starter tutorials i noticed that, At the moment of running my code the Pygame window didn't see to respond, so i put some "print" commands to see how far did it get, I noticed that it stopped on the Loop, Any ideas on how i can fix it? I will leave the code around here
import pygame
pygame.init()
print("First Fase")
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("Test")
print("Second Fase")
x = 50
y =50
width = 40
height = 60
vel = 7
print("Third Fase")
done = False
while not done:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == KEYDOWN:
if event.key == K_ESC:
done = True
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
print("NoErrors")
It has to be pygame.KEYDOWN and pygame.K_ESC rather than KEYDOWN and K_ESC.
But fist of all you have to respect the Indentation. In the following code the loops are not nested:
done = False
while not done:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
The for loop is not in the while, it is a separate loop after the while loop.
You have to format your code like this:
import pygame
pygame.init()
print("First Fase")
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("Test")
print("Second Fase")
x = 50
y =50
width = 40
height = 60
vel = 7
print("Third Fase")
done = False
while not done:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESC:
done = True
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
print("NoErrors")
I'm relatively new to python and very new to pygame. I'm trying to use pygame. All programs seem to work fine, except when I try to quit. The window freezes ("application not responding") and I have to force quit it. I'm using OSX, python 3.6, and running it through sublime text if that matters. Code is below:
import pygame
done = False
size = (400,400)
screen = pygame.display.set_mode(size)
while done==False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
done = True
pygame.display.quit()
pygame.quit()
Thanks for your help!
Try this one, it works for me:
import sys
import pygame
size = (400,400)
screen = pygame.display.set_mode(size)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
you can just do this:
import pygame
done = False
size = (400,400)
screen = pygame.display.set_mode(size)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
this is what i do
or you can use this:
import pygame
pygame.init()
running = True
width, heigth = 800, 600
size = (width, heigth)
screen = pygame.display.set_mode(size)
while running:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
don't use pygame.display.quit()
import pygame
pygame.init()
x = height,width = (800,600)
Display = pygame.display.set_mode(x)
pygame.display.set_caption("Blocky")
red = (157, 139, 215)
black = (0,0,0)
Display.fill(red)
pygame.draw.rect(Display,black,(120,450,600,50))
#It updates every frame
pygame.display.update()
excape = False
while not excape:
for dork in pygame.event.get():
print(dork)
if pygame.event == pygame.QUIT:
pygame.quit()
quit()
Here the print(dork) is working but when i click the exit button of the window it doesn't quit at all..
So how do i both print events and quit the application in 1 while loop?
First of all, you should update the screen in the while not excape loop.
Secondly, set the excape to True if pygame.event is equal to pygame.QUIT.
So your code will look like this:
import pygame, sys
pygame.init()
x = height,width = (800,600)
Display = pygame.display.set_mode(x)
pygame.display.set_caption("Blocky")
red = (157, 139, 215)
black = (0,0,0)
Display.fill(red)
pygame.draw.rect(Display,black,(120,450,600,50))
#It updates every frame
excape = False
while not excape:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
excape = True
pygame.quit()
sys.exit()
pygame.display.update()
You need to cycle through EVERY pygame event and check if the event is a quit.
while not excape:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
excape = True
I want to get a certain loop to run after the mouse has previously been clicked.
I initiated a variable called mouse_clicked to false, and then change it to True after the mouse has been clicked. However, this doesn't seem to get things going afterwards.
Here's my code:
import sys, pygame
size = width, height = 320, 240
screen = pygame.display.set_mode(size)
running = True
mouse_pressed = False
while running:
while mouse_pressed:
rect = pygame.Rect(10, 20, 30, 30)
pygame.draw.rect(screen, (255,0,0), rect)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pressed = True
if event.type == pygame.QUIT:
running = False
sys.exit(0)
Thanks!
omer
EDITED after answering too quickly
move your loop around:
while running:
rect = pygame.Rect(10, 20, 30, 30)
pygame.draw.rect(screen, (255,0,0), rect)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pressed = True
if event.type == pygame.QUIT:
running = False
while mouse_pressed:
# do your stuff
mouse_pressed = False
In your version, the whole loop never starts, since mouse_pressed is initialized to False.
It looks like your second loop is not even starting: you are initiating mouse_pressed as False. Therefore,
while mouse_pressed
will necessarily stop the loop before it ever began.
Hope this helps!