I'm having trouble with pygame - python

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()

Related

the background color of my program is only visable when I close the app icon

I have an app icon open but I can' see a white background in my application window untill I close the app. What do I do?
import pygame
# Internationalizing Pygame
pygame.init()
# Title and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load()
pygame.display.set_icon(icon)
# player
playerIMG = pygame.image.load()
playerX = 370
playerY = 30
screen = pygame.display.set_mode((1200, 600))
def player():
screen.blit(playerIMG, (playerX, playerY))
running = True
while running: # RGB
screen.fill((222, 222, 222))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
player()
pygame.display.update()
I don't know what to do.
It is a matter of Indentation you have to draw the scene and update the display in the application loop:
running = True
while running: # RGB
screen.fill((222, 222, 222))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# INDENTATION
#-->|
player()
pygame.display.update()

Pygame Window not Responding but no Error

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()

screen.fill not showing background color

I've started to program a game but I don't know why the background screen color is not changing. It's simply black. What am I doing wrong?
import sys
import pygame
def run_game():
pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Alien Invasion")
bg_color = (230, 230, 230)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(bg_color)
pygame.display.flip()
run_game()
I'm on a Windows machine and using VS Code.
It is a matter of Indentation. You must draw the background and update the display in the application loop rather than after the application loop:
def run_game():
pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Alien Invasion")
bg_color = (230, 230, 230)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# INDENTATION
#-->|
screen.fill(bg_color)
pygame.display.flip()

How to make my pygame game window resizeable?

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()

Pygame script freezing on windows (Program not responding)

I am new to stackoverflow, I decided to join because I sometimes have problems with programming. This one is really annoying, I cant figure out why it doesn't work. Any help would be appreciated!
I get the windows "Program not responding" error message
A simple display:
import pygame
pygame.init()
BLUE = pygame.Color(0, 0, 255)
size = [1280, 720]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Hangman")
done = False
clock = pygame.time.Clock()
while not done:
# Leaves the fps at 30
clock.tick(30)
screen.fill(BLUE)
pygame.display.flip()
The expected result is a blue screen, instead I get a blue screen that crashes
In your game loop handling events will prevent freezing.
import pygame
pygame.init()
BLUE = pygame.Color(0, 0, 255)
size = [1280, 720]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Hangman")
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
else:
print event
# Leaves the fps at 30
clock.tick(30)
screen.fill(BLUE)
pygame.display.flip()

Categories

Resources