我正在用Java制作基于GUI的文本冒险游戏。如何更新消息框以显示结果?

时间:2015-11-17 14:24:14

标签: java

我现在添加了更多提示,消息框应该说不同的东西,但是他们被困在“你前进并撞墙”和“你现在面向相反的方向”他们不应该改变他们应该和我添加的第三个选项没有出现。我已经可以在第一时间显示消息,但在您做出第一个决定并且按钮更改后,消息框不会。再次,这是我的代码:

package game;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class game extends JFrame implements ActionListener {

JLabel prompt;
JTextField name;
JTextField name1;
JButton click;
JButton click1;
String storeName;
String storeName1;
JButton click2;
private int gameState = 0;



public game(){

    setLayout(null);
    setSize(550,250);
    setTitle("Text Adventure");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    prompt = new JLabel("You find yourself in a tunnel.");
    click = new JButton("Go forward!");
    click1 = new JButton("Turn around!");
    click2 = new JButton();
    name = new JTextField();
    name1 = new JTextField();
    prompt.setBounds(170,30,1300,30);
    click.setBounds(100,130,100,30);
    click.addActionListener(this);
    click1.setBounds(300,130,125,30);
    click1.addActionListener(this);
    click2.setBounds(185,175,145,30);
    click2.addActionListener(this);
    add(click);
    add(click1);
    add(name);
    add(name1);
    add(prompt);


}

public void actionPerformed(ActionEvent e) {

    if (gameState == 0){

    if(e.getSource() == click) {

        JOptionPane.showMessageDialog(null, "You go forward and hit a wall.");
    }

    if(e.getSource() == click1) {

        JOptionPane.showMessageDialog(null, "You are now facing the opposite direction.");
        prompt.setBounds(50,30,1300,30);
        prompt.setText("What would you like to do now that you are facing the opposite direction?");
        click.setText("Go forward!");
        click1.setText("Turn on light.");
    }
        gameState = 1;
    }

    else if (gameState == 1){

        if(e.getSource() == click) {

            JOptionPane.showMessageDialog(null, "You walk down the tunnel until you hit an intersection.");
            prompt.setBounds(100,30,1300,30);
            prompt.setText("Which way would you like to go at the intersection?");
            click.setText("Left!");
            click1.setText("Right!");
            add(click2);
            click2.setText("Keep going!");
        }
        if(e.getSource() == click1){

            JOptionPane.showMessageDialog(null, "You turn on your light.");
            prompt.setBounds(220,30,1300,30);
            prompt.setText("What now?");
            click.setText("Go forward!");
            click1.setText("Sit down.");

        }
        gameState = 2;
    }
    else if (gameState == 2) {

        if (e.getSource() == click) {

            JOptionPane.showMessageDialog(null, "You turn left and walk into a dark room.");
            prompt.setBounds(210,30,1300,30);
            prompt.setText("What do you do?");
            click.setText("Cry");
            click1.setText("Shout 'Hello?'");
            click2.setText("Explore the room");

        }

}
}

public static void main(String args[]){

    game s = new game();
    s.setVisible(true);
    }
}

2 个答案:

答案 0 :(得分:2)

Mage Xy在answer给了你一个解决方案。他还告诉你最大的缺点。

  

但是,这个解决方案的主要缺点是巨大的代码膨胀。正如您已经看到的那样,只有两个房间占用了很多代码空间...如果您想拥有100个房间怎么办?你的actionPerformed方法将长达数千行代码!

您可以将自己的房间看作一棵树。根据您在问题中所说的内容,您就拥有了这种结构。

Tunnel <-> Room

换句话说,您可以从隧道移动到房间,从房间移动到隧道。

当您想构建Java Swing GUI游戏时,它有助于将游戏模型与游戏视图和游戏控制器分开。这种关注点分离使您可以一次专注于游戏的一部分。

那么,你的游戏的游戏模型会是什么样子?

让我们从房间定义开始。根据您的代码和Mage Xy的代码,这是一种可能性

package com.ggl.text.adventure;

import java.util.List;

public class Room {

    private final int roomNumber;

