#ok, I think I have it broken down into pieces now... check out this code: #---- import sys, pygame, math pygame.init() pixelspercm=10.0 gravity=9.80 * 10.0 # meter per second per second (juiced up a little :) ) framerate=50.0 # per second lossperbounce=0.33 # lose energy per bounce framestosilence=3#if a bounce occurs AGAIN within the next 3 frames, consider the ball dead class Ball(object): def __init__(self, id,pos=(0,0)): self.speed=0.0 # meter per second self.diameter=0.025 # meter self.pos=pos self.previous=0#count how many frames ago we had the previous bounce self.name=id#just some name to show for feedback image=pygame.Surface((int(self.diameter*2*pixelspercm*100),int(self.diameter*2*pixelspercm*100))) image.set_colorkey((0,0,0)) image.fill((0,0,0)) pygame.draw.circle(image, (255,255,255), (int(self.diameter*pixelspercm*100),int(self.diameter*pixelspercm*100)), int(self.diameter*pixelspercm*100)) self.image=image def updatepos(self,ms): if not (self.speed==0 and self.pos[1]==300): distance=self.DeltaX2(self.speed, gravity,ms/1000.0) if distance+self.pos[1]<300: self.speed=self.FinalVelocity1(self.speed,gravity,ms/1000.0) self.pos=(self.pos[0],distance+self.pos[1]) if self.previous>0: self.previous=self.previous-1 print self.name, 'normal cycle',self.previous else: print self.name dis2=300-self.pos[1]# distance to ground print 'distance to ground=',dis2 ms2=self.MsTillGround(dis2,self.speed,self.FinalVelocity1(self.speed,gravity,ms/1000.0)) #ms till ground print 'ms till ground=',ms2 endvel=self.FinalVelocity1(self.speed, gravity, (ms-ms2)/1000.0)#speed at ground speedatground=-endvel*(1-lossperbounce)#lose energy print 'speed at ground=',speedatground dis3=self.DeltaX2(speedatground, gravity,(ms-ms2)/1000.0)#deltaX after all ms print 'deltaX after all ms=',dis3 endvel2=self.FinalVelocity1(speedatground, gravity, (ms-ms2)/1000.0)#speed after time print 'speed after time=',endvel2 if self.previous>1: self.pos=(self.pos[0],300) self.speed=0.0 print 'self.speed=',self.speed print 'self.pos=',self.pos print 'SILENCE!' else: self.speed=endvel2 self.pos=(self.pos[0],300+dis3) print 'self.speed=',self.speed print 'self.pos=',self.pos self.previous=framestosilence def MsTillGround(self, Dx, Vi, Vf): #Dx=1/2*(Vi+Vf)*t #t=Dx/(1/2*(Vi+Vf)) t=Dx/(0.5*(Vi+Vf)) return t*1000.0 # t is in seconds, but we want the amount of milliseconds def FinalVelocity1(self,Vi,A,t): # ### 1 # Vf=Vi+A*t # Final Velocity = Initial Velocity + Acceleration * time # meter/second = meter/second + meter/second/second * second return Vi+A*t def AverageVelocity(self,Vi,Vf): # ### 2 # Va=(Vi+Vf)/2 # Average Velocity = ( Initial Velocity + Final Velocity )/2 # meter/second = ( meter/second + meter/second )/2 return (Vi+Vf)/2 def DeltaX1(self,Vi,Vf,t): # ### 3 # Dx=1/2*(Vi+Vf)*t # DeltaX = 1/2 * ( Initial Velocity + Final Velocity ) * time # meter = 1/2 * ( meter/second + meter/second )* second return 0.5*(Vi+Vf)*t def DeltaX2(self,Vi,A,t): # ### 4 # Dx=Vi*t+ 1/2*a* t^2 # notice: t^2 in python is: t**2 # DeltaX = Initial Velocity * time+1/2*Acceleration* time*time # meter = meter/second * second+1/2*meter/second/second * second*second return Vi*t+0.5*A*t**2 def FinalVelocity2(self,Vi,A,Dx): # ### 5 # Vf^2=Vi^2+2*a*Dx # Final Velocity^2=Initial Velocity^2+2*Acceleration*DeltaX # meter/second*meter/second= meter/second*meter/second+2*meter/second/second*meter Vf2=Vi**2+2*A*Dx return math.sqrt(Vf2) screen = pygame.display.set_mode((200, 400), 0, 32) balls = [] balls.append(Ball('Ball 1',(78,15))) balls.append(Ball('Ball 2',(12,158))) balls.append(Ball('Ball 3',(122,56))) newtime=oldtime=0 while True: newtime=pygame.time.get_ticks() # milliseconds since start of program if newtime-oldtime>(1000.0/framerate)*2: print 'LAGGING!' if newtime-oldtime>1000.0/framerate: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() screen.fill((0, 0, 0)) for ball in balls: ball.updatepos(newtime-oldtime) for ball in balls: screen.blit(ball.image, ball.pos) pygame.draw.line(screen, (255,255,255), (0,350), (200,350), 1) pygame.display.update() oldtime=newtime #---- Connect and share in new ways with Windows Live. Connect now! |