screen.fill not showing background color - python

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

Related

I'm having trouble with pygame

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

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 not responding, any tips?

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

Trouble with pygame rendering

I've got an issue where pygame is only rendering ablack screen with a strange glitchy green line on it.
I'm following a tutorial on RealPython.com and everything went smoothly with the first attempt. Once I did the second attempt this is what renders, which is clearly wrong.
#sidescrolling air-shooter
import pygame
#import pygame locals
from pygame.locals import(
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
QUIT,
)
#constants for screen width/height
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.Surface((75,25))
self.surf.fill((255, 255,255))
self.rect = self.surf.get_rect()
pygame.init()
#create screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Instantiate player
player = Player()
#keep the game running!
running = True
#loop time!
while running:
#look at all the events
for event in pygame.event.get():
#did the user hit a key?
if event.type == KEYDOWN:
#Was it escape? uh oh we gotta stop
if event.key == K_ESCAPE:
running = False
elif event.type ==QUIT:
running = False
#fill screen with white
screen.fill((255, 255, 255))
# Create a surface and pass in a tuple containing legth and width
surf = pygame.Surface((50, 50))
# Give the surface a color to separate it from the Background
surf.fill((0, 0, 0))
rect = surf.get_rect()
#This line says "Draw surf onto the screen at the center"
screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
#Draw the player on the screen
screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
pygame.display.flip()
pygame.quit()
I've even checked the source code for their project and it seems to match up.
It is a matter of Indentation. You need to draw the scene and update the display in the application loop instead of after the application loop:
#loop time!
while running:
#look at all the events
for event in pygame.event.get():
#did the user hit a key?
if event.type == KEYDOWN:
#Was it escape? uh oh we gotta stop
if event.key == K_ESCAPE:
running = False
elif event.type ==QUIT:
running = False
#-->| INDENTATION
#fill screen with white
screen.fill((255, 255, 255))
# Create a surface and pass in a tuple containing legth and width
surf = pygame.Surface((50, 50))
# Give the surface a color to separate it from the Background
surf.fill((0, 0, 0))
rect = surf.get_rect()
#This line says "Draw surf onto the screen at the center"
screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
#Draw the player on the screen
screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
pygame.display.flip()
pygame.quit()

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

Categories

Resources