Question: Modify the provided Tank code to keep the tanks from moving off the screen. The tank should have an inelastic collision with the wall. The
Modify the provided Tank code to keep the tanks from moving off the screen.
The tank should have an inelastic collision with the wall.
The wall should only restrict motion in the direction perpendicular to itself, but motion parallel to itself is unrestricted and without friction so that that tanks can slide along the wall.
If the tank runs into the wall perpendicularly, then it should stop completely, but should move again when the angle changes.
If the tank hits at an angle, then it should slide along the wall, but won't ever move through the wall.
All tanks should be impacted by the code
----------------------------------------------------------------------------------------------------------------------------------------------
import turtle import math import random
class tanks: def __init__(self,x,y,angle,size,v,health,color,name,active, targetx,targety): self.x=x self.y=y self.size=size self.angle=angle self.v=v self.health=health self.color=color self.name=name self.active=active self.targetx=targetx self.targety=targety
def draw(self): turtle.color(self.color) turtle.pu() turtle.goto(self.x,self.y-self.size) turtle.pd() turtle.setheading(0) turtle.circle(self.size) turtle.pu() turtle.goto(self.x,self.y) turtle.pd() turtle.goto(self.x+math.cos(self.angle)*self.size * 1.5,self.y+math.sin(self.angle)*self.size *1.5) def move(self): self.x+=math.cos(self.angle)* self.v self.y+=math.sin(self.angle)* self.v self.draw()
def target(self, x,y): self.targetx=x self.targety=y
def kmove(): # Arrow control functions global velocity velocity=.1 def kstop(): global velocity velocity =0 def kleft(): global rotate rotate=.01 def kleftstop(): global rotate rotate=0 def kright(): global rotate rotate=-.01 def krightstop(): global rotate rotate=0 def kend(): global end end=1
def control(t): t.angle+=rotate t.v=velocity
screen = turtle.Screen() screen.setup(500,500) screen.setworldcoordinates(0,0,200.0,200.0) screen.tracer(0)
screen.onkeypress(kmove, "Up") # Arrow control key methods screen.onkeyrelease(kstop, "Up") screen.onkeypress(kleft, "Left") screen.onkeyrelease(kleftstop, "Left") screen.onkeypress(kright, "Right") screen.onkeyrelease(krightstop, "Right") screen.onkeypress(kend, "Escape") turtle.speed(0) turtle.hideturtle() velocity=0 rotate=0 end=0
user=tanks(10,10,0,5,0,10,"blue","Tank 1",True,0,0) # create user tank numtanks= 4 enemy = [x for x in range(numtanks)] # set up enemy tank array for i in range(numtanks): # loop to create enemy tanks x = random.randint(5,195) y = random.randint(5,195) angle = random.randint(0,360) enemy[i]=tanks(x,y,angle,5,0.2,10,"red","Tank 1",True,0,0)
screen.listen() while not end: # game loop control(user) turtle.clear() for i in enemy: i.target(user.x,user.y) i.move() user.move() screen.update()
----------------------------------------------------------------------------
Please explain how the correction
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
