I've just started working with pygame and all the sample (getting started) code I have run, gives me a blank screen. Including the space invaders example which play the sound but shows only the grey blank screen as shown below.
grey blank screen
I am getting no errors in my terminal, I have tried with both python3 and python2.7. I have also tried with many different scripts but still I am getting the grey blank screen. Below is an example script I have tried to run. Any help would be great, thanks.
import pygame
from pygame.locals import *
yellow = (255,255,0) # RGB color tuple
# initialize screen
pygame.init()
screen = pygame.display.set_mode((350, 250))
pygame.display.set_caption('Basic Pygame program')
# fill background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(yellow)
# display some text
font = pygame.font.Font(None, 36)
text = font.render("Hello from Monty PyGame", 1, (10, 10, 10))
textpos = text.get_rect()
textpos.centerx = background.get_rect().centerx
background.blit(text, textpos)
# blit everything to the screen
screen.blit(background, (0, 0))
pygame.display.flip()
# event loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
raise SystemExit
screen.blit(background, (0, 0))
pygame.display.flip()
It's a matter of Indentation. You have to draw the background and update the display in the application loop rather than the event loop:
while True:
for event in pygame.event.get():
if event.type == QUIT:
raise SystemExit
# INDENTATION
#<--|
screen.blit(background, (0, 0))
pygame.display.flip()
Related
I made a window in pygame, tried to make the window white and even though I only call pygame.display.update() once it keeps flashing between white and black.
This is my code:
import pygame
running = True
pygame.init()
White = (255, 255, 255)
while running == True:
screen = pygame.display.set_mode((1400, 800))
screen.fill(White)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
I tried changing it to pygame.display.flip() but it still stayed the same.
I also tried to put pygame.display.update() out of the loop but then the screen just stayed black.
You need to create the Pygame window (pygame.display.set_mode) once before the application loop instead of constantly creating a new window in the application loop:
import pygame
running = True
pygame.init()
screen = pygame.display.set_mode((1400, 800))
White = (255, 255, 255)
while running == True:
screen.fill(White)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
I've been trying to put text over a sprite (a dialogue box I imported) but no text comes up. The current code is me trying to blit the text onto the screen but it isn't working. The code works on a separate document, just not on this one. I currently have it creating the colour, then a box for the text to display in. After that, the box is made to fit its surroundings, a font is initialised through SysFont. Then my text is blit to the screen with a display.update to keep it constantly up the top of the layers. Even after hashing the code that creates the sprites and blits them, the text doesn't show up and instead the mouse coordinates and keyboard and mouse buttons are shown underneath the screen in a textbox. Anything helps as I'm new to coding.
import pygame
import os
import random
import sys
pygame.init()
pygame.font.init()
# Create Screen
FrameHeight = 3000
FrameWidth = 10000
screen = pygame.display.set_mode((FrameWidth, FrameHeight))
#Create Sprites
flag = True
background = pygame.image.load("fantasy-village-bg.jpg")
icon1 = pygame.image.load("Elder.png")
Dialogue = pygame.image.load("Dialogue-box.png")
def village():
#Repeating loop for beginning shot of the game
while flag == True:
#Background loops
screen.fill((23, 234, 80))
screen.blit(background, (0, 0))
#Village elder loops
screen.blit(icon1, (0, 0))
#Dialogue box loops
screen.blit(Dialogue, (-100, 400))
pygame.display.flip()
village()
# PYGAME FRAME WINDOW and documentation for keylogging and mouse tracking
pygame.mouse.set_visible(0)
pygame.display.set_caption("Riftka: Adventure Awaits")
#Dealing with the event of a crash"""
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
print(event)
pygame.display.update()
#First set of dialogue
black = (0,0,0)
WINDOW_WIDTH = 500
WINDOW_HEIGHT = 500
def show_text( msg, color, x=WINDOW_WIDTH//2, y=WINDOW_WIDTH//2 ):
global WINDOW
text = font.render( msg, True, color)
WINDOW.blit(text, ( x, y ) )
pygame.init()
WINDOW = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
pygame.display.set_caption("Text")
# Create the font (only needs to be done once)
font = pygame.font.SysFont(None, 25)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
show_text("We have been waiting for you traveller", blue)
pygame.display.update()
Images are layered in the order they're blit to the screen.
I've separated out your display text loop and added in some draw functions to show the layering
import pygame
pygame.init()
black = (0,0,0)
blue = pygame.Color("dodgerblue")
WINDOW_WIDTH = 500
WINDOW_HEIGHT = 500
def show_text( window, font, msg, color,):
text = font.render( msg, True, color)
# center the text on the window
text_rect = text.get_rect(center=window.get_rect().center)
window.blit(text, text_rect)
pygame.init()
WINDOW = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
pygame.display.set_caption("Text")
# Create the font (only needs to be done once)
font = pygame.font.SysFont(None, 25)
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
WINDOW.fill(black)
pygame.draw.rect(WINDOW, pygame.Color("purple"), [250, 200, 200, 100])
show_text(WINDOW, font, "We have been waiting for you traveller", blue)
pygame.draw.rect(WINDOW, (pygame.Color("red")), [320, 220, 50, 60])
pygame.display.update()
clock.tick(30) # limit FPS
This should look something like this:
This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Closed 1 year ago.
When running this code:
import pygame,time
GREEN = (30, 156, 38)
WHITE = (255,255,255)
pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (0,0,100,100))
time.sleep(3)
Pygame shows a black screen for 3 seconds, but doesn't draw a rectangle.
I am using Atom using script package to run code
You have to implement an application loop. The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
limit frames per second to limit CPU usage
import pygame
GREEN = (30, 156, 38)
WHITE = (255,255,255)
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
# applicaition loop
run = True
while run:
# limit frames per second
clock.tick(60)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# clear display
screen.fill(WHITE)
# draw objects
pygame.draw.rect(screen, GREEN, (0, 0, 100, 100))
# update display
pygame.display.flip()
pygame.quit()
exit()
Note, you must do the event handling. See pygame.event.get() respectively pygame.event.pump():
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
Update the screen with:
pygame.display.update()
at the end of your code you have posted.
You have to update the screen like that:
pygame.display.flip()
to render what you just drew.
Your code should look like this:
import pygame
import time
pygame.init()
GREEN = (30, 156, 38)
WHITE = (255,255,255)
screen = pygame.display.set_mode((640, 480))
# draw on screen
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (0,0,100,100))
# show that to the user
pygame.display.flip()
time.sleep(3)
Off-topic: You should also get the events to allow the user to close the window:
import pygame
from pygame.locals import QUIT
import time
pygame.init()
GREEN = (30, 156, 38)
WHITE = (255, 255, 255)
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock() # to slow down the code to a given FPS
# draw on screen
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (0, 0, 100, 100))
# show that to the user
pygame.display.flip()
start_counter = time.time()
while time.time() - start_counter < 3: # wait for 3 seconds to elapse
for event in pygame.event.get(): # get the events
if event.type == QUIT: # if the user clicks "X"
exit() # quit pygame and exit the program
clock.tick(10) # limit to 10 FPS
# (no animation, you don't have to have a great framerate)
Note that you must put all of this into a game loop if you want to repeat it like a classic game.
Borders should look like on the picture. I've tried setting a new picture over it but that's not what it is.
Are you talking about this?
import pygame
BLACK = (0,0,0)
pygame.init()
# Set up the drawing window
screen = pygame.display.set_mode([500, 500])
width,height = pygame.display.get_surface().get_size()
# Run until the user asks to quit
running = True
while running:
pygame.draw
# Did the user click the window close button?
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Fill the background with white
screen.fill((255, 255, 255))
pygame.draw.lines(screen, BLACK, False, [(0,0), (width-1,0), (width-1,height-1), (0, height-1), (0,0)], 3)
# Flip the display
pygame.display.flip()
pygame.quit()
Whenever I try to include "screen.fill(insert value here)" to my pygame code, the window instantly crashes. I can comment it out and my code works just fine. I'm really not sure why.
I've tried moving the line around, using other people's code in part - no luck.
import pygame
pygame.init()
win = pygame.display.set_mode((1000, 800))
pygame.display.set_caption("Tetris")
img=pygame.image.load("C:\\Users\\Charlotte\\Desktop\\tetris_grid.png")
win.blit(img,(450, 150))
running = True
while running:
screen.fill(0, 0, 0)
pygame.display.update()
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
When I run the code with that line in it, the window opens and then it instantly closes.
If you are trying to fill the screen with black, the color must be passed as a tuple:
win.fill((0, 0, 0))
Also, you never assign screen, maybe you intended to use win?
The doc for Surface.fill.
The color parameter to fill() has to be either a single grayscale value or a tuple of 3 RGB or 4 RGBA components. So it has to be either:
win.fill(0)
or
win.fill((0, 0, 0))
Further you've to blit() the image in the main loop. To continuously draw the scene in the main application loop, you've to:
handle the events
clear the window
draw the scene (blit the image)
update the display
Furthermore I recommend to use tick() of pygame.time.Clock rather than pygame.time.delay() tpo control the frames per second.
import pygame
pygame.init()
win = pygame.display.set_mode((1000, 800))
pygame.display.set_caption("Tetris")
clock = pygame.time.Clock()
img=pygame.image.load("C:\\Users\\Charlotte\\Desktop\\tetris_grid.png")
running = True
while running:
clock.tick(60)
# handle the events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# clear the display
win.fill(0)
# draw the scene
win.blit(img,(450, 150))
# update the display
pygame.display.update()
pygame.quit()