pygame实现简易飞机大战

利用pygame实现了简易版飞机大战。源代码如下:

# -*- coding:utf-8 -*-
import pygame
import sys
from pygame.locals import *
from pygame.font import *
import time
import random

class Hero(object):
 #玩家 英雄类
 def __init__(self, screen_temp):
  self.x = 210
  self.y = 700
  self.life = 21
  # self.life = 100
  self.image = pygame.image.load("./feiji/hero1.png")
  self.screen = screen_temp
  self.bullet_list = []#用来存储子弹对象的引用
  #爆炸效果用的如下属性
  self.hit = False #表示是否要爆炸
  self.bomb_list = [] #用来存储爆炸时需要的图片
  self.__create_images() #调用这个方法向bomb_list中添加图片
  self.image_num = 0 #用来记录while True的次数,当次数达到一定值时才显示一张爆炸的图,然后清空,,当这个次数再次达到时,再显示下一个爆炸效果的图片
  self.image_index = 0#用来记录当前要显示的爆炸效果的图片的序号

 def __create_images(self):
  #添加爆炸图片
  self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n1.png"))
  self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n2.png"))
  self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n3.png"))
  self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n4.png"))

 def display(self):
  #显示玩家的飞机
  #如果被击中,就显示爆炸效果,否则显示普通的飞机效果
  if self.hit == True:
   self.screen.blit(self.bomb_list[self.image_index], (self.x, self.y))#(self.x, self.y)是指当前英雄的位置
   #blit方法 (一个对象,左上角位置)
   self.image_num += 1
   print(self.image_num)
   if self.image_num == 7:
    self.image_num = 0
    self.image_index += 1
    print(self.image_index) #这里子弹打住英雄时没有被清除掉,所以打一下,就死了
   if self.image_index > 3:
    time.sleep(1)
    exit()#调用exit让游戏退出
    #self.image_index = 0
  else:
   if self.x< 0: #控制英雄,不让它跑出界面
    self.x = 0
   elif self.x > 382:
    self.x = 382
   if self.y < 0:
    self.y = 0
   elif self.y > 750:
    self.y = 750
   self.screen.blit(self.image,(self.x, self.y))#z这里是只要没有被打中,就一直是刚开始的样子

  #不管玩家飞机是否被击中,都要显示发射出去的子弹
  for bullet in self.bullet_list:
   bullet.display()
   bullet.move()

 def move(self, move_x,move_y):
  self.x += move_x
  self.y += move_y

 def fire(self):
  #通过创建一个子弹对象,完成发射子弹
  bullet = Bullet(self.screen, self.x, self.y)#创建一个子弹对象
  self.bullet_list.append(bullet)

 def bomb(self):
  self.hit = True

 def judge(self):
  global life
  if life <= 0:
   self.bomb()

class Bullet(object):
 #玩家子弹类
 def __init__(self, screen_temp, x_temp, y_temp):
  self.x = x_temp + 40
  self.y = y_temp - 20
  self.image = pygame.image.load("./feiji/bullet.png")
  self.screen = screen_temp

 def display(self):
  self.screen.blit(self.image, (self.x, self.y))

 def move(self):
  self.y -= 10

class Bullet_Enemy(object):
 #敌机子弹类
 def __init__(self, screen_temp, x_temp, y_temp):
  self.x = x_temp + 25
  self.y = y_temp + 30
  self.image = pygame.image.load("./feiji/bullet1.png")
  self.screen = screen_temp

 def display(self):
  self.screen.blit(self.image,(self.x,self.y))

 def move(self, hero):
  self.y += 10
  global life
  if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
  #if self.y in range(hero.y, hero.y + 40) and self.x in range(hero.x, hero.x + 40):
   life -= 10
   #self.bullet_list.remove()
   print("---judge_enemy---")
   return True
   if life<=0:
    hero.bomb()
  return False

class Bullet_Boss(object):
 #boss子弹类1
 def __init__(self, screen_temp, x_temp, y_temp):
  self.x = x_temp + 80
  self.y = y_temp + 230
  self.image = pygame.image.load("./feiji/bullet2.png")
  self.screen = screen_temp

 def display(self):
  self.screen.blit(self.image, (self.x, self.y))

 def move(self, hero):
  self.y += 6
  self.x += 2
  global life
  if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
  #if self.y in range(hero.y, hero.y + 40) and self.x in range(hero.x, hero.x + 40):
   life -= 20
   #self.bullet_list.remove()
   print("---judge_boss---")
   return True
   if life<=0:
    hero.bomb()
  return False

