首个游戏项目
'''
v1.05 实现坦克的移动
'''
import pygame
SCREEN_WIDTH = 900
SCREEN_HEIGHT = 550
VERSION = 'V1.05'
# 主逻辑类
class MainGame():
# 游戏主窗口
window = None
# 我方坦克(唯一性)设置成类属性
tank1 = None
def startGame(self):
# 调用窗口初始化方法
pygame.display.init()
# 在窗口左上角设置游戏标题
pygame.display.set_caption('坦克大战'+VERSION)
# 创建一个窗口屏幕显示表面
MainGame.window = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
# 调用创建我方坦克的方法
self.createMytank()
while True:
# 填充窗口背景色
MainGame.window.fill((100,100,200))
# 将获取的事件类型在填充完窗口背景色后时刻处于循环中
self.getEvents()
# 展示我方坦克
self.showMytank()
# 更新
pygame.display.update()
# 获取所有事件处理的方法
def getEvents(self):
# 获取队列中的所有事件,遍历事件,对type为QUIT和KEYDOWN的两种事件类型进行处理
eventList = pygame.event.get()
# 遍历所有事件
for e in eventList:
# 当事件类型是QUIT时,退出游戏
if e.type == pygame.QUIT:
self.showMytank()
# 退出游戏
self.gameOver()
# 当事件类型是KEYDOWN时,进行细化处理
elif e.type == pygame.KEYDOWN:
# 按下键盘上的向上按键时的处理
if e.key == pygame.K_UP:
print('向上移动')
MainGame.tank1.direction = 'U'
MainGame.tank1.move()
# 按下键盘上的向下按键时的处理
elif e.key == pygame.K_DOWN:
print('向下移动')
MainGame.tank1.direction = 'D'
MainGame.tank1.move()
# 按下键盘上的向左按键时的处理
elif e.key == pygame.K_LEFT:
print('向左移动')
MainGame.tank1.direction = 'L'
MainGame.tank1.move()
# 按下键盘上的向右按键时的处理
elif e.key == pygame.K_RIGHT:
print('向右移动')
MainGame.tank1.direction = 'R'
MainGame.tank1.move()
# 按下键盘上的空格按键时的处理
elif e.key == pygame.K_SPACE:
print('biubiu~')
def createMytank(self):
# 创建我方坦克并赋予其位置属性坐标
MainGame.tank1 = MyTank(SCREEN_WIDTH/2,SCREEN_HEIGHT/3*2)
#展示我方坦克
def showMytank(self):
MainGame.tank1.displayTank()
def gameOver(self):
exit()
# 基本坦克类
class BaseTank():
# 基本坦克属性 位置 方向 速度 图片
def __init__(self):
self.direction = None
self.rect = None
self.images = None
self.image = None
self.speed = 0
#v1.05 新增移动方法
def move(self):
pass
# 我方坦克类
class MyTank(BaseTank):
def __init__(self,x,y):
#引用父类属性
super(MyTank, self).__init__()
# 设置具体的图片集
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 返回的是一个surface)
self.image = self.images[self.direction]
# 先基于图像获取坦克的位置和大小
self.rect = self.image.get_rect()
# 修改坦克坐标,改成自定义位置
self.rect.centerx = x
self.rect.centery = y
# 设置坦克速度
self.speed = 30
#展示坦克
def displayTank(self):
self.image = self.images[self.direction]
# 把坦克的图像(即表面)和位置放到另一个图像或表面窗口
MainGame.window.blit(self.image,self.rect)
def move(self):
# 移动,基于在指定的方向移动
if self.direction == 'U':
if self.rect.centery > self.rect.height/2:
self.rect.centery -= self.speed
elif self.direction == 'D':
if self.rect.centery < SCREEN_HEIGHT - self.rect.height/2:
self.rect.centery += self.speed
elif self.direction == 'L':
if self.rect.centerx > self.rect.height/2:
self.rect.centerx -= self.speed
elif self.direction == 'R':
if self.rect.centerx < SCREEN_WIDTH - self.rect.height/2:
self.rect.centerx += self.speed
# 敌方坦克类
class EnemyTank(BaseTank):
pass
# 子弹类
class Bullet():
pass
# 爆炸类
class Explode():
pass
# 墙壁类
class Wall():
pass
# 音效类
class Audio():
pass
#创建一个主逻辑类的对象
game = MainGame()
# 开始游戏
game.startGame()