python满屏 飘爱心 源代码
时间: 2023-08-24 22:08:08 浏览: 155
```python
import random
import time
# 获取屏幕宽度和高度
from ctypes import windll, Structure, c_long, byref
class POINT(Structure):
_fields_ = [("x", c_long), ("y", c_long)]
def queryMousePosition():
pt = POINT()
windll.user32.GetCursorPos(byref(pt))
return pt.x, pt.y
width = windll.user32.GetSystemMetrics(0)
height = windll.user32.GetSystemMetrics(1)
# 爱心字符
heart = "\u2665"
# 循环打印爱心
while True:
# 获取随机位置
x = random.randint(0, width-1)
y = random.randint(0, height-1)
# 判断是否在鼠标附近,如果是则跳过本次循环
mouse_x, mouse_y = queryMousePosition()
if abs(x - mouse_x) < 100 and abs(y - mouse_y) < 100:
continue
# 打印爱心
print(heart, end="", flush=True)
# 设置随机延时
time.sleep(random.uniform(0.1, 0.5))
```
注意:在 Windows 下运行时需要使用管理员权限,否则会报错。