📄 packman6.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
public class packman6 extends Applet 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;
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();
boolean 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("Your Health: " + health + "%", 420, 84);
g.drawString("Your Scores: " + Scores, 420, 105);
if(!gameStarted){
g.setColor(Color.cyan);
g.drawString("Press any key to start", 153, 273);
}
if(gameover && gameFlow > 100){
g.setColor(Color.red);
g.drawString("GAME OVER", 179, 238);
}
}
public void keyPressed(KeyEvent e){
if(!gameStarted)
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;
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
Player.ChangeDirection(3);
UP_TYPED = false;
DOWN_TYPED = false;
LEFT_TYPED = false;
RIGHT_TYPED = true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -