基于python的飞机大战
今天在这里和大家分享一下简易的python飞机大战如果没有安装树pygame的库,请执行这个代码
pip install pygame
如果是使用的树莓派,系统里面可能会包含这个库文件,所以如果这一步没有执行成功,也可以尝试直接运行这个里面的代码。
整个代码:
#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.bulletHoder.pop(playerArray.bulletHoder.index(bulletTemp))
enemyArray.xPos=random.randint(0,375)
enemyArray.yPos=0
print("you win")
if bulletTemp.yPos<=0:
playerArray.bulletHoder.pop(playerArray.bulletHoder.index(bulletTemp))
n+=1
m+=1
def getStartFunction():
player1=hostplayer(0,height-25,25,25)
last_shoot1=0
playerArray=
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):
playerArray.xPos-=5 #参数可调
if playerArray.xPos<=0:
playerArray.xPos=0
if(keys):
playerArray.xPos+=5 #参数可调
if playerArray.xPos>=width-25:
playerArray.xPos=width-25
if (keys):
now1=pygame.time.get_ticks()
if now1-last_shoot1>500:
playerArray.bulletHoder.append(bullet(playerArray.xPos+10,playerArray.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()
下面我们就来读读代码吧
窗口设置和初始化
#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()
生成和控制主机和敌机,并分别对其进行控制
def getStartFunction():
player1=hostplayer(0,height-25,25,25)
last_shoot1=0
playerArray=
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):
playerArray.xPos-=5 #参数可调
if playerArray.xPos<=0:
playerArray.xPos=0
if(keys):
playerArray.xPos+=5 #参数可调
if playerArray.xPos>=width-25:
playerArray.xPos=width-25
if (keys):
now1=pygame.time.get_ticks()
if now1-last_shoot1>500:
playerArray.bulletHoder.append(bullet(playerArray.xPos+10,playerArray.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)
通过调节对应位置的参数,可以控制主机的左右运行速度
if(keys):
playerArray.xPos-=5 #参数可调
if playerArray.xPos<=0:
playerArray.xPos=0
if(keys):
playerArray.xPos+=5 #参数可调
if playerArray.xPos>=width-25:
playerArray.xPos=width-25
这里的500是对每两个子弹之间的响应时间的控制
if (keys):
now1=pygame.time.get_ticks()
if now1-last_shoot1>500:
playerArray.bulletHoder.append(bullet(playerArray.xPos+10,playerArray.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 #参数可调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.bulletHoder.pop(playerArray.bulletHoder.index(bulletTemp))
enemyArray.xPos=random.randint(0,375)
enemyArray.yPos=0
print("you win")
if bulletTemp.yPos<=0:
playerArray.bulletHoder.pop(playerArray.bulletHoder.index(bulletTemp))
n+=1
m+=1
if e_player.yPos>height:
e_player.xPos=random.randint(0,375)
e_player.yPos=0
print("you lost")
将子弹和敌机进行相应的判断和对溢出的子弹进行清除
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.bulletHoder.pop(playerArray.bulletHoder.index(bulletTemp))
enemyArray.xPos=random.randint(0,375)
enemyArray.yPos=0
print("you win")
if bulletTemp.yPos<=0:
playerArray.bulletHoder.pop(playerArray.bulletHoder.index(bulletTemp))
n+=1
m+=1
画出相应的主机敌机以及子弹
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()
运行结果(视频):
下面我们来读读代码哈哈哈 hnyzcj 发表于 2020-11-3 06:25
下面我们来读读代码哈哈哈
没毛病呀,有什么梗吗 这是ssh到raspberryPi上运行的? lkk255 发表于 2020-11-4 17:28
这是ssh到raspberryPi上运行的?
是的,通过Mobaxterm。这个是操作步骤。https://mc.dfrobot.com.cn/thread-307477-1-1.html#pid473066
页:
[1]