Fork me on github

python游戏之飞机大战

简单的飞机大战

如下图所示,这就是我自己做的飞机大战,图片都是自己画的(系统自己的画图工具),声音是在网上下载的。参考了哔哩哔哩一个视频的逻辑,然后自己就想做,于是就做了一个简单的飞机大战,代码也是我自己想出来的,(废了我一上午的时间).

在这里插入图片描述

简单的说一下过程

这是我的结构
在这里插入图片描述

因为是随意做的,所以说所有的代码都放在了一个文件Plane.py
有 1 个纯音乐(网上下载的), 1 个我方飞机的图片, 1张我方飞机的子弹,1张敌方飞机的图片和1张敌方飞机的子弹

实现功能有

  • 用户通过键盘控制我方飞机,按空格发射子弹
  • 敌方飞机在指定的范围内自由移动,随机发射子弹
  • 可以设置敌方飞机发射子弹的速度,
  • 也可以设置我方的子弹速度和移动速度

主要思路

  • 就是设计4个类,对着4个类进行一些逻辑操作
  • 飞机与子弹的关系
    如图:
    在这里插入图片描述
import pygame
import random
import time
from pygame.locals import *

# 设置屏幕大小
WIDTH = 800
HEIGT = 1000

# 飞机的图片和对应的大小
PLANE_IMG = "myplane.png"
PLANE_IMG_WIDTH = 96
PLANE_IMG_HEIGHT = 104

# 初始化 我方飞机的位置
PLANE_X = int((WIDTH - PLANE_IMG_WIDTH) / 2)
PLANE_Y = HEIGT - PLANE_IMG_HEIGHT

# 设置我方飞机子弹的图片,他对应的高度和宽度
MY_PLANE_BULLET = "myBullet.png"
MY_PLANE_BULLET_WIDTH = 19
MY_PLANE_BULLET_HEIGHT = 29

# 敌方飞机的图片和敌方飞机的图片大小
# 这样方便后期修改
ENEMY_PLANE = "enemy.png"
ENEMY_PLANE_WIDTH = 100
ENEMY_PLANE_HEIGHT = 105

# 敌方飞机的子弹和敌方飞机的图片
ENEMY_PLANE_BULLET = "enemyBullet.png"

# 设置我方子弹速度
MY_PLANE_BULLET_SPEED = 5

# 设置敌方子弹速度
ENEMY_PLANE_BULLET_SPEED = 3

# 设置敌方的难度
ENEMY_PLANE_MOVE_SPEED = 5
# 设置我方的移动速度
MY_PLANE_MOVE_SPEED = 40



def showGameInterFace():

    # initialize all imported pygame modules
    # 实例所有的模块
    pygame.init()

    # set_mode(size=(0, 0), flags=0, depth=0, display=0) -> Surface
    # Initialize a window or screen for display
    # 实例化一个窗口或屏幕用于显示,返回一个界面对象
    screen = pygame.display.set_mode(size=(WIDTH, HEIGT))

    # set_caption(title, icontitle=None) -> None
    # Set the current window caption
    # 设置窗口的标题
    pygame.display.set_caption("简单的飞机对战")

    # image = MissingModule("image", urgent=1)
    # 加载图片到屏幕上
    background = pygame.image.load("bac.png")

    # load(filename) -> None
    # load(object) -> None
    # Load a music file for playback
    # 导入一个音乐文件
    pygame.mixer_music.load("1.mp3")

    # set_volume(value) -> None
    # set the music volume
    # 设置音乐音量
    pygame.mixer_music.set_volume(0.2)

    # play(loops=0, start=0.0) -> None
    # Start the playback of the music stream
    pygame.mixer_music.play(-1)

    myPlane = Plane(screen)
    enemyPlane = EnemyPlane(screen)
    while True:

        key_control(myPlane)


        # blit(source, dest, area=None, special_flags=0) -> Rect
        # draw one image onto another
        # 把一幅画到另一幅上
         # dest 参数是用来定位的
        screen.blit(source=background, dest=(0, 0))

        myPlane.display()
        enemyPlane.display()
        enemyPlane.autoMove()
        enemyPlane.sendBullet()


        # update(rectangle=None) -> None
        # update(rectangle_list) -> None
        # Update portions of the screen for software displays
        # 为软件显示更新屏幕的部分
        # 在屏幕上显示
        pygame.display.update()

# 键盘控制
def key_control(myPlane):
    # get(eventtype=None) -> Eventlist
    # get(eventtype=None, pump=True) -> Eventlist
    # get events from the queue
    # 从队列中获取时间, 返回了一个事件列表
    event_list = pygame.event.get()
    for event in event_list:
        # QUIT = 12
        if event.type == QUIT:
            print("退出程序")
            exit()
        # KEYDOWN = 2 表示用户是否按下了键盘的某一个键
        elif event.type == KEYDOWN:
            # 我的想法:ord("a"),这个比较的是ASCII码
            # 一开始 event.key == "a" 是不对的,因为这个event.key比较的是ASCII码
            if event.key == K_a or event.key == K_LEFT:
                print("left")
                myPlane.moveLeft()
                pass
            elif event.key == K_d or event.key == K_RIGHT:
                print("right")
                myPlane.moveRight()
                pass
            elif event.key == K_w or event.key == K_UP:
                print("up")
                myPlane.moveUp()
                pass
            elif event.key == K_s or event.key == K_DOWN:
                print("down")
                myPlane.moveDown()
                pass
            elif event.key == K_SPACE:
                print("space")
                myPlane.sendBullet()

                pass
            pass
        pass
    pass

