用于Python中下落和跳跃物体的重力的物理



我正在尝试制作一款《flappy bird》的克隆游戏,但我似乎无法正确使用物理原理。我不擅长物理,每次我尝试数字,它总是看起来不稳定,不像原来的。现在我有一个下降和跳跃的增量,每次通过乘以一个常数来改变它,使它在跳跃时下降得更快,变慢,但它看起来不太对。是否有其他方法可以实现跳跃的物理效果?

EDIT我不能添加其余的代码,因为这与问题无关,所以这段代码将不会在我的其余代码中没有某些模块变量运行。

这是我的鸟类

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))

这与其说是一个编程问题,不如说是一个物理问题,但是:

为了使物理更逼真,你需要跟踪鸟的位置(self.xself.y),速度(self.vxself.vy)和加速度(self.axself.ay)。

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

最新更新