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

Username:   Password: 
 RegisterRegister   
 Char Movement Help
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
TheLastFall




PostPosted: Wed Nov 22, 2006 12:27 pm   Post subject: Char Movement Help

Okay, I have what I need to make everything move. Just I need to make the program more solid. I'm using the c.getChar statment and it just delays my whole program by so much. It has to wait for input ever time, is there a way where it doesn't ask for input and it just does what you want it to do? It would also help to know how to use arrow keys instead of the character keys. Thats not too important though. My Code looks like this:
code:

char chars = ' ';
        int rad = 15;
        int xp = c.getWidth () / 2;
        int yp = 0;
        int spd = 3;
        int dir = 0;
        Color clr, clr1;
        clr = new Color ((int) (Math.random () * 256),
                (int) (Math.random () * 256),
                (int) (Math.random () * 256));
        clr1 = new Color ((int) (Math.random () * 256),
                (int) (Math.random () * 256),
                (int) (Math.random () * 256));

        while (chars != 'q')
        {
            chars = c.getChar (); // How do I make this not wait?
            delay (5);
            c.clear ();
            c.setColor (Color.white);
            c.fillRect (0, 0, c.getWidth (), c.getHeight ());

            if (chars == 'e' && chars != 'E')
            {
                clr = new Color ((int) (Math.random () * 256),
                        (int) (Math.random () * 256),
                        (int) (Math.random () * 256));
                c.setColor (clr);
                c.fillOval (xp, yp, rad, rad);
            }

            if (chars == 'E' && chars != 'e')
            {

                clr1 = new Color ((int) (Math.random () * 256),
                        (int) (Math.random () * 256),
                        (int) (Math.random () * 256));
                c.setColor (clr1);
                c.fillOval (xp, yp, rad, rad);
            }
            if (chars == KeyEvent.VK_UP)
            {
                c.setColor (clr);
                c.fillOval (xp, yp, rad, rad);
                yp -= spd;
            }
            else if (chars == KeyEvent.VK_DOWN)
            {
                c.setColor (clr);
                c.fillOval (xp, yp, rad, rad);
                yp += spd;
            }
            else if (chars == KeyEvent.VK_LEFT)
            {
                c.setColor (clr);
                c.fillOval (xp, yp, rad, rad);
                xp -= spd;
            }
            else if (chars == KeyEvent.VK_RIGHT)
            {
                c.setColor (clr);
                c.fillOval (xp, yp, rad, rad);
                xp += spd;
            }

            if (chars != 'w' && chars == 'W')
            {
                c.setColor (clr1);
                c.fillOval (xp, yp, rad, rad);
                yp -= spd;
            }
            else if (chars != 's' && chars == 'S')
            {
                c.setColor (clr1);
                c.fillOval (xp, yp, rad, rad);
                yp += spd;
            }
            else if (chars != 'a' && chars == 'A')
            {
                c.setColor (clr1);
                c.fillOval (xp, yp, rad, rad);
                xp -= spd;
            }
            else if (chars != 'd' && chars == 'D')
            {
                c.setColor (clr1);
                c.fillOval (xp, yp, rad, rad);
                xp += spd;
            }
// Limit
            if (xp < 0)
            {
                xp = 0;
            }
            if (yp < 0)
            {
                yp = 0;
            }
            if (xp > 980)
            {
                xp = 980;
            }
            if (yp > 645)
            {
                yp = 645;
            }
        }
}

Yes I know my code is really bad, there is ways to shorten it but I'm just focused on the fix of the code right now, then I'll edit. Some one help please.
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Wed Nov 22, 2006 3:41 pm   Post subject: (No subject)

You should use KeyListeners. They won't hold up your program and are alot easier to use. Or if you're planning on using applets for your program, you can use the Applet classes's methods for key pressing.
ericfourfour




PostPosted: Wed Nov 22, 2006 4:40 pm   Post subject: (No subject)

Is c a pointer to Graphics? If it is, Graphics.fillOval takes the top left corner the width and the height to make an oval, not the radius.

Another thing is what HellblazerX said. Why are you not using KeyListeners? I remember trying to find out how to get input for my program and I stumbled upon KeyListeners first (not some getChar method). I just don't see how the way you do it makes it any easier. I'm not trying to say the way you are doing it is wrong. I'm just saying you will have an easier time using KeyListeners.

Another thing, why do you have delay (5) in your program? You don't have to go write your own method to delay the program. It is already made. Look up Threads. That way you can just use thread.sleep (miliseconds).

I would also recommend using a back buffer for your program. It is really easy to implement and will remove flickering.

