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 static void main(string[] args){
}
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;
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
player.ChangeDirection(3);
UP_TYPED = false;
DOWN_TYPED = false;
LEFT_TYPED = false;
RIGHT_TYPED = true;
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void update(Graphics g) {
Graphics offScreenGraphics;
if (offScreenImage == null) {
//返回创建一幅用于双缓冲的、可在屏幕外绘制的图像。
//如果组件是不可显示的,则返回值可能为 null。
offScreenImage = createImage(545, 482);
}
offScreenGraphics = offScreenImage.getGraphics();
offScreenGraphics.setColor(Color.white);
offScreenGraphics.fillRect(0, 0, 545, 482);
paint(offScreenGraphics);
g.drawImage(offScreenImage, 0, 0, this);
}
/**
* 布置豆子
*/
private static Gold[] golds(){
}//略
private static Wall[] walls(){
}//略
}
5 个解决方案
#1
与楼主一样的问题
一样的豆豆代码
想请问楼主,我的游戏点击空格不反应
玩不了啊?
#2
没人吗。。。。。。。。。。。。。
#3
package Pacgame;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class Run {
public static void main(String[] args) {
Packman applet = new Packman(); //applet文件名
JFrame frame = new JFrame("马里奥守护战之穿越时空:豆豆空间");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.getContentPane().add(
applet, BorderLayout.CENTER);
frame.setSize(580,540);//;(650,520);
applet.init();
applet.start();
frame.setVisible(true);
}
}
试一试可行吗?
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class Run {
public static void main(String[] args) {
Packman applet = new Packman(); //applet文件名
JFrame frame = new JFrame("马里奥守护战之穿越时空:豆豆空间");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.getContentPane().add(
applet, BorderLayout.CENTER);
frame.setSize(580,540);//;(650,520);
applet.init();
applet.start();
frame.setVisible(true);
}
}
试一试可行吗?
#4
package Pacgame;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class Run {
public static void main(String[] args) {
Packman applet = new Packman(); //applet文件名
JFrame frame = new JFrame("马里奥守护战之穿越时空:豆豆空间");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.getContentPane().add(
applet, BorderLayout.CENTER);
frame.setSize(580,540);//;(650,520);
applet.init();
applet.start();
frame.setVisible(true);
}
}
试一试可行吗?
啊。。。抱歉这几天有其他的考试暂时没时间弄这个了,你也是交java学期末的实践作业吗?
#5
是的,但是我的程序对键盘操作不反应,豆豆不走动,
快绝望死了,怎么调试都不成功。
你的能行吗?
快绝望死了,怎么调试都不成功。
你的能行吗?
#1
与楼主一样的问题
一样的豆豆代码
想请问楼主,我的游戏点击空格不反应
玩不了啊?
#2
没人吗。。。。。。。。。。。。。
#3
package Pacgame;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class Run {
public static void main(String[] args) {
Packman applet = new Packman(); //applet文件名
JFrame frame = new JFrame("马里奥守护战之穿越时空:豆豆空间");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.getContentPane().add(
applet, BorderLayout.CENTER);
frame.setSize(580,540);//;(650,520);
applet.init();
applet.start();
frame.setVisible(true);
}
}
试一试可行吗?
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class Run {
public static void main(String[] args) {
Packman applet = new Packman(); //applet文件名
JFrame frame = new JFrame("马里奥守护战之穿越时空:豆豆空间");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.getContentPane().add(
applet, BorderLayout.CENTER);
frame.setSize(580,540);//;(650,520);
applet.init();
applet.start();
frame.setVisible(true);
}
}
试一试可行吗?
#4
package Pacgame;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class Run {
public static void main(String[] args) {
Packman applet = new Packman(); //applet文件名
JFrame frame = new JFrame("马里奥守护战之穿越时空:豆豆空间");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.getContentPane().add(
applet, BorderLayout.CENTER);
frame.setSize(580,540);//;(650,520);
applet.init();
applet.start();
frame.setVisible(true);
}
}
试一试可行吗?
啊。。。抱歉这几天有其他的考试暂时没时间弄这个了,你也是交java学期末的实践作业吗?
#5
是的,但是我的程序对键盘操作不反应,豆豆不走动,
快绝望死了,怎么调试都不成功。
你的能行吗?
快绝望死了,怎么调试都不成功。
你的能行吗?