On Python object-oriented sample code

Source: Internet
Author: User
This article discusses the sample code of Python object-oriented approach

Import Randomclass sprite:step = [ -2,+2,-3,+3] def __init__ (self,gm,point=none): SELF.GM = GM If Poin        T is None:self.point = Random.randint (0,20) else:self.point = Point def jump (self): Astep = Random.choice (sprite.step) if 0 <= self.point + astep <= 20:self.point + = Astepclass A NT (Sprite): def __init__ (Self,gm,point=none): Super (). __init__ (Gm,point) self.gm.set_point (' Ant ', self.poi NT) def jump (self): Super (). Jump () self.gm.set_point (' Ant ', Self.point) class Worm (Sprite): Def __init__        (Self,gm,point=none): Super (). __init__ (Gm,point) self.gm.set_point (' Worm ', self.point) def jump (self):  Super (). Jump () self.gm.set_point (' Worm ', Self.point) class Gamemap:def __init__ (self): Self.ant_point        = None Self.worm_point = None def catched (self): print (' Ant: ', Self.ant_point, ' worm: ', Self.worm_point) If Self.ant_point Is not none and Self.worm_point are not none and self.ant_point = = Self.worm_point:return True def set_point ( Self,src,point): if src = = ' ant ': self.ant_point = point if src = = ' worm ': self.worm_p        oint = pointif __name__ = = ' __main__ ': GM = gamemap () worm = worm (gm) ant = Ant (gm) while not gm.catched (): Worm.jump () Ant.jump ()

<br>

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.