7 Replies - 2704 Views - Last Post: 20 July 2012 - 01:13 AM Rate Topic: ***-- 2 Votes

#1 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Java tag game tagging not working properly

Posted 12 July 2012 - 11:56 PM

Hi, I have made a little three-person tag game in my recent spare time. But upon adding the third player, I had to change the tagging system. Now, the first player stays it when they touch other players, though the collision detection is working fine because other tasks needing to be done are done. Thank you, and here is my code.

package package1;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.Timer;

public class Main extends Applet implements Runnable, ActionListener {
	private static final long serialVersionUID = 1L;
	
	//Variables
	int x = 15;
	int y = 15;
	int x1 = 760;
	int y1 = 560;
	int x2 = 15;
	int y2 = 560;
	int playerwhoisit = 1;
	int directionToUpdate, directionToUpdate2, directionToUpdate3;
	int upgradenumber;
	int rand1 = (int) (Math.random()*800);
	int rand2 = (int) (Math.random()*600);
	int p1speed = 3, p2speed = 3, p3speed = 3;
	String whosit;
	String notification = "";
	
	
	public void init() {
		addKeyListener(hello);
		setFocusable(true);
		repainting.start();
	}
	
	Timer repainting = new Timer(10, new ActionListener() {
		public void actionPerformed(ActionEvent evx) {
			switch(directionToUpdate) {
			case 0:
				break;
			case 1:
				y -= p1speed;
				break;
			case 2:
				y += p1speed;
				break;
			case 3:
				x -= p1speed;
				break;
			case 4:
				x += p1speed;
				break;
			case 10:
				break;
			}
			switch(directionToUpdate2) {
			case 10:
				break;
			case 11:
				y1 -= p2speed;
				break;
			case 12:
				y1 += p2speed;
				break;
			case 13:
				x1 -= p2speed;
				break;
			case 14:
				x1 += p2speed;
				break;
			}
			switch(directionToUpdate3) {
			case 20:
				break;
			case 21:
				y2 -= p3speed;
				break;
			case 22:
				y2 += p3speed;
				break;
			case 23:
				x2 -= p3speed;
				break;
			case 24:
				x2 += p3speed;
				break;
				
			}
			repaint();
			if(upgradenumber < 1200)upgradenumber++;
		}
		});
	
	public void paint(Graphics g) {
		Rectangle player = new Rectangle(x, y, 15, 15);
		Rectangle player2 = new Rectangle(x1, y1, 15, 15);
		Rectangle player3 = new Rectangle(x2, y2, 15, 15);
		Rectangle obstacle1 = new Rectangle(260, 360, 100, 10);
		Rectangle obstacle2 = new Rectangle(320, 360, 10, 70);
		Rectangle obstacle3 = new Rectangle(470, 112, 100, 100);
		Rectangle obstacle4 = new Rectangle(350, 290, 10, 70);
		Rectangle obstacle5 = new Rectangle(147, 114, 10, 60);
		Rectangle obstacle6 = new Rectangle(57, 204, 60, 10);
		Rectangle obstacle7 = new Rectangle(187, 204, 60, 10);
		Rectangle upgrade = new Rectangle(rand1, rand2, 10, 10);
		Rectangle top = new Rectangle(0, 0, 800, 1);
		Rectangle bottom = new Rectangle(0, 599, 800, 1);
		Rectangle right = new Rectangle(799, 1, 1, 600);
		Rectangle left = new Rectangle(0, 0, 1, 600);
		
		g.setColor(Color.red);
		g.drawRect(x, y, 15, 15);   //Player 1
		g.setColor(Color.blue);
		g.drawRect(x1,y1, 15, 15);  //Player 2
		g.setColor(Color.green);
		g.drawRect(x2,  y2, 15, 15);
		g.setColor(Color.black);
		if (playerwhoisit == 1) {
			g.fillOval(x + 3, y + 3, 9, 9);
		}
		if (playerwhoisit == 2) {
			g.fillOval(x1 + 3, y1 + 3, 9, 9);
		}
		if (playerwhoisit == 3) {
			g.fillOval(x2 + 3, y2 + 3, 9, 9);
		}
		
		g.fillRect(260, 360, 100, 10);
		g.fillRect(320, 360, 10, 70);
		g.fillRect(470, 112, 100, 100);
		g.fillRect(350, 290, 10, 70);
		g.fillRect(147, 114, 10, 60); //_|_ on left
		g.fillRect(57, 204, 60, 10);
		g.fillRect(187, 204, 60, 10);
		
		
		g.drawString("Who's it: " + whosit + "   playerwhoisit: " + playerwhoisit + "   upgradenumber: " + upgradenumber + "p1 X: " + x + "p1 Y: " + y, 370 - 50, 15);
		if (playerwhoisit == 1)whosit = "Player 1";
		if (playerwhoisit == 2)whosit = "Player 2";
		if (playerwhoisit == 3)whosit = "Player 3";
		g.drawString(notification, 360, 27);
		
		if(player.intersects(player2) && playerwhoisit == 1) {
				playerwhoisit = 2;
				x = 15;
				y = 15;
				x1 = 760;
				y1 = 560;
		}
		if(player.intersects(player3) && playerwhoisit == 1) {
			playerwhoisit = 3;
			x = 15;
			y = 15;
			x2= 15;
			y2 = 560;
	}
		if(player2.intersects(player3) && playerwhoisit == 2) {
			playerwhoisit = 3;
			x1 =760;
			y1 = 560;
			x2= 15;
			y2 = 560;
	}
		if(player2.intersects(player) && playerwhoisit == 2) {
			playerwhoisit = 1;
			x = 15;
			y = 15;
			x2= 15;
			y2 = 560;
	}
		if(player3.intersects(player) && playerwhoisit == 3) {
			playerwhoisit = 1;
			x = 15;
			y = 15;
			x2= 15;
			y2 = 560;
	}
		if(player3.intersects(player2) && playerwhoisit == 3) {
			playerwhoisit = 2;
			x1 = 760;
			y1 = 560;
			x2= 15;
			y2 = 560;
	} 
		if(player.intersects(obstacle1) || player.intersects(obstacle2) || player.intersects(obstacle3) || player.intersects(obstacle4) || player.intersects(obstacle5) || player.intersects(obstacle6) || player.intersects(obstacle7) || player.intersects(top) || player.intersects(bottom) || player.intersects(right) || player.intersects(left)) {
			x = 15;
			y = 15;
			notification = "Player 1 got zapped!";
			p1speed = 3;
		}
		if(player2.intersects(obstacle1) || player2.intersects(obstacle2) || player2.intersects(obstacle3) || player2.intersects(obstacle4) || player2.intersects(obstacle5) || player2.intersects(obstacle6) || player2.intersects(obstacle7) || player2.intersects(top) || player2.intersects(bottom) || player2.intersects(right) || player2.intersects(left)) {
			x1 = 760;
			y1 = 560;
			notification = "Player 2 got zapped!";
			p2speed = 3;
		}
		if(player3.intersects(obstacle1) || player3.intersects(obstacle2) || player3.intersects(obstacle3) || player3.intersects(obstacle4) || player3.intersects(obstacle5) || player3.intersects(obstacle6) || player3.intersects(obstacle7) || player3.intersects(top) || player3.intersects(bottom) || player3.intersects(right) || player3.intersects(left)) {
			x2 = 15;
			y2 = 560;
			notification = "Player 3 got zapped!";
			p2speed = 3;
		}
		if(player.intersects(upgrade)) {
			p1speed++;
			upgradenumber = 0;
			rand1 = (int) (Math.random()*800);
			rand2 = (int) (Math.random()*600);
			notification = "Player 1 got a speed upgrade!";
		}
		if(player2.intersects(upgrade)) {
			p2speed++;
			upgradenumber = 0;
			rand1 = (int) (Math.random()*800);
			rand2 = (int) (Math.random()*600);
			notification = "Player 2 got a speed upgrade!";
		}
		if(player3.intersects(upgrade)) {
			p3speed++;
			upgradenumber = 0;
			rand1 = (int) (Math.random()*800);
			rand2 = (int) (Math.random()*600);
			notification = "Player 3 got a speed upgrade!";
		}
		
		if(upgradenumber == 1200) {
			g.fillRect(rand1, rand2, 10, 10);
		}
	}
	
