[ Pobierz całość w formacie PDF ]

paddle, and the ball spritesheet are shown in Figure 5-1, Figure 5-2, and
Figure 5-3, respectively.
Figure 5-1. The brick spritesheet
Figure 5-2. The player paddle
Figure 5-3. The ball spritesheet
You may notice that the bricks and the player paddle are nearly square, rather
than rectangular. This is good because it gives you a chance to stretch the
image into a rectangle using OpenGL.
Next, you need to add some more variables to the PBGameVars file. Add the
following lines to your PBGameVars. Don t worry too much about the ones that
you don t understand right now; I will explain them when we use them.
public static float playerBankPosX = -.73f;
public static int playerAction = 0;
public static final int PLAYER MOVE LEFT 1 = 1;
public static final int PLAYER RELEASE = 3;
public static final int PLAYER MOVE RIGHT 1 = 4;
CHAPTER 5: Creating the Player Character and Obstacles 41
public static final float PLAYER MOVE SPEED = .2f;
public static final int PADDLE = R.drawable.goldbrick;
public static final int BRICK BLUE = 1;
public static final int BRICK BROWN = 2;
public static final int BRICK DARK GRAY = 3;
public static final int BRICK GREEN = 4;
public static final int BRICK LITE GRAY = 5;
public static final int BRICK PURPLE = 6;
public static final int BRICK RED = 7;
public static final int BRICK SHEET = R.drawable.bricksheet;
public static final int BALL SHEET = R.drawable.ballsheet;
public static final float BALL SPEED = 0.01f;
public static float ballTargetY = 0.01f;
public static float ballTargetX = -1.125f;
With this little bit of housekeeping taken care of, it is time to jump into creating
some items for the game. Let s start with the player paddle.
Creating the Player Paddle Class
Add a new class called PBPlayer to your project. This class will look very much
like the class that you created for the background, PBBackground. The class
contains a constructor, a draw() method, and a loadTexture() method. The
code for the PBPlayer class is shown in Listing 5-1.
Listing 5-1. The PBPlayer Class
package com.jfdimarzio;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
public class PBPlayer {
private FloatBuffer vertexBuffer;
private FloatBuffer textureBuffer;
private ByteBuffer indexBuffer;
42 CHAPTER 5: Creating the Player Character and Obstacles
private int[] textures = new int[1];
private float vertices[] = {
0.0f, 0.0f, 0.0f,
1.5f, 0.0f, 0.0f,
1.5f, .25f, 0.0f,
0.0f, .25f, 0.0f,
};
private float texture[] = {
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f,
};
private byte indices[] = {
0,1,2,
0,2,3,
};
public PBPlayer() {
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
indexBuffer = ByteBuffer.allocateDirect(indices.length);
indexBuffer.put(indices);
indexBuffer.position(0);
}
public void draw(GL10 gl) {
gl.glBindTexture(GL10.GL TEXTURE 2D, textures[0]);
gl.glFrontFace(GL10.GL CCW);
gl.glEnable(GL10.GL CULL FACE);
gl.glCullFace(GL10.GL BACK);
gl.glEnableClientState(GL10.GL VERTEX ARRAY);
gl.glEnableClientState(GL10.GL TEXTURE COORD ARRAY);
b
CHAPTER 5: Creating the Player Character and Obstacles 43
gl.glVertexPointer(3, GL10.GL FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL FLOAT, 0, textureBuffer);
gl.glDrawElements(GL10.GL TRIANGLES, indices.length, GL10.
GL UNSIGNED BYTE, indexBuffer);
gl.glDisableClientState(GL10.GL VERTEX ARRAY);
gl.glDisableClientState(GL10.GL TEXTURE COORD ARRAY);
gl.glDisable(GL10.GL CULL FACE);
}
public void loadTexture(GL10 gl,int texture, Context context) {
InputStream imagestream =
context.getResources().openRawResource(texture);
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(imagestream);
}catch(Exception e){
}finally { [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • szkicerysunki.xlx.pl
  •