3233浏览
查看: 3233|回复: 4

[资源] 基于python的飞机大战

[复制链接]
今天在这里和大家分享一下简易的python飞机大战
如果没有安装树pygame的库,请执行这个代码
[mw_shl_code=python,false]pip install pygame[/mw_shl_code]
如果是使用的树莓派,系统里面可能会包含这个库文件,所以如果这一步没有执行成功,也可以尝试直接运行这个里面的代码。


整个代码:
[mw_shl_code=python,false]#Import pygame
import pygame, sys, time
import random

#Pygame init
pygame.init()
size=width,height=400,500
win=pygame.display.set_mode(size)
pygame.display.set_caption('Pygame 游戏之旅')
clock=pygame.time.Clock()

class hostplayer:
    def __init__(self,x,y,width,height):
        self.xPos=x
        self.yPos=y
        self.widthValue=width
        self.heightValue=height
        self.bulletHoder=[]
        
class bullet:
    def __init__(self,x,y,width,height):
        self.xPos=x
        self.yPos=y
        self.widthValue=width
        self.heightValue=height
        

class enemyplayer:
    def __init__(self,x,y,width,height):
        self.xPos=x
        self.yPos=y
        self.widthValue=width
        self.heightValue=height

        
def drawGame(playerArray,enemyArray):
    pygame.draw.rect(win,(255,255,255),(0,0,width,height))
    for i in playerArray:
        pygame.draw.rect(win,(255,0,0),(i.xPos,i.yPos,i.widthValue,i.heightValue))
        for q in i.bulletHoder:
            pygame.draw.rect(win,(0,255,0),(q.xPos,q.yPos,q.widthValue,q.heightValue))
    for k in enemyArray:
        pygame.draw.rect(win,(0,0,255),(k.xPos,k.yPos,k.widthValue,k.heightValue))
    pygame.display.update()

def bulletjunction(playerArray,enemyArray):
    for player in playerArray:
        m=0
        for bulletTemp in player.bulletHoder:
            for e_player in enemyArray:
                n=0
                if bulletTemp.xPos>=e_player.xPos and bulletTemp.xPos<=e_player.xPos+25 and bulletTemp.yPos>=e_player.yPos and bulletTemp.yPos<=e_player.yPos+25:
                    playerArray[m].bulletHoder.pop(playerArray[m].bulletHoder.index(bulletTemp))
                    enemyArray[n].xPos=random.randint(0,375)
                    enemyArray[n].yPos=0
                    print("you win")
                if bulletTemp.yPos<=0:
                    playerArray[m].bulletHoder.pop(playerArray[m].bulletHoder.index(bulletTemp))
                n+=1
        m+=1

def getStartFunction():
    player1=hostplayer(0,height-25,25,25)
    last_shoot1=0
    playerArray=[player1]
    k=0
    enemyArray=[]
    enemyXpos=random.randint(0,375)
    enemyYpos=0
    enemy=enemyplayer(enemyXpos,enemyYpos,25,25)
    enemyArray.append(enemy)
    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                sys.exit()
               
        keys=pygame.key.get_pressed()
        if(keys[pygame.K_LEFT]):
            playerArray[0].xPos-=5 #参数可调
            if playerArray[0].xPos<=0:
                playerArray[0].xPos=0
        if(keys[pygame.K_RIGHT]):
            playerArray[0].xPos+=5 #参数可调
            if playerArray[0].xPos>=width-25:
                playerArray[0].xPos=width-25
        if (keys[pygame.K_UP]):
            now1=pygame.time.get_ticks()
            if now1-last_shoot1>500:
                playerArray[0].bulletHoder.append(bullet(playerArray[0].xPos+10,playerArray[0].yPos+10,5,5))
                last_shoot1=now1
        for player in playerArray:
            for bulletTemp in player.bulletHoder:
                bulletTemp.yPos-=5 #参数可调
        for e_player in enemyArray:
            e_player.yPos+=2 #参数可调
            if e_player.yPos>height:
                e_player.xPos=random.randint(0,375)
                e_player.yPos=0
                print("you lost")
        bulletjunction(playerArray,enemyArray)
        drawGame(playerArray,enemyArray)
        
getStartFunction()[/mw_shl_code]


下面我们就来读读代码吧

窗口设置和初始化
[mw_shl_code=python,false]#Import pygame
import pygame, sys, time
import random

#Pygame init
pygame.init()
size=width,height=400,500
win=pygame.display.set_mode(size)
pygame.display.set_caption('Pygame 游戏之旅')
clock=pygame.time.Clock()[/mw_shl_code]


生成和控制主机和敌机,并分别对其进行控制
[mw_shl_code=python,false]def getStartFunction():
    player1=hostplayer(0,height-25,25,25)
    last_shoot1=0
    playerArray=[player1]
    k=0
    enemyArray=[]
    enemyXpos=random.randint(0,375)
    enemyYpos=0
    enemy=enemyplayer(enemyXpos,enemyYpos,25,25)
    enemyArray.append(enemy)
    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                sys.exit()
               
        keys=pygame.key.get_pressed()
        if(keys[pygame.K_LEFT]):
            playerArray[0].xPos-=5 #参数可调
            if playerArray[0].xPos<=0:
                playerArray[0].xPos=0
        if(keys[pygame.K_RIGHT]):
            playerArray[0].xPos+=5 #参数可调
            if playerArray[0].xPos>=width-25:
                playerArray[0].xPos=width-25
        if (keys[pygame.K_UP]):
            now1=pygame.time.get_ticks()
            if now1-last_shoot1>500:
                playerArray[0].bulletHoder.append(bullet(playerArray[0].xPos+10,playerArray[0].yPos+10,5,5))
                last_shoot1=now1
        for player in playerArray:
            for bulletTemp in player.bulletHoder:
                bulletTemp.yPos-=5 #参数可调
        for e_player in enemyArray:
            e_player.yPos+=2 #参数可调
            if e_player.yPos>height:
                e_player.xPos=random.randint(0,375)
                e_player.yPos=0
                print("you lost")
        bulletjunction(playerArray,enemyArray)
        drawGame(playerArray,enemyArray)[/mw_shl_code]


通过调节对应位置的参数,可以控制主机的左右运行速度
[mw_shl_code=python,false]        if(keys[pygame.K_LEFT]):
            playerArray[0].xPos-=5 #参数可调
            if playerArray[0].xPos<=0:
                playerArray[0].xPos=0
        if(keys[pygame.K_RIGHT]):
            playerArray[0].xPos+=5 #参数可调
            if playerArray[0].xPos>=width-25:
                playerArray[0].xPos=width-25[/mw_shl_code]


这里的500是对每两个子弹之间的响应时间的控制
[mw_shl_code=python,false]        if (keys[pygame.K_UP]):
            now1=pygame.time.get_ticks()
            if now1-last_shoot1>500:
                playerArray[0].bulletHoder.append(bullet(playerArray[0].xPos+10,playerArray[0].yPos+10,5,5))
                last_shoot1=now1[/mw_shl_code]


通过调节这里的变量可以控制子弹的上行速度
[mw_shl_code=python,false]        for player in playerArray:
            for bulletTemp in player.bulletHoder:
                bulletTemp.yPos-=5 #参数可调[/mw_shl_code]


通过控制这里的变量可以控制敌机的下行速度
[mw_shl_code=python,false]        for e_player in enemyArray:
            e_player.yPos+=2 #参数可调[mw_shl_code=python,false]def bulletjunction(playerArray,enemyArray):
    for player in playerArray:
        m=0
        for bulletTemp in player.bulletHoder:
            for e_player in enemyArray:
                n=0
                if bulletTemp.xPos>=e_player.xPos and bulletTemp.xPos<=e_player.xPos+25 and bulletTemp.yPos>=e_player.yPos and bulletTemp.yPos<=e_player.yPos+25:
                    playerArray[m].bulletHoder.pop(playerArray[m].bulletHoder.index(bulletTemp))
                    enemyArray[n].xPos=random.randint(0,375)
                    enemyArray[n].yPos=0
                    print("you win")
                if bulletTemp.yPos<=0:
                    playerArray[m].bulletHoder.pop(playerArray[m].bulletHoder.index(bulletTemp))
                n+=1
        m+=1[/mw_shl_code]
            if e_player.yPos>height:
                e_player.xPos=random.randint(0,375)
                e_player.yPos=0
                print("you lost")[/mw_shl_code]


将子弹和敌机进行相应的判断和对溢出的子弹进行清除
[mw_shl_code=python,false]def bulletjunction(playerArray,enemyArray):
    for player in playerArray:
        m=0
        for bulletTemp in player.bulletHoder:
            for e_player in enemyArray:
                n=0
                if bulletTemp.xPos>=e_player.xPos and bulletTemp.xPos<=e_player.xPos+25 and bulletTemp.yPos>=e_player.yPos and bulletTemp.yPos<=e_player.yPos+25:
                    playerArray[m].bulletHoder.pop(playerArray[m].bulletHoder.index(bulletTemp))
                    enemyArray[n].xPos=random.randint(0,375)
                    enemyArray[n].yPos=0
                    print("you win")
                if bulletTemp.yPos<=0:
                    playerArray[m].bulletHoder.pop(playerArray[m].bulletHoder.index(bulletTemp))
                n+=1
        m+=1[/mw_shl_code]


画出相应的主机敌机以及子弹
[mw_shl_code=python,false]def drawGame(playerArray,enemyArray):
    pygame.draw.rect(win,(255,255,255),(0,0,width,height))
    for i in playerArray:
        pygame.draw.rect(win,(255,0,0),(i.xPos,i.yPos,i.widthValue,i.heightValue))
        for q in i.bulletHoder:
            pygame.draw.rect(win,(0,255,0),(q.xPos,q.yPos,q.widthValue,q.heightValue))
    for k in enemyArray:
        pygame.draw.rect(win,(0,0,255),(k.xPos,k.yPos,k.widthValue,k.heightValue))
    pygame.display.update()[/mw_shl_code]


运行结果(视频):
基于python的飞机大战图1








hnyzcj  版主

发表于 2020-11-3 06:25:08

下面我们来读读代码哈哈哈
回复

使用道具 举报

DFSH_Cranberry  中级技师
 楼主|

发表于 2020-11-3 10:04:48

hnyzcj 发表于 2020-11-3 06:25
下面我们来读读代码哈哈哈

没毛病呀,有什么梗吗
回复

使用道具 举报

lkk255  中级技师

发表于 2020-11-4 17:28:13

这是ssh到raspberryPi上运行的?
回复

使用道具 举报

DFSH_Cranberry  中级技师
 楼主|

发表于 2020-11-5 14:21:05

lkk255 发表于 2020-11-4 17:28
这是ssh到raspberryPi上运行的?

是的,通过Mobaxterm。这个是操作步骤。https://mc.dfrobot.com.cn/thread-307477-1-1.html#pid473066
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail