计算python中赛车游戏的距离

时间:2014-05-01 01:28:32

标签: python pygame

在python和spyral中实现赛车游戏,我无法跟踪距离。我有速度,raceDistance,currentDistance currentTim等...在更新功能中我设置currentDistance = speed * currentTime。这似乎有效,但我试图在问题得到正确回答时尝试提高速度。如果我只是提高速度,则计算currentDist就像它在整个比赛中的新速度一样。

self.speed = 10    
self.raceDistance = 100

def update(self): 
    self.tempTime = time.time() - timeStart 
    self.currentDistance = self.tempTime * self.speed      
    self.timeText.update("Current Time: %.2f" % self.tempTime) 

    if(self.currentDistance >= self.raceDistance):
        global Game_music
        Game_music.stop()
        finishTime = time.time() - timeStart                      
        print "Finish Time = %.2f" % finishTime            
        self.goToMenu()

可以在这里查看所有代码: https://github.com/mreist/Team-Titan/blob/MaxxTesting/SpeedSolver/game/Race.py

1 个答案:

答案 0 :(得分:0)

您在计算速度时遇到问题的原因是:

self.currentDistance = self.tempTime * self.speed 

每次调用该函数时,您都需要重置self.currentDistance,而不是添加它。

更新距离的最简单方法是在每个时间点添加当前距离。

例如:

class Whatever(object):
    def get_new_distance(self, old_distance, delta_time, rate):
        delta_distance = delta_time * rate
        new_distance = old_distance + delta_distance
        return new_distance

    def update(self):
       self.delta_time = time.time() - self.last_time_call
       self.last_time_call = time.time() #for use in the next update

       self.current_distance = \
           self.get_new_distance(self.current_distance, self.delta_time,\
           self.rate)