找回密码
 中文实名注册
搜索
查看: 70|回复: 0

地图

[复制链接]

6

主题

0

回帖

3万

积分

超级版主

积分
33838
发表于 2025-10-7 17:19:06 | 显示全部楼层 |阅读模式
[Python] 纯文本查看 复制代码
import turtle

# ---------------------------
# 配置区
# ---------------------------

# 画布配置
WIDTH, HEIGHT = 1200, 1000

# 格子配置
GRID_W, GRID_H = 8, 5          # 网格数:8列 x 5行,共40格
CELL_SIZE = 100                # 每格边长
MARGIN_LEFT = - (GRID_W * CELL_SIZE) // 2
MARGIN_TOP = (GRID_H * CELL_SIZE) // 2

# 颜色定义
COLOR_GREEN = "#6ee7a3"   # 绿色格子(题目格 - 8格)
COLOR_YELLOW = "#f7e46a"  # 黄色格子(幸运格 - 4格)
COLOR_RED = "#f58f8f"     # 红色格子(挑战格 - 12格)
COLOR_WATER = "#a5d8ff"   # 海洋背景
COLOR_SAND = "#f4e2c0"    # 沙滩颜色
COLOR_TREE = "#228B22"    # 树颜色
COLOR_TREASURE = "#FFD700"  # 宝箱颜色

# 格子类型分布(蛇形路径,绿色8格,黄色4格,红色12格,其余为空格)
GRID_TYPES = [
    0, 0, 0, 2, 1, 0, 0, 1,   # 第一行(从左到右)
    3, 0, 1, 0, 0, 0, 3, 2,   # 第二行(从右到左)
    1, 3, 0, 0, 1, 0, 2, 0,   # 第三行(从左到右)
    0, 1, 0, 1, 3, 0, 3, 0,   # 第四行(从右到左)
    1, 0, 3, 0, 2, 1, 0, 0    # 第五行(从左到右)
]

# ---------------------------
# 绘制工具函数
# ---------------------------


def move_to_grid(i):
    """计算格子左下角的坐标 (x, y)。蛇形路径布局。"""
    row = i // GRID_W
    col = i % GRID_W
    if row % 2 == 1:  # 奇数行反向
        col = GRID_W - 1 - col
    x = MARGIN_LEFT + col * CELL_SIZE
    y = MARGIN_TOP - row * CELL_SIZE
    return x, y


def draw_square(t, x, y, size, fill_color, border_color="black"):
    """绘制一个正方形单元格(左下角为参考点)。"""
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.fillcolor(fill_color)
    t.pencolor(border_color)
    t.begin_fill()
    for _ in range(4):
        t.forward(size)
        t.left(90)
    t.end_fill()


def label_square(t, x, y, size, text):
    """在格子中心添加文字标签。"""
    t.penup()
    t.goto(x + size * 0.5, y + size * 0.5 - 10)
    t.color("black")
    t.write(text, align="center", font=("Arial", 14, "bold"))


def draw_background():
    """绘制岛屿背景和海洋。"""
    screen.bgcolor(COLOR_WATER)
    t = turtle.Turtle()
    t.speed(0)
    t.penup()
    t.goto(-WIDTH // 2, HEIGHT // 2)
    t.pendown()
    t.fillcolor(COLOR_SAND)
    t.begin_fill()
    for _ in range(2):
        t.forward(WIDTH)
        t.right(90)
        t.forward(HEIGHT)
        t.right(90)
    t.end_fill()
    t.hideturtle()


def draw_path():
    """绘制蛇形棋盘路径。"""
    t = turtle.Turtle()
    t.speed(0)
    t.hideturtle()
    for i in range(40):
        x, y = move_to_grid(i)
        gtype = GRID_TYPES[i]
        if gtype == 1:
            fill_color = COLOR_GREEN
            label = "题"
        elif gtype == 2:
            fill_color = COLOR_YELLOW
            label = "运"
        elif gtype == 3:
            fill_color = COLOR_RED
            label = "战"
        else:
            fill_color = "white"
            label = ""

        # 绘制格子
        draw_square(t, x, y, CELL_SIZE, fill_color)
        label_square(t, x, y, CELL_SIZE, f"{i + 1}\n{label}")

    # 标注起点和终点
    t.penup()
    start_x, start_y = move_to_grid(0)
    end_x, end_y = move_to_grid(39)
    t.goto(start_x + CELL_SIZE * 0.5, start_y + CELL_SIZE * 0.5)
    t.color("blue")
    t.write("起点", align="center", font=("Arial", 14, "bold"))
    t.goto(end_x + CELL_SIZE * 0.5, end_y + CELL_SIZE * 0.5)
    t.color("red")
    t.write("终点", align="center", font=("Arial", 14, "bold"))







# ---------------------------
# 主函数
# ---------------------------


def draw_map():
    """绘制《数学冒险岛》完整地图。"""
    draw_background()
    draw_path()
    
    turtle.done()

# ---------------------------
# 执行
# ---------------------------


if __name__ == "__main__":
    screen = turtle.Screen()
    screen.setup(WIDTH, HEIGHT)
    draw_map()
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

快速回复 返回顶部 返回列表