今天跟视频学习了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=Nonemy_tank=NoneenemyTankList=[]enemyTankCount=5myBulletList=[]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_tankMainGame.my_tank=Noneself.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=100for 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=Trueclass 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=leftself.rect.top=topself.speed=5self.stop=Trueself.live=Trueself.oldLeft=self.rect.leftself.oldTop = self.rect.topdef move(self):self.oldLeft= self.rect.leftself.oldTop = self.rect.topif self.direction=='L':if self.rect.left >0:self.rect.left -= self.speedelif self.direction=='U':if self.rect.top > 0:self.rect.top -= self.speedelif self.direction=='D':if self.rect.top+self.rect.height < SCREEN_HEIGHT:self.rect.top +=self.speedelif self.direction == 'R':if self.rect.left+self.rect.height < SCREEN_WIDTH:self.rect.left += self.speeddef shot(self):return Buttle(self)def stay(self):self.rect.left=self.oldLeftself.rect.top=self.oldTopdef 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=leftself.rect.top=topself.speed=speedself.flag=Trueself.step=20def 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=60else:self.move()self.step -=1def 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.directionself.rect=self.image.get_rect()if self.direction == 'U':self.rect.left=tank.rect.left+tank.rect.width/2-self.rect.width/2self.rect.top=tank.rect.top-self.rect.heightelif self.direction == 'D':self.rect.left = tank.rect.left + tank.rect.width / 2 - self.rect.width / 2self.rect.top = tank.rect.top + tank.rect.heightelif self.direction == 'L':self.rect.left = tank.rect.left - tank.rect.width / 2 - self.rect.width / 2self.rect.top = tank.rect.top + tank.rect.width/2 -self.rect.width/2elif self.direction == 'R':self.rect.left = tank.rect.left + tank.rect.widthself.rect.top = tank.rect.top + self.rect.width/2-self.rect.width/2self.speed=6self.live=Truedef move(self):if self.direction=='U':if self.rect.top >0:self.rect.top-=self.speedelse:self.live=Falseelif self.direction=='R':if self.rect.left +self.rect.width <SCREEN_WIDTH:self.rect.left +=self.speedelse:self.live=Falseelif self.direction =='L':if self.rect.left >0:self.rect.left -=self.speedelse:self.live=Falseelif self.direction=='D':if self.rect.top +self.rect.height <SCREEN_HEIGHT:self.rect.top +=self.speedelse:self.live=Falsedef hitWall(self):for wall in MainGame.wallList:if pygame.sprite.collide_rect(self,wall):self.live=Falsewall.hp-=1if wall.hp<=0:wall.live=Falsedef 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=Falseself.live=Falseexplode=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 = FalseMainGame.my_tank.live = Falseclass Wall():def __init__(self,left,top):self.image=pygame.image.load('TANK/img/steels.gif')self.rect=self.image.get_rect()self.rect.left=leftself.rect.top=topself.live=Trueself.hp=3def displayWall(self):MainGame.window.blit(self.image,self.rect)
class Explode():def __init__(self,tank):self.rect=tank.rectself.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=0self.image=self.images[self.step]self.live=Truedef displayExplode(self):if self.step<len(self.images):self.image=self.images[self.step]self.step+=1MainGame.window.blit(self.image,self.rect)else:self.live=Falseself.step=0
if __name__=='__main__':MainGame().startGame()
详细资料提取:
链接:https://pan.baidu.com/s/1RN_f9WZkviudE6UlaLeUWQ?pwd=539e
提取码:539e