class Bullet_Boss1(object):
 #boss子弹类2
 def __init__(self, screen_temp, x_temp, y_temp):
  self.x = x_temp + 80
  self.y = y_temp + 230
  self.image = pygame.image.load("./feiji/bullet2.png")
  self.screen = screen_temp

 def display(self):
  self.screen.blit(self.image, (self.x, self.y))

 def move(self, hero):
  self.y += 6
  self.x -= 2
  global life
  if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
  #if self.y in range(hero.y, hero.y + 40) and self.x in range(hero.x, hero.x + 40):
   life -= 20
   #self.bullet_list.remove()
   print("---judge_boss---")
   return True
   if life<=0:
    hero.bomb()
  return False

class Bullet_Boss2(object):
 #boss子弹类3
 def __init__(self, screen_temp, x_temp, y_temp):
  self.x = x_temp + 80
  self.y = y_temp + 230
  self.image = pygame.image.load("./feiji/bullet2.png")
  self.screen = screen_temp

 def display(self):
  self.screen.blit(self.image, (self.x, self.y))

 def move(self, hero):
  self.y += 6
  global life
  if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
  #if self.y in range(hero.y, hero.y + 40) and self.x in range(hero.x, hero.x + 40):
   life -= 20
   #self.bullet_list.remove()
   print("---judge_boss---")
   return True
   if life<=0:
    hero.bomb()
  return False

class Base(object):
 #基类 类似于抽象类
 def __init__(self, screen_temp, x, y, image_name):
  self.x = x
  self.y = y
  self.screen = screen_temp
  self.image = pygame.image.load(image_name)
  self.alive = True

 def display(self):
  if self.alive == True:
   self.screen.blit(self.image, (self.x, self.y))

 def move(self):
  self.y += 5

class bomb_bullet(Base):
 #炸弹类
 def __init__(self, screen_temp):
  Base.__init__(self, screen_temp, random.randint(45, 400), 0, "./feiji/bomb.png")

 def judge(self, hero):
  if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
   self.alive = False
   hero.bomb()

  if self.y >= 850:
   #self.alive = False
   self.y = 0
   self.x = random.randint(45, 400)
   #print("bomb.y = %d"%self.y)

class supply(Base):
 #补给类
 def __init__(self, screen_temp):
  Base.__init__(self, screen_temp, random.randint(45, 400), -300, "./feiji/bomb-1.gif")

 def judge(self, hero):
  global life
  if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
   self.alive = False
   life += 10

  if self.y >= 1500:
   self.y = 0
   self.x = random.randint(45, 400)
   self.alive = True

class clear_bullet(Base):
 def __init__(self, screen_temp):
  Base.__init__(self, screen_temp, random.randint(45, 400), 0, "./feiji/bomb-2.gif")
  self.alive = False

 def judge(self, hero, enemies):
  global q
  q += 1
  #self.move()
  if q == 20:
   #self.move()
   self.alive = True
   q = 0
   if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
    self.alive = False
    for enemy in enemies:
     enemy.hit == True

class EnemyPlane(object):
 #敌机类
 def __init__(self, screen_temp):
  self.x = random.randint(15, 480)
  self.y = 0
  self.image = pygame.image.load("./feiji/enemy0.png")
  self.screen = screen_temp
  self.bullet_list = []#用来存储子弹对象的引用
  #self.direction = "right"#用来设置这个飞机默认的移动方向
  self.hit = False
  self.bomb_list = []
  self.__create_images()
  self.image_num = 0
  self.image_index = 0
  #利用产生的随机数,随机确定飞机初始移动方向
  self.k = random.randint(1, 20)
  if self.k <= 10:
   self.direction = "right"
  elif self.k > 10:
   self.direction = "left"

 def display(self, hero):
  #显示敌人的飞机
  if not self.hit:
   self.screen.blit(self.image, (self.x,self.y))
  else:
   self.screen.blit(self.bomb_list[self.image_index], (self.x,self.y))
   self.image_num += 1
   if self.image_num == 3 and self.image_index < 3:
    self.image_num = 0
    self.image_index += 1
    #print(self.image_index)
   # if self.image_index > 2:
   #  time.sleep(0.1)

  for bullet in self.bullet_list:
   bullet.display()
   if(bullet.move(hero)):
    self.bullet_list.remove(bullet)

 def move(self):
  #利用随机数来控制飞机移动距离,以及移动范围
  d1 = random.uniform(1,3)
  d2 = random.uniform(0.2,3)
  p1 = random.uniform(50,100)
  p2 = random.uniform(-200,0)
  if self.direction == "right":
   self.x += d1
  elif self.direction == "left":
   self.x -= d1

  if self.x > 480 - p1:
   #480 - 50
   self.direction="left"
  elif self.x < p2:
   self.direction = "right"
  self.y += d2

 def bomb(self):
  self.hit = True

 def __create_images(self):
  self.bomb_list.append(pygame.image.load("./feiji/enemy0_down1.png"))
  self.bomb_list.append(pygame.image.load("./feiji/enemy0_down2.png"))
  self.bomb_list.append(pygame.image.load("./feiji/enemy0_down3.png"))
  self.bomb_list.append(pygame.image.load("./feiji/enemy0_down4.png"))

 def fire(self):
  #利用随机数来控制敌机的开火,1/80的概率
  s = random.randint(0,800)
  bullet1 = Bullet_Enemy(self.screen, self.x, self.y)
  if s < 10:
   self.bullet_list.append(bullet1)

class EnemyPlanes(EnemyPlane):
 #敌机群类 继承自EnemyPlane类
 def __init__(self, screen_temp):
  EnemyPlane.__init__(self, screen_temp)
  self.num = 0
  self.enemy_list = [] #用列表存储产生的多架敌机
  self.screen = screen_temp

 def add_enemy(self, num):
 #产生多架敌机的函数
  self.num = num
  for i in range(num):
   enemy = EnemyPlane(self.screen)
   self.enemy_list.append(enemy)

 def display(self, hero):
  for i in range(self.num):
   self.enemy_list[i].display(hero)

 def move(self):
  for i in range(self.num):
   self.enemy_list[i].move()

 def fire(self):
  #s = random.randint(0,1000)
  for i in range(self.num):
   self.enemy_list[i].fire()

class Boss(EnemyPlane):
 #boss敌机类 继承自EnemyPlane类
 def __init__(self,screen_temp):
  EnemyPlane.__init__(self,screen_temp)
  self.x = 150
  self.y = 0
  self.bomb_list = []
  self.__create_images()
  self.image = pygame.image.load("./feiji/enemy2.png")
  self.screen = screen_temp
  self.bullet_list = []

 def __create_images(self):
  #self.bomb_list.append(pygame.image.load("./feiji/enemy2.png"))
  self.bomb_list.append(pygame.image.load("./feiji/enemy2.png"))
  self.bomb_list.append(pygame.image.load("./feiji/enemy2_down1.png"))
  self.bomb_list.append(pygame.image.load("./feiji/enemy2_down2.png"))
  self.bomb_list.append(pygame.image.load("./feiji/enemy2_down3.png"))
  self.bomb_list.append(pygame.image.load("./feiji/enemy2_down4.png"))
  self.bomb_list.append(pygame.image.load("./feiji/enemy2_down5.png"))
  self.bomb_list.append(pygame.image.load("./feiji/enemy2_down6.png"))

 def display(self, hero):
  #显示敌人的飞机
  global g
  #print(g)
  self.screen.blit(self.bomb_list[g], (self.x,self.y))
  for bullet in self.bullet_list:
   bullet.display()
   if(bullet.move(hero)):
    self.bullet_list.remove(bullet)

 def move(self):
  d1 = 0
  self.y += 0

 def fire(self):
  global s
  s += 1
  bullet1 = Bullet_Boss(self.screen, self.x, self.y)
  bullet2 = Bullet_Boss1(self.screen, self.x, self.y)
  bullet3 = Bullet_Boss2(self.screen, self.x, self.y)
  if s == 20:
   s = 0
   self.bullet_list.append(bullet1)
   self.bullet_list.append(bullet2)
   self.bullet_list.append(bullet3)

def judge1(hero,enemy):
 #判断敌机的炸毁
 for bullet1 in hero.bullet_list:
  if bullet1.y in range(int(enemy.y),int(enemy.y + 30)) and bullet1.x in range(int(enemy.x-10),int(enemy.x + 50)):
   hero.bullet_list.remove(bullet1)
   enemy.bomb()
  if bullet1.y < 0 or bullet1.x < 0 or bullet1.x > 480: #删除越界的玩家子弹
   hero.bullet_list.remove(bullet1) 

def judge3(hero,boss):
 #判断boss的炸毁
 global goal, g, goal0
 for bullet3 in hero.bullet_list:
  if bullet3.y in range(int(boss.y), int(boss.y + 60)) and bullet3.x in range(int(boss.x), int(boss.x + 100)):
   hero.bullet_list.remove(bullet3)
   g += 1
   boss.image = boss.bomb_list[g]
   print("g = %d"%g)
   if g >= 6:
    boss.y, g, goal = 0, 0, 0
    boss.bomb()
    goal0 += 10 

def clear_enemy(enemies):
 #清除敌机群类中被炸毁的敌机
 global goal, goal0
 for enemy in enemies.enemy_list:
  if enemy.hit == True and enemy.image_index == 3:
   enemies.enemy_list.remove(enemy)
   enemies.num -= 1
   goal += 1
   goal0 += 5
   print("goal = %d"%goal)
  if enemy.y >= 850:
   enemies.enemy_list.remove(enemy)
   enemies.num -= 1

def judge_num(enemies):
 #判断频幕上敌人的数量,如果为零,继续添加敌人
 n = random.randint(1,5)
 if len(enemies.enemy_list) == 0:
  enemies.add_enemy(n)

def show_text(screen_temp):
 #在屏幕上显示文字
 text = "GOAL:" + str(goal0) + "Life:" + str(life)
 font_size = 50
 pos = (0,0)
 color = (0,255,0)
 cur_font = pygame.font.SysFont("宋体",font_size)
 text_fmt = cur_font.render(text, 1, color)
 screen_temp.blit(text_fmt, pos)

def creat_bomb(screen_temp):
 bomb = bomb_bullet(screen_temp)
 bomb_list = []
 bomb_list.apend(bomb)

#定义的全局变量
goal = 0 #玩家得分
goal0 = 0
g = 0 #击中boss的次数
life = 100#生命值
s = 0 #判断大boss是否发射子弹
q = 0

def main():
 #主函数执行
 #获取事件,比如按键等
 bb = False
 move_x = 0
 move_y = 0
 pygame.init()
 screen = pygame.display.set_mode((480,852),0,32)
 #         210,400
 background = pygame.image.load("./feiji/background.png")
 pygame.display.set_caption("飞机大战")
 atlas = pygame.image.load("./feiji/New Atlas.png")
 #创建玩家飞机
 hero = Hero(screen)
 #创建敌机群
 enemis = EnemyPlanes(screen)
 enemis.add_enemy(5)
 #创建boss对象
 boss = Boss(screen)
 #创建炸弹对象
 bomb = bomb_bullet(screen)
 #创建补给对象
 supply0 = supply(screen)
 clear = clear_bullet(screen)
 left_key, right_key, up_key, down_key, done = 0, 0, 0, 0, 0
 # mark = 0#用来判断boss发射子弹
 while True:
  if done:
   if done % 8 == 0:
    done = 1
    hero.fire()
   else:
    done += 1
  for event in pygame.event.get():
   #判断是否是点击了退出按钮
   if event.type == QUIT:
    print("exit")
    exit()
   #判断是否是按下了键
   if event.type == KEYDOWN :
    #down
    #检测按键是否是a或者left

    if event.key == K_a or event.key == K_LEFT:
     #print('left')
     move_x = -5
     left_key += 1 

    #检测按键是否是d或者right
    elif event.key == K_d or event.key == K_RIGHT:
     #print('right')
     move_x = 5
     right_key += 1

    elif event.key == K_w or event.key == K_UP:
     move_y = -5
     up_key += 1

    elif event.key == K_s or event.key == K_DOWN:
     move_y = 5
     down_key += 1

    #检测按键是否是空格键
    elif event.key == K_SPACE:
     #print('space')
     hero.fire()
     done = 1
     #enemis.fire()

    elif event.key == K_b:
     print('b')
     hero.bomb()

   if event.type == KEYUP:
    if event.key == K_a or event.key == K_LEFT:
     left_key -= 1
     if right_key == 0:
      move_x = 0
     else:
      move_x = 5

    if event.key == K_d or event.key == K_RIGHT:
     right_key -= 1
     if left_key == 0:
      move_x = 0
     else:
      move_x = -5

    if event.key == K_w or event.key == K_UP:
     up_key -= 1
     if down_key == 0:
      move_y = 0
     else:
      move_y = 5 

    if event.key == K_s or event.key == K_DOWN:
     down_key -= 1
     if up_key == 0:
      move_y = 0
     else:
      move_y = -5

    if event.key == K_SPACE:
     done = 0 

  screen.blit(background, (0, 0))
  hero.move(move_x, move_y)
  hero.display()
  hero.judge()
  enemis.display(hero)
  enemis.move()
  enemis.fire()
  bomb.display()
  bomb.judge(hero)
  bomb.move()
  supply0.display()
  supply0.judge(hero)
  supply0.move()
  #clear.display()
  #clear.judge(hero, enemis)
  #clear.move()
  for i in range(enemis.num):
   judge1(hero, enemis.enemy_list[i])
   #enemis.enemy_list[i].judge(hero)
  clear_enemy(enemis)
  judge_num(enemis)
  show_text(screen)
  if goal >= 15:
   boss.display(hero)
   boss.move()
   # mark+=1
   # if mark==8:
   boss.fire()
    # mark = 0
   #boss.judge
   judge3(hero, boss)
  pygame.display.update()

if __name__ == "__main__":
 main()

方法是利用面向对象的思想,写了基本的敌机类、英雄类、武器类等,利用继承关系产生多架敌机。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • python实现飞机大战微信小游戏

    0.前言 我学一种语言,可以说学任何东西都喜欢自己动手实践,总感觉自己动手一遍,就可以理解的更透彻,学python也一样,自己动手写代码,但更喜欢做点小东西出来,一边玩一边学.下面我就展示一下我最近做的一个小游戏. 1.素材准备 首先我们先来预览一下游戏的最终运行界面 根据游戏界面,我们可以清楚的知道必须要先准备游戏背景图片,飞机图片,子弹图片等等.这些素材我已经放到网上, 点我下载 ,里面包括了我的代码和图片素材. 2.代码部分 库依赖: pygame 本游戏主要有两个py文件,主文件plan

  • 用Python写飞机大战游戏之pygame入门(4):获取鼠标的位置及运动

    目标是拷贝微信的飞机大战,当然拷贝完以后大家就具备自己添加不同内容的能力了. 首先是要拿到一些图片素材,熟悉使用图像处理软件和绘画的人可以自己制作,并没有这项技能的同学只能和我一样从网上下载相应的素材了. 网上可以找到相应的这样的图片,注意,所有的元件图片要是png类型的图片,那样可以有透明的背景,否则会有白色的边框露出来. 找到素材以后我们就要开始搭建我们的飞机大战了. 微信上的飞机大战是由手指控制的,在电脑上,我们就先用鼠标代替了. 按照之前我们在天空上移动云的那个程序,我们可以知道该怎么做

  • pygame实现简易飞机大战

    利用pygame实现了简易版飞机大战.源代码如下: # -*- coding:utf-8 -*- import pygame import sys from pygame.locals import * from pygame.font import * import time import random class Hero(object): #玩家 英雄类 def __init__(self, screen_temp): self.x = 210 self.y = 700 self.life

  • Pygame库200行代码实现简易飞机大战

    写在开头,因为这个小游戏的实验主要是帮助我熟悉pygame库的使用,所以游戏的一些地方可能存在不完善处,还望包涵. 安装使用库 pygame简介 pygame是跨平台python模块,专为电子游戏设计,包括图像.声音.建立在SDL基础上,允许实时电子游戏研发而无需被低级语言束缚, 开发者可以把精力放在游戏的架构上. pgame中主要模块介绍 (1) pygame pygame模块会自动导入其它的pygame相关模块. pygame模块包括surface函数, 可以返回一个新的surface 对象

  • python pygame模块编写飞机大战

    本文实例为大家分享了python pygame模块编写飞机大战的具体代码,供大家参考,具体内容如下 该程序没有使用精灵组,而是用列表存储对象来替代精灵组的动画效果.用矩形对象的重叠来判断相撞事件.该程序可以流畅运行,注释较为详细,希望可以帮助大家. import pygame from pygame.locals import * from sys import exit import time import random # 创建子弹类,把子弹的图片转化为图像对象,设定固定的移动速度 clas

  • Pygame框架实现飞机大战

    本文实例为大家分享了Pygame框架实现飞机大战的具体代码,供大家参考,具体内容如下 飞机大战主游戏类 """ 项目名称:简单飞机大战游戏 基于框架:Pygame 开发日期:2020.07 开发人员:Y 版本:**** """ import sys # 导入内置模块sys 调用sys.exit() import pygame # 导入第三方模块pygame from plane_sprites import * # 导入自定义模块内所有工具 cl

  • python之pygame模块实现飞机大战完整代码

    本文实例为大家分享了python之pygame模块实现飞机大战的具体代码,供大家参考,具体内容如下 Python飞机大战步骤: 1.数据区 2.主界面 3.飞船 4.事件监控及边界 5.外星人 6.记分系统 飞机大战效果图: 源码: """ 功能:飞机大战 time:2019/10/3 """ import os import pygame import sys import time from pygame.sprite import Spri

  • Python Pygame实战之飞机大战的实现

    目录 导语 一.环境安装 1)各种素材(图片.字体等) 2)运行环境 二.代码展示 1)文章思路 2)附代码讲解 3)主程序 三.效果展示 总结 导语 三月疫情原因,很多地方都封闭式管理了! 在回家无聊的打酱油,小编今天给大伙带来了一波小游戏——全民左右飞机大战!在这个快熬不下去的日子里,打打飞机消遣闲暇时间,也是蛮惬意的,这几天小编必须全身心投入到飞机大战中来!肝了几天这款小游戏终于面市啦! 这次的游戏操作很简单,就是左右移动飞机,躲避敌人飞机,打落敌机会随机掉落金币,我们 要打击的敌人!每个

  • JavaScript实现简易飞机大战

    本文实例为大家分享了JavaScript实现简易飞机大战的具体代码,供大家参考,具体内容如下 话不多说,直接上代码 <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta name="viewport" content="width=device-width, initial-scale

  • java实现简易飞机大战

    目录 整体思路 代码实现 英雄战机类 敌机类 子弹类 图片工具类 游戏窗体类 启动游戏类 运行效果图 本文实例为大家分享了java实现简易飞机大战的具体代码,供大家参考,具体内容如下 整体思路 1.创建游戏窗体,添加面板JPanel,重写JPanel中的paint方法,遍历所有飞机和子弹绘制,用定时器进行重绘,实现动画效果2.添加敌机和发射子弹用的是多线程3.碰撞检测采用的是矩形类Rectangle中的intersects方法 代码实现 用手机查看代码好像只显示62行 英雄战机类 package

  • python实现飞机大战游戏(pygame版)

    简介 使用python实现pygame版的飞机大战游戏: 环境:Windows系统+python3.8.0 游戏规则: 1.点击"PLAY"或者按键"P"开始游戏: 2.敌机根据设置频率从顶部随机位置生成,生成后向下移动: 3.飞船在底部中间生成,玩家使用上下左右键控制飞船移动,敲击空格键发射子弹: 4.子弹打到敌机,该敌机产生爆炸效果并累计分数到右上角: 5.消灭10只飞机后,等级升高,敌机生成频率变快,下落速度也变快: 6.当三条命都消失了,游戏结束. 游戏运行

  • python版飞机大战代码分享

    利用pygame实现了简易版飞机大战.源代码如下: # -*- coding:utf-8 -*- import pygame import sys from pygame.locals import * from pygame.font import * import time import random class Hero(object): #玩家 英雄类 def __init__(self, screen_temp): self.x = 210 self.y = 700 self.life

随机推荐