我在这个方法工作流中缺少什么?

2024-06-16 11:44:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习如何使用一本书中的方法,在这个练习中,我试图找出一个弹丸的最大高度。我很确定我把这个等式炮弹.py,但每次我执行Exercise 1.py时,我得到的结果总是cball.getMaxY(),我可能错过了哪些简单的事情?在

# projectile.py

"""projectile.py
Provides a simple class for modeling the 
flight of projectiles."""

from math import sin, cos, radians

class Projectile:

    """Simulates the flight of simple projectiles near the earth's
    surface, ignoring wind resistance. Tracking is done in two
    dimensions, height (y) and distance (x)."""

    def __init__(self, angle, velocity, height):
        """Create a projectile with given launch angle, initial
        velocity and height."""
        self.xpos = 0.0
        self.ypos = height
        theta = radians(angle)
        self.xvel = velocity * cos(theta)
        self.yvel = velocity * sin(theta)

        #Find time to reach projectile's maximum height
        self.th = self.yvel/9.8

    def update(self, time):
        """Update the state of this projectile to move it time seconds
        farther into its flight"""
        #Find max height
        self.maxypos = self.yvel - (9.8 * self.th)

        self.xpos = self.xpos + time * self.xvel
        yvel1 = self.yvel - 9.8 * time
        self.ypos = self.ypos + time * (self.yvel + yvel1) / 2.0
        self.yvel = yvel1

    def getY(self):
        "Returns the y position (height) of this projectile."
        return self.ypos

    def getX(self):
        "Returns the x position (distance) of this projectile."
        return self.xpos

    def getMaxY(self):
        "Returns the maximum height of the projectile."
        return self.maxypos

^{pr2}$

Tags: ofthepyselftimedefflightheight
1条回答
网友
1楼 · 发布于 2024-06-16 11:44:04

这句话没什么意义:

self.maxypos = self.yvel - (9.8 * self.th)

保持简单;尝试类似于:

^{pr2}$

之后更新self.ypos。您还需要在__init__中初始化self.maxypos = self.ypos。在

您不需要所有这些琐碎的getter;只需访问属性(Python is not Java)。在

相关问题 更多 >