Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  loop help  (Read 1295 times)
0 Members and 1 Guest are viewing this topic.
Offline JavaFoot

Junior Newbie




Java games rock!


« Posted 2004-10-11 00:37:04 »

I've made a text based turn based fighting application game. You fight an enemy until your enemy or your life points run out. It works perfectly as a plain application, but I'm running into problems converting it to swing, so I can make it look nice with a GUI. I'm having the text display in a JTextArea. Problem is when it loops it never gives me the chance to attack, it only allows the enemy to attack until my life points reach 0. What can I do to have it stop when its my turn, so I can type in my attack?
Here is the part of the code that is creating this problem:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
               while(!gameover) //loops until gameover
           {
                   if(first_player) //what happens if the player or eggman is chosen first
                  {
                     players[r] = 0; //eggman's turn
           
                    //attack damages
                    int E_attack1 = 5;
                     int E_attack2 = 10;
                     int E_attack3 = 15;
                     
                     //attacks in an array
                    int[] attacks = { E_attack1, E_attack2, E_attack3 };
                     int A = attacks.length;
                     int p = (int) (Math.random() * A); //makes the attacks random

                     //the effects of what the damages will do to the Player's Life Points
                    if(attacks[p] == E_attack1)
                    {
                     
                     Player_LP -=E_attack1;
                     
                     }
                     else
                     if(attacks[p] == E_attack2)
                     {
                     
                     Player_LP -=E_attack2;

                     }
                     else
                     if(attacks[p] == E_attack3)
                     {
                     
                          Player_LP -=E_attack3;
                     
                      }
                      text.append("Eggman does "+ attacks[p] + " damage." + "\n"); //the print out of the attack and damage
             
                      }
                      //players turn
                     else
                     {
                       
                       //players attack damages
                      int attack1 = 5;
                       int attack2 = 10;
                       int attack3 = 15;
                       text.append( "Choose attack1 , attack2, or attack3: "  + "\n"); //gives player a choice of attacks
                      inputField.getText(); //gets the text
                      text.getText(); //gets the text from the text area

                       //what happens when one of the attacks is chose
                      if(text.equals("attack1"))
                      {
                        //the effect of the attack on Eggman's life points
                      Eggman_LP -=attack1;
                       //the print out of the attack and the damage
                      text.append( "\n" + "You attack with attack1 causing " + attack1 + " damage to Eggman."+"\n");
                       }
                       else

                      if (text.equals("attack2"))
                     {
                            Eggman_LP -=attack2;
                            text.append( "\n" + "You attack with attack2 causing " + attack2 + " damage to Eggman."+"\n");
                     }
                     else
                     if(text.equals("attack3"))
                    {
                         Eggman_LP -=attack3;
                         text.append( "\n" + "You attack with attack3 causing " + attack3 + " damage to Eggman."+"\n");
                    }
                    }
                    //make it turn base
                   first_player = !first_player;
                    //how to determine gameover
                   if (Player_LP <=0)
                   {
                       gameover = true;
                       text.append("Eggman Wins");
                   }
                   else
                   if (Eggman_LP <=0)
                  {
                        gameover = true;
                        text.append("You Win");  
                 
                  }
            }

And the game appears like this when I run it:

Quote

What is your player name: DKN
You chose DKN as your player name.
Your enemy will be Eggman.
You and your opponent will have 100 life points.
The player to lose all of their life points loses the game.
You go first
Choose attack1 , attack2, or attack3:
Eggman does 10 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 10 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 15 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 15 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 5 damage.
Choose attack1 , attack2, or attack3:
Eggman does 15 damage.
Eggman Wins
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #1 - Posted 2004-10-11 01:08:22 »

At the very end of the while loop you just add this line (just before the final closing braket):

first_player=!first_player;

first_player gets the value of the opposite of first_player. So... you get alternating true/false there.

弾幕 ☆ @mahonnaiseblog
Offline JavaFoot

Junior Newbie




Java games rock!


« Reply #2 - Posted 2004-10-11 01:55:16 »

Quote
At the very end of the while loop you just add this line (just before the final closing braket):

first_player=!first_player;

first_player gets the value of the opposite of first_player. So... you get alternating true/false there.


um i already have that there? or if you mean between the two } at the end, i get the same results.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #3 - Posted 2004-10-11 02:32:14 »

Oh... yea you already have it there... somewhere.

Well, the formatting is pretty screwed and I won't start couting '{' and '}'.

弾幕 ☆ @mahonnaiseblog
Offline ap_kelly

Junior Member




Java rocks!


« Reply #4 - Posted 2004-10-11 03:38:08 »

This section of code is where your problem is, the text.getText() is just returning you the value of the TextArea, in this case "Choose attack1...."

1  
2  
3  
4  
5  
         text.append( "Choose attack1 , attack2, or attack3: "  + "\n");
        inputField.getText();
        text.getText();
 
        if(text.equals("attack1"))


I can't remember the code off the top of my head to read input from the keyboard. Check out the Swing tutorials on the Sun site.

If the user types in "Attack1" it will not match any of the 'if' statments since you don't use equalsIgnoreCase(), use this instead of just equals().

I'd also recomend having an else statement that would catch all other inputs (e.g. hit1) and you can then output a message saying "Unknown attack".

It's hard to debug the code, since there are no comments in it. Always put comments in your code if you're going to post to a forum, it'll make our job much easier when helping you out.

Hope that helps,

Andy.

Offline JavaFoot

Junior Newbie




Java games rock!


« Reply #5 - Posted 2004-10-12 00:49:38 »

well I added a keylistener, but get the same results. also I've been looking around the net at problems similar to mine and they don't seem to have working solutions to them. Is there not something that can wait for user input in a loop? Huh
Offline mikelomax

Senior Newbie




I know exactly what i'm doing... asking for help


« Reply #6 - Posted 2004-10-12 10:17:34 »

Quote
well I added a keylistener, but get the same results. also I've been looking around the net at problems similar to mine and they don't seem to have working solutions to them. Is there not something that can wait for user input in a loop? Huh


That depends on the input, if it's a simple on/off function then you can create a boolean attribute and make it so the loop calls another loop (a while or a do while) that will loop until the user action changes the boolean value. The same goes for text input, you just have to make it so the String attribute is set to something like "" or "<empty>" (something that isn't likely to be typed in by the user). This is then set by an Listener (i.e. something seperate from the code) and then is dealt with by the loops when set

Example:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
    while(gamePlaying) {

        ....code
        waitForInput();
    }
}

private void waitForInput() {
    while(input.equals("<empty>") {
        //this will change when submit button is clicked and textbox is readfrom
   }
}

The reason for having: A firewall

Independance day, the aliens were destroyed because a computer was used to access their network and disable key systems. If they had had a firewall, the earth would be another worthless rock just floating in space
Offline kevglass

JGO Kernel


Medals: 121
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #7 - Posted 2004-10-12 11:11:23 »

I believe what you actually want to do is add an action listener to your text field (inputField?). The action listener will get fired (actionPerformed()) when enter is pressed which I assume is when you'd like you users input to be processed.

So you move the code from your else leg that handles player input into a function which you call from the action listener. The boolean flag for whose turn it is stays in and you only make the action listener do anything if its the players turn. Finally, the action listener is what actually changes the flag over.

Kev

Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #8 - Posted 2004-10-12 11:41:05 »

Quote
well I added a keylistener, but get the same results. also I've been looking around the net at problems similar to mine and they don't seem to have working solutions to them. Is there not something that can wait for user input in a loop? Huh


People who have a similar problem are, like you, missing the point of swing entirely. You have attempted to use Swing as it if were a C-style of programming; C is now ? 30 years old, and most of the idioms have been replaced with "superior" approaches.

It's not that there's anything inherently wrong with what you're trying, but Java only supports more modern approaches (feel free to debate whether that was a good or bad decision by Sun - it certainly put off a lot of C and C++ developers from switching).

What you need to do is read about event-triggered logic, because Swing *only* uses event-triggered logic. Of course, you can always implement anything you want, but to do your style of logic means jumping through several hoops writing lots of code yourself which will be hard to maintain - whereas simply learning and understanding the way Swing expects you to write your program will mean writing ZERO code, and everything Just Works.

What you should do is google for "java tutorial sun" and find the java.sun.com tutorials that teach you all about programming java. Find the "trail" about awt or swing, and follow it. By the time you get to the end (should take less than 30 minutes to do the basics) , you should have a good idea of how swing works, and how the "event-triggered" approach to logic works. There is a huge trail about swing that tells you all the details, but you don't want that, you just want the basics first...

malloc will be first against the wall when the revolution comes...
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (15 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!