Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Arrowkey movement for Pong
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
grahamhallett




PostPosted: Mon Jan 01, 2007 2:05 pm   Post subject: Arrowkey movement for Pong

This is the movement part of my program so far:
public void keyTyped(KeyEvent e)
{
keypressed += e.getKeyChar();

if (keypressed.equals("w"))
{
repaint();


try{
Thread.sleep(100);
}
catch(InterruptedException d)
{

}

Graphics g = getGraphics();

g.setColor(Color.GREEN);
g.fillRect(10,paddle1y, 10,40);
paddle1y = paddle1y- paddle1ychange;
keypressed ="";

}
else if (keypressed.equals("s"))
{
repaint();


try{
Thread.sleep(100);
}
catch(InterruptedException d)
{

}

Graphics g = getGraphics();

g.clearRect(10,paddle1y, 10,40);
paddle1y = paddle1y+paddle1ychange;
keypressed ="";
}

if (keypressed.equals("i"))
{
repaint();


try{
Thread.sleep(100);
}
catch(InterruptedException d)
{

}

Graphics g = getGraphics();

g.clearRect(380,paddle2y, 10,40);
paddle2y = paddle2y-paddle2ychange;
keypressed = "";

}
else if (keypressed.equals("k"))
{
repaint();


try{
Thread.sleep(100);
}
catch(InterruptedException d)
{

}

Graphics g = getGraphics();

g.setColor(Color.GREEN);
g.fillRect(380,paddle2y, 10,40);
paddle2y = paddle2y+paddle2ychange;
keypressed ="";
}





else{
keypressed = "";
}
}

public void keyPressed(KeyEvent d)
{

}

public void keyReleased(KeyEvent f)
{

}

public void update(Graphics g)
{
paint(g);
}

}

I am trying to change the movement with i and k to using movement with the arrowkeys but i am not sure how. Any suggestions? This program is pong by the way.
Sponsor
Sponsor
Sponsor
sponsor
ericfourfour




PostPosted: Mon Jan 01, 2007 7:08 pm   Post subject: (No subject)

I'm not to sure what you are asking but is this what you are trying to do?

code:
if i pressed
    move paddle up on y-axis
else if k pressed
    move paddle down on y-axis


If so, that is not very hard to implement once you understand what you are doing.

Also if you are going to use threads why not implement one and use the method run to repaint your program instead of writing it inside every if.

All painting should also be done in the paint method. It was made for a reason.

You should also try to stick to one way to use curly brackets. At some parts i see them immediately after the command and other I see them on the next line. For example:
code:
try{
Thread.sleep(100);
}
catch(InterruptedException d)
{

}
Thread.sleep(100) should also be indented in this example. Actually, most of your code switched between different styles making it very hard to read. You should try sticking to one, preferably the one that is used among most Java programmers (look at some source code to see).
grahamhallett




PostPosted: Tue Jan 02, 2007 11:58 pm   Post subject: (No subject)

I am simply trying ot make the paddle move with arrow keys. I was just showing you what i ahve for the movement so far i was just wondering what i have to put in order to do the arrow keys.
ericfourfour




PostPosted: Wed Jan 03, 2007 11:22 pm   Post subject: (No subject)

I'm too lazy to check what it is named, but instead of using e.getKeyChar(); use the one that returns an integer. Then use the VK_'s for the keys. If you have an IDE with an intellisense thing you should be able to find the appropriate one by typing KeyEvent., and if not you can either look up the KeyEvent javadoc or find a list.
grahamhallett




PostPosted: Thu Jan 04, 2007 1:40 pm   Post subject: (No subject)

I am a newbie -_- No idea what you are talking about ha.
ericfourfour




PostPosted: Thu Jan 04, 2007 5:10 pm   Post subject: (No subject)

Look for KeyEvent in the javadoc. You might even find a good tutorial from there.
grahamhallett




PostPosted: Sat Jan 06, 2007 10:04 am   Post subject: (No subject)

Anyone else? :O
wtd




PostPosted: Sat Jan 06, 2007 10:11 am   Post subject: (No subject)

You will be more likely to receive help if your code is posted in its entirety, in code tags, and is well-formatted.
Sponsor
Sponsor
Sponsor
sponsor
grahamhallett




PostPosted: Sat Jan 06, 2007 10:38 am   Post subject: (No subject)