Finally, I spy with my little eye a piece of code that appears 12 times. Your life would be a lot easier if it was in a separate method.
Java:
(int) (Math.random () * 256)
[Gandalf]




PostPosted: Wed Nov 22, 2006 4:51 pm   Post subject: (No subject)

ericfourfour wrote:
Is c a pointer to Graphics?

Woah woah woah... Pointer? If anything, that should be reference, since Java does not directly have pointers.
ericfourfour




PostPosted: Wed Nov 22, 2006 7:12 pm   Post subject: (No subject)

Really? That's nice to know. I'm so used to languages with pointers I really did not know it was called that. I'll edit that then. Thanks. Smile
wtd




PostPosted: Thu Nov 23, 2006 12:37 am   Post subject: (No subject)

Actually, I've made this mistake myself in the past. As it happens, Gandalf, you've got it exactly backward. Smile

Java does not have true references. In terms of types, it has a limited selection of native types (int, char, double, etc.) and the rest are pointers. It just doesn't offer the option of arbitrary pointer arithmetic.

Where do you think the name NullPointerException comes from? Wink
TheLastFall




PostPosted: Thu Nov 23, 2006 8:43 am   Post subject: (No subject)

I've been looking up help on KeyListeners and none of it makes any sense to me. I looked at the link you gave me and it's just to open for errors, I already tried to deal with that and it didn't work out.
wtd




PostPosted: Thu Nov 23, 2006 11:05 am   Post subject: (No subject)

In order to understand KeyListener objects, you will have to understand interfaces. Do you?
Sponsor
Sponsor
Sponsor
sponsor
TheLastFall




PostPosted: Tue Dec 12, 2006 12:28 pm   Post subject: (No subject)

I don't really know what those are, could you help me with them?
wtd




PostPosted: Tue Dec 12, 2006 1:35 pm   Post subject: (No subject)

My Introduction to Java covers interfaces. Smile
Aziz




PostPosted: Wed Dec 13, 2006 9:41 am   Post subject: (No subject)

Indeed, you need to learn some java fundamentals. Is this program in a Frame or Applet? Or.......RTP? Cussing
Neville




PostPosted: Fri Dec 15, 2006 10:21 pm   Post subject: (No subject)

I wouldn't say you need to really bite in and truly understand interfaces (and why they are great), because learning how to use listeners will teach you interfaces. The basic idea here is to let Java do... well pretty much all your work.

Look up three classes:
code:
java.awt.event.KeyEvent
java.awt.event.KeyListener
java.awt.Component

Specifically, look at addKeyListener() in the Component class, and the three methods, keyPressed(), keyReleased(), and keyTyped() in the KeyListener class.

Basically what Java does is, when you press a key, the Component class records the input and everything about it into a nice package class, KeyEvent. It then delivers this event to any KeyListeners that have been added to it (using addKeyListener), by invoking one of those three methods (pressed, released, and typed, which do what you'd expect). So all you have to do is extend a KeyListener, implement the three methods, register the listener on your main Component, and let Java do the work.

The newer version of Java provides two great classes called InputMap and ActionMap to handle keyboard input, but I'd stick to the old model for now... it's a bit more work, but you'll learn interfaces along the way.
TheLastFall




PostPosted: Mon Dec 18, 2006 3:43 pm   Post subject: (No subject)

Okay, I still don't follow. I've searched the net, I've read the help and it does nothing. Key listeners don't make any sense. Nothing specifies what it does. Seriously I need help. I'm getting pissed at it. If I ask for help I don't want a fucking link that sends me right to the exact same help menu that I can find in the Java help. Don't you think that would be the first place I look? God! Seriously I have an idea how to do it but I don't know how to set the method. None of the links explain. If they do they explain it only in the terms that an experianced programmer can understand. Yea it may make sense to you guys but how about to someone who is just starting off. I know how to set damn methods but Key Listeners don't make anysense. I could bascially do everything if you didn't evolve key listeners. Sure I can make it using character keys but thats just stupid. Especially the "Wating For Input" thing that waits ever single time you press in a key. No game, good game, would use that. I'm a very understanding person but this isn't something easily understood. Why don't you "Programming Gods" think back to when you were new, think back when you needed help. Would you fucking understand what you're telling me?
Aziz




PostPosted: Tue Dec 19, 2006 12:27 am   Post subject: (No subject)

I was going to help, but not now. And nobody else will probably do so either. Swearing at people trying to help is NOT the way top get it. We were giving you links because that's the BEST way to learn something. A KeyListener is an interface. You need to understand interfaces. We could help you, but not when you're throwing a fit.
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  [ 14 Posts ]
Jump to:   


Style:  
Search: