听涛心情

希望能与我分享

导航

游戏入门学习二

关于游戏的绘画原理设计
一:我们在游戏中看到的各种特效一般都是由不同的图片构成的。例如一个汽车,当它转弯时就将另一张图片画上去。对于普通的一个活体来说,至少应该包含四个方向的图片。通常会有16张活更多。
二:游戏中的图片通常有很多。每张图片都有自己的结构定义,如果将这些图片用一张大图来存储会节省空间开销。这样做的缺点是你需要自己将需要的图片从这张大图中分离出来。例如Png格式的大图,如下代码分离:
/**
 * Extracts a portion of an image using clipping.
 * @param source The source image.
 * @param x The starting x position of the clipping rectangle. 
 * @param y The starting y position of the clipping rectangle.
 * @param width The width of the clipping rectangle.
 * @param height The height of the clipping rectangle.
 * @return A new Image object containing only the portion of the image
 * withing the x, y, width, height rectangle.
 */
public final static Image getImageRegion(Image source, int x, int y, int width, int
height)
{
   // create a placeholder for our resulting image region
   Image result = Image.createImage(width, height);
 
   if (x + width > source.getWidth() || y + height > source.getHeight())
      System.out.println("Warning: attempting extract using (" +
                     x + "," + y + "," + width + "," + height + ") when image is " +
                     "(" + source.getWidth() + "," + source.getHeight() + ")");
 
   // draw the image, offset by the region starting position
   result.getGraphics().drawImage(source, -x, -y, Graphics.TOP|Graphics.LEFT);
 
   return result;
}

 如果一个场景是由多个图片构成,例如子弹(飞行,爆炸或撞击其它物体)。这时就需要将表示同一个物体的图片合在一组。代码如下:

/**
 * Gets an array of images by breaking a larger image into smaller frames.
 * @param sourceImage The image to extract the frames from.
 * @param sourceX The starting x position in the source image to use.
 * @param sourceY The starting y position in the source image to use.
 * @param framesWide The number of frames across the source image to extract.
 * @param framesHigh The number of frames down the source image to extract.
 * @param frameWidth The width of each of those frames.
 * @param frameHeight The height of each of those frames.
 * @return An array containing an image for each frame.
 */
public final static Image[] extractFrames(Image sourceImage, int sourceX,
                                          int sourceY,
                                          int framesWide, int framesHigh,
                                          int frameWidth, int frameHeight) 
{
   // extract all the frames from the source image
   Image[] frames = new Image[framesWide * framesHigh];
   int frameCount = 0;
 
   for (int fy = 0; fy < framesHigh; fy++)
      for (int fx = 0; fx < framesWide; fx++)
         frames[frameCount++] =
                  getImageRegion(sourceImage, sourceX + (fx * frameWidth),
                                   sourceY + (fy * frameHeight),
                                   frameWidth, frameHeight);
   return frames;
}

三:现在需要的图片已经获得,下面只需要将其按照某种逻辑显示出来。所谓的逻辑即是某种动作。
比如说1941这个8位游戏(当然,这个例子不太合适),飞机会发射很多子弹,每个子弹都有自己的状态,我们如果需要某种机制,让每个子弹去共享同一个子弹图片集。这样,每个子弹根据自己的状态来获取子弹图片集里相应的图片。同时,我们需要有人来组织他们。这个任务通常由Sprite来完成。他们来控制某个子弹的整个逻
有人也许会问,为生么不放在图片集里做这个工作,这里的解释是我们可能有很多不同的对象,比如说子弹,汽车,飞机等等。图片集ImageSet类是大家的共享类,不应该让这种逻辑也在这个类里实现。所以引出了Sprite类。对于不同的sprite,可以声明不同的对象例如:Sprite bullet;Sprite car;等等。

posted on 2005-10-24 19:16  Cornell  阅读(358)  评论(0)    收藏  举报