1

我正在使用 pygame 创建我的第一个游戏,我发现为了使事物动画化,最流行的方法是使用 bit blit。
但是,我对此有几个问题:

  1. 据我了解,当您使用 bit blit 时,您必须在屏幕上“重绘”之前存在的每个对象,以使其正常工作。它是否正确?

  2. 如果是这样...我正在使用矩形(矩形)绘制建筑物的“场景”(每个建筑物都有不同的颜色(随机生成),不同的高度(随机),并且它们还有两种不同颜色交替的窗户)。对于我的 Building 类来说,记住建筑物及其窗户的每种颜色的最佳方式是什么,这样当我对建筑物进行位 blit 时,建筑物不会获得不同的颜色以使其更逼真?

4

4 回答 4

3

你可以有一个简单的建筑类:

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

这几乎是绘制任何东西的最基本方法,您可以用这种方式绘制角色,键甚至应该在角色上方的图块,例如像Tuff这样的平台游戏中的水图块。这里的树也在一个大列表中(实际上,出于性能原因,我维护了一个较小的列表,其中包含 1 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-12T10:41:12.103 回答
1

是的,将屏幕视为您在其上绘画的画布。一旦场景完成并显示给观看者,您可以通过在其顶部进行绘画来开始下一个场景(也称为“帧”),替换那里的所有内容。运动是通过在略有不同的地方重复绘制相同的东西来表示的。这很像电影中的传统动画——展示一系列微妙不同的画面来呈现运动的错觉。您通常每秒执行数十次。

以这种方式工作的不仅仅是 pygame/SDL 的 bit blit - 几乎所有实时计算机图形都以这种方式工作。但是,某些系统可能会向您隐藏这一点,并在幕后进行。

对于您的建筑物及其颜色,您希望屏幕上的内容代表您的游戏对象。你不想画一些东西然后试图“记住”你画的东西。渲染应该只是对象的视图,而不是权威的东西。因此,当您生成这些随机高度和颜色时,这将在绘图阶段之前很久就完成。可能在创建时将这些值存储为构建对象的一部分。然后,当您在每一帧中绘制建筑物时,您需要的所有信息都在那里,并且每次绘制时都将保持一致。

于 2009-10-12T09:28:08.107 回答
0

您可以在这里找到第一个问题的答案:
http ://en.wikipedia.org/wiki/Bit_blit

于 2009-10-12T03:29:25.693 回答
0
  1. 是的。您需要使用“画家算法”从后向前绘制场景。

  2. 所以,对于每一帧动画,你要先画背景,然后是建筑物,然后是建筑物前面的任何东西。如果背景覆盖整个屏幕,则无需“清除”屏幕。

于 2009-10-12T03:37:24.713 回答