import javax.swing.*;import java.awt.*;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.ArrayList;import java.util.Random;public class Game extends JApplet implements Runnable, KeyListener { //String for the JOptionpane.showInputDialog String Name,GoodBye; int UserPick;// Set show input dialog // MediaTrack with help us to create an applet with lots of images MediaTracker track; //set Thread Thread thread; int width, height; // for drawing walls ArrayList walls; //setting walls as our background Image backgroundBuf, screenBuf; //for setting the background Graphics2D background; boolean booleanBuildWalls=false;// will help me to build new walls for the second level. boolean RemoveGoodBye = true;// help us to remove the word Good bye when Yes get selected //set your score that you will accumulate during the game. JLabel Scores; double finalScore=0; //player and drod(fireball) Fun player; Fun fairy; //set Money as collecting points ArrayList coinsMoney; //set “exit” for the door and “flowers” for the final prize. Little exit,flowers; //set the value of the Money image int MoneyPoints=0; // image of Money and final prize. Image imgAsCoinMoney,imgflowers; Random random; public void init(){ //set up confirmation of the player: int UserPick = JOptionPane.showConfirmDialog(this, “QUESTION TO ASK”, “MESSAGE OF THE HEAD”, UserPick = JOptionPane.showConfirmDialog(this, “would you like to play?”, “Hello”, JOptionPane.YES_NO_OPTION); //Set up conditions if (UserPick == 0) // if yes is selectedF { //I DRAWN THE NAME IN PAINT METHOD Name = JOptionPane.showInputDialog(this, “PUT IN YOUR NAME”); Scores = new JLabel(“SCORES: ” + finalScore); coinsMoney = new ArrayList<>(); booleanBuildWalls=true; RemoveGoodBye=false; width = this.getWidth(); height = this.getHeight(); // set WALLS walls = new ArrayList(); if (booleanBuildWalls == true) { builwalls(); } loadImages();//set up playerImages (the player will look like he is walking) //set player locations player.setLocation(49, 49); //set fairy location. fairy.setLocation(width – 69, height – 69); random = new Random(); setCoins(); addKeyListener(this);// will read the keyboard in oder to make the player move } else if (UserPick == 1) //IF NO get selected { RemoveGoodBye=true; GoodBye = new String(“GOOD BYE”); System.out.println(“GOOD BYE”); } } //reset the game to beginning. public void RestartGame() { UserPick = JOptionPane.showConfirmDialog(this, “Do you want play again?”, “GREAT JOB! “+Name+” SCORE =”+finalScore, JOptionPane.YES_NO_OPTION); //Set up conditions if (UserPick == 0) // if yes is selected { // “NAME” IS DRAW IN PAINT METHOD finalScore=0; Name = JOptionPane.showInputDialog(this, “ENTER YOUR NAME”); Scores = new JLabel(“SCORES: ” + finalScore); coinsMoney = new ArrayList<>(); booleanBuildWalls=true; RemoveGoodBye=false; width = this.getWidth(); height = this.getHeight(); // set WALLS walls = new ArrayList(); // rebuild and repaint the walls builwalls(); paintWalls(); loadImages();//set up playerImage //set player locations player.setLocation(49, 49); fairy.setLocation(width – 69, height – 69); random = new Random(); setCoins(); addKeyListener(this);// will read the keyboard in oder to make the splayer move } else if (UserPick == 1) //IF NO get selected { removeAll(); RemoveGoodBye=true; GoodBye = new String(“GOOD BYE”); System.out.println(“GOOD BYE “+Name); System.out.println(“Your Score =” +finalScore); try { thread.sleep(2000);//help to make the game run during 3000 milisecond before getting close } catch (InterruptedException e) { e.printStackTrace(); } System.exit(0); } } public void start()//help set the thread { try{ track.waitForAll(); }catch (Exception e){}; if( thread == null) thread = new Thread(this); thread.start(); setFocusable( true); } public void stop(){ if(thread!= null) thread.interrupt(); thread=null; } //will help us to draw the Name of the player,his score, paint the walls and our message of goodbye. public void paint(Graphics g) { if (RemoveGoodBye==true) // help to remove the word Goodbye before restart the game. { g.drawString(” ” + GoodBye, this.getWidth() / 2 – 50, this.getHeight() / 2 – 50); } else if(booleanBuildWalls==false){paintWalls();} g.drawImage(screenBuf,0,0,this); g.drawString(“Name: “+Name,20,20); g.drawString(“Score :”+finalScore,300,50); } public void update(Graphics g) { paint(g); } public void builwalls(){ int wallThickness = 10; walls.add( new Rectangle(0,0,width, wallThickness)); //top walls.add(new Rectangle(0,0,wallThickness, height)); //left walls.add(new Rectangle(width-wallThickness,0,wallThickness,height)); walls.add(new Rectangle(0, height-wallThickness, width, wallThickness)); walls.add(new Rectangle(0, 100,100, wallThickness)); //left walls.add(new Rectangle(50, height-wallThickness-120,wallThickness,70)); //horizontal top walls.add(new Rectangle(50, height-wallThickness-120,width-150,wallThickness)); walls.add(new Rectangle(50, height-wallThickness-50,width-120,wallThickness)); walls.add(new Rectangle(200, height-230,wallThickness,130)); } public void SetLevel2()// build walls for the second levels. { int wallThickness=15; walls=new ArrayList<>(); walls.add( new Rectangle(0,0,width, wallThickness)); //top walls.add(new Rectangle(0,0,wallThickness, height)); //left walls.add(new Rectangle(width-wallThickness,0,wallThickness,height)); walls.add(new Rectangle(0, height-wallThickness, width, wallThickness)); walls.add(new Rectangle(34, 150,100, wallThickness)); //First wall horizontal walls.add(new Rectangle(200, 0,wallThickness,200)); // second wall vertical walls.add(new Rectangle(50, height-wallThickness-120,wallThickness,70)); //horizontal top walls.add(new Rectangle(width/2, height-wallThickness-120,150,wallThickness)); walls.add(new Rectangle(50, height-wallThickness-50,width-100,wallThickness-10)); exit.setVisible(false);// make the door visible setCoins(); flowers.setVisible(false);// make the final prize visible } public void paintWalls()// set wallls look like rectangle { backgroundBuf = createImage(width,height); background = (Graphics2D)backgroundBuf.getGraphics(); background.setColor(Color.LIGHT_GRAY); background.fillRect(0,0, width,height); background.setColor(Color.orange); for ( int i=0; i= 10000) flowers.setVisible(true); flowers.paintComponent(gapplet); moveFairy(); flowers.paintComponent(gapplet); player.paintComponent(gapplet); for (Fun f : coinsMoney) { f.paintComponent(gapplet); } if (flowers.isVisible() && player.getDimensions().intersects(flowers.getDimensions())) { booleanBuildWalls = false; gapplet.setFont(new Font(“Serif”, Font.BOLD, 42)); gapplet.setColor(Color.BLUE); gapplet.drawString(“YOU WIN”, 60, 120); exit.setVisible(false); System.out.println(“YOU WIN!”); RestartGame();// restart the game at the beguining } repaint(); try { Thread.sleep(10); } catch (Exception ex) { stop(); } } } } //fairy move by using random method public void moveFairy() { boolean badLocation = true; int numTries = 10; while(badLocation && numTries >0) { int newx = random.nextInt(fairy.speed); int newy = random.nextInt(fairy.speed); if ( random.nextInt(2) == 0) newx = -1*newx; if( random.nextInt(2) == 0) newy = -1*newy; fairy.setLocation(fairy.x+newx, fairy.y+newy); if (checkCollisionWalls(fairy.getDimensions())) fairy.undoMove(); else badLocation = false; numTries = numTries -1; } } @Override public void keyTyped(KeyEvent e) {} @Override public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); if (code == KeyEvent.VK_UP || code == KeyEvent.VK_8) player.moveUp(); else if( code == KeyEvent.VK_RIGHT) player.moveRight(); else if( code == KeyEvent.VK_LEFT) player.moveLeft(); else if ( code == KeyEvent.VK_DOWN || code == KeyEvent.VK_2) player.moveDown(); if (checkCollisionWalls( player.getDimensions())) player.undoMove(); //the conditions of checkcollisionMoney is over there because he is going to help //the progam to always check if there are any collision between the player and the coins //each time the keyboard is press. if(checkCollisionCoins()){} } public boolean checkCollisionCoins() { boolean coinIntersect=false; int index=0; for(Fun f:coinsMoney)// catch each coins { int cost=500; MoneyPoints =cost; if(player.getDimensions().intersects(f.getDimensions())){coinIntersect=true;index=coinsMoney.indexOf(f);} } if(coinIntersect==true){coinsMoney.remove(index); finalScore=finalScore+MoneyPoints;} return coinIntersect; } @Override public void keyReleased(KeyEvent e) { player.stopMoving(); }}

x

Hi!
I'm Erica!

Would you like to get a custom essay? How about receiving a customized one?

Check it out