Results 1 to 3 of 3
  1. #1
    tornbacchus is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default need help coding my game (inkball)

    im trying to code a game for my class, but i need two lines ( or more probably) that will allow the ball to be redrawn white so it doesnt mess up the drawn lines, and for the ball to bounce off of the lines that ive drawn.

    its a java applet

    here is the code
    Java Code:
    // The "Pong" class.
    import java.applet.*;
    import java.awt.*;
    
    public class finalproject extends Applet implements Runnable
    {
        private int lastx, lasty;
        int x = 20, y = 20;
        int r = 20;
        int dx = 1, dy = -2;
    
        int a = 100, b = 100;
        int l = 10, w = 50;
        int da = -2, db = 2;
    
        Image buffer;
        Graphics g;
        Graphics bufferG;
    
        int delay = 4;
        Thread running;
    
        public void init ()
        {
            buffer = this.createImage (bounds ().width, bounds ().height);
            bufferG = buffer.getGraphics ();
            g = this.getGraphics ();
    
        }
    
    
        public boolean mouseDown (Event e, int mouseX, int mouseY)
        {
            lastx = mouseX;
            lasty = mouseY;
            return true;
        }
    
    
        public boolean mouseDrag (Event e, int mouseX, int mouseY)
        {
            g.setColor (Color.black);
            g.drawLine (lastx, lasty, mouseX, mouseY);
            lastx = mouseX;
            lasty = mouseY;
            return true;
        }
    
    
        public void update (Graphics g)
        {
            paint (g);
        }
    
    
        public void start ()
        {
            if (running == null)
            {
                running = new Thread (this, "running");
                running.start ();
            }
        }
    
    
        public void stop ()
        {
            if (running != null && running.isAlive ())
            {
                running.stop ();
            }
            running = null;
        }
    
    
        public void run ()
        {
            for (;;)
            {
                x = x + dx;
                y = y + dy;
    
                a = da;
                b = db;
    
                if (x > bounds ().width - 10)
                    dx = -dx;
                if (x < 10)
                {
                    dx = -dx;
                    x = 15;
                }
                if (y > bounds ().height - 10)
                    dy = -dy;
                if (y < 0)
                    dy = -dy;
    
                if (a > bounds ().width - 10)
                    da = -da;
                if (a < 0)
                    da = -da;
                if (b > bounds ().height - 50)
                    db = -db;
                if (b < 0)
                    db = -db;
    
                if ((x < a + 10) && (x + 10 > a) && (y + 10 > b) && (y < b + 50) && (dx > da))
                {
                    dx = -dx;
                    da = -da;
                }
                if ((x < a + 10) && (x + 10 > a) && (y + 10 > b) && (y < b + 50) && (dx < da))
                {
                    dx = -dx;
                    da = -da;
                }
                if ((y + 50 > b) && (y < b + 50) && (x + 10 > a) && (x < a + 10) && (dy < db))
                {
                    dy = -dy;
                    db = -db;
                }
                repaint ();
                try
                {
                    running.sleep (delay);
                }
                catch (InterruptedException e)
                {
                    ;
                }
            }
        }
    
    
        public void paint (Graphics g)
        {
            g.setColor (Color.yellow);
            g.fillOval (x, y, r, r);
            for (int wastetime = 1 ; wastetime <= 199999 ; wastetime++)
            g.setColor (Color.white);
            g.fillOval (x, y, r, r);
        }
    
    
        // Place the body of the drawing method here
    } // paint method
    i sort of have the ball being redrawn as white, but its not timed correctly with the delay.

    if you run it, youll see what im trying to do. its sort of like an inkball game, where you draw the line with the mouse, then the ball should bounce off of it.

    thanks for any help, im new at this
    Last edited by tornbacchus; 06-08-2009 at 10:48 PM.

  2. #2
    tornbacchus is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    ne1? .

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    You are using deprecated methods that are very rarely used now. Using them just adds problems to your code and makes it harder for anyone to understand. Also, the graphics do not work well in the first place... you may want to rethink this, as it appears to have many faults.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. Better coding
    By tomiu in forum New To Java
    Replies: 1
    Last Post: 04-09-2009, 07:19 PM
  3. Help with really simple coding
    By tigertomas in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 04:47 AM
  4. coding help
    By accies76 in forum New To Java
    Replies: 5
    Last Post: 11-12-2008, 08:15 PM
  5. Help On Coding problem
    By mandrake446 in forum New To Java
    Replies: 3
    Last Post: 12-08-2007, 07:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •