首页 > 编程语言 >Python飞机大战游戏

Python飞机大战游戏

时间:2023-02-12 21:33:48浏览次数:38  
标签:__ 游戏 Python self def update 大战 pygame rect

# 导入模块顺序:官方标准模块、第三方模块、应用程序模块
import random
import pygame


SCREEN_RECT = pygame.Rect(0, 0, 480, 700)
FRAME_PER_SEC = 60
CREATE_ENEMY_EVENT = pygame.USEREVENT
HERO_FIRE_EVENT = pygame.USEREVENT+1

# 定义子类,继承自Sprite类
# 精灵和精灵组
"""
简化开发步骤(图像加载、位置变化、绘制图像)提供的两个类
pygame.sprite.Sprite(存储图像数据和位置)
pygame.sprite.Group
update更新精灵位置
精灵组
update:让组中所有精灵调用update方法
draw:将组中所有精灵的图像各自绘制到不同的位置
"""


class GameSprite(pygame.sprite.Sprite):
    # 如果父类不是object的基类,在初始化方法中必须主动调用父类的初始化方法
    def __init__(self, image_name, speed=1):
        # 调用父类的初始化方法
        super().__init__()
        # 定义对象的属性,图像、位置、速度
        self.image = pygame.image.load(image_name)
        self.rect = self.image.get_rect()
        self.speed = speed

    def update(self):
        self.rect.y += self.speed


class Background(GameSprite):

    def __init__(self, is_alt=False):
        super().__init__("./images/background.png")
        if is_alt:
            self.rect.y = -self.rect.height

    def update(self):
        super().update()
        if self.rect.y > SCREEN_RECT.height:
            self.rect.y = -self.rect.height


class Enemy(GameSprite):

    def __init__(self):
        # 创建精灵
        super().__init__("./images/enemy1.png")
        # 指定初始随机速度和随机位置
        self.speed = random.randint(1, 3)
        self.rect.bottom = 0
        max_x = SCREEN_RECT.width-self.rect.width
        self.rect.x = random.randint(0, max_x)

    def update(self):
        super().update()
        # 敌机飞离屏幕,从精灵组删除
        if self.rect.y >= SCREEN_RECT.height:
            # 将它从所有精灵组移出,精灵被自动销毁,__del__方法自动被调用
            self.kill()

    def __del__(self):
        pass


class Hero(GameSprite):

    def __init__(self):
        super().__init__("./images/me1.png", 0)
        self.rect.bottom = SCREEN_RECT.bottom-120
        self.rect.centerx = SCREEN_RECT.centerx
        self.bullets = pygame.sprite.Group()

    def update(self):
        self.rect.x += self.speed
        if self.rect.x <= 0:
            self.rect.x = 0
        elif self.rect.right > SCREEN_RECT.right:
            self.rect.right = SCREEN_RECT.right

    def fire(self):
        for i in (0, 1, 2):
            bullet = Bullet()
            # 跟随英雄移动
            bullet.rect.bottom = self.rect.y-20*i
            bullet.rect.centerx = self.rect.centerx
            self.bullets.add(bullet)


class Bullet(GameSprite):
    def __init__(self):
        super().__init__("./images/bullet1.png", -2)

    def update(self):
        super().update()
        if self.rect.bottom < 0:
            self.kill()

    def __del__(self):
        pass

# 飞机大战游戏
from sprites import *


