Вы находитесь на странице: 1из 19

import import import import import import import import import import import import import import import

import

java.io.FileInputStream; java.io.FileNotFoundException; java.io.IOException; java.nio.ByteBuffer; java.nio.ByteOrder; java.nio.FloatBuffer; org.lwjgl.opengl.Display; org.lwjgl.opengl.DisplayMode; org.lwjgl.opengl.GL11; org.lwjgl.util.glu.GLU; org.lwjgl.util.glu.Sphere; org.lwjgl.input.Keyboard; org.newdawn.slick.openal.Audio; org.newdawn.slick.openal.AudioLoader; org.newdawn.slick.opengl.Texture; org.newdawn.slick.opengl.TextureLoader;

/** * Loads up a new pacman level with given score and lives * * @author Jack Taylor */ public class Game { private boolean done = false; private boolean fullscreen = false; private boolean f1 = false; private boolean f2 = false; private boolean f3 = false; private boolean f4 = false; private boolean f5 = false; private float tipX = 0.0f; private float tipY = 0.0f; private float playerX; private float playerY; private int playerXStart; private int playerYStart; private int zombieXStart; private int zombieYStart; private float playerRot; private Grid grid; private float lightAmbient[] = {0.0f, 0.0f, 0.0f, 0.0f }; // Ambient Light Values private float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // Diffuse L ight Values private float lightPosition[] = { 0.0f, 0.0f, -100.0f, -75.0f }; // Light Po sition Texture wallTexture; Texture pillTexture; Texture floorTexture; Texture lifeTexture; Texture powerUpTexture; Audio backgroundMusic; Sphere sphere; Player player; Zombie zombie1; Zombie zombie2; Zombie zombie3;

Zombie zombie4; private long startTime; private int lives; private int score; private boolean powerUp; private long powerUpTime; ByteBuffer temp; private int gameType; private boolean win; private float runSpeed; //spotlight settings private float lightAmbient2[] = { 15.0f,1.0f,1.0f,1.0f }; // Pacman Diffuse Light Values // private float lightDiffuse2[] = { 5.0f, 1.0f, 1.0f, 1.0f }; // Pacman Spotlight location // private float lightPosition2[] = {playerX, playerY, 1.0f, 1.0f }; TextGenerator fonts; /** * Constructor * * @param score current score * @param lives current lives */ public Game(int score, int lives) { this.score = score; this.lives = lives; } /** * Launch point * @param fullscreen boolean value, set to true to run in fullscreen mode */ public void run(boolean fullscreen) { this.fullscreen = fullscreen; try { init(); countdown(); while (!done) { mainloop(); render(); Display.update(); Display.sync(60); } cleanup(); } catch (Exception e) { e.printStackTrace(); System.exit(0); } } /** * All updating is done here. Key and mouse polling as well as window closi ng and * custom updates, such as AI. */ private void mainloop() {

if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { pressed done = true; } if(Display.isCloseRequested()) { s closed done = true; } if(Keyboard.isKeyDown(Keyboard.KEY_F1) && !f1) { f1 = true; switchMode(); } if(!Keyboard.isKeyDown(Keyboard.KEY_F1)) { f1 = false; } if(Keyboard.isKeyDown(Keyboard.KEY_F2) && !f2) { f2 = true; //change screen size to 800x600 switchSize1(); } if(!Keyboard.isKeyDown(Keyboard.KEY_F2)) { f2 = false; } if(Keyboard.isKeyDown(Keyboard.KEY_F3) && !f3) { f3 = true; //change screen size to 1200x1024 switchSize2(); } if(!Keyboard.isKeyDown(Keyboard.KEY_F3)) { f3 = false; } if(Keyboard.isKeyDown(Keyboard.KEY_F4) && !f4) { f3 = true; //classic game view gameType =1; } if(!Keyboard.isKeyDown(Keyboard.KEY_F4)) { f3 = false; } if(Keyboard.isKeyDown(Keyboard.KEY_F5) && !f5) { f3 = true; //dynamic game view gameType=2; } if(!Keyboard.isKeyDown(Keyboard.KEY_F5)) { f3 = false; } if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {

// Exit if Escape is

// Exit if window i

//check if player can move to the left (i.e. cell to lef t is not a wall) if(grid.isLeftClear(5))

{ //make sure board is not tilted to far if(tipX>-17.5) { //not at limit, tilt more tipX-=0.1; } //set direction player is facing playerRot= -90.0f; //reset the y axis to avoid overlapping textures //also stops user from holding up/down + left/ri ght to confuse checks and move diagonal through walls playerX = grid.getLocation(5)[0]; //move player to the left playerY-=runSpeed; //check to see player has entered a new cell, if so update the grid //if playerY is a whole number then at a cell if(playerY<=grid.getLocation(5)[1]-1) { //know that teleport A is entered from left only so check if new cell is it if(grid.getCell(Math.round(playerX), Math.round( playerY))==3) { //at teleporter so place player outside teleporter B instead of normal action //place one cell to left of teleport B a s it is entered from right grid.placePlayer(grid.getLocation(4)[0], grid.getLocation(4)[1]-1); //also set playerX and player Y to new c ell playerX = grid.getLocation(5)[0]; playerY = grid.getLocation(5)[1]; }else if(grid.getCell(Math.round(playerX), Math. round(playerY))==11) // power pill { powerUp = true; powerUpTime = System.currentTimeMillis() ; lightAmbient[0] = 3.5f; GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (F loatBuffer)temp.asFloatBuffer().put(lightAmbient).flip()); // Setup The Ambient Light score+=50; }else if(grid.getCell(Math.round(playerX), Math. round(playerY))==2) // normal pill { score+=10; } //at normal cell so place player grid.placePlayer(Math.round(playerX), Math.round

(playerY)); } } } if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) { //check if the player can move to the right (i.e. cell to right is not a wall) if(grid.isRightClear(5)) { //make sure board is not tilted to far if(tipX<17.5) { tipX+=0.1; } //set direction player is facing playerRot= 90.0f; //reset the y axis to avoid overlapping textures //also stops user from holding up/down + left/right to c onfuse checks and move diagonal through walls playerX = grid.getLocation(5)[0]; //move player to the right playerY+=runSpeed; //check to see player has entered a new cell, if so upda te the grid //if playerY is a whole number then at a cell if(playerY>=grid.getLocation(5)[1]+1) { //know that teleport B is entered from right onl y so check if new cell is it if(grid.getCell(Math.round(playerX), Math.round( playerY))==4) { //at teleporter so place player outside teleporter A instead of normal action //place one cell to right of teleport A as it is entered from right grid.placePlayer(grid.getLocation(3)[0], grid.getLocation(3)[1]+1); //also set playerX and player Y to new c ell playerX = grid.getLocation(5)[0]; playerY = grid.getLocation(5)[1]; }else if(grid.getCell(Math.round(playerX), Math. round(playerY))==11) // power pill { powerUp = true; powerUpTime = System.currentTimeMillis() ;

lightAmbient[0] = 3.5f; GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (F loatBuffer)temp.asFloatBuffer().put(lightAmbient).flip()); // Setup The Ambient Light score+=50; }else if(grid.getCell(Math.round(playerX), Math. round(playerY))==2) // normal pill { score+=10; } //at a valid cell so set new cell grid.placePlayer(Math.round(playerX), Math.round (playerY)); } } } if(Keyboard.isKeyDown(Keyboard.KEY_UP)) { //check if the player can move up (i.e. cell above is not a wall ) if(grid.isUpClear(5)) { //make sure board is not tilted to far if(tipY>-17.5) { //not at limit, tilt more tipY-=0.1; } //set direction player is facing playerRot= 180.0f; //reset the x axis to avoid overlapping textures //also stops user from holding up/down + left/right to c onfuse checks and move diagonal through walls playerY = grid.getLocation(5)[1]; //move player up playerX-=runSpeed; //check to see player has entered a new cell, if so upda te the grid //if playerX is a whole number then at a cell if(playerX<=grid.getLocation(5)[0]-1) { if(grid.getCell(Math.round(playerX), Math.round( playerY))==11) // power pill { powerUp = true; powerUpTime = System.currentTimeMillis() ; lightAmbient[0] = 5.0f; GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (F loatBuffer)temp.asFloatBuffer().put(lightAmbient).flip()); // Setup The Ambient Light score+=50;

}else if(grid.getCell(Math.round(playerX), Math. round(playerY))==2) // normal pill { score+=10; } //at a valid cell so set new cell grid.placePlayer(Math.round(playerX), Math.round (playerY)); } } } if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { //check if the player can move up (i.e. cell above is not a wall ) if(grid.isDownClear(5)) { //make sure board is not tilted to far if(tipY<17.5) { tipY+=0.1; } //set direction player is facing playerRot= 0.0f; //reset the x axis to avoid overlapping textures //also stops user from holding up/down + left/right to c onfuse checks and move diagonal through walls playerY = grid.getLocation(5)[1]; //move player down playerX+=runSpeed; //check to see player has entered a new cell, if so upda te the grid //if playerX is a whole number then at a cell if(playerX>=grid.getLocation(5)[0]+1) { if(grid.getCell(Math.round(playerX), Math.round( playerY))==11) // power pill { powerUp = true; powerUpTime = System.currentTimeMillis() ; lightAmbient[0] = 3.5f; GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (F loatBuffer)temp.asFloatBuffer().put(lightAmbient).flip()); // Setup The Ambient Light score+=50; }else if(grid.getCell(Math.round(playerX), Math. round(playerY))==2) // normal pill { score+=10; }

