python - 控制椭圆路径周围的旋转时间 - Pygame
问题描述
我需要使蓝色圆圈比粉色圆圈快 4 倍。有人可以建议我这样做的方法吗?
现在,两个圆圈(粉红色和蓝色)在此图像中显示的椭圆路径中围绕红色圆圈旋转。
这是我的代码。
#Elliptical orbits
import pygame
import math
import sys
pygame.init()
screen = pygame.display.set_mode((1000, 300))
pygame.display.set_caption("Elliptical orbit")
clock = pygame.time.Clock()
while(True):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
xRadius = 250
yRadius = 100
x2Radius = 100
y2Radius = 50
for degree in range(0,360,10):
x1 = int(math.cos(degree * 2 * math.pi / 360) * xRadius) + 300
y1 = int(math.sin(degree * 2 * math.pi / 360) * yRadius) + 150
x2 = int(math.cos(degree * 2 * math.pi / 360) * x2Radius) + 300
y2 = int(math.sin(degree * 2 * math.pi / 360) * y2Radius) + 150
screen.fill((0, 0, 0))
pygame.draw.circle(screen, (255, 0, 0), [300, 150], 35)
pygame.draw.ellipse(screen, (255, 255, 255), [50, 50, 500, 200], 1)
pygame.draw.ellipse(screen, (255, 0, 255), [200, 100, 200, 100], 1)
pygame.draw.circle(screen, (0, 0, 255), [x1, y1], 15)
pygame.draw.circle(screen, (255, 0, 255), [x2, y2], 5)
pygame.display.flip()
clock.tick(5)
解决方案
不要通过应用程序循环中的额外循环来控制游戏。使用应用程序循环。请注意,您必须在for
-loop 中处理事件。pygame.event.get()
分别见pygame.event.pump()
:
对于游戏的每一帧,您都需要对事件队列进行某种调用。这确保您的程序可以在内部与操作系统的其余部分进行交互。
添加 2 个变量degree1
,degree2
然后每帧将它们增加不同的数量。增加degree1
4 和degree2
1
degree1 = (degree1 + 4) % 360
degree2 = (degree2 + 1) % 360
另外增加每秒帧数以获得更流畅的动画:
clock.tick(50)
import pygame, math, sys
pygame.init()
screen = pygame.display.set_mode((1000, 300))
pygame.display.set_caption("Elliptical orbit")
clock = pygame.time.Clock()
degree1 = 0
degree2 = 0
while(True):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
xRadius = 250
yRadius = 100
x2Radius = 100
y2Radius = 50
degree1 = (degree1 + 4) % 360
degree2 = (degree2 + 1) % 360
x1 = int(math.cos(degree1 * 2 * math.pi / 360) * xRadius) + 300
y1 = int(math.sin(degree1 * 2 * math.pi / 360) * yRadius) + 150
x2 = int(math.cos(degree2 * 2 * math.pi / 360) * x2Radius) + 300
y2 = int(math.sin(degree2 * 2 * math.pi / 360) * y2Radius) + 150
screen.fill((0, 0, 0))
pygame.draw.circle(screen, (255, 0, 0), [300, 150], 35)
pygame.draw.ellipse(screen, (255, 255, 255), [50, 50, 500, 200], 1)
pygame.draw.ellipse(screen, (255, 0, 255), [200, 100, 200, 100], 1)
pygame.draw.circle(screen, (0, 0, 255), [x1, y1], 15)
pygame.draw.circle(screen, (255, 0, 255), [x2, y2], 5)
pygame.display.flip()
clock.tick(50)
推荐阅读
- javascript - Javascript - 检查对象和数组的 null 或 undefined 的最简单有效的方法
- java - IntelliJ IDEA 花费了很长时间来编译一个简单的“Hello World!”。使用 MacBook 编写代码
- mysql - 如何使用 Nodejs 在 MySQL 中的 post 请求后插入数组项
- xml - 使用 XSLT 遍历兄弟元素
- smtp - Virtualmin 服务器通过另一个 Virtualmin 服务器发送/接收邮件?
- python-iris - 使用 Iris 计算一些季节性气候指标
- linux - Linux:仅在字符串中查找最后一个“/”的位置
- python - Python 格式:模块文档字符串和首次导入之间的正确空行数是多少?
- swift - Swift 如何处理协议是否有解决此错误的方法?
- python - PyDrive - 擦除文件的内容