我正在创建我的第一个游戏使用游戏,我发现,为了动画的东西,最流行的方法是使用比特blit。
不过,我对此有几个问题:
发布于 2009-10-12 10:41:12
您可以有一个简单的建筑类:
class Building:
def __init__(self, x, y, w, h, color):
self.x = x
self.y = y
self.w = w
self.h = h
self.color = color
def draw(self):
// code for drawing the rect at self.x,self.y
// which is self.w wide and self.h high with self.color here关于窗口,您可以在列表(x、y、w、h)中为每个建筑物指定每个窗口,也可以简单地创建如下所示的建筑类:
class Building:
def __init__(self, x, y, w, h, color, wx, wy):
self.x = x
self.y = y
self.w = w
self.h = h
self.color = color
self.wx = wx
self.wy = wy
def draw(self):
// code for drawing the rect at self.x,self.y
// which is w wide and h high with self.color here
// Draw wx windows horizontally and wy windows vertically
for y in range(0, self.wy):
for x in range(0, self.wx):
// draw Window code here另一种方法是将你的建筑物“预写”成一个图像,然后再显示出来(如果你有很多建筑的话,这也会更快)。
然后你的游戏就会像这样
buildingList = [Building(0, 0, 15, 50, RED), Building(0, 0, 40, 30, BLUE)]
while gameIsRunning:
// Clear screen code here
// Show Building
for b in buildingList:
b.draw()
// More stuff这几乎是绘制任何东西的最基本的方法,你可以用这种方式画字符,键,甚至是应该高于你的字符的瓷砖,例如像凝灰岩这样的平台中的水瓦片。这里的树也在一个大列表中(好吧,实际上,我维护了一个较小的列表,其中的树位于1/2的循环屏幕上,这是因为性能原因。有1500多棵“树”)。
编辑:在不同的窗口颜色的情况下,有两个可能的解决方案。
每个建筑物使用不同的窗口颜色:
class Building:
def __init__(self, x, y, w, h, color, wx, wy, windowColor):
self.x = x
self.y = y
self.w = w
self.h = h
self.color = color
self.wx = wx
self.wy = wy
self.windowColor = windowColor
def draw(self):
// code for drawing the rect at self.x,self.y
// which is self.w wide and self.h high with self.color here
// Draw wx windows horizontally and wy windows vertically
for y in range(0, self.wy):
for x in range(0, self.wx):
// draw Window code here using self.windowColor可能性2,每个窗口有不同的颜色:
class Building:
def __init__(self, x, y, w, h, color, windows):
self.x = x
self.y = y
self.w = w
self.h = h
self.color = color
self.wx = wx
self.wy = wy
self.windows = windows
def draw(self):
// code for drawing the rect at self.x,self.y
// which is self.w wide and self.h high with self.color here
// Draw all windows
for w in windows:
// draw Window at w[0] as x, w[1] as y with w[2] as color
// Create a building at 0,0 that is 20 wide and 80 high with GRAY color and two windows, one at 2,2 which is yellow and one at 4, 4 that's DARKBLUE.
b = Building(0, 0, 20, 80, GRAY, [(2, 2, YELLOW), (4, 4, DARKBLUE)])发布于 2009-10-12 09:28:08
是的,把屏幕想象成画布一样。一旦场景完成并显示给观众,你就开始下一个场景(又称“框架”),在它的顶部进行绘画,取代那里的一切。运动是通过在稍微不同的地方反复画同样的东西来表现的。它很像电影中的传统动画-展示一系列微妙的不同的图片来呈现运动的错觉。通常情况下,每秒要这样做几十次。
这不仅是游戏/SDL的比特blit的工作方式-几乎所有的实时计算机图形为这种方式工作。然而,有些系统可能会对您隐瞒这一点,并将其隐藏在幕后。
对于你的建筑物和它们的颜色,你想要进入屏幕的东西是你的游戏对象的代表。你不想画一些东西,然后试着“记住”你画的东西。渲染应该只是对象的视图,而不是权威的东西。所以当你产生这些随机的高度和颜色时,这将在绘图阶段之前很久就完成了。将这些值存储为构建对象的一部分,可能是在创建时。然后,当你来绘制建筑的每一个框架,所有你需要的信息就在那里,并将保持一致,每次你画它。
发布于 2009-10-12 03:29:25
你可以在这里找到你的第一个问题的答案:
布利特
https://stackoverflow.com/questions/1552535
复制相似问题