首页 > 解决方案 > 看不见的坏人?py游戏无法显示坏人

问题描述

大家好,我在游戏中遇到了一个问题,坏人是看不见的。我不确定我在哪里犯了错误。我收到的错误是 Traceback(最近一次通话最后一次):编辑:请原谅我的错误,因为我没有显示适当的部分。

文件“c:\appdata\local\programs\python\python37-32\lib\site-packages\pgzero\clock.py”,第 168 行,在 tick cb() 文件“gamemap1.py”,第 636 行,在 ghost_start ghost_map[ghost_y][ghost_x] = 49 + (room_the_player_is_in % 3) IndexError: list assignment index out of range


def start_room():
    if room_the_player_is_in == 42: 
        airlock_door_frame = 0
        clock.schedule_interval(ghost_start, 0.05)
        ghost_start()

def deplete_health(penalty):
    global health, game_over
    if game_over:
        return # cant continue lossign health if dead
    health = health - penalty
    draw_health()
    if health < 1:
        end_the_game("You're dead")

def game_loop():
    global user_x, user_y, room_the_player_is_in
    global from_player_x, from_player_y
    global sprite_image 
    global selected_item, item_carrying, health 
    global player_offset_x, player_offset_y
    global frame_of_sprite, direction

    if game_over:
        return

    if frame_of_sprite > 0:
        frame_of_sprite += 1
        time.sleep(0.05)
        if frame_of_sprite == 3:
            frame_of_sprite = 0
            player_offset_x = 0
            player_offset_y = 0

# save player's current position
    old_player_x = user_x
    old_player_y = user_y

######################################################################################
  # if is standing somewhere they shouldn't, move them back.
    if room_map[user_y][user_x] not in items_player_may_stand_on or ghost_map[user_y][user_x] != 0:
        user_x = old_player_x
        user_y = old_player_y
        frame_of_sprite = 0

    if room_map[user_y][user_x] == 29: # if player on winning dsapceship start win game 
       game_completion_sequence()

    if direction == "right" and frame_of_sprite > 0:
        player_offset_x = -1 + (0.25 * frame_of_sprite)
    if direction == "left" and frame_of_sprite > 0:
        player_offset_x = 1 - (0.25 * frame_of_sprite)
    if direction == "up" and frame_of_sprite > 0:
        player_offset_y = 1 - (0.25 * frame_of_sprite)
    if direction == "down" and frame_of_sprite > 0:
        player_offset_y = -1 + (0.25 * frame_of_sprite)


###############################Show image in generated window#######################################################

def draw_image(image, y, x):
    screen.blit(
        image,
        (top_left_x + (x * ROOM_SIZE),
         top_left_y + (y * ROOM_SIZE) - image.get_height())
        )

def draw_player():
    sprite_image = SPRITE[direction][frame_of_sprite]
    draw_image(sprite_image, user_y + player_offset_y,
               user_x + player_offset_x)

def draw():
    if game_over:
        return
##############################Show image in generated window#######################################################

    # Clear the game arena area.
    box = Rect((100, 150), (800, 600))
    screen.draw.filled_rect(box, PINK)
    box = Rect ((0, 0), (800, top_left_y + (room_height - 1)*30))
    screen.surface.set_clip(box)
    floor_type = get_floor_type()

    for y in range(room_height): # Lay down floor tiles, then items on floor.
        for x in range(room_width):
            draw_image(items[floor_type][0], y, x)
            if room_map[y][x] in items_player_may_stand_on: 
                draw_image(items[room_map[y][x]][0], y, x)
            # Next line enables shadows to fall on top of objects on floor

    for y in range(room_height):
        for x in range(room_width):
            item_here = room_map[y][x]
            # Player cannot walk on 255: it marks spaces used by wide objects.
            if item_here not in items_player_may_stand_on + [265]:
                image = items[item_here][0]# no vaule in

                if (room_the_player_is_in in planet_surface 
                    and y == room_height - 1
                    and room_map[y][x] == 1) or(room_the_player_is_in not in planet_surface
                    and y == room_height - 1
                    and room_map[y][x] == 1
                    and x > 0
                    and x < room_width - 1): 
                    # Add transparent wall image in the front row.
                    image = inviso_wall[wall_transparency_frame]

                draw_image(image, y, x)
                ghost_here = ghost_map[y][x]
                if ghost_here != 0: # If there's a hazard at this position
                    draw_image(items[ghost_here][0], y, x)###issue here???
        if (user_y == y):
                draw_player()
    screen.surface.set_clip(None)

def adjust_wall_transparency():
    global wall_transparency_frame

    if (user_y == room_height - 2
        and room_map[room_height - 1][user_x] == 1
        and wall_transparency_frame < 4):  
        wall_transparency_frame += 1 # Fade wall out.

    if ((user_y < room_height - 2
            or room_map[room_height - 1][user_x] != 1)
            and wall_transparency_frame > 0):
        wall_transparency_frame -= 1 # Fade wall in.

def show_text(text_to_show, line_number):
    if game_over:
        return
    text_lines = [15, 50]



################################GAME COMPLETION SEQUANCE#############################################################################################################

def game_completion_sequence():
    global game_over 
    game_over = True
    sounds.winnerr.play(0)

    screen.draw.text("MAZE", (200, 400), color = "white",
                     fontsize = 128)
    screen.draw.text("COMPLETE", (145, 480), color = "white",
                     fontsize = 128)

    game_over = True# win the maze

def end_the_game(reason):
    global game_over
    show_text(reason, 1)
    game_over = True
    sounds.loser.play()
    screen.draw.text("GAME OVER", (80, 400), color = "white",
                     fontsize = 160)






################################BAD GUY DARA ########################################################################################################################

ghost_data = {# dictionary
    #room number: [[y, x, direction, bounce addition to direction]]
    26: [[1, 8, 2, 1], [7, 3, 4, 1]],
    27: [[1, 8, 2, 1], [7, 3, 4, 1]],
    28: [[1, 8, 2, 1], [7, 3, 4, 1]], 
    29: [[1, 8, 2, 1], [7, 3, 4, 1]],
    30: [[1, 8, 2, 1], [7, 3, 4, 1]],
    31: [[1, 8, 2, 1], [7, 3, 4, 1]],
    34: [[5, 1, 1, 1], [5, 5, 1, 2]],
    35: [[4, 4, 1, 2], [2, 5, 2, 2]],
    36: [[2, 1, 2, 2]], 
    37: [[1, 8, 2, 1], [7, 3, 4, 1]],
    38: [[1, 8, 2, 1], [7, 3, 4, 1]],
    39: [[1, 8, 2, 1], [7, 3, 4, 1]],
    40: [[3, 1, 3, -1], [6, 5, 2, 2], [7, 5, 4, 2]],
    41: [[4, 5, 2, 2], [6, 3, 4, 2], [8, 1, 2, 2]],
    44: [[2, 1, 2, 2], [4, 3, 2, 2], [6, 5, 2, 2]],
    45: [[2, 1, 2, 2], [4, 3, 2, 2], [6, 5, 2, 2]],
    46: [[2, 1, 2, 2]],
    47: [[2, 1, 2, 2], [4, 3, 2, 2], [6, 5, 2, 2]],
    48: [[1, 8, 3, 2], [8, 8, 1, 2], [3, 9, 3, 2]],
    49: [[2, 1, 2, 2], [4, 3, 2, 2], [6, 5, 2, 2]]
    }



def ghost_start():
    global current_room_ghost_list, ghost_map
    if room_the_player_is_in in ghost_data.keys():
        current_room_ghost_list = ghost_data[room_the_player_is_in]
        for ghost in current_room_ghost_list:
            ghost_y = ghost[0]
            ghost_x = ghost[1]
            ghost_map[ghost_y][ghost_x] = 49 + (room_the_player_is_in % 3)
        clock.schedule_interval(ghost_move, 0.20)

def ghost_move():
    global current_room_ghost_list, ghost_data, ghost_map, old_player_x, old_player_y

    if game_over:
        return

    for ghost in current_room_ghost_list:
        ghost_y = ghost[0]
        ghost_x = ghost[1]
        ghost_direction = ghost[2]

        old_ghost_x = ghost_x
        old_ghost_y = ghost_y
        ghost_map[old_ghost_y][old_ghost_x] = 0 

        if ghost_direction == 1: # up
            ghost_y -= 1
        if ghost_direction == 2: # right
            ghost_x += 1
        if ghost_direction == 3: # down
            ghost_y += 1
        if ghost_direction == 4: # left
            ghost_x -= 1

        ghost_should_bounce = False

        if (ghost_y == user_y and ghost_x == user_x) or (ghost_y == from_player_y and ghost_x == from_player_x and frame_of_sprite > 0):
            sounds.ouch.play()######################################################
            deplete_health(1)
            ghost_should_bounce = True

        # Stop going out of the doors
        if ghost_x == room_width: 
            ghost_should_bounce = True
            ghost_x = room_width - 1
        if ghost_x == -1: 
            ghost_should_bounce = True
            ghost_x = 0
        if ghost_y == room_height:
            ghost_should_bounce = True
            ghost_y = room_height - 1
        if ghost_y == -1:
            ghost_should_bounce = True
            ghost_y = 0

        # Stop when hazard hits scenery or another hazard.
        if room_map[ghost_y][ghost_x] not in items_player_may_stand_on or ghost_map[ghost_y][ghost_x] != 0:
            ghost_should_bounce = True

        if ghost_should_bounce:
            ghost_y = old_ghost_y # Move back to last valid position.
            ghost_x = old_ghost_x
            ghost_direction += ghost[3]
            if ghost_direction > 4:
                ghost_direction -= 4
            if ghost_direction < 1:
                ghost_direction += 4
            ghost[2] = ghost_direction

        ghost_map[ghost_y][ghost_x] = 49 + (room_the_player_is_in % 3)
        ghost[0] = ghost_y
        ghost[1] = ghost_x



标签: python

解决方案


推荐阅读