Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
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   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Figuring out the Mouse Listener on: 2011-05-12 21:51:59
So, how do you assign text to the card? Why not assign a point value at the same time? I'd assume a card could easily have a private "instance variable" to hold the point value, as well as a public getter and setter for the same.

I did that and used the bit of code ra4king posted and it works, at least as far as I can tell so far Cheesy Thanks guys.
2  Game Development / Newbie & Debugging Questions / Re: Figuring out the Mouse Listener on: 2011-05-12 20:28:49
Oops, didn't realize I didn't put the Card class up.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public class Card{

    private String face;
    private String suit;


    public Card(String cardFace, String cardSuit)
    {
        face = cardFace;
        suit = cardSuit;
    }


    @Override
    public String toString()
    {
        return face + " of " + suit;
    }
}


I was pretty tired when I was trying to work on this last night so that's why some parts look pretty nonsensical .
3  Game Development / Newbie & Debugging Questions / Re: Figuring out the Mouse Listener on: 2011-05-12 04:45:45
Should I try reposting it?
4  Game Development / Newbie & Debugging Questions / Re: Figuring out the Mouse Listener on: 2011-05-12 04:17:41
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  
 public int getCardValue()
    {
        int pointValue = 0;
        for(int j = 0; j < 2; j++)
        {
            for(int i = 0; i < faces.length; i++)
            {
                if(faces[i].equals("Ace"))
                {
                    pointValue += 11;
                    break;
                }
                if(faces[i].equals("Two"))
                {
                    pointValue += 2;
                    break;
                }
                if(faces[i].equals("Three"))
                {
                    pointValue += 3;
                    break;
                }
                if(faces[i].equals("Four"))
                {
                    pointValue += 4;
                    break;
                }
                if(faces[i].equals("Five"))
                {
                    pointValue += 5;
                    break;
                }
                if(faces[i].equals("Six"))
                {
                    pointValue += 6;
                    break;
                }
                if(faces[i].equals("Seven"))
                {
                    pointValue += 7;
                    break;
                }
                if(faces[i].equals("Eight"))
                {
                    pointValue += 8;
                    break;
                }
                if(faces[i].equals("Nine"))
                {
                    pointValue += 9;
                    break;
                }
                else
                    pointValue += 10;
 
            }
        }
        return pointValue;
    }


This is pretty much the part I'm trying to figure out. I was wanting to maybe set a value for the card in the card class, but I couldn't really get it working, so I tried doing this. But this just looks at the faces string array of the deck class and I want it to be looking at the cards dealt to the player. I tried using the array I'm using for the cards for the player doing playerCards.get(i).equals() but it wants an object and so I can't do that either.
5  Game Development / Newbie & Debugging Questions / Re: Figuring out the Mouse Listener on: 2011-05-12 01:57:10
Alright, so due to lack of time to work on this, I'm gonna be do the gui as a side project and I'm gonna do this through text. I'm having trouble figuring out how to go about adding values from the cards to get the player/computer's score. So here's what I have.

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  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
public class BlackJack {

    Player player = new Player();
    Player dealer = new Player();
    Deck deck = new Deck();
    boolean valid;
    enum Status {WON, LOST, CONTINUE};
    Status gameStatus = Status.CONTINUE;

    public void run()
    {
        JOptionPane.showMessageDialog(null, "Welcome to the wonderful world of Black Jack!");

        String temp = JOptionPane.showInputDialog("How much do you wish to"
                    + " sacrifice to the BlackJack gods?");

        while(!valid)
        {
            if( Double.parseDouble(temp) > 0.0){
                valid = !valid;
                player.setCredits(Double.parseDouble(temp));
                System.out.println("Your credits: " + player.getCredits());
            }
            else
            {
                temp = JOptionPane.showInputDialog("Invalid, enter a positive"
                        + " number or q to exit");
                if(temp.equals("q"))
                    System.exit(0);
            }
        }

        while(gameStatus.equals(gameStatus.CONTINUE))
        {
            valid = !valid;
            temp = JOptionPane.showInputDialog("How much you wanna bet bro?");
            while(!valid)
            {
                if(Double.parseDouble(temp) > 0.0 &&
                        Double.parseDouble(temp) <= player.getCredits())
                {
                    valid = !valid;
                    player.setBet(Double.parseDouble(temp));
                    System.out.println("Betting: " + player.getBet());
                    player.setCredits(player.getCredits() - player.getBet());
                }
                else
                {
                    temp = JOptionPane.showInputDialog("Invalid or insufficient"
                            + " funds, enter a positive number");
                }

            }
            System.out.println("Dealing Cards...");
            deck.shuffle();
           
            player.playerCards.add(deck.dealCard());
            dealer.playerCards.add(deck.dealCard());
            player.playerCards.add(deck.dealCard());
            dealer.playerCards.add(deck.dealCard());

            System.out.printf("Dealer shows: %s\nYou have: %s\n",
                    dealer.displaySecondCard(), player.displayHand());

            System.out.println(player.getCardValue());



        }
    }
}

public class Deck {

    private Card[] deck;
    private int currentCard;
    String[] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
            "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    private static final int NUMBER_OF_CARDS = 52;
    private static final Random myR = new Random();

    public Deck()
    {
       
        String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};

        deck = new Card[NUMBER_OF_CARDS];
        currentCard = 0;

        for (int count = 0; count < deck.length; count++)
            deck[count] = new Card(faces[count % 13], suits[count / 13]);
    }

    public void shuffle()
    {

        currentCard = 0;

        for(int first = 0; first < deck.length; first++)
        {
            int second = myR.nextInt(NUMBER_OF_CARDS);

            Card temp = deck[first];
            deck[first] = deck[second];
            deck[second] = temp;
        }

    }

   

    public Card dealCard()
    {

        if(currentCard < deck.length)
            return deck[currentCard++];
        else
            return null;
    }

}

public class Player extends Deck{

    private double bet;
    private double credits;
    private double insurance;
    boolean hasBlackJack, hasInsurance;
    public ArrayList< Card > playerCards = new ArrayList< Card >();

   
    public double getBet()
    {
        return bet;

    }

    public void setBet(double num)
    {
        bet = num;
    }

    public double getCredits()
    {
        return credits;
    }

    public void setCredits(double num)
    {
        credits = num;
    }

    public double getInsurance()
    {
        return insurance;
    }

    public void setInsurance(double num)
    {
        insurance = num;
    }

    public String displaySecondCard()
    {
        return String.format("%s", playerCards.get(1));
    }

    public String displayHand()
    {
        return String.format("%s,  %s", playerCards.get(0), playerCards.get(1));
    }


    public int getCardValue()
    {
        int pointValue = 0;
        for(int j = 0; j < 2; j++)
        {
            for(int i = 0; i < faces.length; i++)
            {
                if(faces[i].equals("Ace"))
                {
                    pointValue += 11;
                    break;
                }
                if(faces[i].equals("Two"))
                {
                    pointValue += 2;
                    break;
                }
                if(faces[i].equals("Three"))
                {
                    pointValue += 3;
                    break;
                }
                if(faces[i].equals("Four"))
                {
                    pointValue += 4;
                    break;
                }
                if(faces[i].equals("Five"))
                {
                    pointValue += 5;
                    break;
                }
                if(faces[i].equals("Six"))
                {
                    pointValue += 6;
                    break;
                }
                if(faces[i].equals("Seven"))
                {
                    pointValue += 7;
                    break;
                }
                if(faces[i].equals("Eight"))
                {
                    pointValue += 8;
                    break;
                }
                if(faces[i].equals("Nine"))
                {
                    pointValue += 9;
                    break;
                }
                else
                    pointValue += 10;

            }
        }
        return pointValue;
    }


I've been messing with it for around 3-4 hours and can't figure it out. Any help is appreciated.
6  Game Development / Newbie & Debugging Questions / Re: Figuring out the Mouse Listener on: 2011-04-29 19:12:16
I don't think I'm getting that exception anymore but I'm getting some other error or something but I don't remember what it is. I'll see if I can post it after I get back from class.
7  Game Development / Newbie & Debugging Questions / Re: Figuring out the Mouse Listener on: 2011-04-28 19:48:23
Ah, thank you so much! I was wondering how I set where the button shows up. I messed around with it a bit earlier but I was never able to change the location of the button when everything is first initialized. It always stays center aligned at the top. It seems that I can set the location of it any time after it's started but if I do something like resize the window goes back to being at the top and centered.
I was also thinking of adding a little casino music or something but I'm not too worried about that.
8  Game Development / Newbie & Debugging Questions / Re: Figuring out the Mouse Listener on: 2011-04-28 09:03:44
I'm not too sure what would be causing an infinite loop in the constructor, maybe you can see it?
Here's what I got.

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  
public class BlackJackPanel extends JPanel implements ActionListener {

    BlackJack blackJack = new BlackJack();
    JButton button = new JButton("Button");
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Stuff");
    JMenuItem menuStart = new JMenuItem("Start");
    JMenuItem menuExit = new JMenuItem("Exit");
    boolean buttonClicked;

    public BlackJackPanel(){
        button.addActionListener(this);
        menuStart.addActionListener(this);
        menuExit.addActionListener(this);
    }


    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.blue);
        g.fillRect(1, 1, this.getWidth(), this.getHeight());
        g.setColor(Color.yellow);
        if(buttonClicked)
        {
            g.drawString("CATS a;kldsjf;lkadjfl;kadkj;fas", 50, 50);
        }

    }

    public void actionPerformed(ActionEvent e) {

        if(e.getSource().equals(button))
        {
            buttonClicked = !buttonClicked;
            repaint();
        }
        if(e.getSource().equals(menuStart))
        {

        }
        if(e.getSource().equals(menuExit))
            System.exit(0);
    }

}

public class BlackJack extends JPanel implements ActionListener, KeyListener, MouseListener {

    BlackJackPanel panel = new BlackJackPanel();
    private int credits;
    boolean validInput;
    Random myR = new Random();




    public BlackJack()
    {
        JFrame frame = new JFrame("BlackJack");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setSize(500,500);
        frame.add(panel);
        panel.add(panel.button);
        panel.menuBar.add(panel.menu);
        panel.menu.add(panel.menuStart);
        panel.menu.add(panel.menuExit);
        panel.menuStart.addActionListener(this);
        panel.menuExit.addActionListener(this);
        frame.setJMenuBar(panel.menuBar);
        frame.setVisible(true);

    }


    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource().equals(panel.menuStart))
        {
            run();
        }
    }
     
    public void run()
    {
        while(true)
        {

        }  
    }
9  Game Development / Newbie & Debugging Questions / Re: Figuring out the Mouse Listener on: 2011-04-28 08:44:34
Working a bit more with the action listener. In the blackJackPanel I have small menu bar set up and one of the options is to start a new black jack game. So clicking that would call the method in blackJack that starts running the game but I can't really do that cause I'd need to instantiate blackJack and doing that in blackJackPanel causes a stack overflow or something. I can easily just get it done by having the actionPerformed in blackJack do this but I was wondering if there was a way to do it from the panel class. Thanks for all your help guys, I really really appreciate it. Also, philfrei, your Hexara game looks pretty good. How long has it taken you to make that?
10  Game Development / Newbie & Debugging Questions / Re: Figuring out the Mouse Listener on: 2011-04-28 01:32:32
Alright, so I've pretty much figured out what I need to know for the action listener, I think. I'm still having troubles with what I want to do with the gui part. I tried making the frame and everything in the BlackJack constructor but then the paint component wouldn't do anything. If I made the frame and everything in main the paint component works but I'm not quite sure how to get the action listener to work if I have to add stuff to the gui from main. I'm pretty much learning this on my own as this isn't really covered in the class at all.
Should I be going with this?

I'd instantiate the JFrame in my BlackJack class, then "add" a BlackJackJPanel (separate class, subclassing JPanel) to the JFrame, and put my button in the subclassed JPanel.

The subclassing JPanel would be the "extends JPanel" part, right? We're just getting into super/sub classes and whatnot so I'm not sure if I completely understand it yet. So, if I did this, I'd need to instantiate the blackjackpanel and add it to the frame in blackjack and I'd be able to use the button and actionlistener and all that? Would I need to put the paintcomponent in the blackjackpanel too? Or could I leave it in the blackjack class?
11  Game Development / Newbie & Debugging Questions / Re: Figuring out the Mouse Listener on: 2011-04-26 08:49:22
Does anything call the start() method? If nothing calls it, then your thread isn't initialized, therefore you have no loop.
I had it in the main but I guess I forgot to include the main in my post.
I decided to start over with this class and see if that would make a difference but paintComponent still isn't doing anything. All I have for it is this.

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  
public class BlackJack extends JPanel implements KeyListener, MouseListener {

    private JButton button = new JButton("This is a JButton");
    boolean buttonClicked;

    public BlackJack()
    {
        JFrame frame = new JFrame("BlackJack");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setVisible(true);
        frame.setSize(500,500);
        frame.addKeyListener(this);
        frame.addMouseListener(this);
    }

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

        g.setColor(Color.BLACK);
        g.fillRect(1, 1, getWidth(), getHeight());
    }

    public void run()
    {
        while(true)
        {
            repaint();
            try{
                Thread.sleep(1000);
            }
            catch(InterruptedException e){}

        }
    }
}

public static void main(String[] args) {
    BlackJack game = new BlackJack();
    game.run();
}


At this point I'm just trying to get it to draw anything...I could just be being very stupid about this as I am tired from work and it's almost 1am. Maybe I'll figure it out in the morning or something.
12  Game Development / Newbie & Debugging Questions / Re: Figuring out the Mouse Listener on: 2011-04-26 07:37:40
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
    public void run(){

        while(true)
        {
            repaint(); // <--- add this line
           try
            {
                Thread.sleep (25);
            }
            catch (InterruptedException ex){}
        }    
    }

That should solve your immediate problem, but if someone clicks and releases in less than 25ms (unlikely) then it won't work!

Ah. I completely forgot about the repaint(), sadly that didn't fix it. After having the time to mess with it a bit I found that the paintcomponent isn't doing anything at all, and with the setLayout thing it sets the button in the center of the frame and can't really change it's location, and without the layout, the button takes up the whole panel. I thought about making the frame and everything in the main class like I did with my pong game and scrap the constructor but then I wouldn't be able to use the button in the Black Jack class. I'm kinda at a loss of what to do lol.
13  Game Development / Newbie & Debugging Questions / Figuring out the Mouse Listener on: 2011-04-26 00:13:53
I'm taking a java class and have to make a Black Jack game for the final and I wanted to implement a GUI for it. Only problem is I don't really know how to use the Mouse Listener. Or rather, I don't know how to get it to do what I want it to do. For now all I want to do is draw a string when I button is clicked or pressed. I can get it to print in the command line but it won't draw the string. I'm also going off of the experience I got from making a pong game for my midterm so I don't know if some of this stuff is needed for a game like Black Jack. Here's what I got to start off with if it helps you at all, any help is appreciated.

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  
public class BlackJack extends JPanel implements Runnable, KeyListener, MouseListener {

    private JButton button = new JButton("This is a JButton");
    boolean buttonClicked;


    public BlackJack() {
            JFrame frame = new JFrame("BlackJack");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new FlowLayout());
            frame.setSize(500,500);
            button.addMouseListener(this);
            frame.add(button);
            frame.setVisible(true);
            button.setVisible(true);


    }



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

        if(buttonClicked)
        {
            g.setColor(Color.BLACK);
            g.drawString("The button was clicked", 50,50);
        }
    }

    public void mouseClicked(MouseEvent e) {
       
        if(e.getSource().equals(button))
        {
            if(!buttonClicked)
                buttonClicked = true;
            else
                buttonClicked = false;

        }
       
    }

    public void start(){
        Thread th = new Thread (this);
        th.start ();
    }

    public void run(){

        while(true)
        {
            try
            {
                Thread.sleep (25);
            }
            catch (InterruptedException ex){}
        }
       
    }


I think that's all I need to put up for this. Anything else I have so far is just empty key and mouse listener methods.
14  Game Development / Newbie & Debugging Questions / Re: Problem with a paddle for pong game on: 2011-03-24 03:32:52
Well in if statement, if the (x_pos+x_width) is not exactly player2X, it will return false. What if you are 1 pixel off?

Try this:
1  
2  
if((x_pos+x_width) >= player2X && (x_pos+x_width) <= (player2X+player2W) && (y_pos+y_height) >= player2Y && y_pos <= (player2Y+player2H))
   moveBallRight=false;

This will check if he intersects anywhere.

Also, have you checked the java.awt.geom package? There is Ellipse2D and Rectangle2D and you can do ellipse.intersects(rectangle).
http://ra4king.is-a-geek.net/javadocs/java/awt/geom/package-summary.html

