Object-oriented turret game (inheritance, object storage to list)

Source: Internet
Author: User

Implementation ideas:

1. Create a turret class that contains 2 variables, one of which is the turret name, and the other is the attack.
2. Write an attack function in the turret class, and refer to the case effect to write the corresponding statement. Can not write for the time being.
3. Write an upgrade function in the turret class, let attack by 2, refer to the case effect.
4. Create a single turret class and the group turret class are inherited from the turret respectively. Rewrite the attack function separately, the output statement see the case effect.
5. The individual turret class defines a skill reduction method, see case effect.
6. The group turret class defines a skill freezing method.
7. Create 2 individual turrets and 2 crowd turrets in the main program. Store these 4 objects in a list. Iterate through the list to try to complete the case effect.

#Parent class TurretclassPata:def __init__(Self,name,tak): Self.name=name Self.tak=TakPrint('%s construction completed, attack damage%d'% (Self.name, Self.tak), end=',')defGongji (self):Print('{} for monomer output'. Format (self.name))defShengji (self):Print('%s Upgrade complete, attack damage, current attack%d'% (self.name,self.tak*2))#Single TurretclassDan (Pata):def __init__(Self,name,tak): Super ().__init__(Name,tak)Print('capable of attacking a single target')defJiansu (self):Print('%s release skill deceleration'%(self.name))#crowd turretclassQun (Pata):def __init__(Self,name,tak): Super ().__init__(Name,tak)Print('ability to attack group targets')defGongji (self):Print('{} for group output'. Format (self.name))defJiansu (self):Print('%s release skill Freeze'%(self.name))#Create main program save list traversaldefMan ():Print('Game Start') D1= Dan ('Turnip Cannon No. 1th', 20) D2= Dan ('Turnip Cannon No. 2nd', 20) Q1= Qun ('Mushroom Cannon No. 1th', 10) Q2= Qun ('Mushroom Cannon No. 2nd', 10) LT=[D1,D2,Q1,Q2]Print('Monster Invasion') forIinchLt:i.gongji () I.jiansu ( )Print('Turret Upgrade') forKinchLt:k.shengji ()if __name__=='__main__': Man ()

Case List

Object-oriented turret game (inheritance, object storage to list)

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.