//at a valid cell so set new cell grid.placePlayer(Math.round(playerX), Math.round (playerY)); } } } }

private void switchMode() { fullscreen = !fullscreen; try { System.out.println("gets here"); Display.setFullscreen(fullscreen); } catch(Exception e) { e.printStackTrace(); } } private void switchSize1() { try { Display.setDisplayMode(new DisplayMode(800,600)); } catch(Exception e) { e.printStackTrace(); }} private void switchSize2() { try { Display.setDisplayMode(new DisplayMode(1280,1024)); } catch(Exception e) { e.printStackTrace(); } } /** * For rendering all objects to the screen * @return boolean for success or not * @throws IOException * @throws FileNotFoundException */ private boolean render() throws FileNotFoundException{ GL11.glEnable(GL11.GL_LIGHTING); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT GL11.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer GL11.glLoadIdentity(); // Reset The Current Modelview Matrix //zoom out if(gameType==1) {

GL11.glDisable(GL11.GL_LIGHT2); GL11.glTranslatef(0.0f, 0.0f, -45.0f); GL11.glRotatef(tipX,0.0f, 0.1f,0.0f); GL11.glRotatef(tipY,0.1f, 0.0f,0.0f); GL11.glTranslatef(-15.0f, 15.0f, 0.0f); }else { //enable spotlight in zoom mode GL11.glEnable(GL11.GL_LIGHT2); GL11.glLight(GL11.GL_LIGHT2, GL11.GL_AMBIENT, (FloatBuffer)temp.asF loatBuffer().put(lightAmbient2).flip()); // Setup The Ambient Light GL11.glLight(GL11.GL_LIGHT2, GL11.GL_DIFFUSE, (FloatBuffer)temp.asF loatBuffer().put(lightDiffuse2).flip()); // Setup The Diffuse Light GL11.glLight(GL11.GL_LIGHT2, GL11.GL_POSITION,(FloatBuffer)temp.asF loatBuffer().put(lightPosition2).flip()); // Position The Light GL11.glLightf (GL11.GL_LIGHT2, GL11.GL_SPOT_CUTOFF, 10.0f); GL11.glTranslatef(-playerY, playerX, -10.0f); GL11.glRotatef(-9.0f,0.0f, 0.1f,0.0f); GL11.glRotatef(3.0f,0.1f, 0.0f,0.0f); } int gridWidth = grid.getWidth(); int gridHeight = grid.getHeight(); //set win criteria to true win = true; //cycle through each cell and create a cube where a wall is need ed for(int currentRow = 0; currentRow < gridWidth; currentRow++) { //for each row check column for(int currentColumn = 0; currentColumn < gridHeight; c urrentColumn++) { //start creation of current cell GL11.glPushMatrix(); //move position to current cell GL11.glTranslatef((float)currentRow, -(float)cur rentColumn, 0.0f); if(grid.getCell(currentColumn,currentRow)==1) { //wall found so create a wall wallTexture.bind(); GL11.glBegin(GL11.GL_QUADS); GL11.glNormal3f( 0.0f, 1.0f, 0.0 f); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 0.5f, 0.5f,-0.5

f); //top right of quad (top) GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-0.5f, 0.5f,-0.5 f); //top left of quad (top) GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-0.5f, 0.5f, 0.5 f); //bottom left of quad (top) GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 0.5f, 0.5f, 0.5 f); //bottom right of quad (top) GL11.glNormal3f( 0.0f,-1.0f, 0.0 f); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 0.5f,-0.5f, 0.5 f); //top right of quad (bottom) GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-0.5f,-0.5f, 0.5 f); //top left of quad (bottom) GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-0.5f,-0.5f,-0.5 f); //bottom left of quad (bottom) GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 0.5f,-0.5f,-0.5 f); //bottom right of quad (bottom) GL11.glNormal3f( 0.0f, 0.0f, 1.0 f); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 0.5f, 0.5f, 0.5 f); //top right of quad (front) GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-0.5f, 0.5f, 0.5 f); //top left of quad (front) GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-0.5f,-0.5f, 0.5 f); //bottom left of quad (front) GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 0.5f,-0.5f, 0.5 f); //bottom right of quad (front) GL11.glNormal3f( 0.0f, 0.0f,-1.0 f); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 0.5f,-0.5f,-0.5 f); //top right of quad (back) GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-0.5f,-0.5f,-0.5 f); //top left of quad (back) GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-0.5f, 0.5f,-0.5 f); //bottom left of quad (back) GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 0.5f, 0.5f,-0.5 f); //bottom right of quad (back) GL11.glNormal3f(-1.0f, 0.0f, 0.0 f); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-0.5f, 0.5f, 0.5

