Why doesnt the image load?
Question:
for some reason the mario image wont load in the game
the forest.png is the background if you were to run
and the mario.png is the character
I have done some tests but no matter what it keeps not loading
this is also the game from the Tech With Tim tutorial I followed it line by line
yet it still doesn’t work
(No errors it just doesnt load the character)
import pygame
import os, sys
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("the game!!!")
APP_FOLDER = os.path.dirname(os.path.realpath(sys.argv[0]))
#background_image = pygame.image.load(r"C:UsersNONAMEDesktopCodingGamesLearningforest.png").convert()
background_image = pygame.image.load(os.path.join(APP_FOLDER, 'forest.png')).convert()
#mario = pygame.image.load(r"C:UsersNO NAMEDesktopCodingGamesLearningmario.png").convert()
R_mario = pygame.image.load(os.path.join(APP_FOLDER, 'mario.png')).convert()
L_mario = pygame.transform.flip(R_mario, True, False)
screenWidth = 500
x = 50
y = 425
width = 40
height = 60
vel = 10
isJump = False
jumpCount = 10
left = False
right = True
walkcount = 0
def redrawGameWindow():
global walkcount
win.blit(background_image, [0, 0])
pygame.display.update()
if left == True:
win.blit(L_mario, (x, y))
print("work left")
elif right == True:
win.blit(R_mario, (x, y))
print("work right")
#mainloop
run = True
while run:
pygame.time.delay(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
Left = True
right = False
elif keys[pygame.K_a] and x > vel:
x -= vel
right = True
left = False
else:
right = False
left = False
if keys[pygame.K_RIGHT] and x < screenWidth - width - vel:
x += vel
if keys[pygame.K_d] and x < screenWidth - width - vel:
x += vel
if not(isJump):
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 1
else:
isJump = False
jumpCount = 10
redrawGameWindow()
pygame.quit()
Answers:
You have to blit
the character before updating the display:
def redrawGameWindow():
global walkcount
win.blit(background_image, [0, 0])
if left == True:
win.blit(L_mario, (x, y))
print("work left")
elif right == True:
win.blit(R_mario, (x, y))
print("work right")
pygame.display.update()
Note, however, if right == False
and left == False
, nothing is drawn at all.
for some reason the mario image wont load in the game
the forest.png is the background if you were to run
and the mario.png is the character
I have done some tests but no matter what it keeps not loading
this is also the game from the Tech With Tim tutorial I followed it line by line
yet it still doesn’t work
(No errors it just doesnt load the character)
import pygame
import os, sys
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("the game!!!")
APP_FOLDER = os.path.dirname(os.path.realpath(sys.argv[0]))
#background_image = pygame.image.load(r"C:UsersNONAMEDesktopCodingGamesLearningforest.png").convert()
background_image = pygame.image.load(os.path.join(APP_FOLDER, 'forest.png')).convert()
#mario = pygame.image.load(r"C:UsersNO NAMEDesktopCodingGamesLearningmario.png").convert()
R_mario = pygame.image.load(os.path.join(APP_FOLDER, 'mario.png')).convert()
L_mario = pygame.transform.flip(R_mario, True, False)
screenWidth = 500
x = 50
y = 425
width = 40
height = 60
vel = 10
isJump = False
jumpCount = 10
left = False
right = True
walkcount = 0
def redrawGameWindow():
global walkcount
win.blit(background_image, [0, 0])
pygame.display.update()
if left == True:
win.blit(L_mario, (x, y))
print("work left")
elif right == True:
win.blit(R_mario, (x, y))
print("work right")
#mainloop
run = True
while run:
pygame.time.delay(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
Left = True
right = False
elif keys[pygame.K_a] and x > vel:
x -= vel
right = True
left = False
else:
right = False
left = False
if keys[pygame.K_RIGHT] and x < screenWidth - width - vel:
x += vel
if keys[pygame.K_d] and x < screenWidth - width - vel:
x += vel
if not(isJump):
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 1
else:
isJump = False
jumpCount = 10
redrawGameWindow()
pygame.quit()
You have to blit
the character before updating the display:
def redrawGameWindow():
global walkcount
win.blit(background_image, [0, 0])
if left == True:
win.blit(L_mario, (x, y))
print("work left")
elif right == True:
win.blit(R_mario, (x, y))
print("work right")
pygame.display.update()
Note, however, if right == False
and left == False
, nothing is drawn at all.