首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将图像加载到游戏中

无法将图像加载到游戏中
EN

Stack Overflow用户
提问于 2022-01-19 01:16:18
回答 1查看 124关注 0票数 1

在这里输入图像描述我正在从一本叫做“电脑编码python For Kids”的书里学到巨蟒--这是一本书,https://www.amazon.com/Computer-Coding-Python-Games-Kids/dp/0241317797--基本上就是你试着点击红色雪花的地方,然后才能找到它。我的问题是,即使我把图像和代码一起存储在同一个文件中,它似乎也找不到图像。如果我尝试编写(C:\Users\Nigel\Desktop\python\查找红色雪花\image\红雪),则会得到Unicode错误。这是我的密码

代码语言:javascript
复制
import pgzrun
import random

FONT_COLOUR = (255,255,255)
WIDTH = 1920
HEIGHT = 1080
CENTER_X = WIDTH/2
CENTER_Y = HEIGHT/2
CENTER = (CENTER_X,CENTER_Y)
FINAL_LEVEL = 15
START_SPEED = 10
COLOURS = ["green","blue"]

game_over = False
game_complete = False
current_level = 1
snows =[]
animations =[]

def draw():
    global snows,current_level,game_over,game_complete
    screen.clear()
    screen.blit("moutain",(0,0))
    if game_over:
        display_message("GAME OVER!","Try again.")
    elif game_complete:
        display_message("YOU WON!","Well done.")
    else:
        for snow in snows:
            snow.draw()


def update():
    global snows
    if len(snows) == 0:
        snows = make_snows(current_level)

def make_snows(number_of_extra_snows):
    colours_to_create=get_colours_to_create(number_of_extra_snows)
    new_snows = create_snows(colours_to_create)
    layout_snows(new_snows)
    animate_snows(new_snows)
    return new_snows

def get_colours_to_create(number_of_extra_snows):
    colours_to_create = ["red"]
    for i in range(0,number_of_extra_snows):
        random_colour = random.choice(COLOURS)
        colours_to_create.append(random_colour)
    return colours_to_create

def create_snows(colours_to_create):
    new_snows=[]
    for colour in colours_to_create:
        snow = Actor(r"C:\Users\Nigel\Desktop\python-games\find the red snowflake\image\red-snow") 
        new_snows.append(snow)
    return new_snows

def layout_snows(snows_to_layout):
    number_of_gaps = len(snows_to_layout) + 1
    gap_size = WIDTH/number_of_gaps
    random.shuffle(snoews_to_layout)
    for index,snow in enumerate(snows_to_layout):
        new_x_pos = (index + 1) * gap_size
        snow.x = new_x_pos

def animate_snows(snows_to_animate):
    for snow in snows_to_animate:
        duration = START_SPEED - current_level
        snow.anchor = ("center","bottom")
        animation = animate(snow,duration=duration, on_finished=handle_game_over,y=HEIGHT)
        animations.append(animation)

def handle_game_over():
    global game_over
    game_over = True

def on_mouse_down(pos):
    global snows,current_level
    for snow in snows:
        if snow.collidepoint(pos):
            if "red" in snow.image:
                red_snow_click()
            else:
                handle_game_over()

def red_snow_click():
    global current_level,snows, animations,game_complete
    stop_animations(animations)
    if currents_level == FINAL_LEVEL:
        game_complete = True
    else:
        current_level = current_level + 1
        snows = []
        animations =[]

def stop_animations(animations_to_stop):
    for animation in animations_to_stop:
        if animation.running:
            animation.stop()

def display_message(heading_text,sub_heading_text):
    screen.draw.text(heading_text, fontsize = 60, center=CENTRE,COLOR=FONT_COLOUR)
    screen.draw.text(sub_heading_text,
                     fontsize=30,
                     center=(CENTRE_X,CENTER_Y +30),
                     color=FONT_COLOUR)
pgzrun.go()

这是错误信息

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:\Users\Nigel\Desktop\python-games\find the red snowflake\image\find the red snowflake.py", line 108, in <module>
    pgzrun.go()
  File "C:\Users\Nigel\AppData\Roaming\Python\Python310\site-packages\pgzrun.py", line 31, in go
    run_mod(mod)
  File "C:\Users\Nigel\AppData\Roaming\Python\Python310\site-packages\pgzero\runner.py", line 113, in run_mod
    PGZeroGame(mod).run()
  File "C:\Users\Nigel\AppData\Roaming\Python\Python310\site-packages\pgzero\game.py", line 217, in run
    self.mainloop()
  File "C:\Users\Nigel\AppData\Roaming\Python\Python310\site-packages\pgzero\game.py", line 252, in mainloop
    update(dt)
  File "C:\Users\Nigel\AppData\Roaming\Python\Python310\site-packages\pgzero\game.py", line 194, in <lambda>
    return lambda dt: update()
  File "C:\Users\Nigel\Desktop\python-games\find the red snowflake\image\find the red snowflake.py", line 36, in update
    snows = make_snows(current_level)
  File "C:\Users\Nigel\Desktop\python-games\find the red snowflake\image\find the red snowflake.py", line 40, in make_snows
    new_snows = create_snows(colours_to_create)
  File "C:\Users\Nigel\Desktop\python-games\find the red snowflake\image\find the red snowflake.py", line 55, in create_snows
    snow = Actor(r"C:\Users\Nigel\Desktop\python-games\find the red snowflake\image\red-snow")
  File "C:\Users\Nigel\AppData\Roaming\Python\Python310\site-packages\pgzero\actor.py", line 88, in __init__
    self.image = image
  File "C:\Users\Nigel\AppData\Roaming\Python\Python310\site-packages\pgzero\actor.py", line 103, in __setattr__
    return object.__setattr__(self, attr, value)
  File "C:\Users\Nigel\AppData\Roaming\Python\Python310\site-packages\pgzero\actor.py", line 218, in image
    self._orig_surf = self._surf = loaders.images.load(image)
  File "C:\Users\Nigel\AppData\Roaming\Python\Python310\site-packages\pgzero\loaders.py", line 120, in load
    self.validate_root(name)
  File "C:\Users\Nigel\AppData\Roaming\Python\Python310\site-packages\pgzero\loaders.py", line 99, in validate_root
    raise KeyError(
KeyError: "No 'images' directory found to load image 'C:\\Users\\Nigel\\Desktop\\python-games\\find the red snowflake\\image\\red-snow'."
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-19 01:22:36

如果您得到了该路径的unicode错误,这意味着您可能没有正确地键入它--您在路径中看到的那些反斜杠通常用于“转义字符”,当您试图键入文件路径时,它们可能会产生干扰。如果您还没有按以下方式键入路径,请尝试以下解决方案之一:

代码语言:javascript
复制
r"C:\Users\Nigel\Desktop\python-games\find the red snowflake\image\red-snow"         # type r before the opening quote to make it a raw-string, which ignores escape characters
"C:\\Users\\Nigel\\Desktop\\python-games\\find the red snowflake\\image\\red-snow"   # use double-slashes to escape being an escape character

另外,请确保在路径中为图像添加文件扩展名。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70764259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档