This commit is contained in:
Luthics 2023-06-26 14:46:13 +08:00
commit acba386d83
3 changed files with 115 additions and 0 deletions

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# Python AI | 小学期 19 班
使用 DayX 文件夹标注每一天的内容
Day1 - 2023/6/26

53
day1/xie.py Normal file
View File

@ -0,0 +1,53 @@
import asyncio
import queue
import random
import time
q = queue.Queue(10)
make_num = 3
use_num = 2
speed = 0.8
async def maker():
while True:
while not q.full():
ids = []
# 判断队列中是否有足够的位置供生产
if(q.qsize() > q.maxsize - make_num):
print('× 空间不足')
break
for i in range(make_num):
id = random.randint(1, 100)
q.put(id)
ids.append(id)
time.sleep(speed)
print("+ " + ','.join([str(i) for i in ids]))
print("^ "+ ','.join([str(i) for i in q.queue]))
await user()
async def user():
while True:
while not q.empty():
ids = []
# 判断队列中是否有足够的产品供消费
if(q.qsize() < use_num):
print('× 产品不足')
break
for i in range(use_num):
id = q.get()
ids.append(id)
time.sleep(speed)
print('- ' + ','.join([str(i) for i in ids]))
print("^ "+ ','.join([str(i) for i in q.queue]))
await maker()
async def main():
# 创建并启动两个协程
await asyncio.gather(maker(), user())
if __name__ == '__main__':
asyncio.run(main())

59
main.py Normal file
View File

@ -0,0 +1,59 @@
import pygame
# 初始化pygame
pygame.init()
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
# 设置窗口大小和标题
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame光波")
# 循环标志
done = False
# 时钟对象
clock = pygame.time.Clock()
# 圆心位置列表和半径列表
circles = []
# 游戏循环
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
# 当左键被按下时,将圆心位置和半径保存到列表中
circle_pos = event.pos
circle_radius = 1
circles.append((circle_pos, circle_radius))
# 填充背景色
screen.fill(BLACK)
# 遍历圆心位置列表,绘制每一个光波
for pos, radius in circles:
pygame.draw.circle(screen, YELLOW, pos, radius, 5)
radius += 5
# 如果圆的半径已经大于一定值,从列表中删除该光波
if radius > 200:
circles.remove((pos, radius))
# 更新圆的半径
else:
circles[circles.index((pos, radius))] = (pos, radius)
# 更新屏幕
pygame.display.flip()
# 控制帧率
clock.tick(60)
# 退出游戏
pygame.quit()