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

"""共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("你的输入有误!")

猜你喜欢

转载自www.cnblogs.com/skbarcode/p/10804454.html
今日推荐