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()
Related
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()
When I run this code it doesn't respond. Any tips?
import pygame
pygame.init()
pygame.font.init()
pygame.font.init()
screen = pygame.display.set_mode([500,500])
white = (255,255,255)
font = pygame.font.SysFont('Comic Sans MS', 30)
text = font.render(input('Choose a charecter:'),False,(0,0,0))
warrior = pygame.image.load("Warrior.png")
goblin = pygame.image.load("Goblin.png")
kk = pygame.image.load("Karate Kid.png")
#main loop
running = True
while running:
screen.fill(white)
screen.blit(text,(255,10))
screen.blit(warrior, (200,100))
screen.blit(goblin, (300,100))
screen.blit(kk, (400,100))
for event in pygame.event.get():
if event.type == pygame.QUIT: pygame.quit()
I have added the code:
"for event in pygame.event.get():
if event.type == pygame.QUIT: pygame.quit()"
but it still is not working.
The input command blocks the program execution, so it crashes the window since events cant be processed. Easy fix is to simply to create the window after getting the input. You also need to update the window using pygame.display.update().
import pygame
pygame.init()
pygame.font.init()
pygame.font.init()
white = (255,255,255)
font = pygame.font.SysFont('Comic Sans MS', 30)
text = font.render(input('Choose a charecter:'),False,(0,0,0))
warrior = pygame.image.load("Warrior.png")
goblin = pygame.image.load("Goblin.png")
kk = pygame.image.load("Karate Kid.png")
screen = pygame.display.set_mode([500,500])
#main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.fill(white)
screen.blit(text,(255,10))
screen.blit(warrior, (200,100))
screen.blit(goblin, (300,100))
screen.blit(kk, (400,100))
pygame.display.update()
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
Hi I'm having some trouble realizing my ideas. I first wanted to draw rectangles with two mouse clicks, but it didn't work properly so i reduced it to this: to draw fixed-size rectangles with one mouse click.
However it still doesn't work/...
import pygame
windowSize = (500,500)
white = (255,255,255)
black = (0,0,0)
pygame.init()
screen = pygame.display.set_mode(windowSize)
running = 1
while running:
screen.fill(white)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
running = 0
THE PROBLEM IS HERE SOMEWHERRE
elif event.type == pygame.MOUSEBUTTONDOWN:
rect = pygame.Rect(event.dict["pos"],(30,50))
pygame.draw.rect(screen,black,rect,1)
pygame.display.flip()
I know there might be a lot of conceptual errors with my code... please help!
You are filling white the entire screen every tick. So after you actually draw the screen become blank again on the next tick. Just move screen.fill(white) out of main cycle:
import pygame
windowSize = (500,500)
white = (255,255,255)
black = (0,0,0)
pygame.init()
screen = pygame.display.set_mode(windowSize)
running = 1
screen.fill(white)
while running:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
rect = pygame.Rect(event.dict["pos"],(30,50))
pygame.draw.rect(screen,black,rect,1)
pygame.display.flip()