I am new to Pygame but I understand that this is a common problem, however, I have looked at every place that I can to solve this problem and none of the solutions there apply to my program's problem. The problem is that everything in my program except the player sprite does not load until I try to close the program.
screenwidth = 500
import random
import pygame
running = True
gameExit = False
COLOR = (255, 100, 98)
WALLCOLOR = (255,255,0)
SURFACE_COLOR = (0, 0, 0)
x = 500
y = 500
vel = 5
class Sprite(pygame.sprite.Sprite):
def __init__(self, color, height, width):
super().__init__()
self.image = pygame.Surface([100, 100])
self.image.fill(SURFACE_COLOR)
self.image.set_colorkey(COLOR)
pygame.draw.rect(self.image,
color,
pygame.Rect(0, 0, width, height))
self.rect = self.image.get_rect()
def moveRight(self, pixels):
self.rect.x += pixels
def moveLeft(self, pixels):
self.rect.x -= pixels
def moveForward(self, speed):
self.rect.y += speed * speed/10
def moveBack(self, speed):
self.rect.y -= speed * speed/10
display_surface = pygame.display.set_mode((500, 500))
# Creating the image surface
image = pygame.image.load('DJ9ZEF.png')
# putting our image surface on display
# surface
display_surface.blit(image,(100,100))
pygame.init()
LIGHTGREEN = (0, 255, 0)
screenx = 1080
screeny = 600
size = (screenx, screeny)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Project")
all_sprites_list = pygame.sprite.Group()
playerCar = Sprite(LIGHTGREEN, 100, 100)
playerCar.rect.x = 50
playerCar.rect.y = 250
all_sprites_list.add(playerCar)
exit = True
clock = pygame.time.Clock()
while exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
exit = True
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] :
playerCar.moveLeft(10)
if keys[pygame.K_RIGHT] :
playerCar.moveRight(10)
if keys[pygame.K_DOWN]:
playerCar.moveForward(10)
if keys[pygame.K_UP]:
playerCar.moveBack(10)
all_sprites_list.update()
screen.fill(SURFACE_COLOR)
all_sprites_list.draw(screen)
pygame.display.flip()
clock.tick(60)
wall1size = width, height = (1000, 200)
img = pygame.image.load('bird.png')
grassimg = pygame.image.load('grass.jpg')
screen.blit(grassimg, (0, 0))
grassimg1 = pygame.image.load('grass.jpg')
screen.blit(grassimg, (500, 0))
coinimg = pygame.image.load('coin.png')
coinimg = pygame.transform.scale(coinimg, (200,200))
screen.blit(coinimg, (850, 195))
empty_surface = pygame.Surface(wall1size)
pygame.Surface.fill(empty_surface, (255, 0, 0))
screen.blit(empty_surface, (250, -50))
px = 0
py = 0
wall1size1 = width, height = (1200, 2000)
empty_surface1 = pygame.Surface(wall1size1)
pygame.Surface.fill(empty_surface, (255, 0, 0))
screen.blit(empty_surface, (250, 425))
px = 0
py = 0
running = True
while running:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
pygame.display.update()
pygame.quit()
I expect the program to update while running, but it does not.