首页 > 移动平台 > 详细

9*9的矩形,中间有个星号,按不同方向键,星星对应移动

时间:2019-05-03 12:06:19      阅读:146      评论:0      收藏:0      [点我收藏+]
"""共9*9个字符,随机打印一个星星,用户输入L/R/u/d,向相应方向移动一位,到达最左/右停止移动,输入EXIT退出"""
#导入随机数
from random import randint


#建一个类
class Star:
def __init__(self):#位置参数
self.row = randint(0,9)
self.col = randint(0,9)

#向左移动
def left(self):
if self.row>=1:
self.row-=1


#向右移动
def right(self):
if self.row<9:
self.row+=1

def up(self):
if self.col>=1:
self.col-=1

def down(self):
if self.col<9:
self.col+=1


#打印
def draw(self):
for i in range(0,10):
if i==self.col:
print("."*self.row+"*"+"."*(9-self.row))
else:
print("."*10)


#初始化
if __name__=="__main__":
a = 0#运行状态开关

star = Star()#建立实例

while a ==0:#运行开关
star.draw()#实例执行类方法
command = input("\n请输入移动方向或退出:L/l or R/r or D/d or U/u or exit")#提示对话
#向左移
if command.lower() =="r":
star.left()
elif command.lower() =="l":#向右移
star.right()
elif command.lower() == "u": # 向上移
star.up()
elif command.lower() == "d": # 向下移
star.down()
elif command.lower() =="exit": #退出
a+=1
else:print("你的输入有误!")

9*9的矩形,中间有个星号,按不同方向键,星星对应移动

原文:https://www.cnblogs.com/skbarcode/p/10804454.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!