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

📄 packman.java

📁 Java源代码-主要用于浏览器聊天工具端口扫描
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.applet.*;

public class Packman extends JApplet implements KeyListener, ActionListener{

	private Ticker t;
	private Image offScreenImage;
	//定义吃豆者
	private Player player;
	//定义四个敌人
	private Enemy Red, Cyan, Pink, Orange;
	//健康值与分数
	private int health, Scores;
	//游戏是否结束
	private boolean gameover;
	//游戏是否开始
	private boolean gameStarted;
	private int alarmTime;
	private boolean alarm;

	private Fruit fruit;//定义水果

	private boolean UP_TYPED, DOWN_TYPED, LEFT_TYPED, RIGHT_TYPED;

	//定义墙
	private Image[] WALL;
	private AudioClip[] SOUND;

	private Wall[] wall;
	//定义豆子的数组
	private Gold[] gold;

	private int gameFlow;

	private boolean levelComplete;

	/**
	 * Applet初始化
	 */
	public void init(){
		
		player = new Player();
		health = 100;
		
		//初始化敌人
		Red = new Enemy(210, 189, 1, false, 0);
		Cyan = new Enemy(210, 231, 2, true, 0);
		Pink = new Enemy(220, 231, 3, true, 66);
		Orange = new Enemy(200, 231, 4, true, 132);

		//添加键盘事件侦听
		addKeyListener(this);
		requestFocus();

		t = new Ticker(30);
		t.addActionListener(this);

		gold = golds();

		wall = walls();
		WALL = new Image[47];
		for(int i = 0; i < 47; i++)
			WALL[i] = getImage(getDocumentBase(), "Image\\" + "Wall" + (i + 1) + ".jpg");
		SOUND = new AudioClip[8];
		for(int i = 0; i < 8; i++)
			SOUND[i] = getAudioClip(getDocumentBase(), "Sound\\" + (i + 1) + ".au");

	}

	public void start (){
		if(t != null)
			t.start();
	}

	public void stop(){
		SOUND[2].stop();
		SOUND[5].stop();
		t.stop();
		t = null;
	}

	public void actionPerformed(ActionEvent e){
		if(gameStarted)
			gameFlow++;

		if(alarm)
			alarmTime++;

		if(alarm && alarmTime == 330 && !gameover){
			alarm = false;
			SOUND[2].stop();
			SOUND[5].loop();
		}

		if(gameFlow == 110 && !gameover)
			SOUND[5].loop();

		if(health <= 0 && !gameover){
			gameover = true;
			gameFlow = 0;
			Red.stop();
			Cyan.stop();
			Pink.stop();
			Orange.stop();
			player.stop();
			if(fruit != null)
				fruit = null;
			SOUND[5].stop();
			SOUND[2].stop();
		}

		if(gameover && gameFlow == 66){
			player.Dead();
			SOUND[3].play();
		}

		if(fruit != null && fruit.getAppearTime() >660)
			fruit = null;
		if(fruit == null && gameFlow != 0 && gameFlow%1980 ==0 && !gameover)
			fruit = new Fruit();
		
		levelComplete = true;
		for(int i = 0; i < gold.length; i++){
			if(gold[i] != null){
				levelComplete = false;
				break;
			}
		}

		if(levelComplete){
			gold = null;
			gold = golds();
		}
		if(fruit != null)
			fruit.move(wall);

		if(gameStarted){
			player.move(wall);
			Red.move(player.getxPos(), player.getyPos(), wall);
			Cyan.move(player.getxPos(), player.getyPos(), wall);
			Pink.move(player.getxPos(), player.getyPos(), wall);
			Orange.move(player.getxPos(), player.getyPos(), wall);
		}

		if(fruit != null){
			if((fruit.getBorder()).intersects(player.getBorder())){
				fruit = null;
				Scores+=75;
				health+=50;
				if(health > 100)
					health = 100;
				SOUND[1].play();
			}
		}

		for(int i = 0; i < gold.length; i++){
			//如果吃豆者吃的是小金豆
			if(gold[i] != null){
				if((gold[i].getBorder()).intersects(player.getBorder()) && !(gold[i].bigGold())){
					if(gameFlow > 110)
						SOUND[0].play();
					gold[i] = null;
					Scores+=10;
					break;
				}
			}
			if(gold[i] != null){
				if((gold[i].getBorder()).intersects(player.getBorder()) && gold[i].bigGold()){
					gold[i] = null;
					Red.Alarm(1);
					Cyan.Alarm(1);
					Pink.Alarm(1);
					Orange.Alarm(1);
					Scores+=40;
					if(gameFlow > 110){
						SOUND[5].stop();
						SOUND[2].loop();
						alarm = true;
						alarmTime = 0;
					}
					break;
				}
			}
		}

		if((player.getBorder()).intersects(Red.getBorder())){
			if(Red.status() == 1){
				Red.Ghost();
				Scores+=50;
				SOUND[7].play();
			}
			if(Red.status() == 0 && !gameover)
				health--;
		}
		if((player.getBorder()).intersects(Cyan.getBorder())){
			if(Cyan.status() == 1){
				Cyan.Ghost();
				Scores+=50;
				SOUND[7].play();
			}
			if(Cyan.status() == 0 && !gameover)
				health-=2;
		}
		if((player.getBorder()).intersects(Pink.getBorder())){
			if(Pink.status() == 1){
				Pink.Ghost();
				Scores+=50;
				SOUND[7].play();
			}
			if(Pink.status() == 0 && !gameover)
				health-=3;
		}
		if((player.getBorder()).intersects(Orange.getBorder())){
			if(Orange.status() == 1){
				Orange.Ghost();
				Scores+=50;
				SOUND[7].play();
			}
			if(Orange.status() == 0 && !gameover)
				health-=4;
		}
		if(UP_TYPED)
			player.ChangeDirection(0);
		if(DOWN_TYPED)
			player.ChangeDirection(1);
		if(LEFT_TYPED)
			player.ChangeDirection(2);
		if(RIGHT_TYPED)
			player.ChangeDirection(3);

		repaint();
	}

