制作一个简单的角色扮演游戏 - 创建一个新的 JFrame



我正在制作一个4级RPG。它具有Player类,允许我们移动播放器。它具有用于游戏中地图的棋盘类。有一个战斗职业,用于战斗阶段。RPG是负责运行游戏的主要类。我想要的是当玩家来到坐标(500x500)时,我希望程序打开一个新窗口,它将有战斗的JPanel。

编辑:更新了 display() 和 actionPerform() 方法

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Player {
    private int dx,dy,x,y;
    private Image image;
    private ImageIcon front = new ImageIcon("player_front.png");
    private ImageIcon left = new ImageIcon("player_left.png");
    private ImageIcon right = new ImageIcon("player_right.png");
    private ImageIcon back = new ImageIcon("player_back.png");
    public Player() {
        initplayer();
    }
    private void initplayer() {
        image = front.getImage();
        x = 0;
        y = 0;        
    }

    public void move() {
        x += dx;
        y += dy;
    }
    public int getX() {
        return x;
    }
    public void setX(int x){
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public Image getImage() {
        return image;
    }

    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        dx = 0;
        dy = 0;
        if (key == KeyEvent.VK_LEFT && x >= 0) {
            image = left.getImage();
            dx = -1;
        }

        if (key == KeyEvent.VK_RIGHT && x <= 850) {
            image = right.getImage();
            dx = 1;
        }
        if (key == KeyEvent.VK_UP && y >= 0) {
            image = back.getImage();                
            dy = -1;
        }
            if (key == KeyEvent.VK_DOWN && y <= 850) {
                image = front.getImage();
                dy = 1;
        }
    }
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }
        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }
        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }
        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }
    }
}

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel implements ActionListener {
    private Battle battle;
    private Timer timer;
    private Player player;
    private final int DELAY = 10;
    private Image image;
    private JLabel label;
    public Board() {
        initBoard();
    }
    private void initBoard() {
        image = Toolkit.getDefaultToolkit().createImage("map.png");
        addKeyListener(new TAdapter());
        setFocusable(true);
        player = new Player();
        timer = new Timer(DELAY, this);
        timer.start();           
    }
    public Image getImage(){
        return image;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image,0,0,null);
        doDrawing(g);
    }
    private void doDrawing(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;   
        g2d.drawImage(player.getImage(), player.getX(), player.getY(), this);
    }
    public boolean checkBattle(){
        if(player.getX() == 500 && player.getY() == 500){
            return true;
        }
        return false;
    }
    public void actionPerformed(ActionEvent e) {//UPDATED
        player.move();
        if(checkBattle()){
              battle.display();
        }            
        repaint();       
    }

    private class TAdapter extends KeyAdapter {
        @Override
        public void keyReleased(KeyEvent e) {
            player.keyReleased(e);
        }
        @Override
        public void keyPressed(KeyEvent e) {
            player.keyPressed(e);
        }
    }
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 
import java.util.*;

