python飞机大战游戏实例讲解

(编辑:jimmy 日期: 2024/11/13 浏览:2)

记得刚学python那会,作过一个飞机大战小项目,这个项目非常经典,可以帮助初学者提高动手能力,今天把它分享出来。

一、项目介绍

先放几张图片

python飞机大战游戏实例讲解

python飞机大战游戏实例讲解

python飞机大战游戏实例讲解

python飞机大战游戏实例讲解

二、项目实现

1.首先安装库

pip install pygame

2.主要python代码

import pygame

from pygame.locals import *
import random
#https://blog.csdn.net/qq_36079986/article/details/110395731
class HeroPlane(object):# 创建一个飞机的英雄类
 def __init__(self,screen_temp): # 始化方法
 self.x = 200
 self.y = 500
 self.image = pygame.image.load('./img/hero1.png')
 self.screen = screen_temp# 因为在display方法中使用到了screen方法# 但是在类中没有 也不能调用 # 所以说 以传参的方法传递到类中
 self.bullet_list = []
 def display(self):# 创建一个展现飞机的方法
 herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3 = -100, -100, -100, -100, -100, -100
 self.screen.blit(self.image,(self.x, self.y))
 for bullet in self.bullet_list:###遍历子弹类对象,赋值给bullet
  herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3 = bullet.display()###### bullet对象调用display方法, 显示子弹
  bullet.move()
  if self.bullet_list[0].y < 0:
  del self.bullet_list[0]
 return herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3
 def move_left(self):
 self.x -= 50
 def move_right(self):
 self.x += 50
 def move_up(self):
 self.y -= 50
 def move_down(self):
 self.y += 50
 def fire(self):
 print(len(self.bullet_list),'个子弹##########################################')
 self.bullet_list.append(HeroBullet(self.screen,self.x, self.y))
class HeroBullet(object):
 def __init__(self,screen_temp,x_temp,y_temp):
 self.x = x_temp
 self.y = y_temp
 self.image = pygame.image.load('./img/bullet.png')
 self.screen = screen_temp
 def display(self):
 self.screen.blit(self.image,(self.x - 22, self.y-22))
 self.screen.blit(self.image, (self.x + 39, self.y-22))
 self.screen.blit(self.image, (self.x + 100, self.y-22))
 return self.x - 22, self.y - 22, self.x + 39, self.y - 22, self.x + 100, self.y - 22
 def move(self):
 self.y -= 1
class EnemyPlane(object):
 def __init__(self,screen_temp):
 self.ra = random.randint(0,2)
 self.x = random.randint(0,400)
 self.y = 0
 self.image = pygame.image.load('./img/enemy1.png')
 if 0<=self.x<=540:
  if self.ra == 0:
  self.flag = "left"
  elif self.ra == 1:
  self.flag = "right"
  else:
  self.flag = 'middle'
 self.screen.blit(self.image, (self.x, self.y))
 if self.x < 0:
  self.flag = "left"
 elif self.x > 450:
  self.flag = 'right'
 if self.flag == "left":
  self.x += 2
 elif self.flag == 'right':
  self.x -= 2
 self.y += 0.1
 if (random.randint(0,50) == 1):
  self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y))
  # print(len(self.bullet_list),'颗敌方子弹')
 for bullet in self.bullet_list:
  bullet.display()
  if bullet.y > 700:
  del bullet
class EnemyBullet(object):
 def __init__(self,screen_temp,x,y):
 self.x = x + 30
 self.y = y + 90
 self.image = pygame.image.load('./img/bullet-1.gif')
 self.y+= 10
def key_control(hero_temp):
 for event in pygame.event.get(): # 判断输入类型
 if event.type == QUIT:#判断是否是点击了退出按钮
  print("exit")
  exit()
 elif event.type == KEYDOWN:#判断是否是按下了键# 如果是键盘事件(键盘按下事件)
  if event.key == K_a or event.key == K_LEFT:
  hero_temp.move_left()
  elif event.key == K_d or event.key == K_RIGHT:
  hero_temp.move_right()
  elif event.key == K_w or event.key == K_UP:
  hero_temp.move_up()
  elif event.key == K_s or event.key == K_DOWN:
  hero_temp.move_down()
  elif event.key == K_SPACE:#检测按键是否是空格键
  hero_temp.fire()