# 制作我方的飞机
class Plane(object):
    '''
    创建一个飞机对象
    可以实现飞机的左移动和右移动
    还有显示飞机
    '''
    def __init__(self, screen):
        self.screen = screen
        self.x = PLANE_X
        self.y = PLANE_Y
        self.img = pygame.image.load(PLANE_IMG)
        self.bulletList = list()
        pass

    def moveLeft(self):
        if self.x > 0:
            self.x -= MY_PLANE_MOVE_SPEED
        pass

    def moveRight(self):
        if self.x < WIDTH - PLANE_IMG_WIDTH:
            self.x += MY_PLANE_MOVE_SPEED
        pass

    def moveUp(self):
        if self.y > 0:
            self.y -= MY_PLANE_MOVE_SPEED
        pass

    def moveDown(self):
        if self.y < HEIGT - PLANE_IMG_HEIGHT:
            self.y += MY_PLANE_MOVE_SPEED
        pass

    def sendBullet(self):
        img = pygame.image.load(MY_PLANE_BULLET)
        myBullet = Bullet(self.screen, self.x, self.y, img)
        self.bulletList.append(myBullet)
        # print(self.bulletList)
        needDelList = list()
        for item in self.bulletList:
            if item.isYueJie():
                needDelList.append(item)
                pass
        for i in needDelList:
            self.bulletList.remove(i)
            pass


        pass

    def display(self):
        self.screen.blit(source=self.img, dest=(self.x, self.y))
        for bullet in self.bulletList:
            bullet.display()
            bullet.move()
            pass
        pass


    pass

# 制作我方飞机的子弹
class Bullet(object):
    def __init__(self, screen, x, y, img):
        self.screen = screen
        self.x = x
        self.y = y
        self.img = img
        pass

    def move(self):
        self.y -= MY_PLANE_BULLET_SPEED
        pass

    def display(self):
        self.screen.blit(source=self.img, dest=(self.x + int(PLANE_IMG_WIDTH/2), self.y - 20))
        pass

    def isYueJie(self):
        if self.y < 0:
            return True
        else:
            return False
    pass

# 创建敌方飞机
class EnemyPlane(object):
    '''
    创建一个飞机对象
    可以实现飞机的左移动和右移动
    还有显示飞机
    '''
    def __init__(self, screen):
        self.screen = screen
        self.x = 0
        self.y = 0
        self.img = pygame.image.load(ENEMY_PLANE)
        self.bulletList = list()
        self.direction = "right"
        pass

    def autoMove(self):
        if self.direction == "right":
            self.x += ENEMY_PLANE_MOVE_SPEED
        elif self.direction == "left":
            self.x -= ENEMY_PLANE_MOVE_SPEED
        if self.x < 0:
            self.direction = "right"
        elif self.x > WIDTH - ENEMY_PLANE_WIDTH:
            self.direction = "left"

    def sendBullet(self):
        # 设置随机数,循环,让敌方飞机随机发射子弹
        num = random.randint(1, 20)
        if num == 3:
            img = pygame.image.load(ENEMY_PLANE_BULLET)
            enemyBullet = EnemyBullet(self.screen, self.x, self.y, img)
            self.bulletList.append(enemyBullet)
            # print(self.bulletList)
            needDelList = list()
            for item in self.bulletList:
                if item.isYueJie():
                    needDelList.append(item)
                    pass
            for i in needDelList:
                self.bulletList.remove(i)
                pass

        pass


    def display(self):
        self.screen.blit(source=self.img, dest=(self.x, self.y))
        for bullet in self.bulletList:
            bullet.display()
            bullet.move()
            pass
        pass


    pass

# 制作敌方飞机的子弹
class EnemyBullet(object):
    def __init__(self, screen, x, y, img):
        self.screen = screen
        self.x = x
        self.y = y
        self.img = img
        pass

    def move(self):
        self.y += ENEMY_PLANE_BULLET_SPEED
        pass

    def display(self):
        self.screen.blit(source=self.img, dest=(self.x + int(ENEMY_PLANE_WIDTH/2), self.y + ENEMY_PLANE_HEIGHT))
        pass

    def isYueJie(self):
        if self.y > HEIGT:
            return True
        else:
            return False
    pass



if __name__ == '__main__':
    showGameInterFace()


不知道为什么代码显示不出来,如果需要源码,可以联系我

posted @ 2022-01-23 11:59  不想努力的小龙  阅读(227)  评论(0编辑  收藏  举报