Java制作一个简单地小游戏

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

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值