Python 如何在pygame中创建模拟(钟摆、投射物运动)

Python 如何在pygame中创建模拟(钟摆、投射物运动),python,pygame,physics,simulator,Python,Pygame,Physics,Simulator,我创建了一个程序,生成摆的位置、长度、球的位置、速度、角度和轨迹。该项目的任务是找到一个解决方案,使球能够安全地通过“洞穴”着陆。摆锤位于85.75×66.75区域内,长度

我创建了一个程序,生成摆的位置、长度、球的位置、速度、角度和轨迹。该项目的任务是找到一个解决方案,使球能够安全地通过“洞穴”着陆。摆锤位于85.75×66.75区域内,长度<65,球半径=1.25

我想在pygame中创建一个实验模拟,它将运行我的第一个程序生成所有参数,然后显示球将遵循的解决方案和路径。在过去的几天里,我一直在学习pygame,但不知道如何“转移”我的第一个程序。我看了看其他的钟摆模拟器,试图把它改成适合我的实验,但我迷路了,决定来StackOverflow寻求建议。如果有人能告诉我在进行模拟时哪里出了问题,我将不胜感激

第一个节目

import math as m
import numpy as np

# Variables
c = 28.5
Wx = 20
Wy = 30
d = 85.75
f = 66.75
g = 385.826772
ay = -g

# Calculations
for theta in np.arange(1, 90, .01):
    l = Wx + (m.tan(m.radians(theta)) * (f - Wy))
    if Wx <= l <= d:
        phi = 90 - theta
        v = (d - l) / m.sin(m.radians(phi))
        vc = v - 1.25
        if (f - Wy) <= v <= 65:
            h = f - (m.cos(m.radians(phi)) * v)
            a = v * m.sin(m.radians(theta))
            b = v * m.cos(m.radians(theta))
            by = f - b
            bx = l - a
            if h <= f and by <= c:
                vel = m.sqrt((2 * g) * (h - by)) * .95
                velx = vel * m.cos(m.radians(theta))
                vely = vel * m.sin(m.radians(theta))
                y = (-vely**2) / (2 * ay)
                Ymax = y + by
                if m.isclose(Ymax, c, abs_tol= .01):
                    t1 = -vely / ay
                    t2 = m.sqrt((2 * Ymax) / -ay)
                    T = t1 + t2
                    x = velx * T
                    print(' l: {0} v: {1}  vc: {2} h: {3}\n bx: {4} by: {5}\n vel: {6} velx: {7} vely: {8}\n y: {9} Ymax: {10} x: {11} T: {12}\n theta: {13} phi: {14}\n'
                          .format(l, v, vc, h, bx, by, vel, velx, vely, y, Ymax, x, T, theta, phi))

请定义你所说的“转移”是什么意思?转成什么形式?你的意思是将“第一个程序”代码合并到“模拟器”代码中吗?金斯利,你是对的,当我说“转移”时,我的意思是将“第一个程序”实现到模拟器代码中。我想用这些结果来模拟这个实验。
import pygame
import numpy as np
import math as m
from math import pi

# Tarzan Variables
c = 28.5
Wy = 30
Wx = 20
d = 85.75
f = 66.75

# Colors
black = (0, 0, 0)
red = (255, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)

# Pygame Variables
theta = 0
v = 0
vel = 0
acc = 0

# Start Pygame
width, height = 900, 700
pygame.init()
background = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()


# Tarzan
class Pendulum(object):
    def __init__(self, XY, l, radius):
        self.x = XY[0]
        self.y = XY[1]
        self.l = l
        self.radius = radius

    def draw(self, bg):
        pygame.draw.line(bg, white, (self.l, 0), (self.x, self.y), 4)
        pygame.draw.circle(bg, red, (self.x, self.y), self.radius)
        pygame.draw.line(bg, green, (Wx, height), (Wx, (height - Wy)), 4)
        # pygame.draw.circle(bg, white, (self.l, 0), int(v)) --- to see if pendulum is following an arc


def theta_v():
    v = m.sqrt(m.pow(pendulum.x - (width / 2), 2) + m.pow(pendulum.y, 2))
    theta = m.asin(((pendulum.x - (width / 2)) / v))
    return theta, v


def get_path(theta, v):
    pendulum.x = round(pendulum.l + (v * m.sin(theta)))
    pendulum.y = round(v * m.cos(theta))
    pendulum.l = pendulum.x - (v * m.sin(m.radians(theta)))


def redraw():
    background.fill(black)
    pendulum.draw(background)
    pygame.display.update()


pendulum = Pendulum((75, 67), 500, 15)
# Close Pygame
stop = False
acceleration = False
while not stop:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            stop = True
        if event.type == pygame.MOUSEBUTTONDOWN:
            pendulum = Pendulum(pygame.mouse.get_pos(), 500, 15)
            theta, v = theta_v()
            acceleration = True

    if acceleration:
        acc = -.005 * m.sin(theta)
        vel += acc
        vel *= .995
        theta += vel
        get_path(theta, v)
        print(pendulum.x, pendulum.y, (theta * (180 / pi)), v, vel, pendulum.l)
    redraw()
pygame.quit()
quit()