Smartsav10
Smartsav10

Reputation: 123

In Race Program, can not seem to declare winner when it collides with finish line- java

I am creating a code that contains moving graphics in a JPanel. It is a race code with 3 racers. Every time the timer goes through, there is a 75% chance they move, and a 25% chance they hold.

My problem is with getting the program to print out the winner in the system console. For some reason, it always says "Orange" is the winner, just because it is the last color I added.

The intersections are just when the runners touch the finish line. There are a few custom commands but those are just to draw the background and set up the JPanel. Those work fine. The problem is that for some reason, the X value of the 4 runners seems to be every single value at once.

public class Rivals extends JFrame{

    Rivals(){
        Make.frame(this,new RivalsPane(), 512,512,JFrame.EXIT_ON_CLOSE, false);
    }

public static class RivalsPane extends JPanel implements ActionListener{
    Timer t = new Timer(1,this);
    static int x=70,x2=70,x3=70,x4=70,speed=5;
    static boolean done=false;
    static String win = " wins", winner; 

    RivalsPane(){
        Make.panel(this,512,512,null);  
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Make.FootRaceTrack(g);

        Run(g);

        t.start();
    }

    public void Run(Graphics g) {
        Paint.setPen(Color.blue);
        Paint.shadeOval(g,x,90,30,30);
        Paint.setPen(Color.pink);
        Paint.shadeOval(g,x2,190,30,30);
        Paint.setPen(Color.green);
        Paint.shadeOval(g, x3, 290, 30, 30);
        //This is the last one I set, and it always wins
        Paint.setPen(Color.ORANGE); 
        Paint.shadeOval(g, x4, 390, 30, 30);

        Rectangle r1 = new Rectangle(x-30,60, 60, 60);
        Rectangle r2 = new Rectangle(x-30,160, 60, 60);
        Rectangle r3 = new Rectangle(x-30,260, 60, 60);
        Rectangle r4 = new Rectangle(x-30,360, 60, 60);
        Rectangle r5 = new Rectangle(400,0,512,512);

        if(r1.intersects(r5)) { 
            speed = 0; 
            winner ="Blue"; 
            done = true; 
        } else if(r2.intersects(r5)) { 
            speed=0;
            winner = "Pink"; 
            done = true;
        } else if(r3.intersects(r5)) {
            speed=0;
            winner = "Green";
            done = true;
        } else if(r4.intersects(r5)) {
            speed=0;
            winner = "Orange";
            done = true;
        }
if(done==true) System.out.println(winner + "wins");
    }

    public void actionPerformed(ActionEvent e) {
        double rand1 = Math.random();
        double rand2 = Math.random();
        double rand3 = Math.random();
        double rand4 = Math.random();
        if(rand1<.75)x+=speed;
        if(rand2<.75)x2+=speed;
        if(rand3<.75)x3+=speed;
        if(rand4<.75)x4+=speed;
        repaint();
    }   
}


public static void main(String[] args) {
    Do.showFrame(new Rivals());
}   
}

Upvotes: 1

Views: 115

Answers (2)

Alex
Alex

Reputation: 866

Cause of your x position for all rectangles depends on x variable and not x2, x3, x4. From comment.

Upvotes: 1

Smartsav10
Smartsav10

Reputation: 123

All my bounding rectangles were using x with applies to blue only. Change the x's to x2,x3, and x4 and it works. Credit to @Alex

Upvotes: 0

Related Questions