以下是一个示例程序,它创建了8个初始坐标相同的点,并按照不同角度和相同速度移动。

import pygame
import math

# 初始化Pygame
pygame.init()

# 创建窗口
win_width = 800
win_height = 600
window = pygame.display.set_mode((win_width, win_height))
pygame.display.set_caption("Moving Points")

# 设置颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# 创建点对象
class Point:

    def __init__(self, x, y, angle, speed):
        self.x = x
        self.y = y
        self.angle = angle
        self.speed = speed

    def move(self):
        # 根据角度和速度计算新坐标
        self.x += self.speed * math.cos(math.radians(self.angle))
        self.y += self.speed * math.sin(math.radians(self.angle))

        # 碰到边缘时反弹
        if self.x < 0 or self.x > win_width:
            self.angle = 180 - self.angle
        if self.y < 0 or self.y > win_height:
            self.angle = 360 - self.angle

    def draw(self, surface):
        # 在窗口上画一个圆点
        pygame.draw.circle(surface, WHITE, (int(self.x), int(self.y)), 5)

# 创建8个点对象
points = []
for i in range(8):
    angle = i * 45
    speed = 5
    point = Point(win_width/2, win_height/2, angle, speed)
    points.append(point)

# 游戏循环
clock = pygame.time.Clock()
running = True
while running:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 清空窗口
    window.fill(BLACK)

    # 移动和绘制点
    for point in points:
        point.move()
        point.draw(window)

    # 刷新窗口
    pygame.display.update()

    # 控制帧率
    clock.tick(60)

# 退出Pygame
pygame.quit()

这个程序创建了一个名为Point的点对象,它具有xy坐标、角度和速度属性,以及move()draw()方法。在游戏循环中,它创建了8个点对象,并以每45度为间隔设置了不同的角度和相同的速度。然后每帧更新每个点的位置,并在窗口上绘制它们。当点碰到窗口边缘时,它会反弹。

运行这个程序,你应该可以看到8个点沿着不同的方向移动,直到它们离开窗口。


原文地址: https://www.cveoy.top/t/topic/rtg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录