class PlaneGame(object):

    def __init__(self):
        # 游戏初始化
        # 1、使用pygame创建图形窗口
        # 创建游戏主窗口
        # 初始化游戏显示窗口 480*700指定屏幕的宽和高(默认屏幕大小),是否是全屏(默认不传递),颜色位数(默认自动匹配),一般只需要使用元组传递第一个参数即可
        # 返回结果就是游戏的主窗口
        self. screen = pygame.display.set_mode(SCREEN_RECT.size)
        # 2、设置游戏时钟,创建时钟对象
        self.clock = pygame.time.Clock()
        # 3、调用私有方法,创建精灵和精灵组
        self.__create_sprites()
        # 4、设置定时器事件,每隔一秒出现一架敌机,每隔0.5秒发射一次子弹,每次连发三枚
        pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)
        pygame.time.set_timer(HERO_FIRE_EVENT, 500)

    def __create_sprites(self):
        # 游戏坐标系:原点左上角,x轴水平向右增加,y轴水平向下增加
        # 分别表示x y width weight,hero_rect也有这4个和size属性
        # 创建背景精灵和精灵组
        bg1 = Background()
        bg2 = Background(is_alt=True)
        self.bg_group = pygame.sprite.Group(bg1, bg2)
        self.enemy_group = pygame.sprite.Group()
        self.hero = Hero()
        self.hero_group = pygame.sprite.Group(self.hero)

    def start_game(self):
        # 游戏开始
        # 导入并初始化所有pygame模块,这样才能使用pygame的其他模块
        pygame.init()
        # 为使游戏启动后不会立即退出增加的一个游戏循环,本质就是一个无限循环,游戏循环意味着游戏正式开始
        # 设置刷新频率60、检测用户交互、更新所有图像位置、更新屏幕显示
        while True:
            # 1、循环中调用tick(帧率)方法
            self.clock.tick(FRAME_PER_SEC)
            # 2、监听事件
            self.__event_handler()
            # 3、碰撞检测
            self.__check_collide()
            # 4、更新/绘制精灵组
            self.__update_sprites()
            # 5、更新屏幕显示
            pygame.display.update()

    def __event_handler(self):
        # 事件监听
        event_list = pygame.event.get()
        for event in event_list:
            if event.type == pygame.QUIT:
                self.__game_over()
            elif event.type == CREATE_ENEMY_EVENT:
                # 到了敌机出场的时间
                enemy1 = Enemy()
                self.enemy_group.add(enemy1)
            # elif event.type == pygame.K_DOWN and event.type == pygame.K_RIGHT:
            # 不能识别连续按键
            elif event.type == HERO_FIRE_EVENT:
                self.hero.fire()
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[pygame.K_RIGHT]:
            self.hero.speed = 2
        elif keys_pressed[pygame.K_LEFT]:
            self.hero.speed = -2
        else:
            self.hero.speed = 0

    def __check_collide(self):
        # 两个精灵组中所有精灵的碰撞检测,dokill为True,发生碰撞的精灵自动移除,collided计算碰撞的回调函数
        # 子弹摧毁敌机
        pygame.sprite.groupcollide(self.hero.bullets,self.enemy_group,True,True)
        # 某个精灵和指定精灵组中的精灵碰撞
        # 敌机撞毁英雄
        enemies = pygame.sprite.spritecollide(self.hero,self.enemy_group,True)
        if len(enemies)>0:
            self.hero.kill()
            PlaneGame.__game_over()

    def __update_sprites(self):
        # 更新/绘制精灵组
        # self.screen.blit(bgImg, (0, 0))   self.screen.blit(hero, hero_rect)
        self.bg_group.update()
        self.bg_group.draw(self.screen)
        self.enemy_group.update()
        self.enemy_group.draw(self.screen)
        self.hero_group.update()
        self.hero_group.draw(self.screen)
        self.hero.bullets.update()
        self.hero.bullets.draw(self.screen)

    @staticmethod
    def __game_over():
        # 游戏结束
        pygame.quit()
        # 直接退出系统,终止当前正在执行的程序
        exit()


if __name__ == '__main__':
    # 创建游戏对象
    game = PlaneGame()
    # 启动游戏
    game.start_game()

综合体现了Python的面向对象编程的特性,并且对于pygame有了一定的理解。

标签:__,游戏,Python,self,def,update,大战,pygame,rect
From: https://www.cnblogs.com/chang-xiaotong/p/17114760.html

相关文章

  • cnblog_fastapi 中的 schemas 和 models 的区别 - python 后端实战经验分享 - Segment
    pythonfastapischema和model的区别ToavoidconfusionbetweentheSQLAlchemymodelsandthePydanticmodels,wewillhavethefilemodel......来自fastapi......
  • Python黑客编程之Bp字典生成插件
    描述编写一款burpsuite插件,用于从浏览的网页中抓取特定文字,生成字典给Intruder使用代码注册插件创建JMenuItem菜单,在target站点中右键触发回调函数wordlist_menuw......
  • python优缺点分析11
    学--就如同你即将看到的一样,Python极其容易上手。前面已经提到了,Python有极其简单的语法。​ 免费、开源--Python是FLOSS(自由/开放源码软件)之一。简单地说,你可以自......
  • 1行Python代码去除图片水印,网友:一干二净!
    大家好,这里是程序员晚枫。最近小明在开淘宝店(店名:爱吃火锅的小明),需要给自己的原创图片加水印,于是我上次给她开发了增加水印的功能:图片加水印,保护原创图片,一行Python代码搞......
  • 大爽Python入门教程 2-7 *拓展实践,对比与思考
    大爽Python入门公开课教案点击查看教程总目录本文偏难。推荐等第一二三四章上完后,回过来拓展阅读。基础情景思考假设有这样一张成绩表最左边的一列是名字,起名麻......
  • 大爽Python入门教程 2-6 拓展练习
    大爽Python入门公开课教案点击查看教程总目录方位输出第一章有一个思考题,方位变换:小明同学站在平原上,面朝北方,向左转51次之后(每次只转90度),小明面朝哪里?小明转过......
  • Python面向对象---类的基本使用
     ✅作者简介:热爱科研的算法开发者,Python、Matlab项目可交流、沟通、学习。 ......
  • Python爬虫之Js逆向案例(16)-京东商品评论&店铺详情案例
    <center>声明:京东商品评论&店铺详情获取分析仅用于研究和学习,如有侵权,可联系删除</center>一次运行程序,同时获取一下内容:1.获取商店详情;2.获取当前商品评论;3.获取商品的......
  • Python 类型注解
    在Python语言发展的过程中,PEP提案发挥了巨大的作用,如PEP3107和PEP484提案,分别给我们带来了函数注解(FunctionAnnotations)和类型提示(TypeHints)的功能。PEP3107:定义了......
  • Python 高级编程之网络编程 Socket(六)
    目录一、概述二、Pythonsocket模块1)Socket类型1、创建TCPSocket2、创建UDPSocket2)Socket函数1、服务端socket函数2、客户端socket函数3、公共socket函数三、单工,半......