找回密码
 中文实名注册
搜索
查看: 104|回复: 0

让你嗨起来

[复制链接]

11

主题

13

回帖

848

积分

高级会员

积分
848
发表于 2025-7-25 16:32:01 | 显示全部楼层 |阅读模式
[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()

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

快速回复 返回顶部 返回列表