" "1. Start with a canvas 2. Random Turtle and Fish position 3. Move" "ImportRandom as R list_x= [0,10]list_y= [0,10]classTurtle:def __init__(self):#Initial physical Strengthself.power=100#Initial Positionself.x = R.randint (list_x[0],list_x[1])#Here's the point, Randint.SELF.Y = R.randint (list_y[0],list_y[1]) defMove (self):#Random Move positionnew_x = Self.x+r.choice ([1,2,-1,-2]) new_y= Self.y+r.choice ([1,2,-1,-2]) #Check whether the zone is exceeded after the move ifnew_x<list_x[0]: self.x= list_x[0]-(new_x-list_x[0])elifNew_x>list_x[1]: self.x= list_x[1]-(new_x-list_x[1]) Else: self.x=new_x#Check if the y-axis is exceeded ifnew_y<list_y[0]: self.y= list_y[0]-(new_y-list_y[0])elifNew_y>list_y[1]: Self.y= list_y[1]-(new_y-list_y[1]) Else: Self.y=new_y#After you move, you need toSelf.power-= 1#returns the position after the move return(SELF.X,SELF.Y)defEat (self): Self.power+ = 20ifself.power>100: Self.power=100classFish:def __init__(self):#Initial Positionself.x = R.randint (list_x[0],list_x[1]) Self.y= R.randint (list_y[0],list_y[1]) defMove (self):#Random Move positionnew_x = Self.x+r.choice ([1,-1]) new_y= Self.y+r.choice ([1,-1]) #Check whether the zone is exceeded after the move ifnew_x<list_x[0]: self.x= list_x[0]-(new_x-list_x[0])elifNew_x>list_x[1]: self.x= list_x[1]-(new_x-list_x[1]) Else: self.x=new_x#Check if the y-axis is exceeded ifnew_y<list_y[0]: self.y= list_y[0]-(new_y-list_y[0])elifNew_y>list_y[1]: Self.y= list_y[1]-(new_y-list_y[1]) Else: Self.y=new_y#returns the position after the move return(SELF.X,SELF.Y) T=Turtle () fish= [] forIinchRange (10): New_fish=Fish () fish.append (new_fish) whileTrue:if notlen (fish):Print("the fish is eaten, the game is over.") Break if notT.power:Print("The tortoise was exhausted and sacrificed.") BreakPOS=T.move () forEach_fishinchfish[:]:ifEach_fish.move () = =POS:#fish are eaten .t.eat () fish.remove (each_fish)Print("There was a fish eaten.")
Python, Practice turtle eating fish