f); //top right of quad (left) GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-0.5f, 0.5f,-0.5 f); //top left of quad (left) GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-0.5f,-0.5f,-0.5 f); //bottom left of quad (left) GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-0.5f,-0.5f, 0.5 f); //bottom right of quad (left) GL11.glNormal3f( 1.0f, 0.0f, 0.0 f); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 0.5f, 0.5f,-0.5 f); //top right of quad (right) GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( 0.5f, 0.5f, 0.5 f); //top left of quad (right) GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( 0.5f,-0.5f, 0.5 f); //bottom left of quad (right) GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 0.5f,-0.5f,-0.5 f); //bottom right of quad (right) GL11.glEnd(); }else if(grid.getCell(currentColumn,currentRow)= =-1) { //empty cell }else { //all other cell types require at least a floor //create a floor tile floorTexture.bind(); GL11.glBegin(GL11.GL_QUADS); GL11.glNormal3f( 0.0f, 0.0f,1.0f ); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 0.5f,-0.5f,-0.5 f); //top right of quad (back) GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-0.5f,-0.5f,-0.5 f); //top left of quad (back) GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-0.5f, 0.5f,-0.5 f); //bottom left of quad (back) GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 0.5f, 0.5f,-0.5 f); //bottom right of quad (back) GL11.glEnd(); //may be objects above the floor so chec k for them //check if a pill is also present if so add above floor tile

