Python object-oriented examples

Source: Internet
Author: User
This article introduces the python Object-oriented example code. This article introduces the python Object-oriented example code.

import randomclass Sprite:    step = [-2,+2,-3,+3]    def __init__(self,gm,point=None):        self.gm = gm        if point 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 Ant(Sprite):    def __init__(self,gm,point=None):        super().__init__(gm,point)        self.gm.set_point('ant',self.point)    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 is 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_point = pointif __name__ == '__main__':    gm = GameMap()    worm = Worm(gm)    ant = Ant(gm)    while not gm.catched():        worm.jump()        ant.jump()


The above is a detailed description of the python object-oriented sample code. For more information, see other related articles in the first PHP community!

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.