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

----------------------------------------------------------------------------------------------------------------------------------------------

Modify the provided Tank code to keep the tanks from moving offthe screen. The tank should have an inelastic collision with the wall.The wall should only restrict motion in the direction perpendicular to itself,

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

9 1 import turtle 2 import math 3 import random 4 5 Eclass tanks: 6 def _init__(self,x,y,angle,size,v, health, color, name, active, targetx, targety): 7 self.x=x 8 self.y=y self.size=size 10 self.angle-angle 11 self.v=v 12 self.health=health 13 self.color=color 14 self.name=name 15 self.active=active 16 self. targetx=targetx 17 self.targety=targety 18 192 def draw (self): 20 turtle.color (self.color) 21 turtle.pu() 22 turtle.goto (self.x, self.y-self.size) 23 turtle.pd() 24 turtle.setheading (0) 25 turtle.circle(self.size) 26 turtle.pu() 27 turtle.goto (self.x, self.y) 28 turtle.pd() 29 turtle.goto (self.x+math.cos(self.angle) *self.size + 1.5, self.y+math.sin(self.angle) *self.size +1.5) 30 31 def move (self): 32 self.x+=math.cos(self.angle) self.v 33 self.y+=math.sin(self.angle)* self.v 34 self.draw() 35 36 def target (self, x,y): 37 self.targetx=x 38 self.targety=y 39 40 def kmove(): + Arrow control functions 41 global velocity 42 velocity=.1 43 Edef kstop(): global velocity 45 velocity =0 46 def kleft(): 47 global rotate 48 rotate= .01 NNNN 70 49 def kleftstop(): 50 global rotate 51 rotate=0 52 def kright(): 53 global rotate 54 rotate=-.01 55 Edef krightstop(): 56 global rotate 57 rotate=0 58 Edef kend(): 59 global end 60 end=1 61 62 def control(t): 63 t.angle+=rotate 64 t.v=velocity 65 66 screen = turtle. Screen() 67 screen.setup (500,500) 68 screen.setworldcoordinates (0,0,200.0,200.0) 69 screen.tracer (0) 71 screen.onkeypress (kmove, "Up") + Arrow control key methods 72 screen.onkeyrelease (kstop, "Up") 73 screen.onkeypress (kleft, "Left") 74 screen.onkeyrelease (kleftstop, "Left") 75 screen.onkeypress (kright, "Right") 76 screen.onkeyrelease (krightstop, "Right") 77 screen.onkeypress (kend, "Escape") 78 turtle.speed (0) 79 turtle.hideturtle() 80 velocity=0 81 rotate=0 82 end=0 83 84 user=tanks (10,10,0,5,0,10,"blue", "Tank 1",True,0,0) # create user tank 85 numtanks= 4. 86 enemy = [x for x in range (numtanks)] # set up enemy tank array 87 for i in range (numtanks): # loop to create enemy tanks 88 x = random.randint (5,195) 89 y = random.randint (5,195) 90 angle = random.randint (0,360) enemy[i]=tanks (x,y, angle,5,0.2,10, "red", "Tank 1", True,0,0) 92 93 screen.listen() Ewhile not end: # game loop 95 control (user) 96 turtle.clear() 96 97 98 99 100 101 102 turtle.clear() for i in enemy: i.target (user.x, user.y) i.move() user.move() screen.update() 9 1 import turtle 2 import math 3 import random 4 5 Eclass tanks: 6 def _init__(self,x,y,angle,size,v, health, color, name, active, targetx, targety): 7 self.x=x 8 self.y=y self.size=size 10 self.angle-angle 11 self.v=v 12 self.health=health 13 self.color=color 14 self.name=name 15 self.active=active 16 self. targetx=targetx 17 self.targety=targety 18 192 def draw (self): 20 turtle.color (self.color) 21 turtle.pu() 22 turtle.goto (self.x, self.y-self.size) 23 turtle.pd() 24 turtle.setheading (0) 25 turtle.circle(self.size) 26 turtle.pu() 27 turtle.goto (self.x, self.y) 28 turtle.pd() 29 turtle.goto (self.x+math.cos(self.angle) *self.size + 1.5, self.y+math.sin(self.angle) *self.size +1.5) 30 31 def move (self): 32 self.x+=math.cos(self.angle) self.v 33 self.y+=math.sin(self.angle)* self.v 34 self.draw() 35 36 def target (self, x,y): 37 self.targetx=x 38 self.targety=y 39 40 def kmove(): + Arrow control functions 41 global velocity 42 velocity=.1 43 Edef kstop(): global velocity 45 velocity =0 46 def kleft(): 47 global rotate 48 rotate= .01 NNNN 70 49 def kleftstop(): 50 global rotate 51 rotate=0 52 def kright(): 53 global rotate 54 rotate=-.01 55 Edef krightstop(): 56 global rotate 57 rotate=0 58 Edef kend(): 59 global end 60 end=1 61 62 def control(t): 63 t.angle+=rotate 64 t.v=velocity 65 66 screen = turtle. Screen() 67 screen.setup (500,500) 68 screen.setworldcoordinates (0,0,200.0,200.0) 69 screen.tracer (0) 71 screen.onkeypress (kmove, "Up") + Arrow control key methods 72 screen.onkeyrelease (kstop, "Up") 73 screen.onkeypress (kleft, "Left") 74 screen.onkeyrelease (kleftstop, "Left") 75 screen.onkeypress (kright, "Right") 76 screen.onkeyrelease (krightstop, "Right") 77 screen.onkeypress (kend, "Escape") 78 turtle.speed (0) 79 turtle.hideturtle() 80 velocity=0 81 rotate=0 82 end=0 83 84 user=tanks (10,10,0,5,0,10,"blue", "Tank 1",True,0,0) # create user tank 85 numtanks= 4. 86 enemy = [x for x in range (numtanks)] # set up enemy tank array 87 for i in range (numtanks): # loop to create enemy tanks 88 x = random.randint (5,195) 89 y = random.randint (5,195) 90 angle = random.randint (0,360) enemy[i]=tanks (x,y, angle,5,0.2,10, "red", "Tank 1", True,0,0) 92 93 screen.listen() Ewhile not end: # game loop 95 control (user) 96 turtle.clear() 96 97 98 99 100 101 102 turtle.clear() for i in enemy: i.target (user.x, user.y) i.move() user.move() screen.update()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!