Advertisement
phjoe

Pendulum 2

Jan 3rd, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # pendulum, cuma 1 bola, kalo yg lebih dari 1 apalagi yg pk efek damping / nge-per agk rumit, next time
  2. # joe, 03/01/2015
  3.  
  4. import appuifw as A
  5. import graphics as G
  6. import math
  7. sin,cos=math.sin,math.cos
  8.  
  9. class Pend2:
  10.  def __init__(self):
  11.   self.L=150 # length
  12.   self.A=45 # angle
  13.   self.AV=0.0 # angle velocity
  14.   self.dt=0.3 # timestep ++
  15.  
  16.  def update(self,g):
  17.   # 9.81 = gravitasi
  18.   angleAccel=(-9.81/self.L)*sin(self.A)
  19.   self.AV+=angleAccel*self.dt
  20.   self.A+=self.AV*self.dt
  21.  
  22.   cX,cY=W/2,H/8
  23.   ballX=cX+(sin(self.A)*self.L)
  24.   ballY=cY+(cos(self.A)*self.L)
  25.  
  26.   g.line((0,cY,W,cY),0xffffff,width=2)
  27.   g.line((cX,cY,ballX,ballY),0xffffff)
  28.   g.point((cX,cY),0,width=4)
  29.   g.ellipse((ballX-SIZE/2,ballY-SIZE/2,ballX+SIZE/2,ballY+SIZE/2),fill=0)
  30.  
  31.  
  32.  
  33. class App:
  34.  def __init__(self):
  35.   self.run=0
  36.   A.app.screen='full'
  37.   self.c = A.Canvas(redraw_callback=self.redraw,event_callback=None)
  38.   A.app.body=self.c
  39.   A.app.exit_key_handler = self.stop
  40.  
  41.  def stop(self):
  42.   self.run=0
  43.  
  44.  def redraw(self,x):
  45.   if BG:
  46.    self.c.blit(BG)
  47.  
  48.  def play(self):
  49.   self.run=1
  50.   while self.run:
  51.    BG.clear((176,224,230))
  52.    BG.text((5,H-8),u'Pendulum',0,'legend')
  53.    pen.update(BG)
  54.    self.redraw(0)
  55.    A.e32.ao_sleep(1e-04)
  56.  
  57. W,H=G.sysinfo.display_pixels()
  58. SIZE=20
  59. BG=G.Image.new((W,H))
  60.  
  61. pen=Pend2()
  62. App().play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement