⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a4jpanel.java

📁 一个基于Java的小游戏
💻 JAVA
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class A4JPanel extends JPanel implements ActionListener, KeyListener, MouseListener{

	private Rectangle boundary = new Rectangle(10, 10, 450, 450);

	private Wagon wagon;
	private Enemy villain;
	private Timer t;
	private boolean showWelcome;
	private Gold gold;
	private int numberOfGold;
	private int numberOfGoldForVillain; // Counter the number of gold that the villain picked up.
	private Enemy[] thieves;
	private boolean restart = false;

	public A4JPanel() {
		setBackground(Color.white);
		showWelcome = true;
		wagon = new Wagon((boundary.x+boundary.width)/2,(boundary.y+boundary.height)/2);
		villain = new Enemy(20,20,true);
		gold = new Gold(138,255);
		t = new Timer(25,this);
		addMouseListener(this);
		addKeyListener(this);
		villain.setGoldPosition(gold.getCentreX(), gold.getCentreY());
		thieves = new Enemy[2000];

	}

  	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if(showWelcome){
			showWelcome(g);
		}else{
			g.drawRect(boundary.x, boundary.y, boundary.width, boundary.height);
			g.drawString("Number of gold you\'ve got : " + numberOfGold,260,30);
			g.drawString("Number of gold Villain has got : " + numberOfGoldForVillain,260,50);
			wagon.draw(g);
			villain.draw(g);
			gold.draw(g);
			if(isWagonGotGold()){
				setNewGold();
				gold.draw(g);
			}
			if(isVillainGotGold()){
				creatNewThief();
				setNewGold();
				gold.draw(g);
			}
			for(int i = 0; i < numberOfGoldForVillain; i++){
				thieves[i].draw(g);
			}

			if(isIntersectedWithVillain() || isIntersectedWithThieves()){
				t.stop();
				g.setColor(Color.RED);
				g.setFont(new Font("Arial",Font.BOLD,50));
				g.drawString("GAME OVER!", 80,250);
			}
		}
  	}

  	//The welcome message (front page) of the game.
  	private void showWelcome(Graphics g){
		g.drawRect(boundary.x, boundary.y, boundary.width, boundary.height);
		g.setFont(new Font("Arial", Font.BOLD + Font.ITALIC, 50));
		g.setColor(Color.BLUE);
		g.drawString("GOLD RUSH !",70,100);
		g.setColor(Color.GREEN);
		g.setFont(new Font("Arial",Font.BOLD,18));
		g.drawString("USE ARROW KEYS: Move wagon",90,150);
		g.setColor(Color.ORANGE);
		g.fillRect(20,195,430,70);
		g.setColor(Color.WHITE);
		g.fillRect(25,200,420,60);
		g.setColor(Color.BLACK);
		g.setFont(new Font("Arial",Font.ITALIC + Font.BOLD,15));
		g.drawString("Try to collect as more gold as possible, but be carefull",50,220);
		g.drawString("             to avoid the villain and the thieves...",50,250);
		g.drawString("Please press the mouse button to start this game...",40,400);
	}

  	public void actionPerformed(ActionEvent e){
		wagon.move(boundary);
		villain.move(boundary);
		for(int i = 0; i<numberOfGoldForVillain; i++){
			thieves[i].move(boundary);
		}
		repaint();
	}
  	public void mousePressed(MouseEvent e){
		showWelcome = false;
		requestFocus();
		repaint();
	}
  	public void keyPressed(KeyEvent e){
		if(! isIntersectedWithVillain() && ! isIntersectedWithThieves()){
			t.start();
		}
		if(e.getKeyCode() == KeyEvent.VK_UP){
			wagon.faceUp();
		}
		if(e.getKeyCode() == KeyEvent.VK_DOWN){
			wagon.faceDown();
		}
		if(e.getKeyCode() == KeyEvent.VK_LEFT){
			wagon.faceLeft();
		}
		if(e.getKeyCode() == KeyEvent.VK_RIGHT){
			wagon.faceRight();
		}
	}

	//Test if the gold is contained by the wagon rectangle.
	private boolean isWagonGotGold(){
		Point positionOfGold;
		Rectangle wagonBox;
		positionOfGold = new Point(gold.getCentreX(), gold.getCentreY());
		wagonBox = new Rectangle(wagon.getX(), wagon.getY(), wagon.getSize()*2+1, wagon.getSize()*2+1);
		if(wagonBox.contains(positionOfGold)){
			numberOfGold ++;
			return true;
		}
		return false;
	}

	//Test if the gold is contained by the villain rectangle.
	private boolean isVillainGotGold(){
		Point positionOfGold;
		Rectangle villainBox;
		positionOfGold = new Point(gold.getCentreX(), gold.getCentreY());
		villainBox = new Rectangle(villain.getX(), villain.getY(), villain.getSize()*2+1, villain.getSize()*2+1);
		if(villainBox.contains(positionOfGold)){
			numberOfGoldForVillain ++;
			return true;
		}
		return false;
	}

	//Set the new position and gold.
	private void setNewGold(){
		gold = new Gold((int)(Math.random() * 440 + 10),(int)(Math.random() * 440 + 10));
		villain.setGoldPosition(gold.getCentreX(), gold.getCentreY());
	}


	//Test if the enemies and the wagon have an intersection.
	private boolean isIntersectedWithVillain(){
		Rectangle wagonBox;
		Rectangle villainBox;
		wagonBox = new Rectangle(wagon.getX(),wagon.getY(),wagon.getSize()*2,wagon.getSize()*2);
		villainBox = new Rectangle(villain.getX(), villain.getY(), villain.getSize()*2,villain.getSize()*2);
		if(villainBox.intersects(wagonBox)){
			return true;
		}
		return false;
	}
	private boolean isIntersectedWithThieves(){
		Rectangle wagonBox;
		Rectangle[] thievesBox = new Rectangle[thieves.length];
		wagonBox = new Rectangle(wagon.getX(),wagon.getY(),wagon.getSize()*2,wagon.getSize()*2);
		for(int i = 0; i < numberOfGoldForVillain; i++){
			thievesBox[i] = new Rectangle(thieves[i].getX(),thieves[i].getY(),thieves[i].getSize()*2,thieves[i].getSize()*2);
			if(thievesBox[i].intersects(wagonBox)){
				return true;
			}
		}
		return false;
	}

	//Creat a new thief if the villain pick up a gold every time.
	private void creatNewThief(){
		thieves[numberOfGoldForVillain - 1] = new Enemy(villain.getX(),villain.getY(), false);

	}
//---------------------------------------------------------------------------------------------------
  	public void mouseClicked(MouseEvent e){}
  	public void mouseEntered(MouseEvent e){}
  	public void mouseExited(MouseEvent e){}
  	public void mouseReleased(MouseEvent e){}

  	public void keyReleased(KeyEvent e){}
  	public void keyTyped(KeyEvent e){}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -