PageRenderTime 104ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/fysphun.py

http://shedskin.googlecode.com/
Python | 184 lines | 147 code | 29 blank | 8 comment | 16 complexity | 9f13ee125d6d1f5c60507bcd9d640d47 MD5 | raw file
Possible License(s): GPL-3.0
  1. # (c) Alex P-B (chozabu@gmail.com)
  2. # license: http://creativecommons.org/licenses/by-nc-sa/2.5/
  3. from math import sin, cos, pi, hypot
  4. from random import random
  5. def setup(w, h):
  6. global sw, sh
  7. sw, sh = w, h
  8. class Point:
  9. def __init__(self):
  10. self.x=0
  11. self.y=0
  12. self.rad=3
  13. self.ox=self.x
  14. self.oy=self.y
  15. self.locked = 0
  16. def basicphys(self):
  17. self.x,self.ox = self.x+(self.x-self.ox)*0.98,self.x
  18. self.y,self.oy = self.y+(self.y-self.oy)*0.98,self.y
  19. self.y+=0.3
  20. def basiclimits(self):
  21. if self.y > sh:
  22. self.y = sh
  23. self.x = self.ox
  24. if self.x > sw:
  25. self.x = sw
  26. self.y = self.oy
  27. if self.x < 0:
  28. self.x = 0
  29. self.y = self.oy
  30. def twopoint(p1,p2,destdist = 15,mult = 0.5):
  31. xd = p1.x-p2.x
  32. yd = p1.y-p2.y
  33. td = hypot(xd, yd)#+0.0000000001
  34. rads = p1.rad+p2.rad
  35. diffd = (destdist-td)*mult
  36. xd/=td
  37. yd/=td
  38. xd*=diffd
  39. yd*=diffd
  40. p1.x+=xd
  41. p1.y+=yd
  42. p2.x-=xd
  43. p2.y-=yd
  44. class Link:
  45. def __init__(self):
  46. self.p1=None
  47. self.p2=None
  48. self.dist = 15
  49. self.strength = 0.5
  50. self.drawme = 1
  51. def applyme(self):
  52. twopoint(self.p1,self.p2,self.dist,self.strength)
  53. points = []
  54. links = []
  55. wheels = []
  56. drawlinks = 1
  57. def addlinki(p1,p2, dist=30,strength=0.5):
  58. global drawlinks
  59. l= Link()
  60. l.p1 = points[p1]
  61. l.p2 = points[p2]
  62. l.dist = dist
  63. l.strength = strength
  64. l.drawme = drawlinks
  65. links.append(l)
  66. return l
  67. def addlinkrr(p1,p2,strength=0.5):
  68. global drawlinks
  69. l= Link()
  70. l.p1 = p1
  71. l.p2 = p2
  72. xd = p1.x-p2.x
  73. yd = p1.y-p2.y
  74. dist = hypot(xd,yd)
  75. l.dist = dist
  76. l.strength = strength
  77. l.drawme = drawlinks
  78. links.append(l)
  79. return l
  80. def addpoint(x,y):
  81. p= Point()
  82. p.x = x
  83. p.y = y
  84. p.ox = p.x
  85. p.oy = p.y
  86. points.append(p)
  87. return p
  88. class Wheel:
  89. def __init__(self,x,y,numspokes = 8, scale = 34):
  90. global drawlinks
  91. lastspoke = numspokes-1
  92. halfspoke = numspokes/2
  93. self.spokes = []
  94. drawlinks = 1
  95. self.mainpoint = addpoint(x,y)
  96. step = pi/numspokes*2
  97. angle = 0
  98. for i in range(numspokes):
  99. angle+=step
  100. self.spokes.append(addpoint(cos(angle)*scale+x,sin(angle)*scale+y))
  101. for i in range(0,numspokes):
  102. nextspoke = i+1
  103. if nextspoke > lastspoke:
  104. nextspoke = 0
  105. addlinkrr(self.spokes[i],self.mainpoint)
  106. for i in range(0,lastspoke):
  107. for r in range(i+1,lastspoke+1):
  108. addlinkrr(self.spokes[i],self.spokes[r])
  109. addlinkrr(self.spokes[0+halfspoke/2],self.spokes[halfspoke+halfspoke/2])
  110. addlinkrr(self.spokes[0],self.spokes[halfspoke])
  111. def addpower(self,power=1.0):
  112. mp = self.mainpoint
  113. for p in self.spokes:
  114. if p.y > mp.y:
  115. p.x-=power
  116. else:
  117. p.x+=power
  118. if p.x > mp.x:
  119. p.y+=power
  120. else:
  121. p.y-=power
  122. for ir in range(18*2):
  123. addpoint(ir*15,10+cos(ir)*5)
  124. for ir in range(11,18*2-1):
  125. addlinki(ir,ir+1)
  126. drawlinks = 1
  127. #neck
  128. addlinki(0,1)
  129. points[0].rad = 14
  130. #body
  131. addlinki(1,2,60)
  132. #leg1
  133. addlinki(2,3)
  134. addlinki(3,4)
  135. #leg2
  136. addlinki(2,5)
  137. addlinki(5,6)
  138. #arm1
  139. addlinki(1,7)
  140. addlinki(7,8)
  141. #arm2
  142. addlinki(1,9)
  143. addlinki(9,10)
  144. drawlinks = 0
  145. addlinki(0,2,90)
  146. addlinki(7,9,70)
  147. addlinki(3,5,40)
  148. wheels.append(Wheel(300,300))
  149. if __name__ == '__main__':
  150. setup(1,1)
  151. p, l, w = points[0], links[0], wheels[0]
  152. p.basiclimits()
  153. p.basicphys()
  154. l.applyme()
  155. w.addpower()