python 我不能在两个房间之间造一条隧道

jjhzyzn0  于 2023-03-07  发布在  Python
关注(0)|答案(1)|浏览(87)

我在为我的游戏写一个地下城的剧本

import random
from typing import Iterator, Tuple
import tcod

from Gamemap import GameMap
import tile_types

class RectangularRoom:
    def __init__(self, x: int, y: int, width: int, height: int):
        self.x1 = x
        self.y1 = y
        self.x2 = x + width
        self.y2 = y + height

    @property
    def inner(self) -> Tuple[slice, slice]:
        """Return the inner area of this room as a 2D array index."""
        return slice(self.x1 + 1, self.x2), slice(self.y1 + 1, self.y2)

def tunnel_between(
    start: Tuple[int, int], end: Tuple[int, int]
) -> Iterator[Tuple[int, int]]:
    """Return an L-shaped tunnel between these two points."""
    x1, y1 = start
    x2, y2 = end
    if random.random() < 0.5:
        corner_x, corner_y = x2, y1
    else:
        corner_x, corner_y = x1, y2
    for x, y in tcod.los.bresenham((x1, y1), (corner_x, corner_y)).tolist():
        yield x, y
    for x, y in tcod.los.bresenham((corner_x, corner_y), (x2, y2)).tolist():
        yield x, y

def generate_dungeon(map_width, map_height) -> GameMap:
    dungeon = GameMap(map_width, map_height)

    room_1 = RectangularRoom(x=20, y=15, width=10, height=15)
    room_2 = RectangularRoom(x=35, y=15, width=11, height=19)

    dungeon.tiles[room_1.inner] = tile_types.floor
    dungeon.tiles[room_2.inner] = tile_types.floor

    for x, y in tunnel_between(room_2.center, room_1.center):
        dungeon.tiles[x, y] = tile_types.floor

    return dungeon

但是,它给出了这个错误

File "C:\Users\JOAO\PycharmProjects\pythings\venv\ProtoRED\procgen.py", line 45, in generate_dungeon
    for x, y in tunnel_between(room_2.center, room_1.center):
AttributeError: 'RectangularRoom' object has no attribute 'center'

我试图解决这个问题有一段时间了,但是,没有任何帮助。甚至不重写整个代码,有人能帮助我吗?

5w9g7ksd

5w9g7ksd1#

您需要一个.center属性

我认为它应该是2个整数的元组。

class RectangularRoom:
    def __init__(self, x: int, y: int, width: int, height: int):
        self.x1 = x
        self.y1 = y
        self.x2 = x + width
        self.y2 = y + height
        self.center = (int(x + width/2), int(y + height/2))

相关问题