📄 dummygame.java
字号:
import java.util.*;import java.awt.*;import gaming.*;import cs101.lang.*;import javax.swing.JOptionPane;public class DummyGame implements GameKeyControllable,Animate{ GriddedScreen theWorld; WalledSquare[][] walls; Food[][] food; static Pacman pacman; protected AnimatorThread mover; protected int[][] flag = new int[15][15]; protected int superfood = 0; protected int MaxStep;//define the restricted steps in super state Ghost[] ghost = new Ghost[4]; protected int All_Food_Number = 0;//the number of all the foods on the screen protected int life = 3;//the whole number lives of the pacman protected boolean Pause; public DummyGame(String filename) { GameFrame frame = new GameFrame(this, "Lab2_Pacman_Game"); frame.loadGame(filename); this.theWorld = frame.getScreen(); food=new Food[theWorld.getGridHeight()][theWorld.getGridHeight()]; } public void start(){ System.out.println("Game Started,press w a s d to control the pacman!"); JOptionPane.showMessageDialog(null,"WELCOME TO THE PACMAN WORLD! CLICK OK TO START THE GAME."); this.mover=new AnimatorThread(this); this.mover.startExecution(); superfood = 0; life = 3; All_Food_Number = 0; Pause = true; flag[7][7] = 1; flag[14][0] = 1; flag[0][14] = 1; flag[14][14] = 1; flag[0][0] = 1; pacman=new Pacman(7,7);//set the initial pacman location in the centre of the screen theWorld.addScreenObject(pacman); //set the initial position of the four ghost,the four corner respectively ghost[0] = new RealGhost(14,0,pacman); ghost[1] = new RealGhost(0,0,pacman); ghost[2] = new RealGhost(0,14,pacman); ghost[3] = new RealGhost(14,14,pacman); for(int i=0;i<4;i++) theWorld.addScreenObject(ghost[i]);//add the four ghosts to screen /* * set the superfoods onto the screen with the total number of 4 */ do{ int rand1 = (int)(Math.random()*9); int rand2 = (int)(Math.random()*9); walls = this.theWorld.getWalls(); if((!walls[rand1][rand2].hasLeftWall()||!walls[rand1][rand2].hasRightWall()|| !walls[rand1][rand2].hasLowerWall()||!walls[rand1][rand2].hasUpperWall()) &&flag[rand1][rand2] == 0){ food[rand1][rand2] = new Food(rand1,rand2,Color.red,1); theWorld.addScreenObject(food[rand1][rand2]); this.superfood++; this.All_Food_Number++;//increase the total number on the screen flag[rand1][rand2] = 1; } }while(this.superfood != 4); /* * set the normal foods onto the srcreen */ for(int i = 0; i <this.theWorld.getGridHeight();i++) { for(int j = 0; j <this.theWorld.getGridWidth();j++){ walls = this.theWorld.getWalls(); if((!walls[i][j].hasLeftWall()||!walls[i][j].hasRightWall()|| !walls[i][j].hasLowerWall()||!walls[i][j].hasUpperWall()) &&flag[i][j] == 0) {//if the position is available and not put object before then set the normal food this.All_Food_Number++; food[i][j]=new Food(i,j); theWorld.addScreenObject(food[i][j]); } } } theWorld.repaint(); } public void eat(Ghost g) { if(pacman.myColor == Color.red){ g.setX(0); g.setY(14); }//if the ghost was eaten by the pacman,then produce another at the left-lower corner else { if(life != 1){ pacman.setX(7); pacman.setY(7); life--; JOptionPane.showMessageDialog(null,"YOU LOST ONE LIFE! THERE LEFT "+life+" LIFES OF YOU!"); }//if remains live,then start from the center else { JOptionPane.showMessageDialog(null,"YOU LOST THE GAME!"); this.stop(); }//then step out from the program } } public void act(){ for(int i = 0;i<4;i++){ ghost[i].move();//move the ghost in turns if((ghost[i].x) == pacman.getX()&&(ghost[i].y) == pacman.getY()){ eat(ghost[i]);//if the positon of pacman and ghost meet together,then invoke the eat function } } theWorld.repaint(); } //stop the whole game public void stop(){ System.out.println("Stop the game"); this.mover.stopExecution();//terminate the thread theWorld.removeScreenObject(pacman);// for(int i=0;i<15;i++) for(int j=0;j<15;j++){ flag[i][j] = 0; if(food[i][j]!=null)theWorld.removeScreenObject(food[i][j]);} for(int i=0;i<4;i++) theWorld.removeScreenObject(ghost[i]);//remove off the four ghost theWorld.repaint(); } public void reset(){ this.stop(); this.start(); } public void pause(){ this.mover.suspendExecution(); Pause = false; System.out.println("Pause the game"); } public void unpause(){ this.mover.resumeExecution(); Pause = true; System.out.println("Unpause the game"); } public void actOnKey(char key) { if(Pause == true) pacman.actOnKey(key); if(pacman.getColor() == Color.red) this.MaxStep++; if(this.MaxStep == 10) pacman.myColor=Color.white;//the super status was restricted in 10 steps if(food[pacman.getX()][pacman.getY()].shape == 1) { pacman.myColor=Color.red; this.MaxStep=0; }//if the pacman ate another superfood,then it turned to red and clear the restricted MaxStep at the same time if(food[pacman.getX()][pacman.getY()]!=null){ theWorld.removeScreenObject(food[pacman.getX()][pacman.getY()]); food[pacman.getX()][pacman.getY()]=null; this.All_Food_Number--; } if(this.All_Food_Number == 0) { this.stop(); JOptionPane.showMessageDialog(null,"Congratulation!You have won the game!"); }//if the pacman ate all the food then he win the game! theWorld.repaint(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -