Python, Practice turtle eating fish

Source: Internet
Author: User

" "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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.