I recently started learning pygame but I've had an error which shows pygame not responding after some time. It would really help if someone could find my bug! Here's my code:
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Space Invaders")
BLUE = (0, 0, 255)
def player():
pygame.draw.rect(screen,BLUE,(200,150,100,50))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0,0,0))
pygame.display.update()
From my limited knowledge of pygame, it is likely that you set running = True and did a while True: statement, causing too much memory to be used and the script to crash after some time.
You need a proper termination that uses pygame.quit(), followed by sys.exit(). Setting running = False does not terminate the script.
Try this code:
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
sys.exit()
Reference
Try adding a pygame.quit() at the end of your code:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Space Invaders")
BLUE = (0, 0, 255)
def player():
pygame.draw.rect(screen, BLUE, (200, 150, 100, 50))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0,0,0))
pygame.display.update()
pygame.quit()
Related
I was trying to make the display a diferent colour in pygame but the screen just stays black
this is the code i had for it:
import pygame
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("space invaders")
icon = pygame.image.load('ufo(1).png')
pygame.display.set_icon(icon)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 255))
pygame.display.update()
I was following a tutorial on youtube but when i tried to make any colour it just stayed black.
I am following the same tutorial and you have to make sure that your screen.fill and display.udpate command are in your while loop at the right position under the for event like this:
import pygame
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("space invaders")
icon = pygame.image.load('ufo(1).png')
pygame.display.set_icon(icon)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 255))
pygame.display.update()
Output:
The output should give you a blue background like above.
Make sure the screen.fill((0, 0, 255)) and the pygame.display.update() are in the game loop (the while running loop).
import pygame
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 600))
icon = pygame.image.load('ufo(1).png')
pygame.display.set_icon(icon)
pygame.display.set_caption("space invaders")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 255))
pygame.display.update()
Also, if no action or animation is being display that would require the computer to do anything other than just loop through like normal, you can use the pygame.event.wait() method.
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 600))
icon = pygame.image.load('ufo(1).png')
pygame.display.set_icon(icon)
pygame.display.set_caption("space invaders")
running = True
while running:
event = pygame.event.wait()
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 255))
pygame.display.update()
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()
When I run this code the resizeable option is available but when I click on it to maximize the screen, there is this weird white outline that appears. How would I fix it?.
import pygame
pygame.init()
screen = pygame.display.set_mode((900, 500), pygame.RESIZABLE)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
You have to recreate the display Surface in the VIDEORESIZE event (see pygame.event):
import pygame
pygame.init()
screen = pygame.display.set_mode((900, 500), pygame.RESIZABLE)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.VIDEORESIZE:
screen = pygame.display.set_mode(event.size, pygame.RESIZABLE)
pygame.display.update()
very new to python/pygame and was watching some tutorials on YT. https://www.youtube.com/watch?v=P8bx4nits-o <--- This one specifically, and I came across a problem. I was trying to make a white background as in the Video but I couldn't. I don't know where I went wrong.
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption('slither')
white = (255, 255, 255)
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
gameDisplay.fill(white)
pygame.display.update()
pygame.quit()
quit()
[IMG]http://i66.tinypic.com/20swspu.png[/IMG]
If you could help me i'd really appreciate it!
Thank You!
You need indentation to put fill and update inside while loop.
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
gameDisplay.fill(white) # indentation
pygame.display.update() # indentation
This code is not working for me:
import pygame
pygame.init()
red = (255,0,0)
gamedisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption("Snake")
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
gamedisplay.fill(red)
pygame.display.update
pygame.quit()
quit
It doesn't give me a red background.
You are not updating the screen. Fix this line:
pygame.display.update
to
pygame.display.update()