python 简答打飞机(面向对象)

Posted 饭桶大叔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 简答打飞机(面向对象)相关的知识,希望对你有一定的参考价值。

  1 import pygame
  2 import time
  3 import random
  4 from pygame.locals import *
  5 
  6 
  7 class Base(object):
  8     def __init__(self, screen_temp, x, y, image_name):
  9         self.x = x
 10         self.y = y
 11         self.screen = screen_temp
 12         self.image = pygame.image.load(image_name)
 13 
 14 
 15 class BasePlane(Base):
 16     def __init__(self, screen_temp, x, y, image_name):
 17         Base.__init__(self, screen_temp, x, y, image_name)
 18         self.bullet_list = []  # 存储发射出去的子弹对象的引用
 19 
 20     def display(self):
 21         self.screen.blit(self.image, (self.x, self.y))
 22 
 23         for bullet in self.bullet_list:
 24             bullet.display()
 25             bullet.move()
 26 
 27             if bullet.judge():  # 判断子弹是否越界
 28                 self.bullet_list.remove(bullet)
 29 
 30 
 31 class HeroPlane(BasePlane):
 32     def __init__(self, screen_temp):
 33         # 也可以用super().__init__(), 用这个方法不用加参数self
 34         BasePlane.__init__(self,screen_temp,210,700,"./feiji/hero1.png")
 35 
 36     def move_left(self):
 37         self.x = self.x - 5
 38 
 39     def move_right(self):
 40         self.x = self.x + 5
 41 
 42     def fire(self):
 43         self.bullet_list.append(Bullet(self.screen, self.x, self.y))
 44 
 45 
 46 class EnemyPlane(BasePlane):
 47     """敌机的类"""
 48     def __init__(self, screen_temp):
 49         BasePlane.__init__(self,screen_temp,0,0,"./feiji/enemy0.png")
 50         self.direction = "right"  # 用来存储飞机默认的显示方向
 51 
 52 
 53     def move(self):
 54         if self.direction == "right":
 55             self.x += 5
 56 
 57         elif self.direction == "left":
 58             self.x -= 5
 59 
 60         if self.x > 430:
 61             self.direction = "left"
 62         elif self.x < 0:
 63             self.direction = "right"
 64 
 65     def fire(self):
 66         random_num = random.randint(1,100)
 67         if random_num == 8 or random_num == 88:     #降低子弹重复出现的可能
 68             self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y))
 69 
 70 
 71 class BaseBullet(Base):
 72     def __init__(self, screen_temp, x, y, image_name):
 73         Base.__init__(self, screen_temp, x, y, image_name)
 74 
 75     def display(self):
 76         self.screen.blit(self.image, (self.x, self.y))
 77 
 78 
 79 class Bullet(BaseBullet):
 80     def __init__(self, screen_temp, x, y):
 81         BaseBullet.__init__(self,screen_temp,x+40,y-20,"./feiji/bullet.png")
 82 
 83     def move(self):
 84         self.y = self.y - 5
 85 
 86     def judge(self):
 87         if self.y < 0:
 88             return True
 89         else:
 90             return False
 91 
 92 
 93 class EnemyBullet(BaseBullet):
 94     def __init__(self, screen_temp, x, y):
 95 
 96         BaseBullet.__init__(self,screen_temp,x+25,y+40,"./feiji/bullet1.png")
 97 
 98     def move(self):
 99         self.y = self.y + 5
100 
101     def judge(self):
102         if self.y > 852:
103             return True
104         else:
105             return False
106 
107 
108 def key_control(hero_temp):
109     # 获取事件,比如按键等
110     for event in pygame.event.get():
111 
112         if event.type == QUIT:
113             print("exit")
114             exit()
115 
116         elif event.type == KEYDOWN:
117             if event.key == K_a or event.key == K_LEFT:
118                 print(left)
119                 # x = x - 5
120                 hero_temp.move_left()
121 
122             elif event.key == K_d or event.key == K_RIGHT:
123                 print("right")
124                 # x = x + 5
125                 hero_temp.move_right()
126 
127             elif event.key == K_SPACE:
128                 hero_temp.fire()
129                 print("space")
130 
131 
132 def main():
133     # 创建一个窗口,用来显示内容
134     screen = pygame.display.set_mode((480, 852), 0, 32)
135 
136     # 创建一个和窗口大小相同的图片,用来当背景
137     background = pygame.image.load("./feiji/background.png")
138 
139     # 创建一个飞机
140     hero = HeroPlane(screen)
141 
142     # 创建一个敌机
143     enemy = EnemyPlane(screen)
144 
145     # 把背景图片放到窗口中显示
146     while True:
147         # 设定需要显示的背景图
148         screen.blit(background, (0, 0))
149 
150         # 显示飞机
151         hero.display()
152         enemy.display()
153         enemy.move()  # 调用敌机的移动方法
154         enemy.fire()
155 
156         # 更新需要显示的内容
157         pygame.display.update()
158 
159         key_control(hero)
160 
161         # 延迟降低CPU使用率
162         time.sleep(0.01)
163 
164 
165 if __name__ == __main__:
166     main()

 

以上是关于python 简答打飞机(面向对象)的主要内容,如果未能解决你的问题,请参考以下文章

Python——飞机大战(day10)

初识python 面向对象

Python面向对象

python基础-面向对象编程

VSCode自定义代码片段——JS中的面向对象编程

VSCode自定义代码片段9——JS中的面向对象编程