吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 681|回复: 2
收起左侧

[Python 原创] 汉诺塔程序自动运行演示

[复制链接]
backaxe 发表于 2024-5-24 09:06
[Python] 纯文本查看 复制代码
import pygame
import sys
import time

# 游戏初始化
pygame.init()

# 屏幕设置
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("汉诺塔")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)

# 游戏参数

num_disks = 4
tower_positions = [screen_width // 4, screen_width // 2, 3 * screen_width // 4]
disk_height = 20
disk_width_increment = 30
tower_height = screen_height - 50
disk_colors = [(200, 200, 200), (150, 150, 150), (100, 100, 100), (50, 50, 50), (0, 0, 0),
               (255, 100, 100), (100, 255, 100), (100, 100, 255)]
font = pygame.font.SysFont(None, 48)

# 初始状态
towers = [[i for i in range(num_disks, 0, -1)], [], []]
selected_tower = 0
moving_disk = None

# 记录移动步骤的队列
move_steps = []

def draw_towers():
    screen.fill(WHITE)
    
    # 绘制柱子
    for pos in tower_positions:
        pygame.draw.rect(screen, BLACK, (pos - 5, tower_height - (num_disks + 1) * disk_height, 10, (num_disks + 1) * disk_height))

    for i, tower in enumerate(towers):
        for j, disk in enumerate(tower):
            disk_width = disk * disk_width_increment
            color = YELLOW if (moving_disk is not None and i == moving_disk[0] and disk == moving_disk[1]) else disk_colors[disk % len(disk_colors)]
            pygame.draw.rect(screen, color,
                             (tower_positions[i] - disk_width / 2, tower_height - (j + 1) * disk_height,
                              disk_width, disk_height))
    pygame.draw.polygon(screen, RED, [(tower_positions[selected_tower], tower_height), 
                                      (tower_positions[selected_tower] - 10, tower_height + 20), 
                                      (tower_positions[selected_tower] + 10, tower_height + 20)])
    pygame.display.flip()

def move_disk(from_tower, to_tower):
    if len(towers[from_tower]) == 0:
        return
    disk = towers[from_tower][-1]
    if len(towers[to_tower]) == 0 or towers[to_tower][-1] > disk:
        towers[to_tower].append(towers[from_tower].pop())

def hanoi(n, source, target, auxiliary):
    if n == 1:
        move_steps.append((source, target))
    else:
        hanoi(n-1, source, auxiliary, target)
        move_steps.append((source, target))
        hanoi(n-1, auxiliary, target, source)

def auto_move():
    global moving_disk, selected_tower
    if move_steps:
        from_tower, to_tower = move_steps.pop(0)
        selected_tower = from_tower
        if towers[selected_tower]:
            moving_disk = (selected_tower, towers[selected_tower][-1])
            draw_towers()
            time.sleep(0.5)
            move_disk(moving_disk[0], to_tower)
            moving_disk = None
            draw_towers()
            time.sleep(0.5)

def main():
    global selected_tower, moving_disk
    clock = pygame.time.Clock()
    running = True
    
    # 生成移动步骤
    hanoi(num_disks, 0, 2, 1)
    
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        
        auto_move()
        clock.tick(30)

if __name__ == "__main__":
    towers = [[i for i in range(num_disks, 0, -1)], [], []]
    main()


微信截图_20240524090613.png

免费评分

参与人数 3吾爱币 +9 热心值 +3 收起 理由
Empty001 + 1 + 1 我很赞同!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
junjia215 + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Empty001 发表于 2024-5-29 12:07
谢谢@Thanks!
lanail 发表于 2024-5-30 08:21
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-17 12:07

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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