在前面的讲解
逐步实现了游戏地图的构造,接下来就要将其显示出来,就是要显示到游戏界面上。前面的内容中元素都有draw函数,调用后再blit到一起,最后flip就可以了。
四
import os
import pygame
from workerSprite import *
from gameElementSprite import *
from params import Params
from gameMap import *
'''
游戏界面显示
'''
class gameDisplay():
def __init__(self, gamescreen):
self.gamescreen = gamescreen
self.levelspath = Params.get('levelsPath')
self.initCurGame()
'''
游戏初始化
'''
def initCurGame(self):
self.scroll_x = 0
self.scroll_y = 0
'''
画游戏界面
'''
def draw(self, *elems):
for elemyield in elems:
elemyield.draw(self.gamesurface)
看上去是实现了,但实际上并没有读取到关卡的地图文件,同时也没有考虑到如果地图文件大过界面范围的情况,为了能解决这些问题,需要添加代码如下:
'''
游戏界面显示
'''
class gameDisplay():
...
'''
画游戏界面
'''
def draw(self, *elems):
self.scrollWindows() #新增
self.gamesurface.blit(self.gamesurface_bg, (0, 0)) #新增
for elemyield in elems: