[C++] 纯文本查看 复制代码 import pygame
import math
# 初始化 Pygame
pygame.init()
# 设置窗口大小
width, height = 800, 600
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Rotating Rectangle")
# 设定颜色
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
# 定义矩形的参数
rect_width, rect_height = 100, 50
angle = 0
# 主循环
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 清屏
window.fill(BLACK)
# 计算矩形的旋转位置
angle += 300 # 增加旋转角度
if angle >= 360: # 角度重置
angle = 0
# 计算旋转矩形的四个顶点
center_x, center_y = width // 2, height // 2
half_width, half_height = rect_width // 2, rect_height // 2
# 使用三角函数计算矩形的四个顶点
points = [
(center_x + half_width * math.cos(math.radians(angle)) - half_height * math.sin(math.radians(angle)),
center_y + half_width * math.sin(math.radians(angle)) + half_height * math.cos(math.radians(angle))),
(center_x - half_width * math.cos(math.radians(angle)) - half_height * math.sin(math.radians(angle)),
center_y - half_width * math.sin(math.radians(angle)) + half_height * math.cos(math.radians(angle))),
(center_x - half_width * math.cos(math.radians(angle)) + half_height * math.sin(math.radians(angle)),
center_y - half_width * math.sin(math.radians(angle)) - half_height * math.cos(math.radians(angle))),
(center_x + half_width * math.cos(math.radians(angle)) + half_height * math.sin(math.radians(angle)),
center_y + half_width * math.sin(math.radians(angle)) - half_height * math.cos(math.radians(angle))),
]
# 绘制矩形
pygame.draw.polygon(window, BLUE, points)
# 刷新显示
pygame.display.flip()
pygame.time.wait(10)
# 退出 Pygame
pygame.quit()
|