• 技术文章 >Python技术 >Python基础教程

    demo:飞机大战游戏 python小项目

    小妮浅浅小妮浅浅2020-12-03 17:44:06原创2913

    记得刚学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')
            self.screen = screen_temp
            self.bullet_list = []
            if 0<=self.x<=540:
                if self.ra == 0:
                    self.flag = "left"
                elif self.ra == 1:
                    self.flag = "right"
                else:
                    self.flag = 'middle'
        def display(self):
            self.screen.blit(self.image, (self.x, self.y))
        def move(self):
            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
        def fire(self):
            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()
                bullet.move()
                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.screen = screen_temp
        def display(self):
            self.screen.blit(self.image,(self.x, self.y))
        def move(self):
            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()


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

    四、完整版代码及数据 下载地址(下载就能用)

    专题推荐:python游戏
    上一篇:python-字符串替换 下一篇:python中difference的原理

    相关文章推荐

    • python游戏编程讲解之凯撒密码• 如何使用python游戏开发模块—pygame?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网