Python中下落物体和跳跃物体引力的物理学

2024-10-01 07:38:06 发布

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

我在试着做一个松松垮垮的克隆鸟,但我似乎搞不清物理原理。我对物理不太在行,每次我尝试数字的时候,它似乎总是波涛汹涌,不像原来那样。现在我有一个下降和跳跃的增量,每次都会被改变,乘以一个常数,使它下降得更快,在跳跃时变慢,但是看起来不对。 有没有其他方法来做物理跳跃?在

编辑我无法添加其余的代码,因为这与问题无关,因此在我的其余代码中,如果没有cetain模块变量,此代码将无法运行。在

这是我的鸟课

class Player():
    def __init__(self,root,canvas,x=150,y=300,size=40):
        global jumped
        #Sets attributes
        self.size=size
        self.faller=True
        self.x=x
        self.y=y
        self.root=root
        self.fell=4 #The initial amount to fall
        jingle=13 #The initial amount to jump
        self.canvas=canvas
        #sets the image
        im=PhotoImage(file="unnamed copy 2.gif")
        self.photo=im
        self.current=self.canvas.create_image((self.x,self.y),image=self.photo)
    def fall(self): #Always runs in the background, if the user isn't jumping, to fall
        global done,t,points,j,height
        if self.faller and not done: 
            self.y+=self.fell
            self.fell*=t    #Falls and multiplies how much it fell by the exponential constant
            if self.y+(height/2)>=600:  # if it hit the ground, it ends the game
                done=True
                self.fall()  #Runs the method again to execute the code when done is True
                return

            self.canvas.coords(self.current,(self.x,self.y))
            self.canvas.after(20,self.fall) #Runs it again after 20 milliseconds
        elif done and self.faller:
            self.faller=False  #Stops the falling
            end()


    def jump(self,e):
        global done,j,jingle,orange,t
        if not done and orange: #If it isnt dead and it has been a
                                #sufficient time since the user last jumped
            self.faller=False   #Stops the falling
            x=1
            while x<=10:
                if not done:
                    for item in pipes: #Checks if it has hit each time it goes up
                        if item.hit(self): # if it has, it stops and dies
                            done=True
                            return
                self.canvas.after(12*x,self.move) # it moves up a little, 10 times
                x+=1
            self.faller=True #After it is done, it lets itself fall again
            self.fell=4  #Sets the amount it falls back to the default
            jingle=13 #Sets the amount it jumps back to default
            orange=False #Stops the user from jumping really fast, like holding space
            j=.97 #Sets the exponential constants back to normal
            t=1.09
            self.canvas.after(100,self.toll) #After 100 ms, it lets the user jump again
    def toll(self): #sets the boolean that stops jumping back to True
        global orange
        orange=True
    def move(self): #Moves and multiplies how much it moves by the constant
        global jingle,j
        self.y-=jingle
        jingle*=j
        self.canvas.coords(self.current,(self.x,self.y))
    def changey(self,a): #A method to change the user's position
        self.y=a
        self.canvas.coords(self.current,(self.x,self.y))

Tags: andthetoselftrueifdefit
1条回答
网友
1楼 · 发布于 2024-10-01 07:38:06

这更像是一个物理问题而不是一个编程问题,但是:

为了使物理更真实,你需要跟踪鸟的位置(self.xself.y)、速度(self.vx和{})和加速度(self.ax和{})。在

self.ay应设置为一个常量,该常量决定您希望对象下落的速度。在

self.ax通常应该是0.0。在

在运行循环中,需要这样做:

self.x += self.vx * t
self.y += self.vy * t

self.vx += self.ax * t
self.vy += self.ay * t

相关问题 更多 >