code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FinalProjectHal extends JApplet implements KeyListener//makes it so keys are able to move
{
        int x = 10;//variables
        int y = 12 ;
        int xChange =10;
        int yChange = 10;
       
        String keypressed = "";
       
       
        int paddle1y = 180;//paddle variables
        int paddle2y = 180;

        int paddle1ychange = 10;
        int paddle2ychange = 10;
       
        public void init()
        {
        addKeyListener(this);//allows it so keys can be pressed

       
        }       
        //background and ball
        public void paint(Graphics g)
        {
        g.setColor(Color.YELLOW);
        g.fillRect(1,1,399,399);
        g.setColor(Color.BLACK);
        g.fillOval(x,y,15,15);
        g.fillRect(10,paddle1y,10,40);
        g.fillRect(380,paddle2y,10,40);
        repaint();
       
       

                try {Thread.sleep(100); //delay time in millisecs
                }
                catch(InterruptedException ex) { }
                g.setColor(Color.YELLOW);
                g.fillRect(x,y,15,15);

        x = x + xChange;//set variables to allow bouncing to occurr
        y = y + yChange;
        //bounce part of the program
        if (x>=400|| x<=1)
        {
        xChange = -xChange;
        }       
        else{}
       
        if (y>=400|| y<=1)
        {
        yChange = -yChange;
        }       
        else{}
       
        if(x <= 20 && y >= paddle1y && y<= (paddle1y+40))
        {
                xChange = -xChange;
        }
        else{}
       
        if(x >= 365 && y >= paddle2y && y<= (paddle2y+40))
        {
                xChange = -xChange;
        }
        else{}
       
        }
       
       
        //movement of the paddles part of the program
                public void keyTyped(KeyEvent e)
                {
                        keypressed += e.getKeyChar();
                       
                        if (keypressed.equals("w"))
                        {
                        repaint();
                       
                       
                        try{
                        Thread.sleep(100);
                        }
                        catch(InterruptedException d)
                        {
                       
                        }
                       
                        Graphics g = getGraphics();
                       
                        g.setColor(Color.YELLOW);
                        g.fillRect(10,paddle1y, 10,40);
                        paddle1y = paddle1y- paddle1ychange;
                        keypressed  ="";               
                               
                        }
                        else if (keypressed.equals("s"))
                        {
                        repaint();
                       
                       
                        try{
                        Thread.sleep(100);
                        }
                        catch(InterruptedException d)
                        {
                       
                        }
                       
                        Graphics g = getGraphics();
                       
                        g.clearRect(10,paddle1y, 10,40);
                        paddle1y = paddle1y+paddle1ychange;
                        keypressed  ="";
                        }
                       
                        if (keypressed.equals("i"))
                        {
                        repaint();
                       
                       
                        try{
                        Thread.sleep(100);
                        }
                        catch(InterruptedException d)
                        {
                       
                        }
                       
                        Graphics g = getGraphics();
                       
                        g.clearRect(380,paddle2y, 10,40);
                        paddle2y = paddle2y-paddle2ychange;
                        keypressed  = "";              
                               
                        }
                        else if (keypressed.equals("k"))
                        {
                        repaint();
                       
                       
                        try{
                        Thread.sleep(100);
                        }
                        catch(InterruptedException d)
                        {
                       
                        }
                       
                        Graphics g = getGraphics();
                       
                        g.setColor(Color.YELLOW);
                        g.fillRect(380,paddle2y, 10,40);
                        paddle2y = paddle2y+paddle2ychange;
                        keypressed  ="";
                        }
                       
                       
                       
                       
                       
                        else{
                        keypressed = "";
                        }
        }
                //needed for movement of paddles when the right keys are pressed.
                public void keyPressed(KeyEvent d)
                {
               
                }
               
                public void keyReleased(KeyEvent f)
                {
               
                }
               
                public void update(Graphics g)
                {
                        paint(g);
                }
       
}
grahamhallett




PostPosted: Sat Jan 06, 2007 10:39 am   Post subject: (No subject)

Here's the code. Not sure what code tags are.
wtd




PostPosted: Sat Jan 06, 2007 11:54 am   Post subject: (No subject)

To deal with your earlier question, the KeyEvent class contains a number of integer constants which represent the various keys on your keyboard. If you use these, then you can identify a key which does not have a reasonable string representation.
grahamhallett




PostPosted: Mon Jan 08, 2007 11:58 am   Post subject: Re: Arrowkey movement for Pong

okay i figured out the different integers for the arrow keys and figured out what i needed. Thanks a lot everyone.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: