我正在尝试使用javascript制作基于文本的RPG。我目前有一个可以自行运行的脚本;因为它完全自动化并且不接收来自玩家的输入。但是我想这样做,以便当用户按下按钮时我可以从文本字段中获取值。因此,例如,如果用户在文本字段中输入攻击并按下按钮,它将进入攻击功能。我正在尝试做的就是将我的一个Java小程序变成javascript。首先,这是java applet代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Random;
/**
*
* @author C.C
*/
public class slime extends javax.swing.JApplet {
Random rng = new Random();
int choice;
int player = 100;
int king = 100;
int slime = 50;
/**
* Initializes the applet slime
*/
@Override
public void init() {
initComponents();
choice = rng.nextInt(10)+1;
if(choice >=7)
{
battle.append("You are the hero. You have 100 health." + "\n");
battle.append("You encountered a King Slime!" + "\n\n");
}
else
{
battle.append("You are the hero. You have 100 health." + "\n");
battle.append("You encountered a slime!" + "\n\n");
}
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(slime.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(slime.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(slime.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(slime.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the applet */
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* This method is called from within the init() method to initialize the
* form. WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
battle = new javax.swing.JTextArea();
attack = new javax.swing.JButton();
heal = new javax.swing.JButton();
charge = new javax.swing.JButton();
run = new javax.swing.JButton();
battle.setColumns(20);
battle.setRows(5);
jScrollPane1.setViewportView(battle);
attack.setText("Attack");
attack.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
attackActionPerformed(evt);
}
});
heal.setText("Heal");
heal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
healActionPerformed(evt);
}
});
charge.setText("Charge");
charge.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
chargeActionPerformed(evt);
}
});
run.setText("Run");
run.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
runActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(heal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(attack, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(charge, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
.addComponent(run, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(attack)
.addComponent(charge))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(heal)
.addComponent(run))
.addContainerGap())
);
}// </editor-fold>
private void attackActionPerformed(java.awt.event.ActionEvent evt) {
int damage = rng.nextInt(10)+1;
int damage2 = rng.nextInt(25)+10;
int damage3 = rng.nextInt(20)+5;
if(player <= 0)
{
battle.append("You have died" + "\n\n");
attack.setEnabled(false);
battle.setEnabled(false);
charge.setEnabled(false);
heal.setEnabled(false);
}
else
{
if(damage >=7)
{
battle.append("You did "+ String.valueOf(damage)+ " damage. It was a critcal hit!" + "\n");
if (choice >=7)
{
king = king-damage;
battle.append("The King Slime has " + king + " health left!" + "\n");
if(king <=0)
{
battle.append("You have defeated the King Slime!" + "\n\n");
attack.setEnabled(false);
battle.setEnabled(false);
charge.setEnabled(false);
heal.setEnabled(false);
}
battle.append("The King Slime hit you for " + String.valueOf(damage2) + " damage!" + "\n");
player = player-damage2;
if(player < 0)
{
player = 0;
}
battle.append("You have "+ String.valueOf(player) + " health left." + "\n\n");
}
else
{
slime = slime-damage;
battle.append("The slime has " + slime + " health left!" + "\n");
if(slime <=0)
{
battle.append("You have defeated the slime!" + "\n\n");
attack.setEnabled(false);
battle.setEnabled(false);
charge.setEnabled(false);
heal.setEnabled(false);
}
battle.append("The slime hit you for " + String.valueOf(damage3) + " damage!" + "\n");
player = player-damage3;
if(player < 0)
{
player = 0;
}
battle.append("You have "+ String.valueOf(player) + " health left." + "\n\n");
}
}
else
{
battle.append("You did "+ String.valueOf(damage)+ " damage." + "\n");
if (choice >=7)
{
king = king-damage;
battle.append("The King Slime has " + king + " health left!" + "\n");
if(king <=0)
{
battle.append("You have defeated the King Slime!" + "\n\n");
attack.setEnabled(false);
battle.setEnabled(false);
charge.setEnabled(false);
heal.setEnabled(false);
}
battle.append("The Slime King hit you for " + String.valueOf(damage2) + " damage!" + "\n");
player = player-damage2;
if(player < 0)
{
player = 0;
}
battle.append("You have "+ String.valueOf(player) + " health left." + "\n\n");
}
else
{
slime = slime-damage;
battle.append("The slime has " + slime + " health left!" + "\n");
if(slime <=0)
{
battle.append("You have defeated the slime!" + "\n\n");
attack.setEnabled(false);
battle.setEnabled(false);
charge.setEnabled(false);
heal.setEnabled(false);
}
battle.append("The slime hit you for " + String.valueOf(damage3) + " damage!" + "\n");
player = player-damage3;
if(player < 0)
{
player = 0;
}
battle.append("You have "+ String.valueOf(player) + " health left." + "\n\n");
}
}
}
}
private void healActionPerformed(java.awt.event.ActionEvent evt) {
int heal = rng.nextInt(40)+20;
int damage2 = rng.nextInt(25)+10;
int damage3 = rng.nextInt(20)+5;
if(player <= 0)
{
battle.append("You have died" + "\n\n");
attack.setEnabled(false);
battle.setEnabled(false);
charge.setEnabled(false);
}
else
{
battle.append("You healed for " + heal + " health.\n");
player = player+heal;
if (choice >=7)
{
battle.append("The King Slime has " + king + " health left!" + "\n");
battle.append("The King Slime hit you for " + String.valueOf(damage2) + " damage!" + "\n");
player = player-damage2;
if(player < 0)
{
player = 0;
}
battle.append("You have "+ String.valueOf(player) + " health left." + "\n\n");
}
else
{
battle.append("The slime has " + slime + " health left!" + "\n");
battle.append("The slime hit you for " + String.valueOf(damage3) + " damage!" + "\n");
player = player-damage3;
if(player < 0)
{
player = 0;
}
battle.append("You have "+ String.valueOf(player) + " health left." + "\n\n");
}
}
}
private void chargeActionPerformed(java.awt.event.ActionEvent evt) {
int damage = rng.nextInt(30)+10;
int damage2 = rng.nextInt(25)+10;
int damage3 = rng.nextInt(20)+5;
if(player <= 0)
{
battle.append("You have died" + "\n\n");
attack.setEnabled(false);
battle.setEnabled(false);
charge.setEnabled(false);
heal.setEnabled(false);
}
else
{
battle.append("You skip a turn to charge a more powerful attack!" +"\n");
if (choice >=7)
{
battle.append("The King Slime has " + king + " health left!" + "\n");
battle.append("The King Slime hit you for " + String.valueOf(damage2) + " damage!" + "\n");
player = player-damage2;
if(player < 0)
{
player = 0;
}
battle.append("You have "+ String.valueOf(player) + " health left." + "\n\n");
}
else
{
battle.append("The slime has " + slime + " health left!" + "\n");
battle.append("The slime hit you for " + String.valueOf(damage3) + " damage!" + "\n");
player = player-damage3;
if(player < 0)
{
player = 0;
}
battle.append("You have "+ String.valueOf(player) + " health left." + "\n\n");
}
if(player <=0)
{
battle.append("You have died" + "\n\n");
attack.setEnabled(false);
battle.setEnabled(false);
charge.setEnabled(false);
heal.setEnabled(false);
}
else
{
if (choice >=7)
{
battle.append("You unleash a powerful attack at the King Slime for " + damage + " damage.\n");
king = king-damage;
battle.append("The King Slime has " + king + " health left!" + "\n");
if(king <=0)
{
battle.append("You have defeated the King Slime!" + "\n\n");
attack.setEnabled(false);
battle.setEnabled(false);
charge.setEnabled(false);
heal.setEnabled(false);
}
battle.append("The King Slime hit you for " + String.valueOf(damage2) + " damage!" + "\n");
player = player-damage2;
if(player < 0)
{
player = 0;
}
battle.append("You have "+ String.valueOf(player) + " health left." + "\n\n");
}
else
{
battle.append("You unleash a powerful attack at the slime for " + damage + " damage.\n");
slime = slime-damage;
battle.append("The slime has " + slime + " health left!" + "\n");
if(slime <=0)
{
battle.append("You have defeated the slime!" + "\n\n");
attack.setEnabled(false);
battle.setEnabled(false);
charge.setEnabled(false);
heal.setEnabled(false);
}
battle.append("The slime hit you for " + String.valueOf(damage3) + " damage!" + "\n");
player = player-damage3;
if(player < 0)
{
player = 0;
}
battle.append("You have "+ String.valueOf(player) + " health left." + "\n\n");
}
}
}
}
private void runActionPerformed(java.awt.event.ActionEvent evt) {
if (player==0)
{
battle.append("Running won't do you any good when you are dead. \n\n");
}
else
{
battle.append("You can't run in this game you sissy.\n\n");
}
}
// Variables declaration - do not modify
private javax.swing.JButton attack;
private javax.swing.JTextArea battle;
private javax.swing.JButton charge;
private javax.swing.JButton heal;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton run;
// End of variables declaration
}
以下是我的javascript文件目前的样子:
//Player Function
var players = function(attack, health){
this.health = health;
this.attack = attack;
};
//Make Player Here
var player = new players(50, 1000);
//Monster Function
var monsters = function(attack, health){
this.health = health;
this.attack = attack;
};
//Make Monsters Here
var kingSlime = new monsters(100, 300);
var slime = new monsters(25, 100);
var slimeWarrior = new monsters(50, 200);
//Make String Names Here
var names;
//Make randomnumber Here
var randomnumber = Math.floor(Math.random(10)*10);
//Make selectedmonster Here
var selectedmonster;
if (randomnumber >= 0 && randomnumber < 3){
selectedmonster = kingSlime;
names = "King Slime";
}
else if (randomnumber > 3 && randomnumber < 6){
selectedmonster = slime;
names = "Slime";
}
else if (randomnumber > 6){
selectedmonster = slimeWarrior;
names = "Slime Warrior";
}
//Battle Script
var battle = 1;
var monsterdead = 0;
var playerdead = 0;
//player goes first, playerturn = 1
var playerturn = 1;
var monsterturn = 0;
while (battle == 1){
//Player's Turn
while (playerturn == 1){
//Write Player Attack Script
selectedmonster.health = selectedmonster.health - Math.ceil(Math.random(player.attack)*10);
document.write(names + "'s Health is now " + selectedmonster.health+"<br>");
playerturn = 0;
monsterturn = 1;
}
//Monster's Turn
while (monsterturn == 1){
//Write Monster Attack Script
player.health = player.health - Math.ceil(Math.random(selectedmonster.attack)*10);
document.write("Player's Health is now " + player.health+"<br>");
playerturn = 1;
monsterturn = 0;
}
//When Monster Dies
if (selectedmonster.health < 0){
document.write("You beat the " + names + "!<br>");
monsterdead = 1;
battle = 0;
}
//When Player Dies
if (player.health < 0){
document.write("You have died.<br>");
playerdead = 1;
battle = 0;
}
}
那么我是如何在javascript中添加玩家互动的呢?感谢。
答案 0 :(得分:0)
<button onclick="Attack()"> Attack</button>
var userValue = document.getElementById("my inputs id");
function Attack(){
if(userValue.value = "what im looking for")
{
execute attack
}
}