利用python的pygame的模块来实现简单的飞机大战游戏

游戏运行效果图初次接触pygame模块,目前只能实现一些简单的游戏功能,如有错误跪请各位大神及时指出.

首先我们要先明白大部分2d游戏的原理, 游戏一般是通过玩家的操作不断的更换图片的过程.

那么 我们只需要 提前准备好游戏要使用到的图片 在适当的位置导入,并通过相应的判断进行更换图片即可实现简单游戏.

下面是我的源码

import pygame
from pygame.locals import *
import time
import random


class All(object):
    def __init__(self, x, y, screen_1, image):
        # 接收已创建好的窗口
        self.screen_1 = screen_1
        # 定义飞子弹初始坐标
        self.x = x
        self.y = y
        # 加载照片
        self.image = pygame.image.load(image)


class AllPlane(All): # 继承 All类
    def __init__(self, x, y, screen_1, image):
        # 在创建Allplane的对象时 直接调用父类的属性
        super().__init__(x, y, screen_1, image)
        # 创建空列表用于存放子弹对象的引用
        self.bullets = []

    def image_yidong(self):
        # 更改飞机图片的坐标
        self.screen_1.blit(self.image, (self.x, self.y))
        # 遍历存储子弹对象的列表
        for bullet in self.bullets:
            # 调用子弹的显示坐标方法
            bullet.display()
            # 调用子弹的更改坐标方法
            bullet.move()
            # 判断子弹是否越界,如果越界直接删除
            if bullet.yuejie():
                self.bullets.remove(bullet)


class Plane(AllPlane):
    def __init__(self, screen_1):
        # 在创建Plane的对象时直接调用父类的初始化方法
        super().__init__(130, 500, screen_1, "feiji\\hero1.png")

    def yidong_left(self):  # 控制飞机移动的像素点

        self.x -= 20

    def yidong_right(self):
        self.x += 20

    def yidong_up(self):
        self.y += 20

    def yidong_down(self):
        self.y -= 20

    def fire(self):
        # 将创建的Bullet子弹对象存储在属性列表里
        self.bullets.append(Bullet(self.screen_1, self.x, self.y))


class Dark(AllPlane):
    def __init__(self, screen_1):
        # 在创建Dark对象时直接调用父类的初始化属性
        super().__init__(0, 0, screen_1, "feiji\\enemy0.png")
        # 用来判断敌人飞机的移动方向
        self.fangxiang = "right"

    def yidong(self):
        # 如果飞机越界更改fangxiang属性
        if self.fangxiang == "right":
            self.x += 5
        else:
            self.x -= 5

        if self.x > 300:
            self.fangxiang = "left"
        elif self.x < 0:
            self.fangxiang = "right"

    def fire(self):
        a = random.randint(1, 100)
        if a == 25 or a == 75:  # 通过随机数 降低子弹发射频率
            # 将创建的子弹对象存储在属性列表里
            self.bullets.append(DarkBullet(self.screen_1, self.x, self.y))


class AllBullet(All):
    def display(self):
        # 确定子弹显示的坐标
        self.screen_1.blit(self.image, (self.x, self.y))


class Bullet(AllBullet):
    def __init__(self, screen_1, x, y):
        super().__init__(x + 40, y - 20, screen_1, "feiji\\bullet.png")

    def yuejie(self):
        # 判断是否越界
        if self.y < 0:
            return True
        else:
            return False

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


class DarkBullet(AllBullet):  # 创建敌人子弹类
    def __init__(self, screen_1, x, y):
        super().__init__(x + 15, y + 30, screen_1, "feiji\\bullet1.png")

    def yuejie(self):
        if self.y > 600:
            return True
        else:
            return False

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


def shijian(hero):
    for event in pygame.event.get():

        # 判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
        # 判断是否是按下了键
        elif event.type == KEYDOWN:
            # 检测按键是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                hero.yidong_left()
            # 检测按键是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                hero.yidong_right()
            elif event.key == K_w or event.key == K_UP:
                print('up')
                hero.yidong_down()
            elif event.key == K_s or event.key == K_DOWN:
                print('down')
                hero.yidong_up()

            # 检测按键是否是空格键
            elif event.key == K_SPACE:
                # 每次按空格键创建一个子弹的对象 将其存储在飞机的属性列表里
                print('space')
                hero.fire()


def main():
    # pygame.init
    # 创建窗口
    screen = pygame.display.set_mode((350, 600), 0, 32)
    #  创建标题
    pygame.display.set_caption("飞机大战--凡")
    #  创建一个背景图片
    background = pygame.image.load("./feiji/background.png")

    #  创建一个飞机对象
    hero = Plane(screen)
    dark1 = Dark(screen)

    while True:
        # 将背景图片贴到 0,0
        screen.blit(background, (0, 0))
        # 调用hero的方法 实现飞机的移动
        hero.image_yidong()
        dark1.image_yidong()
        dark1.yidong()
        dark1.fire()

        pygame.display.update()
        # 获取事件,根据用户按键判断x,y的值
        shijian(hero)

        # 获取事件,比如按键等



        time.sleep(0.01)


if __name__ == "__main__":
    main()
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值