用到的库:
import pygame
import time
import random
from pygame.sprite import Sprite
全部代码:
import pygame
import time
import random
from pygame.sprite import Sprite
SCREEN_WIDTH = 710
SCREEN_HEIGHT = 500
BG_COLOR = pygame.Color(100, 149, 237)
TEXT_COLOR = (220, 20, 60)
class BaseItem(Sprite):
def __init__(self, color, width, height):
pygame.sprite.Sprite.__init__(self)
class MainGame():
window = 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])
pygame.display.set_caption('坦克大战')
self.cteateMytank()
self.createEnemTank()
self.createWall()
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 MainGame.my_tank.stop:
MainGame.my_tank.move()
MainGame.my_tank.Tank_hit_Wall()
MainGame.my_tank.myTank_hit_enemTank()
pygame.display.update()
def cteateMytank(self):
MainGame.my_tank = MyTank(350, 300)
music = Music('img/start.wav')
music.play()
def createEnemTank(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 blitEnemyTank(self):
for enemyTank in MainGame.enemyTankList:
if enemyTank.live:
enemyTank.displaytank()
enemyTank.randMove()
enemyTank.Tank_hit_Wall()
if MainGame.my_tank and MainGame.my_tank.live:
enemyTank.enemyTank_hit_myTanke()
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 blitExplode(self):
for explode in MainGame.explodeList:
if explode.live:
explode.displayExplode()
else:
MainGame.explodeList.remove(explode)
def createWall(self):
for i in range(6):
wall = Wall(i*130, 200)
MainGame.wallList.append(wall)
def blitWall(self):
for wall in MainGame.wallList:
if wall.live:
wall.displayWall()
else:
MainGame.wallList.remove(wall)
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.cteateMytank()
if MainGame.my_tank and MainGame.my_tank.live:
if event.key == pygame.K_LEFT:
MainGame.my_tank.direction = 'L'
MainGame.my_tank.stop = True
print('按下左键,坦克是向左移动')
elif event.key == pygame.K_RIGHT:
MainGame.my_tank.direction = 'R'
MainGame.my_tank.stop = True
print('按下右键,坦克是向右移动')
elif event.key == pygame.K_UP:
MainGame.my_tank.direction = 'U'
MainGame.my_tank.stop = True
print('按下上键,坦克是向上移动')
elif event.key == pygame.K_DOWN:
MainGame.my_tank.direction = 'D'
MainGame.my_tank.stop = True
print('按下下键,坦克是向下移动')
elif event.key == pygame.K_SPACE:
print('发射子弹')
if len(MainGame.myBulletList) < 3:
myBullet = Bullet(MainGame.my_tank)
MainGame.myBulletList.append(myBullet)
music = Music('img/hit.wav')
music.play()
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN or event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
if MainGame.my_tank and MainGame.my_tank.live:
MainGame.my_tank.stop = False
def endGame(self):
print('谢谢使用,欢迎下次使用')
exit()
class Tank(BaseItem):
def __init__(self, left, top):
self.images = {
'U':pygame.image.load('img/p1tankU.gif'),
'D':pygame.image.load('img/p1tankD.gif'),
'L':pygame.image.load('img/p1tankL.gif'),
'R':pygame.image.load('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 = 3
self.stop = False
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 Bullet(self)
def displaytank(self):
self.image = self.images[self.direction]
MainGame.window.blit(self.image, self.rect)
def Tank_hit_Wall(self):
for wall in MainGame.wallList:
if pygame.sprite.collide_rect(self, wall):
self.stay()
def stay(self):
self.rect.left = self.oldLeft
self.rect.top = self.oldTop
class MyTank(Tank):
def __init__(self, left, top):
super(MyTank, self).__init__(left, top)
def myTank_hit_enemTank(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('img/enemy1U.gif'),
'L': pygame.image.load('img/enemy1L.gif'),
'D': pygame.image.load('img/enemy1D.gif'),
'R': pygame.image.load('img/enemy1R.gif')
}
self.step = 60
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
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,300)
if num < 10:
return Bullet(self)
def enemyTank_hit_myTanke(self):
if pygame.sprite.collide_rect(self, MainGame.my_tank):
self.stay()
class Wall():
def __init__(self, left, top):
self.image = pygame.image.load('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 Bullet(BaseItem):
def __init__(self, tank):
self.image = pygame.image.load('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.width
elif self.direction == 'L':
self.rect.left = tank.rect.left - self.rect.width
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 + tank.rect.width/2 - self.rect.height/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 == 'D':
if self.rect.top + self.rect.height < SCREEN_HEIGHT:
self.rect.top += self.speed
else:
self.live = False
elif self.direction == 'L':
if self.rect.left > 0:
self.rect.left -= self.speed
else:
self.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):
self.live = False
enemyTank.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):
explade = Explode(MainGame.my_tank)
MainGame.explodeList.append(explade)
self.live = False
MainGame.my_tank.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
class Explode():
def __init__(self, tank):
self.rect = tank.rect
self.images = [
pygame.image.load('img/blast0.gif'),
pygame.image.load('img/blast1.gif'),
pygame.image.load('img/blast2.gif'),
pygame.image.load('img/blast3.gif'),
pygame.image.load('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
class Music():
def __init__(self, filenam):
self.filenam = filenam
pygame.mixer.init()
pygame.mixer.music.load(self.filenam)
def play(self):
pygame.mixer.music.play()
t = MainGame()
t.startGame()