我正在用python和pygame 0创建一个小游戏。在游戏中,我喜欢对不同级别使用不同的背景图像。因此,如果用户达到级别2,则应显示一个新的背景图像。这可能吗。我尝试了此操作,但始终收到错误消息:“ pygame.error:无法打开images \ background2.png”。路径是100%正确的。这是我的完整代码:
from random import randint
import pygame
WIDTH = 800
HEIGHT = 800
apple = Actor("apple")
apple.pos = randint(0, 800), randint(-800, 0)
pear = Actor("pear")
pear.pos = randint(0, 800), randint(-800, 0)
plum = Actor("plum")
plum.pos = randint(0, 800), randint(-800, 0)
donut = Actor("donut")
donut.pos = randint(0, 800), randint(-800, 0)
ice = Actor("ice")
ice.pos = randint(0, 800), randint(-800, 0)
chips = Actor("chips")
chips.pos = randint(0, 800), randint(-800, 0)
burger = Actor("burger")
burger.pos = randint(0, 800), randint(-800, 0)
happysmiley = Actor("happysmiley")
happysmiley.pos = 300, 750
start = Actor("start")
start.pos = 700,750
quitgame = Actor("quit")
quitgame.pos = 600, 750
creditsinfo = Actor("credits")
creditsinfo.pos = 485, 750
back = Actor("back")
back.pos = 600, 100
nextlevel = Actor("nextlevel")
nextlevel.pos = 700, 750
gameover = False
score = 0
gamemode = 1
gamestart = 0
level = 0
background = pygame.image.load("images\\background.png")
background2 = pygame.image.load("images\\background2.png")
pygame.mixer.init()
pygame.mixer.music.load("music\\funmusic.mp3")
pygame.mixer.music.play(-1)
def draw():
global gamemode
screen.clear()
if gamemode == 0 and score >= 100:
endoflevel1()
else:
drawgame()
if gamemode == 3 and score >= 150:
endoflevel2()
def drawgame():
global gamemode
if gamemode == 1:
screen.clear()
screen.blit("background",(0,0))
apple.draw()
pear.draw()
plum.draw()
donut.draw()
ice.draw()
chips.draw()
quitgame.draw()
happysmiley.draw()
start.draw()
creditsinfo.draw()
screen.draw.text("Punkte: " + str(score), (700, 5), color = "black")
elif gamemode == 0:
screen.clear()
screen.blit("background",(0,0))
apple.draw()
pear.draw()
plum.draw()
donut.draw()
ice.draw()
chips.draw()
happysmiley.draw()
screen.draw.text("Punkte: " + str(score), (700, 5), color = "black")
elif gamemode == 2:
screen.clear()
screen.fill("orange")
back.draw()
screen.draw.text("Credits", topleft=(350,10), fontsize=40)
pygame.display.flip()
elif gamemode == 3:
screen.clear()
screen.blit("background2",(0,0))
apple.draw()
pear.draw()
plum.draw()
donut.draw()
ice.draw()
chips.draw()
burger.draw()
happysmiley.draw()
screen.draw.text("Punkte: " + str(score), (700, 5), color = "black")
def update():
global score, gamestart
if gamestart == 1:
if score >= 100:
pygame.mixer.music.stop()
return
if apple.y < 800:
apple.y = apple.y + 4
else:
apple.x = randint(0, 800)
apple.y = randint(-800, 0)
if pear.y < 800:
pear.y = pear.y + 4
else:
pear.x = randint(0, 800)
pear.y = randint(-800, 0)
if plum.y < 800:
plum.y = plum.y + 4
else:
plum.x = randint(0, 800)
plum.y = randint(-800, 0)
if donut.y < 800:
donut.y = donut.y + 4
else:
donut.x = randint(0, 800)
donut.y = randint(-800, 0)
if ice.y < 800:
ice.y = ice.y + 4
else:
ice.x = randint(0, 800)
ice.y = randint(-800, 0)
if chips.y < 800:
chips.y = chips.y + 4
else:
chips.x = randint(0, 800)
chips.y = randint(-800, 0)
if keyboard.left:
happysmiley.x = happysmiley.x - 5
elif keyboard.right:
happysmiley.x = happysmiley.x + 5
if happysmiley.collidepoint (apple.x, apple.y):
score = score + 2
effect = pygame.mixer.Sound("sounds\\bonus.wav")
effect.play()
if happysmiley.collidepoint (pear.x, pear.y):
score = score + 1
effect = pygame.mixer.Sound("sounds\\bonus.wav")
effect.play()
if happysmiley.collidepoint (plum.x, plum.y):
score = score + 1
effect = pygame.mixer.Sound("sounds\\bonus.wav")
effect.play()
if happysmiley.collidepoint (donut.x, donut.y):
score = score - 1
effect = pygame.mixer.Sound("sounds\\no.wav")
effect.play()
if happysmiley.collidepoint (ice.x, ice.y):
score = score - 1
effect = pygame.mixer.Sound("sounds\\no.wav")
effect.play()
if happysmiley.collidepoint (chips.x, chips.y):
score = score - 1
effect = pygame.mixer.Sound("sounds\\no.wav")
effect.play()
if level == 2:
if burger.y < 800:
burger.y = burger.y + 4
else:
burger.x = randint(0, 800)
burger.y = randint(-800, 0)
if happysmiley.collidepoint (burger.x, burger.y):
score = score - 1
effect = pygame.mixer.Sound("sounds\\no.wav")
effect.play()
def on_mouse_down(pos): # wurde Maustaste gedrückt?
global score, gamestart, gamemode
if start.collidepoint(pos):
gamestart = 1
gamemode = 0
if quitgame.collidepoint(pos):
exit()
if creditsinfo.collidepoint(pos):
gamemode = 2
if back.collidepoint(pos):
gamemode = 1
def endoflevel1():
global score, gamemode, gamestart, level
screen.clear()
screen.fill("green")
screen.draw.text("Congratulations! You have successfully completed the 1st level!", topleft=(100,350), fontsize=30, color = "black")
nextlevel.draw()
gamestart = 0
pygame.display.flip()
running = True
while (running):
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if nextlevel.collidepoint(event.pos):
score = 0
gamemode = 3
gamestart = 1
level = 2
running = False
def endoflevel2():
global score, gamemode, gamestart, level
screen.clear()
screen.fill("orange")
screen.draw.text("Congratulations! You have successfully completed the 2nd level!", topleft=(100,350), fontsize=30, color = "black")
nextlevel.draw()
gamestart = 0
pygame.display.flip()
完整的错误消息是:
Traceback (most recent call last):
File "runpy.py", line 193, in _run_module_as_main
File "runpy.py", line 85, in _run_code
File "C:\Program Files\Programmierung\Mu\pkgs\pgzero\__main__.py", line 3, in <module>
main()
File "C:\Program Files\Programmierung\Mu\pkgs\pgzero\runner.py", line 92, in main
exec(code, mod.__dict__)
File "ninjatest2.py", line 53, in <module>
background2 = pygame.image.load("images\\background2.png")
pygame.error: Couldn't open images\background2.png
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: cHRM chunk does not match sRGB