A simple auto-run mini-game simulation: On a one-dimensional map, there is a worm and an ant. Each time they walk through a -3,-2,+2,+3 a random unit of distance (select the method, if the map boundary to abandon the move). When ants and worms are in the same position, ants eat worms and the program ends.
Process oriented:
Import Random
Ant_point=random.randint (0,20)
Worm_point=random.randint (0,20)
print ' Ant_point: ', Ant_point, ' Worm_point: ', Worm_point
STEP=[-2,+2,-3,+3]
While Ant_point!=worm_point:
Astep=random.choice (STEP)
If 0 <=ant_point+astep<=20:
Ant_point+=astep
Astep=random.choice (STEP)
If 0<=worm_point+astep<=20:
Worm_point+=astep
print ' Ant_point: ', Ant_point, ' Worm_point: ', Worm_point
Random.randint (A, B): Used to generate an integer within a specified range. Where parameter A is the lower limit, the parameter B is the upper limit, and Python generates a random number;
Random.choice (): TheChoice () method returns a list of the random items of a tuple or string.
Object-oriented:import random
Class 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+=astep
Class 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 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_point=point
If __name__== ' __main__ ':
Gm=gamemap ()
Worm=worm (GM)
Ant=ant (GM)
While not gm.catched ():
Worm.jump ()
Ant.jump ()
Instance Properties:
property that the class will have after it is instantiated,
Typically created and initialized in the __init__ () method,
Define the:self.< attribute name directly using it;
Reference method:self.< property name;
Self is used to represent an instance of a class,
class external instance name. Property name method definition and reference,
Different instances of the same class whose instance properties are irrelevant,
It is generally not recommended to create and initialize instance properties outside of the __init__ () method.
Out-of-class definitions and modifications are generally not recommended, and methods can be defined separately.
Class Properties :
Class is defined and does not need to be instantiated
Class property enables different instances of the same class to hold the same variable together
Private properties:
does not provide a keyword that restricts access to properties (cannot restrict the various properties of a class from being accessed directly outside the class)
Use the variable name at the beginning of __ to identify it and only the class object can access it
Use the variable name at the beginning of _ to identify that only the class object and its subclasses can be accessed (not mandatory) and can still be accessed externally
Special properties:
Save metadata for object: __doc__,__name__,__dict__,__module__,__base__
Job: Defines a class box that has an instance property length (length), Width (width), and height. Has a private property volume (calculated by the length-width height), and each instance of it can know the number of instances it is instantiated. They are then instantiated five times and output all the properties of each instance.
Object-oriented and process-oriented