# coding= utf-8
"""坦克大战设计
新增功能:
加载我方坦克"""
import pygame
screen_width=700
screen_height=500
bg_color=pygame.Color(0,0,0) # RGB值: 000为黑色
COLOR_PINK =pygame.Color(255,192,203)
# 主类
class MainGame():
window = None
my_tank= None
def __init__(self):
pass
# start game (开始游戏)
def startGame(self):
# load main window(加载主窗口)
# initial window(初始化窗口)
pygame.display.init()
# set size and display window(设计窗口大小并显示窗口)
MainGame.window=pygame.display.set_mode([screen_height,screen_width])
# 初始化我方坦克
MainGame.window=Tank(350,250)
# set title for window (设计窗口标题)
pygame.display.set_caption('Tank War 666')
while True:
# set fill color for window(给窗口设置填充色)
MainGame.window.fill(bg_color)
# 获取事件
self.getEvent()
# 绘制文字
MainGame.window.blit(self.getText('enemy tanks%d'%5),(10,10))
# 调用坦克显示的方法
MainGame.my_tank.displayTank()
# 一直更新显示窗口
pygame.display.update()
# exit game (退出游戏)
def exitGame(self):
print('tanks,bye')
exit()
# 左上角文字的绘制
def getText(self,text):
# 初始化字体模块
pygame.font.init()
# 查看所有的字体
# print(pygame.font.get_fonts())
# 获取字体font对象
font= pygame.font.SysFont('calibri', 20)
#获取字体
text = font.render(text, True,COLOR_PINK)
return text
# get event (获取事件)
def getEvent(self):
#获取所有事件
eventList= pygame.event.get()
# 遍历事件
for event in eventList:
# 判断按下的键是关闭还是键盘按下
# 如果按的是退出,则关闭窗口
if event.type == pygame.QUIT:
self.exitGame()
# 如果是键盘的按下
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print('press key Left, turn left')
elif event.key == pygame.K_RIGHT:
print('press key Left, turn right')
elif event.key == pygame.K_UP:
print('press key Left, turn up')
elif event.key == pygame.K_DOWN:
print('press key Left, turn Down')
# 坦克类
class Tank():
# 添加距离左边left 距离上边top
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='L'
# 根据当前图片方向获取图片 surface
self.image=self.images[self.direction]
# 根据图片获取区域
self.rect=self.image.get_rect()
# 设置区域的left和top
self.rect.left=left
self.rect.top=top
# move(移动)
def move(self):
pass
# shot(射击)
def shot(self):
pass
# show tank(展示坦克的方法)
def displayTank(self):
# 获取展示对象
self.image=self.images[self.direction]
# 调用blit方法展示
MainGame.window.blit(self.image, self.rect)
# our team tank(我方坦克)
class MyTank(Tank):
def __init__(self):
pass
# enemy tank(敌方坦克)
class EnemyTank(Tank):
def __init__(self):
pass
# bullet(子弹类)
class Bullet():
def __init__(self):
pass
# move (移动)
def move(self):
pass
# display bullet(展示子弹)
def displayBullet(self):
pass
# wall class(墙壁类)
class Wall():
def __init__(self):
pass
# display wall (展示墙壁的方法)
def displayWall(self):
pass
class Explode():
def __init__(self):
pass
# display explode (展示爆炸的方法)
def displayExplode(self):
pass
# music (音效类)
class Music():
def __init__(self):
pass
# play music(播放音乐)
def playMusic(self):
pass
if __name__ =='__main__':
MainGame().startGame()
# MainGame().getText() ( 打印查看字体)
老师我这个程序总是闪一下就退了,查了一下没查出来什么原因