	public KeyListener hello = new KeyListener() {
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		if(key == KeyEvent.VK_UP)directionToUpdate = 1;
		if(key == KeyEvent.VK_DOWN)directionToUpdate = 2;
		if(key == KeyEvent.VK_LEFT)directionToUpdate = 3;
		if(key == KeyEvent.VK_RIGHT)directionToUpdate = 4;
		if(key == KeyEvent.VK_W)directionToUpdate2 = 11;
		if(key == KeyEvent.VK_S)directionToUpdate2 = 12;
		if(key == KeyEvent.VK_A)directionToUpdate2 = 13;
		if(key == KeyEvent.VK_D)directionToUpdate2 = 14;
		if(key == KeyEvent.VK_U)directionToUpdate3 = 21;
		if(key == KeyEvent.VK_J)directionToUpdate3 = 22;
		if(key == KeyEvent.VK_H)directionToUpdate3 = 23;
		if(key == KeyEvent.VK_K)directionToUpdate3 = 24;
	}
	
	public void keyTyped(KeyEvent e) {
		int key = e.getKeyCode();
	}public void keyReleased(KeyEvent e) {
		int key = e.getKeyCode();
		if(key == KeyEvent.VK_UP)directionToUpdate = 0;
		if(key == KeyEvent.VK_DOWN)directionToUpdate = 0;
		if(key == KeyEvent.VK_LEFT)directionToUpdate = 0;
		if(key == KeyEvent.VK_RIGHT)directionToUpdate = 0;
		if(key == KeyEvent.VK_W)directionToUpdate2 = 10;
		if(key == KeyEvent.VK_S)directionToUpdate2 = 10;
		if(key == KeyEvent.VK_A)directionToUpdate2 = 10;
		if(key == KeyEvent.VK_D)directionToUpdate2 = 10;
		if(key == KeyEvent.VK_U)directionToUpdate3 = 20;
		if(key == KeyEvent.VK_H)directionToUpdate3 = 20;
		if(key == KeyEvent.VK_J)directionToUpdate3 = 20;
		if(key == KeyEvent.VK_K)directionToUpdate3 = 20;
		
	}
};
	
	@Override
	public void run() {}
	public void stop(){}
	public void start(){}
	public void destroy(){}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		
	}
}

This post has been edited by hunterb100: 12 July 2012 - 11:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Java tag game tagging not working properly

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Java tag game tagging not working properly

Posted 13 July 2012 - 12:10 AM

It's caused by your 'intersects' logic that begins at line 147 and the fact that both two-intersects-three and three-intersects-two (for example) are true at the same time. You pass 'whoisit' in the first intersection but then pass it back in the second intersection. Maybe if/else logic would solve the immediate pass back problem.
Was This Post Helpful? 0
  • +
  • -

#3 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java tag game tagging not working properly

Posted 13 July 2012 - 06:23 PM

