day31 类的组合及继承,文件目录规范

Posted 何必从头

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day31 类的组合及继承,文件目录规范相关的知识,希望对你有一定的参考价值。

Python之路,Day18 = Python基础18-面向对象继承与组合

 

类的继承

 1 def talk():
 2     print("I am come from talk..a")
 3 
 4 class Animal:
 5     def say(self):
 6         print("say")
 7 
 8 class Person(object):
 9     def __init__(self, name, age):
10         self.name = name
11         self.age = age
12 
13     def walk(self):
14         print("%s is walking..."%self.name)
15 
16 class Teacher(Person):
17     def say(self):
18         Animal.say(123)
19     pass
20 
21 class Student(Person):
22     def __init__(self, name, age, tuition):
23         Person.__init__(self, name, age)
24         self.tuition = tuition
25 
26     def walk(self):
27         Person.walk(self)
28         print("I am come from Student...")
29 
30     def talk(self):
31         talk()                     # 对象可以调用外面的函数
32 
33 t = Teacher(\'zhang\', 18)
34 print(t.name, t.age)
35 t.walk()
36 t.say()
37 
38 
39 s = Student(\'liu\', 17, 19800)
40 print(s.name, s.age)
41 s.walk()
42 print(s.tuition)
43 
44 
45 s.talk()

 

类的组合

 1 class BirthDay(object):
 2     def __init__(self, year, mon, day):
 3         self.year = year
 4         self.mon = mon
 5         self.day = day
 6 
 7     def tellInfo(self):
 8         print("comes from BirthDay")
 9         return "year:%s  mon:%s  day:%s"%(self.year, self.mon, self.day)
10 
11 
12 class Teacher(object):
13     def __init__(self, name, age, sex, birth):
14         self.name = name
15         self.age = age
16         self.sex =sex
17         self.birth = birth
18 
19 
20 
21 t = Teacher(\'zhang\', 18, \'male\', BirthDay(1999, 9, 99))
22 
23 print(t.birth.tellInfo())

 

 

文件目录规范

 

 

 飞机大战的例子

  1 import time
  2 import pygame
  3 from sys import exit
  4 
  5 def main():
  6 
  7     screen = pygame.display.set_mode((512, 768), 0, 32)
  8     pygame.display.set_caption("飞机大战之类的练习")
  9     pygame.mouse.set_visible(False)
 10     background = pygame.image.load(r".\\img\\background.jpg")
 11     hero_plan_len = 151
 12     hero_plan_hig = 108
 13 
 14     hero_plan = HeroPlan(screen, hero_plan_len, hero_plan_hig)
 15     enemy_plan = EnemyPlan(screen)
 16 
 17 
 18     while True:
 19         screen.blit(background, (0,0))
 20 
 21         hero_plan.display()
 22         get_input(hero_plan)
 23 
 24         enemy_plan.display()
 25 
 26         pygame.display.update()
 27 
 28         time.sleep(0.03)
 29 
 30 class BasePlan:
 31     def __init__(self, screen, x, y):
 32         self.screen = screen
 33         self.x = x
 34         self.y = y
 35         self.buttle = []
 36         self.count = 0
 37 
 38     def display(self):
 39         self.move()
 40         self.screen.blit(self.plan, (self.x, self.y))
 41         self.fire()
 42 
 43         for buttle in self.buttle:
 44             if buttle.clearButtle():
 45                 self.buttle.remove(buttle)
 46 
 47             buttle.move()
 48             buttle.display()
 49 
 50 
 51     def move(self):
 52         pass
 53 
 54 
 55 class HeroPlan(BasePlan):
 56     def __init__(self, screen, lenth, high):
 57         super().__init__(screen, 180, 640)
 58         self.lenth = lenth
 59         self.high = high
 60         self.plan = pygame.image.load(r".\\img\\hero_plan.png")
 61 
 62 
 63     def fire(self):
 64         if self.count == 2:
 65             self.buttle.append(HeroBullet(self.screen, self.x + 20, self.y - 48))
 66             self.buttle.append(HeroBullet(self.screen, self.x + 105, self.y - 48))
 67             self.count = 0
 68         else:
 69             self.count += 1
 70 
 71 
 72 class EnemyPlan(BasePlan):
 73     def __init__(self, screen):
 74         super().__init__(screen, 0, 0)
 75         self.direction = "right"
 76         self.plan = pygame.image.load(r".\\img\\enemy_plan.png")
 77 
 78     def move(self):
 79         if self.direction == "right":
 80             self.x += 10
 81         elif self.direction == \'left\':
 82             self.x -= 10
 83 
 84         if self.x <= 0:
 85             self.direction = "right"
 86         elif self.x >= 248:
 87             self.direction = "left"
 88 
 89     def fire(self):
 90         if self.count == 20:
 91             self.buttle.append(EnemyButtle(self.screen, self.x+40, self.y+160))
 92             self.buttle.append(EnemyButtle(self.screen, self.x+175, self.y+160))
 93             self.count = 0
 94         else:
 95             self.count += 1
 96 
 97 
 98 class BaseBullet:
 99     def __init__(self, screen, x, y):
100         self.screen = screen
101         self.x = x
102         self.y = y
103 
104     def display(self):
105         self.screen.blit(self.buttle, (self.x, self.y))
106 
107 class HeroBullet(BaseBullet):
108     def __init__(self, screen, x, y):
109         super().__init__(screen, x, y)
110         self.buttle = pygame.image.load(r".\\img\\hero_bullet.png")
111 
112     def move(self):
113         self.y -= 20
114 
115     def clearButtle(self):
116         if self.y < 0:
117             return True
118 
119 
120 class EnemyButtle(BaseBullet):
121     def __init__(self, screen, x, y):
122         super().__init__(screen, x, y)
123         self.buttle = pygame.image.load(r".\\img\\enemy_bullet.png")
124 
125     def move(self):
126         self.y += 10
127 
128     def clearButtle(self):
129         if self.y > 714:
130             return True
131 
132 def get_input(hero_plan):
133     for event in pygame.event.get():
134         if event.type == pygame.QUIT:
135             exit()
136 
137     x, y = pygame.mouse.get_pos()
138     hero_plan.x = x - hero_plan.lenth/2
139     hero_plan.y = y - hero_plan.high/2
140 
141 def key_control(hero_temp):
142 
143     #获取事件,比如按键等
144     for event in pygame.event.get():
145 
146         #判断是否是点击了退出按钮
147         if event.type == QUIT:
148             print("exit")
149             exit()
150         #判断是否是按下了键
151         elif event.type == KEYDOWN:
152             #检测按键是否是a或者left
153             if event.key == K_a or event.key == K_LEFT:
154                 print(\'left\')
155                 hero_temp.move_left()
156             #检测按键是否是d或者right
157             elif event.key == K_d or event.key == K_RIGHT:
158                 print(\'right\')
159                 hero_temp.move_right()
160             #检测按键是否是空格键
161             elif event.key == K_SPACE:
162                 print(\'space\')
163                 hero_temp.fire()
164 
165 
166 if __name__ == "__main__":
167     main()

 

以上是关于day31 类的组合及继承,文件目录规范的主要内容,如果未能解决你的问题,请参考以下文章

Day17:类的继承派生组合和接口

Python20期课堂总结-20180120day7-面向对象之继承组合多态封装等

类的组合与封装

Python基础day-18[面向对象:继承,组合,接口归一化]

Day 5-2 类的继承和派生,重用

Python开发基础-Day18继承派生组合接口和抽象类