Jump to content
  • Advertisement
Sign in to follow this  

[java] game help

This topic is 5465 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my turn base fighting game, I have two players that fight each other. They fight each other with 3 different attacks to chose from, but the attacks don't have an effect on their life points. Their life points are what keep them alive. What I want is for the life points to be deducted each time they are attacked. Also once the life points are deducted, it should stay deducted and continue to be deducted until it reaches 0 and then the game is over. I'm not sure what I'm doing wrong to make this happen. Can someone led me in the right direction? Here is my code:
import java.io.*;
import java.util.Random;
public class Project

	public static void main(String[] args) throws IOException
		String attack; 
		String name; 
		int eggman = 0;
		int player = 1;
		int Eggman_LP = 50;
		int Player_LP = 50;

		int[] players = {0,1};
	        int P = players.length;
                int r = (int) (Math.random() * P);
		boolean gameover=false;
		boolean first_player=((new java.util.Random().nextInt()%2)==0);
		boolean choose_name = ((new java.util.Random().nextInt()%2)==0);
		BufferedReader in;
		in = new BufferedReader(new InputStreamReader(System.in));
		System.out.print("What is your player name: ");//ask for player name
		name = in.readLine(); //get player name
		System.out.println( "\n" + "You chose " + name + " as your player name."+ "\n"); //tell what the player chose
		System.out.println("Your enemy will be Eggman."+ "\n"); //tell who the player will be fighting
		System.out.println("You and your opponent will have 50 life points."+ "\n"); //tell the goal
		System.out.println("The player to lose all of their life points loses the game."+ "\n"); //tell the goal
		players[r] = 0;
		System.out.println("Eggman goes first");
		System.out.println("You go first");
		 players[r] = 0;
		 int E_attack1 = Player_LP-45;
		 int E_attack2 = Player_LP-40;
		 int E_attack3 = Player_LP-35;
		 int[] attacks = { E_attack1, E_attack2, E_attack3 };
    	         int A = attacks.length;
		 int p = (int) (Math.random() * A);
                 System.out.println("Eggman does "+ attacks

+ " damage." + "\n"); } else { int attack1 = Eggman_LP-45; int attack2 = Eggman_LP-40; int attack3 = Eggman_LP-35; System.out.println(name + " choose attack1 , attack2, or attack3: "); attack = in.readLine(); if(attack.equals("attack1")) System.out.println( "\n" + "You attack with attack1 causing " + attack1 + " damage to Eggman."+"\n"); else if (attack.equals("attack2")) System.out.println( "\n" + "You attack with attack2 causing " + attack2 + " damage to Eggman."+"\n"); else if(attack.equals("attack3")) System.out.println( "\n" + "You attack with attack3 causing " + attack3 + " damage to Eggman."+"\n"); } first_player = !first_player; if (Player_LP <=0 || Eggman_LP <=0) { gameover = true; System.out.println("GameOver"); } } } }

Share this post

Link to post
Share on other sites
Umm... deducting from a number is done with the subtraction operator. At the appropriate point in your code (hint: that's the point where you announce how much damage was done), deduct the number of life points dealt by the attack (hint: you know what that is, you stored it in attacks

) from the target's life points (hint: you know who is attacking, according to first_player; therefore you know who is being attacked - the other person. And the life points for the players are stored in Eggman_LP and Player_LP respectively). Hint: you currently don't do this subtraction.

Please try reading your code more carefully, and thinking a bit, before posting. If you got that much written then you are definitely up to the task of figuring out something like this yourself.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!