def run_game():
#初始化游戏
pygame.init()
#播放音效初始化
pygame.mixer.init()
#创建ViewManage对象
view_manager = ViewManager()
#设置显示屏幕,返回surface对象
screen = pygame.display.set_mode((view_manager.screen_width,view_manager.screen_height))
#设置标题
pygame.display.set_caption('坦克大战')
#初始化所有菜单场景
pv.menus = Menu(view_manager,screen)
#创建玩家坦克,敌对坦克,战斗场景
gf.game_start(view_manager,screen)
#设置屏幕绘制速度----刷新频率
#初始化一个时钟对象
clock = pygame.time.Clock()
while(True):
# 指定循环体内部的代码执行的频率
clock.tick(36) #每秒更新60次
#处理游戏事件
gf.check_events(screen,view_manager,pv.player)
#更新游戏屏幕
gf.update_screen(screen,view_manager,pv.player)
#更新整个待显示的 Surface 对象到屏幕上
pygame.display.flip()
#更新部分软件界面显示
#pygame.display.update()
run_game()
class ViewManager:
"""加载所有游戏图片,声音等资源文件"""
def __init__(self):
#游戏窗口尺寸
self.screen_width = 1200
self.screen_height = 800
#字体
self.font=[]
self.font.append(pygame.font.Font('res/font/msyhbd.ttf', 36))
self.font.append(pygame.font.Font('res/font/msyh.ttf', 24))
#音效
self.sounds=[]
self.sounds.append(pygame.mixer.Sound('res/sound/fire.wav'))
self.sounds.append(pygame.mixer.Sound('res/sound/bomb.wav'))
#场景;菜单背景
self.menuback=[]
self.menuback.append(pygame.image.load('res/images/map/menuback.jpg'))
self.menuback.append(pygame.image.load('res/images/map/failureback.jpg'))
#场景;菜单按钮
self.menu=[]
self.menu.append(pygame.image.load('res/images/menu/btn_start.png'))
self.menu.append(pygame.image.load('res/images/menu/btn_restart.png'))
#战斗地图背景
self.map=[]
self.map.append(pygame.image.load('res/images/map/0.jpg'))
self.map.append(pygame.image.load('res/images/map/1.jpg'))
#玩家坦克
self.player_body = []
self.player_body.append(pygame.image.load('res/images/player/0/up.png'))
self.player_body.append(pygame.image.load('res/images/player/0/down.png'))
self.player_body.append(pygame.image.load('res/images/player/0/left.png'))
self.player_body.append(pygame.image.load('res/images/player/0/right.png'))
#怪物坦克
self.monster_body = []
self.monster_body.append(pygame.image.load('res/images/monster/0/up.png'))
self.monster_body.append(pygame.image.load('res/images/monster/0/down.png'))
self.monster_body.append(pygame.image.load('res/images/monster/0/left.png'))
self.monster_body.append(pygame.image.load('res/images/monster/0/right.png'))
#子弹
self.bullet = []
self.bullet.append(pygame.image.load('res/images/bullet_1.png'))
self.bullet.append(pygame.image.load('res/images/bullet_2.png'))
#子弹爆炸的动画
self.bomb = []
self.bomb.append(pygame.image.load('res/images/bomb_1.png'))
self.bomb.append(pygame.image.load('res/images/bomb_2.png'))
self.bomb.append(pygame.image.load('res/images/bomb_3.png'))
self.bomb.append(pygame.image.load('res/images/bomb_4.png'))
self.bomb.append(pygame.image.load('res/images/bomb_5.png'))
self.bomb.append(pygame.image.load('res/images/bomb_6.png'))
class Menu:
"""菜单画面"""
def __init__(self,view_manager,screen):
self.view = view_manager
self.scr = screen
#初始化各个控件的坐标
#开始画面
# 开始按钮
rect = self.view.menu[0].get_rect()
lx = int(self.view.screen_width / 2 - rect.width / 2)
ly = int(self.view.screen_height / 3)
width = rect.width
height = rect.height
self.btn_start_rect = (lx,ly,width,height)
#失败画面
# 再来一次按钮
self.btn_restart_rect=(lx,ly,width,height)
'''绘制开始菜单'''
def start(self):
#背景
self.scr.blit(self.view.menuback[0],(0,0))
#开始按钮
self.scr.blit(self.view.menu[0],(self.btn_start_rect[0],self.btn_start_rect[1]))
'''处于开始按钮'''
def isin_start_btn(self,p):
x,y=p
return gf.inrect(x,y,self.btn_start_rect)
'''绘制失败菜单'''
def gameover(self):
#背景
self.scr.blit(self.view.menuback[1],(0,0))
#按钮再来一次
self.scr.blit(self.view.menu[1],(self.btn_start_rect[0],self.btn_start_rect[1]))
'''处于失败再来一次按钮'''
def isin_restart_btn(self,p):
x,y=p
return gf.inrect(x,y,self.btn_restart_rect)
def game_start(view_manager,screen):
#自动生成敌对坦克的数量,这里每次会从失败的关卡坦克数量重新开始,所以将原本随关卡递增的坦克数量-1
pv.lastmakemonstersnum-=1
if pv.lastmakemonstersnum <= 0:
pv.lastmakemonstersnum = 1
pv.keydownnum = 0
#创建玩家坦克
pv.player = Player(view_manager,screen)
#敌对坦克
pv.monsters = []
#战斗场景
pv.fire = Fire(view_manager,screen)
#处理游戏事件
def check_events(screen,view_manager,player):
'''响应按键和鼠标事件'''
for event in pygame.event.get():
#游戏退出
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
return
#开始菜单
if pv.runstate == pv.RUN_STATE_MENU:
#鼠标点击
if event.type == pygame.MOUSEBUTTONUP:
mp = pygame.mouse.get_pos()
print(mp)
if pv.menus.isin_start_btn(mp) == True:
#进入正式游戏
pv.runstate = pv.RUN_STATE_RUN
return
#失败菜单
if pv.runstate == pv.RUN_STATE_OVER:
#鼠标点击
if event.type == pygame.MOUSEBUTTONUP:
mp = pygame.mouse.get_pos()
print(mp)
if pv.menus.isin_restart_btn(mp) == True:
game_start(view_manager,screen)
#关卡计数 从当前失败关卡继续
pv.gatenum-=1
#进入正式游戏
pv.runstate = pv.RUN_STATE_RUN
return
#战斗场景按键
# 按键按下
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
#发射炮弹
player.fire(screen,view_manager)
# 用户按下向上键
if event.key == pygame.K_UP:
player.move = pv.MOVE_UP
player.lastmove = pv.MOVE_UP
pv.keydownnum+=1
# 用户按下向下键
if event.key == pygame.K_DOWN:
player.move = pv.MOVE_DOWN
player.lastmove = pv.MOVE_DOWN
pv.keydownnum+=1
# 用户按下向左键
if event.key == pygame.K_LEFT:
player.move = pv.MOVE_LEFT
player.lastmove = pv.MOVE_LEFT
pv.keydownnum+=1
# 用户按下向右键
if event.key == pygame.K_RIGHT:
player.move = pv.MOVE_RIGHT
player.lastmove = pv.MOVE_RIGHT
pv.keydownnum+=1
# 按键松开
if event.type == pygame.KEYUP:
# 向上键
if event.key == pygame.K_UP:
pv.keydownnum-=1
# 向下键
if event.key == pygame.K_DOWN:
pv.keydownnum-=1
# 向左键
if event.key == pygame.K_LEFT:
pv.keydownnum-=1
# 向右键
if event.key == pygame.K_RIGHT:
pv.keydownnum-=1
#当按下的方向键全部被释放后,保持我方不再移动
if pv.keydownnum <= 0 or pv.keydownnum > 4 :
pv.keydownnum = 0
player.move = pv.MOVE_STAND
def update_screen(screen,view_manager,player):
'''处理更新游戏界面的方法'''
'''开始菜单'''
if pv.runstate == pv.RUN_STATE_MENU:
pv.menus.start()
return
'''战队失败'''
if pv.runstate == pv.RUN_STATE_OVER:
pv.menus.gameover()
return
'''战斗场景'''
if pv.runstate == pv.RUN_STATE_RUN:
pv.fire.show(view_manager,screen,player,pv.monsters)
return
python坦克大战(4)玩家坦克和敌对坦克