	public void paint(Graphics g){
		g.setColor(Color.black);
		g.fillRect(0, 0, 545, 482);

		for(int i = 0; i < wall.length; i++)
			g.drawImage(WALL[wall[i].getImageIndex() - 1], wall[i].getxPos() - 10, wall[i].getyPos() - 10, this);
		for(int i = 0; i < gold.length; i++){
			if(gold[i] != null && !(gold[i].bigGold() && gameFlow%16 > 7))
				gold[i].draw(g);
		}

		g.setColor(Color.black);
		g.fillRect(179, 200, 21, 21);
		g.fillRect(200, 200, 21, 21);
		g.fillRect(221, 200, 21, 21);
		g.setColor(Color.pink);
		g.fillRect(179, 204, 63, 2);

		player.draw(g);
		if(fruit != null)
			fruit.draw(g);
		if(!(gameover && gameFlow > 66)){
			Red.draw(g);
			Cyan.draw(g);
			Pink.draw(g);
			Orange.draw(g);
		}

		g.setColor(Color.black);
		g.fillRect(0, 220, 10, 23);
		g.fillRect(410, 220, 21, 25);
		g.fillRect(-10, 200, 21, 21);
		g.fillRect(-10, 242, 21, 21);
		g.fillRect(410, 200, 21, 21);
		g.fillRect(410, 242, 21, 21);

		g.setColor(Color.white);
		g.drawString("生命: " + health + "%", 420, 84);
		g.drawString("积分: " + Scores, 420, 105);

		if(!gameStarted){
			g.setColor(Color.cyan);
			g.drawString("       按空格开始",  153, 273);
		}
		if(gameover && gameFlow > 100){
			g.setColor(Color.red);
			g.drawString("游戏结束",  179, 238);
		}
	}

	public void keyPressed(KeyEvent e){
		
		//点击“空格”,游戏开始
		if(e.getKeyCode() == KeyEvent.VK_SPACE){
			SOUND[6].play();
			gameStarted = true;
		}

		//点击方向键,执行相应的操作
		if(e.getKeyCode() == KeyEvent.VK_UP){
			player.ChangeDirection(0);
			UP_TYPED = true;
			DOWN_TYPED = false;
			LEFT_TYPED = false;
			RIGHT_TYPED = false;
		}
		if(e.getKeyCode() == KeyEvent.VK_DOWN){
			player.ChangeDirection(1);
			UP_TYPED = false;
			DOWN_TYPED = true;
			LEFT_TYPED = false;
			RIGHT_TYPED = false;
		}
		if(e.getKeyCode() == KeyEvent.VK_LEFT ){
			player.ChangeDirection(2);
			UP_TYPED = false;
			DOWN_TYPED = false;
			LEFT_TYPED = true;
			RIGHT_TYPED = false;

⌨️ 快捷键说明

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