if(grid.getCell(currentColumn,currentRow )==2) { pillTexture.bind(); sphere = new Sphere(); sphere.setNormals(GLU.GLU_SMOOTH ); sphere.setTextureFlag(true); sphere.draw(0.2f, 7, 7); //if a pill is drawn game has no t been won, change win to false win = false; } //check for power pill if(grid.getCell(currentColumn, currentRo w)==11) { pillTexture.bind(); sphere = new Sphere(); sphere.setNormals(GLU.GLU_SMOOTH ); sphere.setTextureFlag(true); sphere.draw(0.4f, 7, 7); //if power pill is drawn game ha s not been won, change win to false win = false; } } GL11.glPopMatrix(); } } //check power up timer if((System.currentTimeMillis()-powerUpTime)/1000f > 15) //if ela psed time is bigger than 15 { //power up expired, set to false powerUp = false; lightAmbient[0] = 0.5f; GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp. asFloatBuffer().put(lightAmbient).flip()); // Setup The Ambient Lig ht } //draw player //translate to posistion of player GL11.glPushMatrix(); GL11.glTranslatef(playerY, -playerX, 0.0f); GL11.glRotatef(90f, 1.0f, 0.0f, 0.0f); GL11.glRotatef(playerRot, 0.0f, 1.0f, 0.0f); GL11.glScalef(0.15f, 0.15f, 0.15f); player.render();

GL11.glPopMatrix(); //draw zombie1 GL11.glPushMatrix(); GL11.glTranslatef(zombie1.getY(), -zombie1.getX(), 0.0f); GL11.glRotatef(90f, 1.0f, 0.0f, 0.0f); GL11.glRotatef(zombie1.getRot(), 0.0f, 1.0f, 0.0f); GL11.glScalef(0.15f, 0.15f, 0.15f); zombie1.render(); GL11.glPopMatrix(); collision(zombie1, 6); zombie1.move(grid,6,zombieXStart,zombieYStart); //draw zombie2 GL11.glPushMatrix(); GL11.glTranslatef(zombie2.getY(), -zombie2.getX(), 0.0f); GL11.glRotatef(90f, 1.0f, 0.0f, 0.0f); GL11.glRotatef(zombie2.getRot(), 0.0f, 1.0f, 0.0f); GL11.glScalef(0.15f, 0.15f, 0.15f); zombie2.render(); GL11.glPopMatrix(); collision(zombie2,7); zombie2.move(grid,7,zombieXStart,zombieYStart); //draw zombie3 GL11.glPushMatrix(); GL11.glTranslatef(zombie3.getY(), -zombie3.getX(), 0.0f); GL11.glRotatef(90f, 1.0f, 0.0f, 0.0f); GL11.glRotatef(zombie3.getRot(), 0.0f, 1.0f, 0.0f); GL11.glScalef(0.15f, 0.15f, 0.15f); zombie3.render(); GL11.glPopMatrix(); collision(zombie3,8); zombie3.move(grid,8,zombieXStart,zombieYStart); //draw zombie4 GL11.glPushMatrix(); GL11.glTranslatef(zombie4.getY(), -zombie4.getX(), 0.0f); GL11.glRotatef(90f, 1.0f, 0.0f, 0.0f); GL11.glRotatef(zombie4.getRot(), 0.0f, 1.0f, 0.0f); GL11.glScalef(0.15f, 0.15f, 0.15f); zombie4.render(); GL11.glPopMatrix(); collision(zombie4,9); zombie4.move(grid,9, zombieXStart,zombieYStart); statusBar(); //check if the game has been won if(win) { backgroundMusic.stop(); new LevelComplete(score, lives).run(fullscreen); } return true;

} /** * Do all initilization code here. Including Keyboard and OpenGL * @throws Exception Passes any exceptions up to the main loop to be handled */ private void init() throws Exception { initGL(); } /** * Initialize OpenGL * @throws IOException * @throws FileNotFoundException * */ private void initGL() throws FileNotFoundException, IOException { //setup light 1 temp = ByteBuffer.allocateDirect(16); temp.order(ByteOrder.nativeOrder()); GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatB uffer().put(lightAmbient).flip()); // Setup The Ambient Light GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatB uffer().put(lightDiffuse).flip()); // Setup The Diffuse Light GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION,(FloatBuffer)temp.asFloatB uffer().put(lightPosition).flip()); // Position The Light GL11.glEnable(GL11.GL_LIGHT1); // Enable Light One //load texture wallTexture = TextureLoader.getTexture("PNG", new FileInputStream("Brick .png")); pillTexture = TextureLoader.getTexture("PNG", new FileInputStream("Pill. png")); floorTexture = TextureLoader.getTexture("PNG", new FileInputStream("Gras s.png")); lifeTexture = TextureLoader.getTexture("PNG", new FileInputStream("FaceF ront.png")); powerUpTexture = TextureLoader.getTexture("PNG", new FileInputStream("Po werUp.png")); //load background music //music from playonloop.com // backgroundMusic = AudioLoader.getAudio("WAV", new FileInputStream("bac kgroundMusic.wav")); // backgroundMusic.playAsMusic(1.0f, 1.0f, true); //create grid grid = new Grid("grid.txt"); // // // // // // // // blank tile empty tile wall = 1 pill = 2 teleporter teleporter pacman = 5 zombie 1 = (no floor) = -1 (floor only) = 0 A = 3 B = 4 6

// // // // //

zombie 2 = 7 zombie 3 = 8 zombie 4 = 9 zombie maze spawn = 10 power-up = 11 playerX = grid.getLocation(5)[0]; playerY = grid.getLocation(5)[1]; playerXStart = Math.round(playerX); playerYStart = Math.round(playerY); playerRot = 0.0f; zombieXStart = grid.getLocation(10)[0]; zombieYStart = grid.getLocation(10)[1]; player = new Player(); zombie1 = new Zombie(grid.getLocation(6)[0],grid.getLocation(6)[

1], 0.0f, 2,1); zombie2 = new Zombie(grid.getLocation(7)[0],grid.getLocation(7)[ 1], 0.0f, 2,2); zombie3 = new Zombie(grid.getLocation(8)[0],grid.getLocation(8)[ 1], 0.0f, 4,3); zombie4 = new Zombie(grid.getLocation(9)[0],grid.getLocation(9)[ 1], 0.0f, 4,4); //lives = 3; powerUp = false; gameType = 1; win = false; runSpeed= 0.1f; startTime = System.currentTimeMillis(); fonts = new TextGenerator(); } /** * Cleanup all the resources. * */ private void cleanup() { Display.destroy(); } /** * Check for collision with zombie */ private void collision(Zombie zombie, int zombieNo) { if(Math.round(playerX)==Math.round(zombie.getX()) && Math.round(playerY) ==Math.round(zombie.getY())) { //pacman and zombie have collided if(powerUp) // power up active - pacman eats ghost { //reset ghost to start posistion zombie.setX(grid.getLocation(zombieNo)[0]);

zombie.setY(grid.getLocation(zombieNo)[1]); score+=200; }else //not active - ghost eats pacman { //reset pacman to start posistion grid.placePlayer(playerXStart, playerYStart); //also set playerX and player Y to new cell playerX = grid.getLocation(5)[0]; playerY = grid.getLocation(5)[1]; lives-=1; //check amount of lives if(lives==0) { //game over backgroundMusic.stop(); new GameOver(score,lives).run(fullscreen ); } } } } /** * Draw status bar, shows score, lives and power up status */ private void statusBar() { GL11.glDisable(GL11.GL_LIGHTING); GL11.glLoadIdentity(); // Reset The Current Modelview Matrix //move to the right hand side of screen GL11.glTranslatef(1.75f, 1.93f, -5.0f); //draw a rectangle to act as main bar GL11.glPushMatrix(); GL11.glColor3f(0.0f,0.0f,0.0f); GL11.glBegin(GL11.GL_QUADS); GL11.glVertex3f( 0.8f, 0.1f, 0.1f); //top right of quad (front) GL11.glVertex3f(-0.1f, 0.1f, 0.1f); //top left of quad ( front) GL11.glVertex3f(-0.1f,-4.0f, 0.1f); //bottom left of qua d (front) GL11.glVertex3f( 0.8f,-4.0f, 0.1f); //bottom right of qu ad (front) GL11.glEnd(); GL11.glPopMatrix(); //move down and right GL11.glTranslatef(-0.2f, -0.35f, 1.0f); //draw text - "Score" GL11.glPushMatrix(); GL11.glColor3f(1.0f,1.0f,1.0f); GL11.glScalef(2.0f, 2.0f, 2.0f); fonts.glPrint("Score"); GL11.glPopMatrix();

//move down GL11.glTranslatef(0.0f,-0.175f,0.0f); //show the score - auto updates GL11.glPushMatrix(); GL11.glColor3f(1.0f,1.0f,1.0f); GL11.glScalef(1.5f, 1.5f, 1.5f); fonts.glPrint(Integer.toString(score)); GL11.glPopMatrix(); //move down GL11.glTranslatef(0.0f,-0.175f,0.0f); //write text - lives GL11.glPushMatrix(); GL11.glColor3f(1.0f,1.0f,1.0f); GL11.glScalef(2.0f, 2.0f, 2.0f); fonts.glPrint("Lives"); GL11.glPopMatrix(); //move down and left GL11.glTranslatef(-0.03f,-0.225f,0.0f); //draw correct amount of lives GL11.glPushMatrix(); for(int i=0; i<lives; i++) { //draw life icon lifeTexture.bind(); GL11.glBegin(GL11.GL_QUADS); GL11.glNormal3f( 0.0f, 0.0f, 1.0f); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 0.075f, 0.075f, right of quad (front) GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-0.075f, 0.075f, left of quad (front) GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-0.075f,-0.075f, om left of quad (front) GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 0.075f,-0.075f, om right of quad (front) GL11.glEnd(); //shift right GL11.glTranslatef(0.2f,0.0f,0.0f); } GL11.glPopMatrix(); //check if power pill is active if(powerUp) { //move down GL11.glTranslatef(0.0f,-0.65f,0.0f); GL11.glPushMatrix(); GL11.glColor3f(1.0f,0.0f,0.0f);

0.075f); //top 0.075f); //top 0.075f); //bott 0.075f); //bott

GL11.glScalef(2.0f, 2.0f, 2.0f); fonts.glPrint("Rage"); GL11.glPopMatrix(); //move down GL11.glTranslatef(0.0f,-0.175f,0.0f); GL11.glPushMatrix(); GL11.glColor3f(1.0f,0.0f,0.0f); GL11.glScalef(2.0f, 2.0f, 2.0f); fonts.glPrint("Mode"); GL11.glPopMatrix(); //move down GL11.glTranslatef(0.1f,-0.4f,0.0f); GL11.glColor3f(1.0f,1.0f,1.0f); //active so draw powerup indicator powerUpTexture.bind(); GL11.glBegin(GL11.GL_QUADS); GL11.glNormal3f( 0.0f, 0.0f, 1.0f); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 0.28f, 0.28f, 0.28f); ht of quad (front) GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-0.28f, 0.28f, 0.28f); t of quad (front) GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-0.28f,-0.28f, 0.28f); left of quad (front) GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 0.28f,-0.28f, 0.28f); right of quad (front) GL11.glEnd(); } } /** * Countsdown 3,2,1 before starting the game */ private void countdown() { boolean countdownDone = false; //move to the right hand side of screen GL11.glTranslatef(0.0f, 0.0f, -5.0f); while(!countdownDone) { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT GL11.GL_DEPTH_BUFFER_BIT ); // Clear The Screen And The Depth Buffer GL11.glLoadIdentity(); // Reset The Current Modelview Matrix //move to the right hand side of screen GL11.glTranslatef(0.0f, 0.0f, -5.0f); //Display.update(); //Display.sync(60); if(System.currentTimeMillis()-startTime <= 1000) //if less than one second {

//top rig //top lef //bottom //bottom

//show 3 GL11.glPushMatrix(); GL11.glColor3f(1.0f,1.0f,1.0f); GL11.glScalef(10.0f, 10.0f, 10.0f); fonts.glPrint("3"); GL11.glPopMatrix(); }else if(System.currentTimeMillis()-startTime > 1000 && System.c urrentTimeMillis()-startTime <= 2000) // if 1-2 seconds { //show 2 GL11.glPushMatrix(); GL11.glColor3f(1.0f,1.0f,1.0f); GL11.glScalef(10.0f, 10.0f, 10.0f); fonts.glPrint("2"); GL11.glPopMatrix(); }else if(System.currentTimeMillis()-startTime > 2000 && System.c urrentTimeMillis()-startTime <= 3000) // if 2-3 seconds { //show 1 GL11.glPushMatrix(); GL11.glColor3f(1.0f,1.0f,1.0f); GL11.glScalef(10.0f, 10.0f, 10.0f); fonts.glPrint("1"); GL11.glPopMatrix(); }else { System.out.println("gets here 4"); countdownDone = true; } Display.update(); Display.sync(60); } }

Вам также может понравиться