Thank you for your help! I'll try looking into that some time. I'm thinking of making another simple game soon. Thanks again for your help Tongue
15  Game Development / Newbie & Debugging Questions / SOLVED Problem with a paddle for pong game on: 2011-03-23 21:44:05
I'm making a simple little pong game for my java class but I can't figure out how to get the paddle on the right side to bounce the ball back over to the left side, the ball just goes through the paddle. So here's some of the code I got and a screenshot.

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  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
int x_pos = 100;
    int y_pos = 100;
    int x_width = 30;
    int y_height = 30;
    int right = 5;
    int left = -5;
    int up = -5;
    int down = 5;
    int width, height;
    int player1X = 50;
    int player1Y = 200;
    int player1W = 10;
    int player1H = 100;
    int player2X;
    int player2Y = 200;
    int player2W = 10;
    int player2H = 100;
    int player1Score = 0;
    int player2Score = 0;
    boolean moveBallDown, moveBallRight;
    boolean movePlayer1Up, movePlayer1Down;
    boolean movePlayer2Up, movePlayer2Down;
    boolean game, gameOver;

    public void start ()
    {

        Thread th = new Thread (this);
        th.start ();
        game = true;
    }

    @Override
    public void paintComponent( Graphics g )
    {
        super.paintComponent( g );

        g.setColor(Color.blue);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.black);
        g.fillOval(x_pos, y_pos, x_width, y_height);
        g.fillRect(player1X, player1Y, player1W, player1H);
        g.fillRect(player2X, player2Y, player2W, player2H);
        g.setColor(Color.yellow);
        g.drawString("Player1 Score: ", 20, 10);
        g.drawString(Integer.toString(player1Score), 105, 10);
        g.drawString("Player2 Score: ", getWidth() - 150, 10);
        g.drawString(Integer.toString(player2Score), getWidth() - 70, 10);

        if (gameOver)
            g.drawString("GAME OVER", getWidth()/2-30, getHeight()/2);


    }

    public void drawShape (int new_x, int new_y)
    {
        x_pos = new_x;
        y_pos = new_y;
    }

    public void drawPlayer1 ( int new_y)
    {
       
        player1Y = new_y;
    }

    public void drawPlayer2 ( int new_y)
    {
        player2X = getWidth() - 50;
        player2Y = new_y;
    }
   

    public void run ()
    {

        moveBallRight = true;
        moveBallDown = true;

        while (game)
        {

            moveShape();
            drawShape(x_pos, y_pos);
            movePlayer();
            drawPlayer1(player1Y);
            drawPlayer2(player2Y);
            repaint();

            try
            {
                Thread.sleep (25);
            }
            catch (InterruptedException ex){}

            if (player1Score == 10 || player2Score == 10)
            {
                game = false;
                gameOver = true;
            }

        }
       
    }


    public void moveShape()
    {

        if(moveBallRight)
        {
            x_pos += right;
            if (x_pos >= (getWidth() - x_width)){
                moveBallRight = false;
                player1Score++;
            }
        }
        else
        {
            x_pos += left;
            if ( x_pos <= 0){
                moveBallRight =  true;
                player2Score++;
            }
        }
        if(moveBallDown)
        {
            y_pos += down;
            if(y_pos >= (getHeight()-y_height))
                moveBallDown = false;
        }
        else
        {
            y_pos += up;
            if(y_pos <= 0)
                moveBallDown = true;
        }
       
        //bounces ball off left paddle
       if(x_pos == (player1X + player1W) && (y_pos >= player1Y || (y_pos+30) >= player1Y) && y_pos <= (player1Y+75))
            moveBallRight = true;
        //Supposed to bounce ball off right paddle
       if((x_pos + x_width) == player2X && (y_pos >= player2Y || (y_pos+30) >= player2Y) && y_pos <= (player2Y+75))
            moveBallRight = false;


    }




Just to clarify a couple things x/y_pos is the starting point to draw the ball player x/y is the starting point for the paddles. I have getWidth() for the right paddle in the drawPlayer2 if whoever runs this makes the window bigger.

Anyone know what I'm doing wrong here for the last two lines of code?
Pages: [1]
 

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 (17 views)
2014-07-30 21:08:39

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

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

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

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

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

pw (43 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
List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

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
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!