I attempted to fix it, but I'm not quite sure on how to apply the if/else logic. I assume I'm not doing it properly though. Sorry if my code is pretty basic, I've only been teaching myself as I'm in Grade 8, so no programming or computer science courses until Grade 9. New code:

		if(player.intersects(player2) && playerwhoisit == 1) {
				playerwhoisit = 2;
				x = 15;
				y = 15;
				x1 = 760;
				y1 = 560;
		}else if(player.intersects(player3) && playerwhoisit == 1) {
			playerwhoisit = 3;
			x = 15;
			y = 15;
			x2= 15;
			y2 = 560;
	}

		if(player2.intersects(player3) && playerwhoisit == 2) {
			playerwhoisit = 3;
			x1 =760;
			y1 = 560;
			x2= 15;
			y2 = 560;
	}else if(player2.intersects(player) && playerwhoisit == 2) {
			playerwhoisit = 1;
			x = 15;
			y = 15;
			x2= 15;
			y2 = 560;
	}
		
		if(player3.intersects(player) && playerwhoisit == 3) {
			playerwhoisit = 1;
			x = 15;
			y = 15;
			x2= 15;
			y2 = 560;
	}else if(player3.intersects(player2) && playerwhoisit == 3) {
			playerwhoisit = 2;
			x1 = 760;
			y1 = 560;
			x2= 15;
			y2 = 560;
	} 

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java tag game tagging not working properly

Posted 13 July 2012 - 08:09 PM

if(player.intersects(player3) && playerwhoisit == 1) {

why not simply testing that player != player3 ?

And learn how to use array before writing program like that. What will you do when you will write a version with 50 players and 100 obstacles ?
Was This Post Helpful? 1
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Java tag game tagging not working properly

Posted 14 July 2012 - 12:19 AM

Quote

I attempted to fix it, but I'm not quite sure on how to apply the if/else logic. I assume I'm not doing it properly though. Sorry if my code is pretty basic, I've only been teaching myself as I'm in Grade 8, so no programming or computer science courses until Grade 9.

Congrats on your early start, and don't give up.

Though an if/else construct may have helped, I realized that the real problem is you're making intersection comparisons that you don't need which are causing game state changes that you don't want.

Since you currently have 3 players, the intersections, or unique collisions, that matter are 1 with 2, 1 with 3, and 2 with 3. The collisions 2 with 1, 3 with 1, and 3 with 2 are exactly the same (redundant) and unnecessary to check. In fact, checking the redundant collisions is causing the undesired results.

So first determine which players have collided, then which of the 2 players involved in the collision is currently 'it', then transfer the 'it' status from the player currently 'it' to the other involved in the collision.

pbl's point, a good one, is that your current design does not scale up well for the possible future case of having more than 3 players. With 3 players, 3 collisions of interest; four players, 6; 5 players, 10; etc. (Can you calculate the number of unique collisions for any number of players?) Using arrays of players and obstacles will simplify the algorithm to check for collisions.

When you get deeper into OOP and create Player, Game, and Obstacle objects, rather than checking all possible collisions at some frequency, you might add a CollisionListener (your own invention) to the Player objects which would then make the appropriate Person, Obstacle, and Game state changes when a collision occurs.
Was This Post Helpful? 0
  • +
  • -

#6 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java tag game tagging not working properly

Posted 14 July 2012 - 10:41 AM

View Postpbl, on 13 July 2012 - 08:09 PM, said:

why not simply testing that player != player3 ?

And learn how to use array before writing program like that. What will you do when you will write a version with 50 players and 100 obstacles ?


I feel like a retard now because one, I don't know what you mean by testing 'player != player3'. And on the second note, I've attempted to learn to use arrays, but never succeeded in using them.
Was This Post Helpful? 0
  • +
  • -

#7 hunterb100  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 144
  • Joined: 29-August 11

Re: Java tag game tagging not working properly

Posted 19 July 2012 - 07:39 PM

Bump? Been 5 days. Still stuck on that problem.
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Java tag game tagging not working properly

Posted 20 July 2012 - 01:13 AM

Show your latest code. If you're still stuck at the same place - same code, same results - why haven't you tried my suggestion to eliminate redundant intersection checks so that there will not be double tagging? If you didn't understand my point(s), please say so, ask questions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1