Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import division
- from visual import *
- def rad(degrees): #converts an angle in degrees to an angle in radians
- radians=degrees*pi/180
- return radians
- def collision(sphere, block):
- dist=mag(sphere.pos-block.pos)
- if(dist<sphere.radius+otherTank.size.x/2):
- return True
- else:
- return False
- scene.range=20
- scene.width=800
- scene.height=800
- #create objects
- ground = box(pos=vector(0,-15,0), size=(40,1,2), color=color.green)
- mainTank = box(pos=vector(-18,-13, 0), size=(2,2,2), color=color.yellow)
- turret = cylinder(pos=mainTank.pos, axis=(0,0,0), radius=0.5, color=mainTank.color)
- turret.pos.y= turret.pos.y+mainTank.height/2
- angleBar = cylinder(pos=vector(-18,-19,0), axis=(0,0,0), radius=1, color= color.magenta)
- speedBar = cylinder(pos=vector(5,-19,0), axis= (1,0,0), radius=1, color=color.cyan)
- otherTank = box(pos=vector(18, -13, 0), size=(2,2,2), color=(1,0,1))
- #turret
- theta=rad(45)
- dtheta=rad(1)
- L=3
- turret.axis=L*vector(cos(theta), sin(theta),0)
- #bullets
- bulletsList=[]
- splosionList=[]
- m=1
- muzzlespeed=15
- dspeed=1
- hits = 0
- #bar
- angleBar.axis=(5*theta)*vector(1,0,0)
- speedBar.axis= (muzzlespeed/2+0.5)*vector(1,0,0)
- #motion
- g=vector(0,-10,0)
- dt=0.01
- t=0
- while 1:
- scene.mouse.getclick()
- while 1:
- rate(100)
- if scene.kb.keys:
- k = scene.kb.getkey()
- if k =="up":
- theta=theta+dtheta
- turret.axis=L*vector(cos(theta), sin(theta),0)
- angleBar.axis=(5*theta)*vector(1,0,0)
- elif k == "down":
- theta=theta-dtheta
- turret.axis=L*vector(cos(theta), sin(theta),0)
- angleBar.axis=(5*theta)*vector(1,0,0)
- elif k== "left":
- muzzlespeed=muzzlespeed-dspeed
- speedBar.axis=(muzzlespeed/2+0.5)*vector(1,0,0)
- elif k == "right":
- muzzlespeed = muzzlespeed+dspeed
- speedBar.axis= (muzzlespeed/2+0.5)*vector(1,0,0)
- elif k==" ":
- bullet=sphere(pos=turret.pos+turret.axis, radius=0.5, color=color.white)
- bullet.v=muzzlespeed*vector(cos(theta), sin(theta),0)
- bulletsList.append(bullet)
- elif k=="Q":
- break
- if muzzlespeed>20:
- muzzlespeed=20
- if muzzlespeed<1:
- muzzlespeed=1
- if theta>rad(90):
- theta=rad(90)
- if theta<0:
- theta=0
- for thisbullet in bulletsList:
- if(thisbullet.pos.y<ground.y+ground.height/2):
- thisbullet.Fnet=vector(0,0,0)
- thisbullet.v=vector(0,0,0)
- else:
- thisbullet.Fnet=m*g
- thisbullet.v= thisbullet.v + (thisbullet.Fnet/m)*dt
- thisbullet.pos= thisbullet.pos+thisbullet.v*dt
- if collision(thisbullet,otherTank):
- hits=hits+1
- otherTank.v = vector(0,0,0)
- thisbullet.visible=false
- otherTank.color=color.red
- bulletsList.remove(thisbullet)
- print("nice shot m8")
- #print(hits)
- if hits==5:
- #otherTank.visible=false
- splosionList.append(otherTank)
- print("Tank destroyed!")
- hits = 6
- if thisbullet.pos.x>20:
- thisbullet.visible=false
- bulletsList.remove(thisbullet)
- print("maybe you should practice more :)")
- for e in splosionList:
- #e.size.x+=dt
- #e.size.y+=dt
- #e.size.z+=dt
- e.opacity-=dt
- otherTank.length = otherTank.length + otherTank.length*dt
- otherTank.width = otherTank.width + otherTank.width*dt
- otherTank.height = otherTank.height + otherTank.height*dt
- if e.opacity <0:
- splosionList.remove(e)
- t=t+dt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement