-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
125 lines (103 loc) · 3.25 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# def ag(a,b,c): return (a+b+c)/3
# mun1=float(input("请输入第一个数:"))
# mun2=float(input("请输入第二个数:"))
# mun3=float(input("请输入第三个数:"))
# r=ag(mun1,mun2,mun3)
# print("平均值为:",r)
# a = int(input("输入头的数量:"))
# b = int(input("输入脚的数量:"))
# s = (b - 2 * a)/2
# q = a - s
# print("鸡有%d只,兔有%d只"%(s,q))
# import random
# list1=[]
# for _ in range(10):
# random_mun=random.randint(1,50)
# list1.append(random_mun)
# print("生成的列表",list)
# list1=sorted(list1[0:5],reverse=True)
# list1[5:]=sorted(list1[5:])
# print("排序后的列表:",list1)
# import os
# import time
# import random
#
# def clear_screen():
# # Clear the terminal screen by printing a special character sequence
# print("\033[H\033[J", end='')
#
# def move_text(text, width, height):
# # Random initial position
# x, y = random.randint(0, width - len(text)), random.randint(0, height - 1)
# dx, dy = 1, 1 # Movement direction
#
# while True:
# clear_screen()
# # Print text at the current position
# print('\n' * y + (' ' * x) + text)
#
# # Update position
# x += dx
# y += dy
#
# # Change direction upon hitting the boundary
# if x <= 0 or x >= width - len(text):
# dx = -dx
# if y <= 0 or y >= height - 1:
# dy = -dy
#
# # Wait for a short period to control the speed of the animation
# time.sleep(0.1)
#
# def main():
# text = "Python是我最喜欢的语言"
# width, height = 80, 24 # Adjust according to your terminal size
#
# try:
# move_text(text, width, height)
# except KeyboardInterrupt:
# clear_screen()
# print("动画已终止。")
#
# if __name__ == "__main__":
# main()
import os
import time
import random
from colorama import init, Fore
os.environ['TERM'] = 'xterm'
def clear_screen():
# Clear the terminal screen by printing a special character sequence
os.system('cls' if os.name == 'nt' else 'clear')
def move_text(text, width, height):
init(autoreset=True) # Initialize Colorama for automatic reset
# Random initial position
x, y = random.randint(0, width - len(text)), random.randint(0, height - 1)
dx, dy = 1, 1 # Movement direction
while True:
clear_screen()
# Randomly choose a color for the text
color = random.choice([Fore.RED, Fore.GREEN, Fore.YELLOW, Fore.BLUE, Fore.MAGENTA, Fore.CYAN])
# Print text at the current position with the chosen color
print('\033[{};{}H{}{}'.format(y + 1, x + 1, color, text))
# Update position
x += dx
y += dy
# Change direction upon hitting the boundary
if x <= 0 or x >= width - len(text):
dx = -dx
if y <= 0 or y >= height - 1:
dy = -dy
# Wait for a short period to control the speed of the animation
time.sleep(0.1)
def main():
a = "Python是我最喜欢的语言"
text = a
width, height = 80, 24 # Adjust according to your terminal size
try:
move_text(text, width, height)
except KeyboardInterrupt:
clear_screen()
print("动画已终止。")
if __name__ == "__main__":
main()