public class Battle extends JPanel{
    private JFrame frame;
    private JPanel panel1, panel2;
    private JLabel label1, label2, label3, label4;
    private JButton button1, button2, button3, button4;
    private int playerHealth = 100;
    private int opponentHealth = 50;
    private int maxOpponentHealth = 50;
    private int level = 0;
    private int playerDamage, opponentDamage, chance, levelDamage;
    private ImageIcon image;
    public Battle(){
        setPreferredSize(new Dimension(500,500));
        setLayout(new BorderLayout());
        panel1 = new JPanel();
        panel1.setLayout(new FlowLayout());
        label1 = new JLabel("You : " + Integer.toString(playerHealth) + " / 100");
        label2 = new JLabel("Opponent : " + Integer.toString(opponentHealth) + " / 50");
        button1 = new JButton("Power Attack!(%50 hit chance)");
        button2 = new JButton("Normal Attack!(%70 hit chance)");
        button3 = new JButton("Quick Attack!(%90 hit chance)");
        button4 = new JButton("NEXT LEVEL!");   
        panel2 = new JPanel();
        ImageIcon image = new ImageIcon("player_vs_rival.png");
        label4 = new JLabel(image);
        ButtonListener listener = new ButtonListener();
        button1.addActionListener(listener);
        button2.addActionListener(listener);
        button3.addActionListener(listener);
        button4.addActionListener(listener);
        panel1.add(button1);
        panel1.add(button2);
        panel1.add(button3);
        panel1.add(button4);
        panel2.add(label4);
        label3 = new JLabel("");
        add(label1, BorderLayout.WEST);
        add(label2, BorderLayout.EAST);
        add(label3, BorderLayout.SOUTH);
        add(panel1, BorderLayout.CENTER);
        add(panel2, BorderLayout.NORTH);
    }
    public void powerAttack(){
        Random random = new Random();
        playerDamage = random.nextInt(5) + 10;
    }
    public void normalAttack(){
        Random random = new Random();
        playerDamage = random.nextInt(10) + 5;
    }
    public void quickAttack(){
        Random random = new Random();
        playerDamage = random.nextInt(5) + 5;
    }
    public void opponentAttack(){
        Random random = new Random();
        opponentDamage = random.nextInt(5) + 5;
    }
    public boolean checkPowerAttack(){
        Random random = new Random();
        chance = random.nextInt(100);
        if(chance >= 50){
            return true;
        }
        return false;   
    }
    public boolean checkNormalAttack(){
        Random random = new Random();
        chance = random.nextInt(100);
        if(chance >= 30){
            return true;
        }
        return false;
    }
    public boolean checkQuickAttack(){
        Random random = new Random();
        chance = random.nextInt(100);
        if(chance >= 10){
            return true;
        }
        return false;
    }
    public boolean checkOpponentAttack(){
        Random random = new Random();
        chance = random.nextInt(100);
        if(chance >= 25){
            return true;
        }
        return false;
    }
    public void levelDamage(){
        Random random = new Random();
        levelDamage = random.nextInt(5);
        levelDamage = levelDamage * level;
    }
    public void display(){//UPDATED
        JFrame newFrame = new JFrame("New Window"); 
        Battle battle = new Battle();
        newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        newFrame.setVisible(true);
        newFrame.pack();
        newFrame.getContentPane().add(battle);
    }
}
    private class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            Object source = e.getSource();
            if(source == button1){
                if(checkPowerAttack() == true){
                    powerAttack();
                }
                else
                playerDamage = 0;           
                opponentHealth = opponentHealth - playerDamage; 
            }
            if(source == button2){
                if(checkNormalAttack() == true){
                    normalAttack();
                }
                else
                playerDamage = 0;           
                opponentHealth = opponentHealth - playerDamage; 
            }
            if(source == button3){
                if(checkQuickAttack() == true){
                    quickAttack();
                }
                else
                playerDamage = 0;           
                opponentHealth = opponentHealth - playerDamage; 
            }
            if((source == button1 || source == button2) || source == button3){
                if(checkOpponentAttack() == true){
                    opponentAttack();
                }
                else
                opponentDamage = 0;
                playerHealth = playerHealth - opponentDamage - levelDamage; 
            }
            if(source == button4){
                level++;
                opponentHealth = opponentHealth + 25;
                maxOpponentHealth = opponentHealth;
            }
            if(playerHealth <= 0 && opponentHealth > 0){
                JOptionPane.showMessageDialog(null,"YOU LOSE");
            }
            if(opponentHealth <= 0 && playerHealth > 0){
                JOptionPane.showMessageDialog(null,"YOU WIN");
            }
            label1.setText("You : " + Integer.toString(playerHealth) + " / 100");
            label2.setText("Opponent : " + Integer.toString(opponentHealth) + " / " + Integer.toString(maxOpponentHealth));
            if(playerDamage != 0 && opponentDamage != 0){
                label3.setText("You made " + Integer.toString(playerDamage) + " damage to your opponent. " + http://stackoverflow.com/editing-help
                        "Your opponent give " + Integer.toString(opponentDamage + levelDamage) + " damage to you.");
            }
            else if(playerDamage == 0 && opponentDamage != 0){
                label3.setText("YOU MISSED YOUR ATTACK! " + 
                        "Your opponent give " + Integer.toString(opponentDamage + levelDamage) + " damage to you.");
            }
            else if(playerDamage != 0 && opponentDamage == 0){
                label3.setText("You made " + Integer.toString(playerDamage) + " damage to your opponent. " + 
                        "YOUR OPPONENT MISSED HIS ATTACK!");
            }
            else if(playerDamage == 0 && opponentDamage == 0){
                label3.setText("YOU MISSED YOUR ATTACK! " + 
                        "YOUR OPPONENT ALSO MISSED HIS ATTACK!");
            }
        }
    }
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RPG extends JFrame {
    private Board board = new Board();
    private Battle battle = new Battle();
    public RPG() {
        initUI();
    }
    private void initUI() {
        add(board);
        setSize(900, 900);
        setResizable(false);
        setTitle("RPG");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                RPG ex = new RPG();
                ex.setVisible(false);
            }
        });
    }
}

打开一个新窗口就像创建一个新框架一样简单,就像你在 display() 函数中所做的那样。例如,这个:

    JFrame newFrame = new JFrame("New Window"); 
    newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    newFrame.setVisible(true);

应该足以打开一个没有任何内容的新窗口(您可能希望向其添加您在 display() 方法中所做的内容)。由于你想在玩家到达某个位置时执行此操作,只需检查玩家在移动时的坐标并调用类似的代码来打开一个新帧。

编辑:再看一遍,看起来你可以在 checkBattle() 函数中调用 display()?看起来你已经掌握了这里的大部分逻辑。如果你想知道从哪里调用checkBattle(),我会把它放在Player.move()函数中。

相关内容

  • 没有找到相关文章

最新更新