Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. from __future__ import division
  2. from visual import *
  3.  
  4. def rad(degrees): #converts an angle in degrees to an angle in radians
  5. radians=degrees*pi/180
  6. return radians
  7.  
  8. def collision(sphere, block):
  9. dist=mag(sphere.pos-block.pos)
  10. if(dist<sphere.radius+otherTank.size.x/2):
  11. return True
  12. else:
  13. return False
  14.  
  15. scene.range=20
  16. scene.width=800
  17. scene.height=800
  18.  
  19. #create objects
  20. ground = box(pos=vector(0,-15,0), size=(40,1,2), color=color.green)
  21. mainTank = box(pos=vector(-18,-13, 0), size=(2,2,2), color=color.yellow)
  22. turret = cylinder(pos=mainTank.pos, axis=(0,0,0), radius=0.5, color=mainTank.color)
  23. turret.pos.y= turret.pos.y+mainTank.height/2
  24. angleBar = cylinder(pos=vector(-18,-19,0), axis=(0,0,0), radius=1, color= color.magenta)
  25. speedBar = cylinder(pos=vector(5,-19,0), axis= (1,0,0), radius=1, color=color.cyan)
  26. otherTank = box(pos=vector(18, -13, 0), size=(2,2,2), color=(1,0,1))
  27.  
  28. #turret
  29. theta=rad(45)
  30. dtheta=rad(1)
  31. L=3
  32. turret.axis=L*vector(cos(theta), sin(theta),0)
  33.  
  34. #bullets
  35. bulletsList=[]
  36. splosionList=[]
  37. m=1
  38. muzzlespeed=15
  39. dspeed=1
  40. hits = 0
  41.  
  42. #bar
  43. angleBar.axis=(5*theta)*vector(1,0,0)
  44. speedBar.axis= (muzzlespeed/2+0.5)*vector(1,0,0)
  45.  
  46. #motion
  47. g=vector(0,-10,0)
  48. dt=0.01
  49. t=0
  50.  
  51. while 1:
  52. scene.mouse.getclick()
  53. while 1:
  54. rate(100)
  55. if scene.kb.keys:
  56. k = scene.kb.getkey()
  57. if k =="up":
  58. theta=theta+dtheta
  59. turret.axis=L*vector(cos(theta), sin(theta),0)
  60. angleBar.axis=(5*theta)*vector(1,0,0)
  61. elif k == "down":
  62. theta=theta-dtheta
  63. turret.axis=L*vector(cos(theta), sin(theta),0)
  64. angleBar.axis=(5*theta)*vector(1,0,0)
  65. elif k== "left":
  66. muzzlespeed=muzzlespeed-dspeed
  67. speedBar.axis=(muzzlespeed/2+0.5)*vector(1,0,0)
  68. elif k == "right":
  69. muzzlespeed = muzzlespeed+dspeed
  70. speedBar.axis= (muzzlespeed/2+0.5)*vector(1,0,0)
  71. elif k==" ":
  72. bullet=sphere(pos=turret.pos+turret.axis, radius=0.5, color=color.white)
  73. bullet.v=muzzlespeed*vector(cos(theta), sin(theta),0)
  74. bulletsList.append(bullet)
  75. elif k=="Q":
  76. break
  77. if muzzlespeed>20:
  78. muzzlespeed=20
  79. if muzzlespeed<1:
  80. muzzlespeed=1
  81. if theta>rad(90):
  82. theta=rad(90)
  83. if theta<0:
  84. theta=0
  85.  
  86. for thisbullet in bulletsList:
  87. if(thisbullet.pos.y<ground.y+ground.height/2):
  88. thisbullet.Fnet=vector(0,0,0)
  89. thisbullet.v=vector(0,0,0)
  90. else:
  91. thisbullet.Fnet=m*g
  92. thisbullet.v= thisbullet.v + (thisbullet.Fnet/m)*dt
  93. thisbullet.pos= thisbullet.pos+thisbullet.v*dt
  94. if collision(thisbullet,otherTank):
  95. hits=hits+1
  96. otherTank.v = vector(0,0,0)
  97. thisbullet.visible=false
  98. otherTank.color=color.red
  99. bulletsList.remove(thisbullet)
  100. print("nice shot m8")
  101. #print(hits)
  102. if hits==5:
  103. #otherTank.visible=false
  104. splosionList.append(otherTank)
  105. print("Tank destroyed!")
  106. hits = 6
  107. if thisbullet.pos.x>20:
  108. thisbullet.visible=false
  109. bulletsList.remove(thisbullet)
  110. print("maybe you should practice more :)")
  111.  
  112. for e in splosionList:
  113. #e.size.x+=dt
  114. #e.size.y+=dt
  115. #e.size.z+=dt
  116. e.opacity-=dt
  117. otherTank.length = otherTank.length + otherTank.length*dt
  118. otherTank.width = otherTank.width + otherTank.width*dt
  119. otherTank.height = otherTank.height + otherTank.height*dt
  120. if e.opacity <0:
  121. splosionList.remove(e)
  122.  
  123. t=t+dt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement