今天跟视频学习了Python实现坦克大作战小游戏,挺有意思的,一起来玩吧~
按空格发射子弹,上下左右键实现移动,ESC键无限复活。
import pygame,time,random
from pygame.sprite import Sprite
SCREEN_WIDTH=800
SCREEN_HEIGHT=500
BG_color=pygame.Color(45,45,48)
TEXT_COLOR=pygame.Color(0,255,0)
class BaseItem(Sprite):
def __init__(self,color,width,height):
pygame.sprite.Sprite.__init__(self)
class MainGame():
window=None
my_tank=None
enemyTankList=[]
enemyTankCount=5
myBulletList=[]
enemyBulletList=[]
explodeList=[]
wallList=[]
def __init__(self):
pass
#开始游戏
def startGame(self):
pygame.display.init()
MainGame.window=pygame.display.set_mode([SCREEN_WIDTH,SCREEN_HEIGHT])
#MainGame.my_tank=Tank(350,250)
self.createMytank()
self.createEnemyTank()
self.createWall()
pygame.display.set_caption('坦克大作战')
while True:
time.sleep(0.02)
MainGame.window.fill(BG_color)
self.getEvent()
MainGame.window.blit(self.getTextSuface('敌方坦克数量%d'%len(MainGame.enemyTankList)),(10,10))
if MainGame.my_tank and MainGame.my_tank.live:
MainGame.my_tank.displayTank()
else:
del MainGame.my_tank
MainGame.my_tank=None
self.blitEnemyTank()
self.blitMyBullet()
self.blitenemyBullet()
self.blitExplode()
self.blitWall()
if MainGame.my_tank and MainGame.my_tank.live:
if not MainGame.my_tank.stop:
MainGame.my_tank.move()
MainGame.my_tank.hitWall()
MainGame.my_tank.myTank_hit_enemyTank()
pygame.display.update()
def blitWall(self):
for wall in MainGame.wallList:
if wall.live:
wall.displayWall()
else:
MainGame.wallList.remove(wall)
def createWall(self):
for i in range(5):
wall=Wall(i*170,220)
MainGame.wallList.append(wall)
def createMytank(self):
MainGame.my_tank = MyTank(400, 250)
def createEnemyTank(self):
top=100
for i in range(MainGame.enemyTankCount):
left=random.randint(0,600)
speed=random.randint(1,4)
enemy=EnemyTank(left,top,speed)
MainGame.enemyTankList.append(enemy)
def blitExplode(self):
for explode in MainGame.explodeList:
if explode.live:
explode.displayExplode()
else:
MainGame.explodeList.remove(explode)
def blitEnemyTank(self):
for enemyTank in MainGame.enemyTankList:
if enemyTank.live:
enemyTank.displayTank()
enemyTank.randMove()
enemyTank.hitWall()
if MainGame.my_tank and MainGame.my_tank.live:
enemyTank.enemyTank_hit_myTank()
enemyBullet = enemyTank.shot()
if enemyBullet:
MainGame.enemyBulletList.append(enemyBullet)
else:
MainGame.enemyTankList.remove(enemyTank)
def blitMyBullet(self):
for myBullet in MainGame.myBulletList:
if myBullet.live:
myBullet.displayBullet()
myBullet.move()
myBullet.myBullet_hit_enemyTank()
myBullet.hitWall()
else:
MainGame.myBulletList.remove(myBullet)
def blitenemyBullet(self):
for enemyBullet in MainGame.enemyBulletList:
if enemyBullet.live:
enemyBullet.displayBullet()
enemyBullet.move()
enemyBullet.enemyBullet_hit_myTank()
enemyBullet.hitWall()
else:
MainGame.enemyBulletList.remove(enemyBullet)
def endGame(self):
print('谢谢您的使用!')
pygame.quit()
#左上角文字绘制
def getTextSuface(self,text):
pygame.font.init()
font=pygame.font.SysFont('kaiti',18)
textSurface=font.render(text,True,TEXT_COLOR)
return textSurface
#获取事件
def getEvent(self):
eventList=pygame.event.get()
for event in eventList:
if event.type==pygame.QUIT:
#退出
self.endGame()
if event.type==pygame.KEYDOWN:
if not MainGame.my_tank:
if event.key==pygame.K_ESCAPE:
self.createMytank()
if MainGame.my_tank and MainGame.my_tank.live:
if event.key == pygame.K_LEFT:
MainGame.my_tank.direction = 'L'
MainGame.my_tank.stop = False
# MainGame.my_tank.move()
print('向左移')
elif event.key == pygame.K_RIGHT:
MainGame.my_tank.direction = 'R'
MainGame.my_tank.stop = False
# MainGame.my_tank.move()
print('向右移')
elif event.key == pygame.K_UP:
MainGame.my_tank.direction = 'U'
MainGame.my_tank.stop = False
# MainGame.my_tank.move()
print('向上移')
elif event.key == pygame.K_DOWN:
MainGame.my_tank.direction = 'D'
MainGame.my_tank.stop = False
# MainGame.my_tank.move()
print('向下移')
elif event.key == pygame.K_SPACE:
print('发射子弹')
if len(MainGame.myBulletList) <= 3:
myBullet = Bullet(MainGame.my_tank)
MainGame.myBulletList.append(myBullet)
#松开方向键停止
if event.type==pygame.KEYUP:
if event.key==pygame.K_UP or event.key==pygame.K_DOWN or event.key==pygame.K_RIGHT or event.key==pygame.K_LEFT:
if MainGame.my_tank and MainGame.my_tank.live:
MainGame.my_tank.stop=True
class Tank(BaseItem):
def __init__(self,left,top):
self.images={
'U':pygame.image.load('TANK/img/p1tankU.gif'),
'D': pygame.image.load('TANK/img/p1tankD.gif'),
'L': pygame.image.load('TANK/img/p1tankL.gif'),
'R': pygame.image.load('TANK/img/p1tankR.gif'),
}
self.direction='U'
self.image=self.images[self.direction]
self.rect=self.image.get_rect()
self.rect.left=left
self.rect.top=top
self.speed=5
self.stop=True
self.live=True
self.oldLeft=self.rect.left
self.oldTop = self.rect.top
def move(self):
self.oldLeft= self.rect.left
self.oldTop = self.rect.top
if self.direction=='L':
if self.rect.left >0:
self.rect.left -= self.speed
elif self.direction=='U':
if self.rect.top > 0:
self.rect.top -= self.speed
elif self.direction=='D':
if self.rect.top+self.rect.height < SCREEN_HEIGHT:
self.rect.top +=self.speed
elif self.direction == 'R':
if self.rect.left+self.rect.height < SCREEN_WIDTH:
self.rect.left += self.speed
def shot(self):
return Buttle(self)
def stay(self):
self.rect.left=self.oldLeft
self.rect.top=self.oldTop
def hitWall(self):
for wall in MainGame.wallList:
if pygame.sprite.collide_rect(self,wall):
self.stay()
def displayTank(self):
self.image=self.images[self.direction]
MainGame.window.blit(self.image,self.rect)
class MyTank(Tank):
def __init__(self,left,top):
super(MyTank,self).__init__(left,top)
def myTank_hit_enemyTank(self):
for enemyTank in MainGame.enemyTankList:
if pygame.sprite.collide_rect(self,enemyTank):
self.stay()
class EnemyTank(Tank):
def __init__(self,left,top,speed):
super(EnemyTank,self).__init__(left,top)
self.images={
'U':pygame.image.load('TANK/img/enemy1U.gif'),
'D':pygame.image.load('TANK/img/enemy1D.gif'),
'L':pygame.image.load('TANK/img/enemy1L.gif'),
'R':pygame.image.load('TANK/img/enemy1R.gif')
}
#敌方坦克,方向随机
self.direction=self.randDirection()
self.image=self.images[self.direction]
self.rect=self.image.get_rect()
self.rect.left=left
self.rect.top=top
self.speed=speed
self.flag=True
self.step=20
def enemyTank_hit_myTank(self):
if pygame.sprite.collide_rect(self,MainGame.my_tank):
self.stay()
def randDirection(self):
num=random.randint(1,4)
if num == 1:
return 'U'
elif num ==2:
return 'D'
elif num ==3:
return 'L'
elif num ==4:
return 'R'
#敌方坦克随机移动
def randMove(self):
if self.step <=0:
self.direction=self.randDirection()
self.step=60
else:
self.move()
self.step -=1
def shot(self):
num=random.randint(1,100)
if num < 5:
return Bullet(self)
class Bullet(BaseItem):
def __init__(self,tank):
self.image=pygame.image.load('TANK/img/enemymissile.gif')
self.direction=tank.direction
self.rect=self.image.get_rect()
if self.direction == 'U':
self.rect.left=tank.rect.left+tank.rect.width/2-self.rect.width/2
self.rect.top=tank.rect.top-self.rect.height
elif self.direction == 'D':
self.rect.left = tank.rect.left + tank.rect.width / 2 - self.rect.width / 2
self.rect.top = tank.rect.top + tank.rect.height
elif self.direction == 'L':
self.rect.left = tank.rect.left - tank.rect.width / 2 - self.rect.width / 2
self.rect.top = tank.rect.top + tank.rect.width/2 -self.rect.width/2
elif self.direction == 'R':
self.rect.left = tank.rect.left + tank.rect.width
self.rect.top = tank.rect.top + self.rect.width/2-self.rect.width/2
self.speed=6
self.live=True
def move(self):
if self.direction=='U':
if self.rect.top >0:
self.rect.top-=self.speed
else:
self.live=False
elif self.direction=='R':
if self.rect.left +self.rect.width <SCREEN_WIDTH:
self.rect.left +=self.speed
else:
self.live=False
elif self.direction =='L':
if self.rect.left >0:
self.rect.left -=self.speed
else:
self.live=False
elif self.direction=='D':
if self.rect.top +self.rect.height <SCREEN_HEIGHT:
self.rect.top +=self.speed
else:
self.live=False
def hitWall(self):
for wall in MainGame.wallList:
if pygame.sprite.collide_rect(self,wall):
self.live=False
wall.hp-=1
if wall.hp<=0:
wall.live=False
def displayBullet(self):
MainGame.window.blit(self.image,self.rect)
def myBullet_hit_enemyTank(self):
for enemyTank in MainGame.enemyTankList:
if pygame.sprite.collide_rect(enemyTank,self):
enemyTank.live=False
self.live=False
explode=Explode(enemyTank)
MainGame.explodeList.append(explode)
def enemyBullet_hit_myTank(self):
if MainGame.my_tank and MainGame.my_tank.live:
if pygame.sprite.collide_rect(MainGame.my_tank, self):
explode = Explode(MainGame.my_tank)
MainGame.explodeList.append(explode)
self.live = False
MainGame.my_tank.live = False
class Wall():
def __init__(self,left,top):
self.image=pygame.image.load('TANK/img/steels.gif')
self.rect=self.image.get_rect()
self.rect.left=left
self.rect.top=top
self.live=True
self.hp=3
def displayWall(self):
MainGame.window.blit(self.image,self.rect)
class Explode():
def __init__(self,tank):
self.rect=tank.rect
self.images=[
pygame.image.load('TANK/img/blast0.gif'),
pygame.image.load('TANK/img/blast1.gif'),
pygame.image.load('TANK/img/blast2.gif'),
pygame.image.load('TANK/img/blast3.gif'),
pygame.image.load('TANK/img/blast4.gif'),
]
self.step=0
self.image=self.images[self.step]
self.live=True
def displayExplode(self):
if self.step<len(self.images):
self.image=self.images[self.step]
self.step+=1
MainGame.window.blit(self.image,self.rect)
else:
self.live=False
self.step=0
if __name__=='__main__':
MainGame().startGame()
详细资料提取:
链接:https://pan.baidu.com/s/1RN_f9WZkviudE6UlaLeUWQ?pwd=539e
提取码:539e