def main():
 screen = pygame.display.set_mode((480, 650), 0, 32) # 使用screen资源来创建背景# 返回值是一个资源
 pygame.display.set_caption("aircraft game") # 设置窗口标题
 background = pygame.image.load('./img/background.png')## ./ 当前路径 ../ 上一级路径 / 根目录
 hero = HeroPlane(screen)
 enemy_list = []
 j = 0
 while True:
 screen.blit(background, (0, 0)) # 资源中的blit方法能够加载游戏的画面
 herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3 = hero.display() #####刷新
 if len(enemy_list) <= 10:
  enemy_list.append(EnemyPlane(screen))
 if enemy_list[0].y >= 700:
  del enemy_list[0]
 for enemy_each in enemy_list :
  # enemy_each.fire()
  enemy_each.move()
  enemy_each.display()
  # print(len(enemy_list), '个敌机')
  if (enemy_each.x <= herp_x1 -10 <= enemy_each.x + 80 and enemy_each.y -10<= herp_y1<= enemy_each.y + 90) or (enemy_each.x -10 <= herp_x2<= enemy_each.x + 80 and enemy_each.y -10<= herp_y2<= enemy_each.y + 90) or (enemy_each.x-10 <= herp_x3<= enemy_each.x + 80 and enemy_each.y -10<= herp_y3<= enemy_each.y + 90):
  enemy_list.remove(enemy_each)
  # print(len(enemy_list), '个敌机')
  print('jizhon')
 # print(hero.x,type(hero.x))
 if j % 100 == 5:
  print(len(enemy_list),'个敌机')
 pygame.display.update() ### 加载游戏
 key_control(hero)# 调用键盘事件函数
 j += 1
 # print(herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3)
if __name__ == '__main__':
 main()

三、说明:代码功能都在注释里面,我就不做过多解释了;与本项目有关的图片数据我传到资源里面了,大家可以免费下载,我把代码和数据都打包到一起了,大家下载下来导入库就可以直接一些了。

实例补充

import pygame
 
class MyPlane(pygame.sprite.Sprite):
 def __init__(self,bg_size):
 pygame.sprite.Sprite.__init__(self)
 
 self.image1 = pygame.image.load(r'E:\learn pygame\image\me1.png').convert_alpha()
 self.image2 = pygame.image.load(r'E:\learn pygame\image\me2.png').convert_alpha()
 self.image3 = pygame.image.load(r'E:\learn pygame\image\me_protect.png').convert_alpha()
 self.destroy_images = []
 self.destroy_images.extend([ pygame.image.load(r'E:\learn pygame\image\me_destroy_1.png').convert_alpha(),  pygame.image.load(r'E:\learn pygame\image\me_destroy_2.png').convert_alpha(),  pygame.image.load(r'E:\learn pygame\image\me_destroy_3.png').convert_alpha(),  pygame.image.load(r'E:\learn pygame\image\me_destroy_4.png').convert_alpha()  ])
 self.rect = self.image1.get_rect()
 self.width,self.height = bg_size[0],bg_size[1]
 self.rect.left,self.rect.top = (self.width - self.rect.width) // 2,self.height - self.rect.height - 60
 self.speed = 10
 self.active = True
 self.invincible = False
 self.mask = pygame.mask.from_surface(self.image1)
 
 def moveUp(self):
 if self.rect.top > 0:
 self.rect.top -= self.speed
 else:
 self.rect.top = 0
 
 def moveDown(self):
 if self.rect.bottom < self.height - 30:
 self.rect.top += self.speed
 else:
 self.rect.bottom = self.height - 30
 
 def moveLeft(self):
 if self.rect.left > 0:
 self.rect.left -= self.speed
 else:
 self.rect.left = 0
 
 def moveRight(self):
 if self.rect.right < self.width:
 self.rect.left += self.speed
 else:
 self.rect.right = self.width
 
 def reset(self):
 self.rect.left,self.rect.top = (self.width - self.rect.width) // 2,self.height - self.rect.height - 30
 self.active = True 
 self.invincible = True