https://i.cnblogs.com/settings

Python之游戏开发-飞机大战

  1 #!/usr/bin/env python
  2 # coding: utf-8
  3 
  4 import pygame
  5 import time
  6 import random
  7 from pygame.locals import *
  8 
  9 
 10 class Base(object):
 11     def __init__(self, x, y, imageName):
 12         self.x = x
 13         self.y = y
 14         self.imageName = imageName
 15         self.image = pygame.image.load(self.imageName).convert()
 16 
 17     def display(self):
 18         screen.blit(self.image, (self.x, self.y))
 19 
 20 
 21 class Plane(Base):
 22     def __init__(self, screen, x, y, imageName, planeName):
 23         Base.__init__(self, x, y, imageName)
 24         self.screen = screen
 25         self.bulletList = []
 26         self.planeName = planeName
 27 
 28     def display(self):
 29         Base.display(self)
 30 
 31 
 32 class EnemyPlane(Plane):
 33     def __init__(self, screen):
 34         Plane.__init__(self, screen, 0, 0, "./feiji/enemy-3.gif", "Enemy")
 35         self.directioin = "right"  # right表示向右  left表示向左
 36         self.speed = random.randint(1, 5)
 37 
 38     def move(self):
 39         if self.directioin == "right":
 40             self.x += self.speed
 41         elif self.directioin == "left":
 42             self.x -= self.speed
 43             # 到达另外一个边界时,需要反转方向
 44         if self.x > 480:
 45             self.directioin = "left"
 46         elif self.x < 0:
 47             self.directioin = "right"
 48 
 49     def shoot(self):
 50         shootFlagList = [2, 6]
 51         shootFlag = random.randint(1, 100)
 52         if shootFlag in shootFlagList:
 53             self.bulletList.append(Bullet(self.screen, self.planeName, self.x, self.y))
 54             # print("x:%d,y:%d"%(self.x, self.y))
 55 
 56     def display(self):
 57         Plane.display(self)
 58         # print(self.bulletList)
 59 
 60         for bullett in self.bulletList:
 61             if bullett.y <= 700:
 62                 bullett.display()
 63                 bullett.move()
 64                 global hero
 65                 # 以中点为心
 66                 if ((bullett.x - hero.x - 40) ** 2 + (
 67                         bullett.y - hero.y - 40) ** 2) ** 0.5 < 40 and bullett.baozhaflag == 0:
 68                     bullett.baozhaflag = 1
 69                     global score
 70                     if score > 20:
 71                         score -= 20
 72                     else:
 73                         score = 0
 74                     global flagg
 75                     flagg = 1
 76                     print(hero.x, hero.y)
 77                     imageName = "./feiji/hero_blowup_n3.gif"
 78                     im = hero.image
 79                     hero.image = pygame.image.load(imageName).convert()
 80                     print("END")
 81             else:
 82                 self.bulletList.remove(bullett)
 83 
 84 
 85 class playerPlane(Plane):
 86     def __init__(self, screen):
 87         Plane.__init__(self, screen, 230, 600, "./feiji/hero.gif", "player")
 88         self.speed = 20
 89 
 90     def display(self):
 91         Plane.display(self)
 92         # print(self.bulletList)
 93 
 94         for bullett in self.bulletList:
 95             if bullett.y >= 0:
 96                 bullett.display()
 97                 bullett.move()
 98                 global enemy
 99                 if ((enemy.x + 40 - bullett.x) ** 2 + (
100                         enemy.y + 80 - bullett.y) ** 2) ** 0.5 < 40 and bullett.baozhaflag == 0:
101                     bullett.baozhaflag = 1
102                     global escore
103                     if escore > 0:
104                         escore -= 20
105                     global flagge
106                     flagge = 1
107                     print(enemy.x, enemy.y)
108                     imageName = "./feiji/enemy2_down1.gif"
109                     im = enemy.image
110                     enemy.image = pygame.image.load(imageName).convert()
111                     print("END")
112             else:
113                 self.bulletList.remove(bullett)
114 
115     def moveRight(self):
116         if self.x >= 0 and self.x <= 420:
117             self.x += self.speed
118 
119     def moveLeft(self):
120         if self.x <= 480 and self.x >= 20:
121             self.x -= self.speed
122 
123     def sheBullet(self):
124         bui = Bullet(self.screen, "player", self.x + 40, self.y - 4)
125         self.bulletList.append(bui)
126 
127 
128 # 导弹类
129 class Bullet(Base):
130     def __init__(self, screen, bulletName, x, y):
131         self.bulletName = bulletName
132         imageName1 = "./feiji/bullet-1.gif"
133         self.baozhaflag = 0
134         if bulletName == "player":
135             imageName1 = "./feiji/bullet-3.gif"
136 
137         Base.__init__(self, x, y, imageName1)
138 
139     def move(self):
140         if self.bulletName == "player":
141             self.y -= 2
142         else:
143             self.y += 2
144 
145     def display(self):
146         Base.display(self)
147 
148 
149 if __name__ == '__main__':
150     score = 200
151     escore = 100
152     # 1.创建一个窗口
153     screen = pygame.display.set_mode((480, 700), 0, 32)
154     # 2.创建一个图片
155     background = pygame.image.load("./feiji/background.png").convert()
156 
157     color_red = (255, 0, 0)
158     color_green = (0, 255, 0)
159     color_blue = (0, 0, 255)
160     print(pygame.font.get_fonts())
161     pygame.font.init()
162     # font = pygame.font.SysFont(None, 48)
163 
164     # 使用系统字体
165     fontObj3 = pygame.font.SysFont('arial', 20)
166 
167     # 加粗
168     fontObj3.set_bold(True)
169 
170     # 斜体
171     fontObj3.set_italic(True)
172 
173     # 文字具有蓝色背景
174     textSurfaceObj3 = fontObj3.render("敌方血量:" + str(score), True, color_red, color_blue)
175     textRectObj3 = textSurfaceObj3.get_rect()
176     textRectObj3.center = (60, 510)
177 
178     # 文字具有蓝色背景
179     textSurfaceObj2 = fontObj3.render("我方血量:" + str(escore), True, color_red, color_blue)
180     textRectObj2 = textSurfaceObj2.get_rect()
181     textRectObj2.center = (60, 10)
182 
183     # 创建玩家飞机
184     # hero = pygame.image.load("./feiji/hero.gif").convert()
185     global flagg
186     flagg = 0
187     flagge = 0
188     global hero
189     hero = playerPlane(screen)
190     global enemy
191     enemy = EnemyPlane(screen)
192     # 3.图片到背景去
193     imm = hero.image
194     imme = enemy.image
195     while True:
196         screen.blit(background, (0, 0))
197         textSurfaceObj3 = fontObj3.render("Me   :" + str(score), True, color_red, color_blue)
198         textRectObj3 = textSurfaceObj3.get_rect()
199         textRectObj3.center = (60, 510)
200 
201         # 文字具有蓝色背景
202         textSurfaceObj2 = fontObj3.render("Enemy :" + str(escore), True, color_red, color_blue)
203         textRectObj2 = textSurfaceObj2.get_rect()
204         textRectObj2.center = (60, 10)
205 
206         screen.blit(textSurfaceObj3, textRectObj3)
207         screen.blit(textSurfaceObj2, textRectObj2)
208         # screen.blit(hero, (x, 600))
209         # hero.display()
210         # 获取事件,比如按键等
211         for event in pygame.event.get():
212 
213             # 判断是否是点击了退出按钮
214             if event.type == QUIT:
215                 print("exit")
216                 exit()
217             # 判断是否是按下了键
218             elif event.type == KEYDOWN:
219                 # 检测按键是否是a或者left
220                 if event.key == K_a or event.key == K_LEFT:
221                     print('left')
222                     hero.moveLeft()
223 
224                 # 检测按键是否是d或者right
225                 elif event.key == K_d or event.key == K_RIGHT:
226                     print('right')
227                     hero.moveRight()
228 
229                 # 检测按键是否是空格键
230                 elif event.key == K_SPACE:
231                     print('space')
232                     hero.sheBullet()
233 
234                     # 更新需要显示的内容
235 
236         hero.display()
237         if flagg == 1:
238             hero.image = imm
239             flagg = 0
240 
241         # 让敌机自己移动以及发射子弹
242         enemy.move()
243         enemy.shoot()
244         enemy.display()
245         if flagge == 1:
246             enemy.image = imme
247             flsgge = 0
248         pygame.display.update()
249         time.sleep(0.01)

 

posted @ 2019-11-09 10:42  LLLN  阅读(292)  评论(0)    收藏  举报
Copyright © 2021 llln Powered by .NET 5.0 on Kubernetes