package 代码部分;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
public class TestGamePanel extends JPanel implements KeyListener, ActionListener {
//初始化人物坐标
int p1X;
int p1Y;
int p2X;
int p2Y;
boolean isStart = false; //游戏是否开始
boolean p1isFail = false; //游戏是否失败
boolean p2isFail = false;
String fx1; //左:L, 右:R, 上:U, 下:D
String fx2;
Timer timer = new Timer(50,this);//定时器
//积分
int p1score = 0;
int p2score = 0;
//苹果
int AppleX;
int AppleY;
//怪物
int monster1X;
int monster1Y;
int monster2X;
int monster2Y;
int monster3X;
int monster3Y;
int monster4X;
int monster4Y;
int monster5X;
int monster5Y;
//随机积分
Random random = new Random();
public TestGamePanel() {
init();
this.setFocusable(true);
this.addKeyListener(this);
timer.start();
}
//初始化
public void init() {
p1X = 25;
p1Y = 150;
p2X = 700;
p2Y = 550;
fx1 = "L";
fx2 = "R";
monster1X = 25*random.nextInt(28);
monster1Y = 100 + 25*random.nextInt(18);
monster2X = 25*random.nextInt(28);
monster2Y = 100 + 25*random.nextInt(18);
monster3X = 25*random.nextInt(28);
monster3Y = 100 + 25*random.nextInt(18);
monster4X = 25*random.nextInt(28);
monster4Y = 100 + 25*random.nextInt(18);
monster5X = 25*random.nextInt(28);
monster5Y = 100 + 25*random.nextInt(18);
AppleX = 25*random.nextInt(28);
AppleY = 100 + 25*random.nextInt(18);
add(kaishi);
add(chongkai);
guize.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new TestGameRule();
}
});
}
//游戏功能按钮
JButton kaishi = new JButton("开始");
JButton chongkai = new JButton("重新开始");
JButton guize = new JButton("游戏规则");
//画板
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
TestGameData.header.paintIcon(this,g,0,0);
g.setColor(Color.CYAN);
g.fillRect(0,100,780,520);
//画人物
TestGameData.p1player1.paintIcon(this,g,p1X,p1Y);
TestGameData.p2player1.paintIcon(this,g,p2X,p2Y);
//画得分
g.setFont(new Font("华文彩云",Font.BOLD,18)); //设置字体
g.setColor(Color.RED);
g.drawString("玩家1:" + p1score,20,20 );
g.drawString("玩家2:" + p2score,680,20);
//画苹果
TestGameData.apple.paintIcon(this,g,AppleX,AppleY);
//画静态怪物
TestGameData.monster.paintIcon(this,g,monster1X,monster1Y);
TestGameData.monster.paintIcon(this,g,monster2X,monster2Y);
TestGameData.monster.paintIcon(this,g,monster3X,monster3Y);
Te