我正在试图制作一个脆弱的鸟类克隆,我似乎无法使物理学正确。我在物理方面并不擅长,每当我尝试数字时,它总是看起来不稳定而不像原始。现在我有一个下降和跳跃增量,每次改变它乘以一个常数使其下降得更快并且在跳跃时变慢,但看起来不正确。 还有另一种方法可以做跳跃的物理学吗?
编辑我无法添加其余代码,因为这与问题无关,因此在我的其余代码中,如果没有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))
答案 0 :(得分:0)
这更像是一个物理问题,而不是编程问题,但是:
要使物理变得切合实际,您需要跟踪鸟的位置(self.x
和self.y
),速度(self.vx
和self.vy
)和加速度(self.ax
和self.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