"""
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2021/3/15 2:27
# @Author : allen
"""
"""
v1.10:
新增功能:
1. 敌方坦克可以发射子弹
"""
import pygame, time, random
_display = pygame.display
class MianGame:
# 创建游戏主窗口
window = None
display_width = 900
display_height = 600
color_back = pygame.Color(0, 0, 0)
color_red = pygame.Color(255, 0, 0)
tank_num = 5
Vision = "坦克大战v1.03"
Tank_p1 = None
eTank_list = []
eTank_count = 4
# 创建一个子弹列表
bullet_list = []
enemy_bullet_list = []
def __init__(self):
pass
def startGame(self):
# 创建窗口加载窗口(借鉴官方文档) https://www.pygame.org/docs/ref/display.html#pygame.display.init
_display.init()
# 设置游戏窗口的宽和高
MianGame.window = _display.set_mode([self.display_width, self.display_height])
MianGame.Tank_p1 = Tank(450, 300, speed=5)
# MianGame.creationetank()
self.creation_etank()
_display.set_caption(self.Vision)
# 让窗口实现持续刷新操作
while True:
MianGame.window.fill(self.color_back)
self.get_Event()
MianGame.window.blit(self.getTextSurface("剩余敌方坦克数量 %d 辆" % self.tank_num), (4, 4))
MianGame.Tank_p1.dispalayTank()
self.displayetank()
if MianGame.Tank_p1 and not MianGame.Tank_p1.stop:
MianGame.Tank_p1.move()
self.blitBullet()
self.enemy_blit_Bullet()
_display.update()
time.sleep(0.01)
def getTextSurface(self, text):
pygame.font.init()
font = pygame.font.SysFont("kaiti", 18)
textSurface = font.render(text, True, self.color_red)
return textSurface
def endGame(self):
print("感谢体验! - _ - ")
exit()
def get_Event(self):
# 获取队列事件列表
Event_list = pygame.event.get()
for Event in Event_list:
if Event.type == pygame.QUIT:
self.endGame()
if Event.type == pygame.KEYDOWN:
# 判断按下的是哪一个按件
if Event.key == pygame.K_LEFT:
print("坦克向左掉头, 移动")
MianGame.Tank_p1.direction = "L"
MianGame.Tank_p1.stop = False
# MianGame.Tank_p1.move()
if Event.key == pygame.K_RIGHT:
print("坦克向右掉头, 移动")
MianGame.Tank_p1.direction = "R"
MianGame.Tank_p1.stop = False
# MianGame.Tank_p1.move()
if Event.key == pygame.K_DOWN:
print("坦克向下掉头, 移动")
MianGame.Tank_p1.direction = "D"
MianGame.Tank_p1.stop = False
# MianGame.Tank_p1.move()
if Event.key == pygame.K_UP:
print("坦克向上掉头, 移动")
MianGame.Tank_p1.direction = "U"
MianGame.Tank_p1.stop = False
# MianGame.Tank_p1.move()
if Event.key == pygame.K_SPACE:
if len(MianGame.bullet_list) < 3:
m = Bullet(MianGame.Tank_p1)
MianGame.bullet_list.append(m)
print("当前子弹的数量为%d" % len(MianGame.bullet_list))
else:
print("子弹数量不足")
if Event.type == pygame.KEYUP:
# MianGame.Tank_p1.stop = True
if Event.key == pygame.K_UP or pygame.K_LEFT or pygame.K_RIGHT or pygame.K_DOWN:
MianGame.Tank_p1.stop = True
def creation_etank(self):
"""
创建敌方坦克,并将敌方 坦克加到列表中
"""
left = [100, 800, 100, 800]
top = [100, 100, 500, 500]
speed = 1
for i in range(self.eTank_count):
etank = EnemyTank(left[i], top[i], speed)
self.eTank_list.append(etank)
def displayetank(self):
"""
展示敌方坦克
"""
# 遍历敌方坦克列表
for etank in MianGame.eTank_list:
etank.dispalayTank()
etank.randMove()
ebullet = etank.shot()
if ebullet:
MianGame.enemy_bullet_list.append(ebullet)
def blitBullet(self):
"""
在窗口中展示子弹,以及实现子弹的移动
"""
for bullet in self.bullet_list:
if bullet.Live == True:
bullet.displayBullet()
bullet.moveBullet()
else:
MianGame.bullet_list.remove(bullet)
def enemy_blit_Bullet(self):
"""
在窗口中展示敌方坦克发射的子弹
:return:
"""
for ebullet in self.enemy_bullet_list:
if ebullet.Live == True:
ebullet.displayBullet()
ebullet.moveBullet()
else:
MianGame.enemy_bullet_list.remove(ebullet)
class Tank:
def __init__(self, left, top, speed):
self.images = {"U": pygame.image.load(r"E:\python\Python\Day01\脚本\Tank\picture\mtank_u.png"),
"D": pygame.image.load(r"E:\python\Python\Day01\脚本\Tank\picture\mtank_d.png"),
"L": pygame.image.load(r"E:\python\Python\Day01\脚本\Tank\picture\mtank_l.png"),
"R": pygame.image.load(r"E:\python\Python\Day01\脚本\Tank\picture\mtank_r.png")}
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 = speed
self.stop = True
def move(self):
if self.direction == "L":
if self.rect.left > 0:
self.rect.left -= self.speed
elif self.direction == "R":
if self.rect.left + self.rect.height < MianGame.display_width:
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 < MianGame.display_height:
self.rect.top += self.speed
def shot(self):
return Bullet(self)
def dispalayTank(self):
self.image = self.images[self.direction]
MianGame.window.blit(self.image, self.rect)
class MyTank(Tank):
def __init__(self):
pass
class EnemyTank(Tank):
def __init__(self, left, top, speed):
self.images = {"U": pygame.image.load(r"E:\python\Python\Day01\脚本\Tank\picture\etank_u.png"),
"D": pygame.image.load(r"E:\python\Python\Day01\脚本\Tank\picture\etank_d.png"),
"L": pygame.image.load(r"E:\python\Python\Day01\脚本\Tank\picture\etank_l.png"),
"R": pygame.image.load(r"E:\python\Python\Day01\脚本\Tank\picture\etank_r.png")}
self.direction = self.rmdirection()
self.image = self.images[self.direction]
self.rect = self.image.get_rect()
self.rect.left = left
self.rect.top = top
self.stop = True
self.speed = speed
self.step = 200
def rmdirection(self):
rmnum = random.randint(0, 3)
if rmnum == 0:
return "U"
if rmnum == 1:
return "D"
if rmnum == 2:
return "R"
if rmnum == 3:
return "L"
def randMove(self):
if self.step <= 0:
self.direction = self.rmdirection()
self.step = 200
else:
self.move()
self.step -= 1
def shot(self):
"""
重载tank类中shot方法
实现敌方坦克 shot类的不一样
:return:
"""
num = random.randint(1, 1000)
if num <= 5:
return Bullet(self)
class Bullet():
def __init__(self, tank):
self.images = pygame.image.load(r"E:\python\Python\Day01\脚本\Tank\picture\bullent.png")
self.direction = tank.direction
self.rect = self.images.get_rect()
self.speed = 4
self.Live = True
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
if 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
if self.direction == "R":
self.rect.left = tank.rect.left + tank.rect.height
self.rect.top = tank.rect.top + (tank.rect.width / 2) - (self.rect.width / 2)
if self.direction == "L":
self.rect.left = tank.rect.left - self.rect.height
self.rect.top = tank.rect.top + (tank.rect.width / 2) - (self.rect.width / 2)
def moveBullet(self):
if self.direction == "U":
if self.rect.top > 0:
self.rect.top -= self.speed
else:
self.Live = False
if self.direction == "D":
if self.rect.top < MianGame.display_height - self.rect.height:
self.rect.top += self.speed
else:
self.Live = False
if self.direction == "L":
if self.rect.left > 0:
self.rect.left -= self.speed
else:
self.Live = False
if self.direction == "R":
if self.rect.left < MianGame.display_width - self.rect.width:
self.rect.left += self.speed
else:
self.Live = False
def displayBullet(self):
MianGame.window.blit(self.images, self.rect)
class Explode:
def __init__(self):
pass
def displayExplode(self):
pass
class Wall:
def __init__(self):
pass
def displayWall(self):
pass
class Music:
def __init__(self):
pass
def playMusic(self):
pass
if __name__ == "__main__":
tank = MianGame()
tank.startGame()
renew 一下这段时间的结果,快写完了,不过注释比较少,有啥不懂的可以问我