    private int northRoomNumber;
    private int eastRoomNumber;
    private int southRoomNumber;
    private int westRoomNumber;

    private List<String> objectsInRoom;

    private String roomName;

    public Room(int roomNumber) {
        this.roomNumber = roomNumber;
    }

    public int getNorthRoomNumber() {
        return northRoomNumber;
    }

    public void setNorthRoomNumber(int northRoomNumber) {
        this.northRoomNumber = northRoomNumber;
    }

    public int getEastRoomNumber() {
        return eastRoomNumber;
    }

    public void setEastRoomNumber(int eastRoomNumber) {
        this.eastRoomNumber = eastRoomNumber;
    }

    public int getSouthRoomNumber() {
        return southRoomNumber;
    }

    public void setSouthRoomNumber(int southRoomNumber) {
        this.southRoomNumber = southRoomNumber;
    }

    public int getWestRoomNumber() {
        return westRoomNumber;
    }

    public void setWestRoomNumber(int westRoomNumber) {
        this.westRoomNumber = westRoomNumber;
    }

    public List<String> getObjectsInRoom() {
        return objectsInRoom;
    }

    public void setObjectsInRoom(List<String> objectsInRoom) {
        this.objectsInRoom = objectsInRoom;
    }

    public String getRoomName() {
        return roomName;
    }

    public void setRoomName(String roomName) {
        this.roomName = roomName;
    }

    public int getRoomNumber() {
        return roomNumber;
    }

}

此类允许您定义每个房间的名称以及每个房间中包含的对象。房间可以有零个或多个对象。房间号码是每个房间的唯一编号,从零或一开始上升。

您还可以定义进入北,东,南或西时输入的房间号。当那个方向没有空间时,您可以将方向房间号设置为-1。

定义这些房间发生在另一个班级。您可以创建房间列表并定义每个房间。一旦创建了这个房间列表,创建Swing视图和控制器变得更加容易。

答案 1 :(得分:1)

跟进我们在你的问题的评论中所讨论的内容:

实现ActionListener告诉程序的其他部分此类(game)具有actionPerformed(ActionEvent)方法。当您将this指定为按钮的actionListener时,该按钮将在单击时查找actionPerformed方法。按钮不知道actionPerformed1方法是否存在(因为,从编程方面来说,这两种方法完全不相关)。

那么如何为下一个房间提供代码?嗯,这很乏味。

首先,您需要一种方法来跟踪游戏的状态。在你的情况下,也许你可以跟踪哪个&#34;房间&#34;玩家在。这可以通过一个简单的int字段来完成:

private int gameState = 0;

然后,使用您游戏的状态,您需要确定当前与按钮绑定的操作。这是进入actionPerformed方法的部分。

public void actionPerformed(ActionEvent e)
{
    if (gameState == 0)
    {
        if(e.getSource() == click) 
        {
            JOptionPane.showMessageDialog(null, "You go forward and hit a wall.");
        }
        if(e.getSource() == click1)
        {
            JOptionPane.showMessageDialog(null, "You are now facing the opposite direction.");
            prompt.setText("What would you like to do now?");
            click.setText("Go forward!");
            click1.setText("Turn on light.");
            gameState = 1; // Update the current state of the game to represent the new "room"
        }
    }
    else if (gameState == 1)
    {
        if(e.getSource() == click) {

            JOptionPane.showMessageDialog(null, "You walk down the tunnel until you hit an intersection.");
            prompt.setText("Which way would you like to go?");
            click.setText("Left!");
            click1.setText("Right!");
            add(click2);
            click2.setText("Keep going!");
            gameState = 2;
        }
        if(e.getSource() == click1)
        {

        }
    }
    else if (gameState == 3)
    {
        // etc.
    }
}

这可以快速轻松地解决问题,并允许玩家以您想要的方式进行游戏。

但是,这个解决方案的主要缺点是巨大的代码膨胀。正如您已经看到的那样,只有两个房间占用了很多代码空间...如果您想拥有100个房间怎么办?您的actionPerformed方法将长达数千行代码!这完全是荒谬的。

有几种方法可以解决这个问题,但它们更复杂,您选择哪一种方法在很大程度上取决于您的应用程序的需求。