My sprites aren't showing properly in my program. The map displays fine, but the sprite only shows for a split second when I quit the program by pressing the big X at the top right (But not when I exit by pressing ESC)
Display sprites code:
# Protagonist Sprites
protagR = [pygame.image.load("ASG2_Graphics/JoArrow_R1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_1.png")]
protagL = [pygame.image.load("ASG2_Graphics/JoArrow_L1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_1.png")]
protagU = [pygame.image.load("ASG2_Graphics/JoArrow_U1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_1.png")]
protagD = [pygame.image.load("ASG2_Graphics/JoArrow_D1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_1.png")]
# Bullet Sprites
bulletsP = [pygame.image.load("ASG2_Graphics/Bullet_1.png"), pygame.image.load("ASG2_Graphics/Bullet_2.png"), pygame.image.load("ASG2_Graphics/Bullet_3.png"), pygame.image.load("ASG2_Graphics/Bullet_4.png")]
bulletsE = [pygame.image.load("ASG2_Graphics/E_Bullet_1.png"), pygame.image.load("ASG2_Graphics/E_Bullet_2.png"), pygame.image.load("ASG2_Graphics/E_Bullet_3.png")]
# Enemy Sprites
enemyR = [pygame.image.load("ASG2_Graphics/Enemy_R1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_5.png")]
enemyL = [pygame.image.load("ASG2_Graphics/Enemy_L1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_5.png")]
enemyU = [pygame.image.load("ASG2_Graphics/Enemy_U1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_5.png")]
enemyD = [pygame.image.load("ASG2_Graphics/Enemy_D1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_5.png")]
Class code:
class player():
def __init__(self, x, y, width, height, walkCount):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.up = False
self.down = False
self.left = False
self.right = False
self.up = False
self.down = False
self.walkCount = walkCount
def redrawGameWindow(self, window):
dest = (self.x, self.y)
if self.walkCount + 1 >= 30:
self.walkCount = 0
if self.left:
window.blit(protagL[self.walkCount//3], dest)
self.walkCount += 1
elif self.right:
window.blit(protagR[self.walkCount//3], dest)
self.walkCount += 1
elif self.up:
window.blit(protagU[self.walkCount//3], dest)
self.walkCount += 1
elif self.down:
window.blit(protagD[self.walkCount//3], dest)
self.walkCount += 1
else:
window.blit(protagL[self.walkCount//3], dest)
pygame.display.update()
Map displaying code:
def multilineRender(screen, text, x, y, the_font, colour=(0, 0, 0), justification="left"):
justification = justification[0].upper()
# text = text.strip().replace('\r','').split('\n')
max_width = 0
text_bitmaps = []
# Convert all the text into bitmaps, calculate the justification width
for char in text:
text_bitmap = the_font.render(char, True, colour)
text_width = text_bitmap.get_width()
text_bitmaps.append((text_width, text_bitmap))
if (max_width < text_width):
max_width = text_width
# Paint all the text bitmaps to the screen with justification
for (width, bitmap) in text_bitmaps:
xPos = x
width_diff = max_width - width
if justification == 'R': # right-justify
xPos = x + width_diff
elif justification == 'C': # centre-justify
xPos = x + (width_diff // 2)
screen.blit(bitmap, (xPos, y))
y += bitmap.get_height()
pygame.display.update()
Character Displaying Code:
def movePlayer(board):
clock = pygame.time.Clock()
run = True
while run:
clock.tick(15)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
arrowKeys = pygame.key.get_pressed()
if arrowKeys[pygame.K_LEFT] and board.x > 25 + board.vel:
board.x -= board.vel
board.left = True
board.right = False
board.up = False
board.down = False
elif arrowKeys[pygame.K_RIGHT] and board.x < 510 - board.width - board.vel:
board.x += board.vel
board.right = True
board.left = False
board.up = False
board.down = False
elif arrowKeys[pygame.K_UP] and board.y > 40 + board.vel:
board.y -= board.vel
board.right = False
board.left = False
board.up = True
board.down = False
elif arrowKeys[pygame.K_DOWN] and board.y < 450 - board.height - board.vel:
board.y += board.vel
board.right = False
board.left = False
board.up = False
board.down = True
elif arrowKeys[pygame.K_ESCAPE]: # ESC key
stopGame()
else:
board.right = False
board.left = False
board.up = False
board.down = False
board.walkCount = 0
board.redrawGameWindow(window)
pygame.display.quit()
Main code:
def main():
inFile = open("map(TEST).txt", "r")
text = inFile.read().splitlines() # splitlines is to exclude the '\n'
inFile.close()
window.fill((0, 0, 0))
myfont = pygame.font.SysFont("Calibri", 35)
window.fill((0,0,0))
board = player(30, 45, 64, 64, 0)
multilineRender(window, text, 20, 20, myfont, (255, 255, 255))
while True:
# movePlayer(board)
multilineRender(window, text, 20, 20, myfont, (255, 255, 255))
movePlayer(board)
I should also mention that when I quit the program, i get this error:
Traceback (most recent call last): File "C:/Users/User/PycharmProjects/Python_Projects/Asg2/m5_PYGAME_STYLE.py", line 156, in <module> main() File "C:/Users/User/PycharmProjects/Python_Projects/Asg2/m5_PYGAME_STYLE.py", line 153, in main multilineRender(window, text, 20, 20, myfont, (255, 255, 255)) File "C:/Users/User/PycharmProjects/Python_Projects/Asg2/m5_PYGAME_STYLE.py", line 83, in multilineRender screen.blit(bitmap, (xPos, y)) pygame.error: display Surface quit
...but I doubt it's related.
Remove pygame.display.quit()
form movePlayer
And turn the while
loop in movePlayer
to an selection if
. Note, you have a loop in main
, so use it.
Further more, I recommend to do pygame.display.update()
in main.
The main application loop has to:
def main():
inFile = open("map(TEST).txt", "r")
text = inFile.read().splitlines() # splitlines is to exclude the '\n'
inFile.close()
myfont = pygame.font.SysFont("Calibri", 35)
board = player(30, 45, 64, 64, 0)
clock = pygame.time.Clock()
run = True
while run:
clock.tick(15)
# clear the display
window.fill((0, 0, 0))
# draw the scene
board.redrawGameWindow(window)
multilineRender(window, text, 20, 20, myfont, (255, 255, 255))
# update the display
pygame.display.update()
# handle the events and update the objects
run = movePlayer(board)
movePlayer
has to return true
is the game is still running and else false
:
def movePlayer(board):
run = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
arrowKeys = pygame.key.get_pressed()
if arrowKeys[pygame.K_LEFT] and board.x > 25 + board.vel:
board.x -= board.vel
board.left, board.right, board.up, board.down = True, False, False, False
elif arrowKeys[pygame.K_RIGHT] and board.x < 510 - board.width - board.vel:
board.x += board.vel
board.left, board.right, board.up, board.down = False, True, False, False
elif arrowKeys[pygame.K_UP] and board.y > 40 + board.vel:
board.y -= board.vel
board.left, board.right, board.up, board.down = False, False, True, False
elif arrowKeys[pygame.K_DOWN] and board.y < 450 - board.height - board.vel:
board.y += board.vel
board.left, board.right, board.up, board.down = False, False, False, True
elif arrowKeys[pygame.K_ESCAPE]: # ESC key
stopGame()
else:
board.left, board.right, board.up, board.down = False, False, False, False
board.walkCount = 0
return run
multilineRender
does no pygame.display.update()
:
def multilineRender(screen, text, x, y, the_font, colour=(0, 0, 0), justification="left"):
justification = justification[0].upper()
# text = text.strip().replace('\r','').split('\n')
max_width = 0
text_bitmaps = []
# Convert all the text into bitmaps, calculate the justification width
for char in text:
text_bitmap = the_font.render(char, True, colour)
text_width = text_bitmap.get_width()
text_bitmaps.append((text_width, text_bitmap))
if (max_width < text_width):
max_width = text_width
# Paint all the text bitmaps to the screen with justification
for (width, bitmap) in text_bitmaps:
xPos = x
width_diff = max_width - width
if justification == 'R': # right-justify
xPos = x + width_diff
elif justification == 'C': # centre-justify
xPos = x + (width_diff // 2)
screen.blit(bitmap, (xPos, y))
y += bitmap.get_height()