'Pygame black screen. Pygame just displays a black screen when i run this on vscode
Pygame black screen. Pygame just displays a black screen when I run this on vscode. What am I doing wrong? My pygame is just showing a blank screen when I run this code. I have searched for solutions but haven't found anything that applies to this scenario.
import pygame
screen_size = [360, 600]
screen = pygame.display.set_mode(screen_size)
background = pygame.image.load('background.jpg')
spaceship = pygame.image.load('spaceship.jpg')
bullet = pygame.image.load('bullet.jpg')
bullet_y = 500
fired = False
planets = ['p_one.jpg', 'p_two.jpg', 'p_three.png']
p_index = 0
planet = pygame.image.load(planets[p_index])
planet_x = 140
move_direction = 'right'
keep_alive = True
clock = pygame.time.Clock()
while keep_alive:
pygame.event.get()
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE] == True:
fired = True
if fired is True:
bullet_y = bullet_y - 5
if bullet_y == 50:
fired = False
bullet_y = 500
screen.blit(background, [0, 0])
screen.blit(bullet, [180, bullet_y])
screen.blit(spaceship, [160, 500])
if move_direction == 'right':
planet_x = planet_x + 5
if planet_x == 300:
move_direction = 'left'
else:
planet_x = planet_x - 5
if planet_x == 0:
move_direction = 'right'
screen.blit(planet, [planet_x, 50])
if bullet_y < 80 and planet_x > 120 and planet_x < 180:
p_index = p_index + 1
if p_index < len(planets):
planet = pygame.image.load(planets[p_index])
planet_x = 10
else:
print('YOU WIN')
keep_alive = False
pygame.display.update()
clock.tick(60)
Solution 1:[1]
There appear to be indentation issues with your code which prevent your blit operations from occurring unless the space bar has been pressed.
I've reformatted your code minimally to permit operation, I've created coloured squares instead of your images.
import pygame
pygame.init()
screen_size = [360, 600]
screen = pygame.display.set_mode(screen_size)
# background = pygame.image.load('background.jpg')
background = pygame.Surface(screen_size)
background.fill(pygame.Color("grey"))
# spaceship = pygame.image.load('spaceship.jpg')
spaceship = pygame.Surface((30, 30))
spaceship.fill(pygame.Color("blue"))
# bullet = pygame.image.load('bullet.jpg')
bullet = pygame.Surface((10, 10))
bullet.fill(pygame.Color("red"))
bullet_y = 500
fired = False
# planets = ['p_one.jpg', 'p_two.jpg', 'p_three.png']
planets = ["orange", "purple", "aqua"]
p_index = 0
# planet = pygame.image.load(planets[p_index])
planet = pygame.Surface((50, 50))
planet.fill(pygame.Color(planets[p_index]))
planet_x = 140
move_direction = 'right'
keep_alive = True
clock = pygame.time.Clock()
while keep_alive:
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_alive = False
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE] == True:
fired = True
if fired is True:
bullet_y = bullet_y - 5
if bullet_y == 50:
fired = False
bullet_y = 500
if move_direction == 'right':
planet_x = planet_x + 5
if planet_x == 300:
move_direction = 'left'
else:
planet_x = planet_x - 5
if planet_x == 0:
move_direction = 'right'
if bullet_y < 80 and planet_x > 120 and planet_x < 180:
p_index = p_index + 1
if p_index < len(planets):
# planet = pygame.image.load(planets[p_index])
planet.fill(pygame.Color(planets[p_index]))
planet_x = 10
else:
print('YOU WIN')
keep_alive = False
screen.blit(background, [0, 0])
screen.blit(bullet, [180, bullet_y])
screen.blit(spaceship, [160, 500])
screen.blit(planet, [planet_x, 50])
pygame.display.update()
clock.tick(60)
I also fixed the event handler to support a quit event, i.e. closing the window.
Solution 2:[2]
This is caused by indentation. And I suggest you turn on the automatic code formatting function in vscode
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | import random |
Solution 2 | MingJie-MSFT |