欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Pygame实现小游戏飞机大战,不用精灵版

程序员文章站 2022-09-21 09:32:20
之前写过C语言版的,这次用Python写了个,不过原理还是用的C里GDI那套循环,把Python的类当C里的结构体用,因为Pygame的精灵研究了下还是没弄明白不过写完后,和找的几篇别人用精灵写的参考对比,感觉还是他们的简单,尴尬代码:import timeimport randomimport osimport sysimport pygamefrom pygame.locals import *#↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓全局变量声明if 65535: ....

之前写过C语言版的,这次用Python写了个,不过原理还是用的C里GDI那套循环,把Python的类当C里的结构体用,因为Pygame的精灵研究了下还是没弄明白

不过写完后,和找的几篇别人用精灵写的参考对比,感觉还是他们的简单,尴尬

代码:

import time
import random
import os
import sys

import pygame
from pygame.locals import *

#↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓全局变量声明
if 65535:
    os.environ['SDL_VIDEO_WINDOW_POS']="850,50"
    #设置窗口初始位置
    BG_IMAGE = r'D:\VS2019_Object\鼠标打飞机\img\BackGround.png'
    MOUSE_IMAGE = r'D:\VS2019_Object\鼠标打飞机\img\Plane.png'
    ENEMY_IMAGE = r'D:\VS2019_Object\鼠标打飞机\img\enemy.png'
    BULLET_IMAGE = r'D:\VS2019_Object\鼠标打飞机\img\bullet.png'
    TITLE_IMAGE = r'D:\VS2019_Object\鼠标打飞机\img\title.png'
    EXIT_IMAGE = r'D:\VS2019_Object\鼠标打飞机\img\title_exit.png'
    EXIT_B_IMAGE = r'D:\VS2019_Object\鼠标打飞机\img\title_exit_B.png'
    #位图句柄
    pygame.init()
    #初始化pygame
    pygame.display.set_caption('鼠标飞机')
    #设置标题栏
    screen = pygame.display.set_mode((480, 700),DOUBLEBUF,32)
    #窗口大小
    #0表示不使用特性,可用FULLSCREEN,RESIZEBLE,NOFRAME,DOUBLEBUF(双缓冲,使用时需用pygame.display.flip()来刷新屏幕)等替换
    #32=色深
    bg = pygame.image.load(BG_IMAGE).convert()
    #载入背景图
    mouse_cursor = pygame.image.load(MOUSE_IMAGE).convert_alpha()
    #载入鼠标贴图
    bullet_pic = pygame.image.load(BULLET_IMAGE).convert_alpha()
    #载入炮弹图
    boom_pic = pygame.image.load(ENEMY_IMAGE).convert_alpha()
    #载入敌机图
    title_pic = pygame.image.load(TITLE_IMAGE).convert_alpha()
    #载入标题图
    exit_pic = pygame.image.load(EXIT_IMAGE).convert_alpha()
    #载入退出按钮
    exit_B_pic = pygame.image.load(EXIT_B_IMAGE).convert_alpha()
    #载入选择状态退出按钮
    myfont=pygame.font.Font(r'D:\VS2019_Object\PyGame\Font\张海山*体.ttf',30)
    #字体定义
    FPS=60
    #画面刷新速率
    BackGroundMove=0
    BG_v=1
    #背景滚动速度
    time_Pre=time.time()
    time_Now=time.time()
    #画面刷新间隔时间标记
    time_boom_Pre=time.time()
    time_boom_Now=time.time()
    #敌机生成间隔时间标记
    y_line = 600
    #飞机到达此高度结束开场动画
    y_print = 700
    #飞机出生高度
    title_born=120
    #标题创建高度
    heath=3
    #生命值
    point=0
    #分数

    # 使用类创建炮弹结构体
    class Bullet(object):
        class Struct(object):
            def __init__(self, x, y, L):
                self.x = x  #炮弹x坐标
                self.y = y  #炮弹y坐标
                self.L = L  #炮弹是否存活(创建置1,超出屏幕或碰撞销毁则置0)

        def make_struct(self, x, y, L):
            self.x = x
            self.y = y
            self.L = L
            return self.Struct(x, y, L)

    bullet = Bullet()
    boom=Bullet()

    #初始化炮弹类数组
    bullet_N=[Bullet() for i in range(1,32)]
    for i in range(1,31):
        bullet_N[i].x=0
        bullet_N[i].y=0
        bullet_N[i].L=0

    bullet_v=15
    #炮弹飞行速度

    #初始化敌机类数组
    boom_N=[Bullet() for i in range(1,32)]
    for i in range(1,31):
        boom_N[i].x=0
        boom_N[i].y=0
        boom_N[i].L=0

    boom_vx=0
    boom_vy=10
    #敌机水平和垂直方向速度

    fire=False
    #开火状态
    你玩游戏像蔡徐坤=False
    #游戏结束状态标志
    mouse_exit=False
    #鼠标是否在退出按钮悬停
    pygame.mouse.set_visible(False)
    #隐藏系统鼠标图标
    time_Pre=time.time()
    #初始化时间标记

#↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓开场动画
while 1024:
    if y_line>=y_print:#当飞机达到结束高度时跳出循环动画
        break
    screen.blit(bg, (0, 0))
    screen.blit(mouse_cursor, (150, y_print))
    #背景和飞机贴图
    y_print-= 1
    #飞机移动量
    screen.blit(title_pic, (0, title_born))
    #标题贴图
    title_born-=2
    #标题移动速度
    pygame.display.update()
    #刷新画布
    time.sleep(0.01)#_开场动画

time_Now=time.time()
time_boom_Pre=time.time()
#初始化时间标记

#↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓主循环
while 3.14:
    x, y = pygame.mouse.get_pos()
    #获取鼠标坐标
    for event in pygame.event.get():
            if event.type == QUIT:#退出消息
                pygame.quit()
                sys.exit()#调用退出函数
            if event.type ==MOUSEBUTTONDOWN:#鼠标按键消息
                pressed_array = pygame.mouse.get_pressed()
                for index in range(len(pressed_array)):#判断键值
                    if pressed_array[index]:
                        if index == 0:
                            if 你玩游戏像蔡徐坤==False:#判断是否游戏结束状态
                                fire=True#_消息循环
                                print("鼠标左键点击,飞机开火")
                            if mouse_exit==True:#判断是否满足鼠标点击退出
                                pygame.quit()#调用退出函数
                                sys.exit()
                                
    time_Now=time.time()#时间标记更新

    if time_Now-time_Pre>(1/FPS):
        screen.blit(bg, (0,BackGroundMove))
        screen.blit(bg, (0,BackGroundMove-700))
        #上下两张贴图
        BackGroundMove+=BG_v
        if BackGroundMove>=700:
            BackGroundMove=0
        #背景贴图滚动,超出界面则重置位置

        time_boom_Now=time.time()
        #时间标记更新
        if 你玩游戏像蔡徐坤==False:
            if time_boom_Now-time_boom_Pre>1:
                for i in range(1,31):#遍历敌机数组
                    if boom_N[i].L==0:
                        boom_N[i].x=random.randint(0,480)
                        boom_N[i].y=random.randint(0,50)
                        boom_N[i].L=1
                        print("生成敌机")
                        #print(i)
                        #print(boom_N[i].x,boom_N[i].y,boom_N[i].L)
                        time_boom_Pre=time.time()
                        break

        for i in range(1,31):
                if boom_N[i].L!=0:
                    boom_N[i].x+=random.randint(-2,2)
                    boom_N[i].y+=random.randint(1,5)
                    #随机更新敌机位置
                    if boom_N[i].x<=0:
                        boom_N[i].x=0
                    if boom_N[i].x>=450:
                        boom_N[i].x=450
                    #判断水平方向是否撞墙
                    screen.blit(boom_pic, (boom_N[i].x, boom_N[i].y))
                    #print(i)
                    #敌机贴图
                    if boom_N[i].y>=700:
                        print("销毁敌机")
                        boom_N[i].L=0
                    #当敌机超出屏幕,存活状态置0
        

        if fire==True:
            for i in range(1,31):
                if bullet_N[i].L==0:
                    bullet_N[i].x=x
                    bullet_N[i].y=y-50
                    bullet_N[i].L=1
                    #print(i)
                    #print(bullet_N[i].x,bullet_N[i].y,bullet_N[i].L)
                    break

        fire=False
                
        for i in range(1,31):#对存活状态的炮弹Y坐标移动并在移动后的位置贴图
            if bullet_N[i].L!=0:
                bullet_N[i].y-=bullet_v
                screen.blit(bullet_pic, (bullet_N[i].x-3, bullet_N[i].y))
                #炮弹贴图
                if bullet_N[i].y<=0:#如果炮弹超出屏幕范围则置0销毁炮弹
                    print("销毁炮弹")
                    bullet_N[i].L=0

        textImage=myfont.render("生命值:"+str(heath),True,(0,0,0))
        screen.blit(textImage,(10,10))
        textImage=myfont.render("分数:"+str(point),True,(0,0,0))
        screen.blit(textImage,(350,10))
        #打印输出生命值和分数

        x -= mouse_cursor.get_width() / 2
        y -= mouse_cursor.get_height() / 2
        #设置鼠标贴图位置以鼠标坐标为中心
    
        if 你玩游戏像蔡徐坤==False:
            screen.blit(mouse_cursor, (x, y))
            #在新坐标绘制鼠标贴图
        else:
            pygame.mouse.set_visible(True)
            print("游戏结束")
            #显示系统鼠标图标

        for i in range(1,31):#遍历炮弹
            if bullet_N[i].L!=0:#判断炮弹是否存活
                for x in range(1,31):#遍历敌机
                    if boom_N[x].L!=0:#判断敌机是否存活
                        if boom_N[x].x<=bullet_N[i].x<=boom_N[x].x+52:#判断炮弹与敌机在x方向上是否重合
                            if boom_N[x].y<=bullet_N[i].y<=boom_N[x].y+43:#判断炮弹与敌机在y方向上是否重合
                                boom_N[x].L=0#碰撞后销毁敌机
                                print("炮弹与敌机碰撞,销毁敌机和炮弹,得分+100")
                                point+=100#得分+100
                                bullet_N[i].L=0

        for i in range(1,31):#遍历敌机
            if boom_N[i].L!=0:#判断敌机是否存活
                if x-40<=boom_N[i].x<=x+57:#判断本体与敌机在x方向上是否重合
                    if y-43<=boom_N[i].y<=y+99:#判断本体与敌机在x方向上是否重合
                        boom_N[i].L=0#碰撞后销毁敌机
                        print("发生碰撞,销毁敌机,生命-1")
                        heath-=1#生命值-1
                        if heath<=0:
                            print("蔡")
                            你玩游戏像蔡徐坤=True

        if 你玩游戏像蔡徐坤==True:
            if 100<=x<=400 and 480<=y<=520:#鼠标悬停
                screen.blit(exit_B_pic, (0, 500))
                mouse_exit=True
            else:
                screen.blit(exit_pic, (0, 500))
                mouse_exit=False

        pygame.display.update()
        #刷新画布

        time_Pre=time.time()#_按设定帧率更新画面

提取码: bvyr

本文地址:https://blog.csdn.net/qq_36917144/article/details/108242237