简易拖地软件代码设计(Python实现)

2025-05-24ASPCMS社区 - fjmyhfvclm

介绍一个简易的"拖地"模拟软件的Python实现。这个程序通过控制台界面模拟拖地过程,可以记录拖地区域和拖地路径。

程序设计思路

这个简易拖地软件的主要功能:

定义房间布局(矩形区域)

模拟拖把移动路径

记录已拖地区域

显示拖地进度

完整代码实现

python

import os

import time

class FloorMoppingSimulator:

def __init__(self, width=10, height=8):

"""

初始化拖地模拟器

:param width: 房间宽度

:param height: 房间高度

"""

self.width = width

self.height = height

self.room = [[0 for _ in range(width)] for _ in range(height)] # 0表示未拖,1表示已拖

self.current_x = 0

self.current_y = 0

self.path = [] # 记录拖地路径

def display_room(self):

"""显示当前房间状态"""

os.system('cls' if os.name == 'nt' else 'clear') # 清屏

print("简易拖地模拟器")

print("=" * (self.width + 10))

# 显示顶部坐标

print(" ", end="")

for x in range(self.width):

print(f"{x:2}", end="")

print("\n" + "=" * (self.width + 10))

代码资料参考来源:https://gitee.com/ssak/tda

# 显示房间状态

for y in range(self.height):

print(f"{y:2} ", end="")

for x in range(self.width):

if x == self.current_x and y == self.current_y:

print("M ", end="") # M表示拖把当前位置

elif self.room[y][x] == 1:

print(". ", end="") # .表示已拖

else:

print("# ", end="") # #表示未拖

print()

print("\n操作指南:")

print("w: 上, s: 下, a: 左, d: 右, q: 退出, r: 重置, p: 自动拖地")

print(f"已拖地比例: {self.get_cleaned_percentage():.1f}%")

def move(self, direction):

"""移动拖把"""

new_x, new_y = self.current_x, self.current_y

if direction == 'w' and self.current_y > 0:

new_y -= 1

elif direction == 's' and self.current_y < self.height - 1:

new_y += 1

elif direction == 'a' and self.current_x > 0:

new_x -= 1

elif direction == 'd' and self.current_x < self.width - 1:

new_x += 1

else:

print("无法移动,已到达边界!")

return False

代码参考资料来源:https://gitee.com/ssak/tdb

# 标记新位置为已拖地

self.room[new_y][new_x] = 1

self.path.append((new_x, new_y))

self.current_x, self.current_y = new_x, new_y

return True

def auto_mop(self):

"""自动拖地模式(蛇形)"""

print("启动自动拖地模式...")

direction = 1 # 1表示向右,-1表示向左

x, y = 0, 0

while True:

# 标记当前位置为已拖地

self.room[y][x] = 1

self.path.append((x, y))

self.current_x, self.current_y = x, y

self.display_room()

time.sleep(0.1) # 减慢速度以便观察

# 到达边界时改变方向

if direction == 1: # 向右

if x == self.width - 1:

direction = -1

if y < self.height - 1:

y += 1

else:

break

else:

x += 1

else: # 向左

if x == 0:

direction = 1

if y < self.height - 1:

y += 1

else:

break

else:

x -= 1

代码参考资料来源:https://gitee.com/ssak/tdc

def get_cleaned_percentage(self):

"""计算已拖地比例"""

total = self.width * self.height

cleaned = sum(sum(row) for row in self.room)

return (cleaned / total) * 100

def reset(self):

"""重置拖地状态"""

self.room = [[0 for _ in range(self.width)] for _ in range(self.height)]

self.current_x, self.current_y = 0, 0

self.path = []

print("已重置拖地状态")

def run(self):

"""运行拖地模拟器"""

while True:

self.display_room()

cmd = input("请输入操作: ").lower()

if cmd == 'q':

print("退出拖地模拟器")

break

elif cmd == 'r':

self.reset()

elif cmd == 'p':

self.auto_mop()

elif cmd in ['w', 's', 'a', 'd']:

self.move(cmd)

else:

print("无效输入,请使用 w/a/s/d 移动,r 重置,p 自动拖地,q 退出")

if __name__ == "__main__":

print("欢迎使用简易拖地模拟器")

print("默认房间大小: 10x8 (可通过修改代码调整)")

input("按回车键开始...")

simulator = FloorMoppingSimulator()

simulator.run()

代码参考资料来源:https://gitee.com/ssak/tdd

代码说明

初始化:

创建指定大小的房间(默认10x8)

初始化拖把位置和拖地状态矩阵

主要功能:

move(): 手动控制拖把移动

auto_mop(): 自动蛇形拖地模式

get_cleaned_percentage(): 计算已拖地比例

display_room(): 显示当前房间状态和操作指南

用户交互:

使用w/a/s/d控制拖把移动

r重置拖地状态

p启动自动拖地模式

q退出程序

扩展建议

添加障碍物功能,模拟家具等不可拖地区域

实现更智能的路径规划算法

添加拖地效率统计(如拖地速度、覆盖率等)

开发图形界面版本(使用Pygame或Tkinter)

这个简易拖地软件虽然功能简单,但包含了基本的拖地模拟逻辑,可以作为更复杂机器人拖地软件的基础原型。

全部评论