Results 1 to 3 of 3
  1. #1
    vlan is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Question Need help with a third ball in game.

    Hey

    I am doing a java applet game, atm i have two balls. One moving kinda randomly and another moving with the arrow keys.

    The think i need help whit is that i can figure out how to put in a third ball in the game. And the third ball need to move whit some keys like, w, a, s, d or somthing like that.

    plz can anyone help me??

    Here is my code:
    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.geom.Ellipse2D;
    import java.awt.event.KeyListener;
    import java.awt.event.KeyEvent;
    import java.awt.image.BufferedImage;
    import java.util.*;
    
    
    public class TE extends Applet implements KeyListener {
    Image bild1;
    float x = 80.0f, xPlayer = 20.0f;
    float y = 20.0f, yPlayer = 60.0f;
    int horPlayer = 0, verPlayer = 0;
    
    float hor = 1.0f, ver = 0.5f;
    Random rnd = new Random();
    
    
    public void init() {
    this.addKeyListener(this);
    }
    
    public void paint(Graphics g) {
    bild1=getImage(getCodeBase(),"prog3.GIF");
    BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
    Graphics bufferedGraphics = image.getGraphics();
    
    bufferedGraphics.setColor(Color.WHITE);
    bufferedGraphics.fillRect(0, 0, image.getWidth(), image.getHeight());
    
    bufferedGraphics.setColor(Color.BLACK);
    bufferedGraphics.drawRect(10, 10, 150, 150);
    
    bufferedGraphics.setColor(Color.RED);
    int x = (int) this.x;
    int y = (int) this.y;
    bufferedGraphics.fillOval(x, y, 10, 10);
    
    bufferedGraphics.setColor(Color.BLUE);
    x = (int) this.xPlayer;
    y = (int) this.yPlayer;
    bufferedGraphics.fillOval(x, y, 10, 10);
    bufferedGraphics.dispose();
    g.drawImage(image, 0, 0, null);
    
    
    
    
    try {
    Thread.sleep(15);
    } catch (InterruptedException e) {
    System.out.println("EERRRROOOOORRRR");
    }
    uppdatera();
    }
    
    public void uppdatera() {
    if (x + hor > 150.0 || x + hor < 10.0)
    hor = -hor;
    
    x += hor;
    
    if (y + ver > 150.0 || y + ver < 10.0)
    ver = -ver;
    
    y += ver;
    
    
    if (xPlayer + horPlayer > 150.0 || xPlayer + horPlayer < 10.0)
    horPlayer = 0;
    
    xPlayer += horPlayer;
    
    
    if (yPlayer + verPlayer > 150.0 || yPlayer + verPlayer < 10.0)
    verPlayer = 0;
    
    yPlayer += verPlayer;
    
    Shape circlePC = new Ellipse2D.Float(x, y, 10.0f, 10.0f);
    Shape circlePlayer = new Ellipse2D.Float(xPlayer, yPlayer, 10.0f, 10.0f);
    if (circlePC.intersects(circlePlayer.getBounds())) {
    hor = -hor;
    ver = -ver;
    do {
    
    
    circlePC = new Ellipse2D.Float(x, y, 10.0f, 10.0f);
    circlePlayer = new Ellipse2D.Float(xPlayer, yPlayer, 10.0f, 10.0f);
    
    x += hor;
    y += ver;
    
    } while (circlePC.intersects(circlePlayer.getBounds()));
    }
    repaint();
    }
    
    private int mathrandom() {
    // TODO Auto-generated method stub
    return 0;
    }
    
    
    public void keyTyped(KeyEvent e) {
    //empty
    }
    
    public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    
    if (key == KeyEvent.VK_LEFT)
    horPlayer = -2;
    if (key == KeyEvent.VK_RIGHT) {
    horPlayer = 2;
    System.out.println("right");
    }
    if (key == KeyEvent.VK_UP)
    verPlayer = -2;
    if (key == KeyEvent.VK_DOWN)
    verPlayer = 2;
    }
    
    public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();
    
    if (key == KeyEvent.VK_LEFT && horPlayer != 0)
    horPlayer = 0;
    if (key == KeyEvent.VK_RIGHT && horPlayer != 0)
    horPlayer = 0;
    if (key == KeyEvent.VK_UP && verPlayer != 0)
    verPlayer = 0;
    if (key == KeyEvent.VK_DOWN && verPlayer != 0)
    verPlayer = 0;
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    I don't see any references to "ball" in your code. Where are they defined?

Similar Threads

  1. Box Ball Java
    By fcgb9115 in forum New To Java
    Replies: 1
    Last Post: 04-19-2010, 02:33 AM
  2. How to create a bouncing ball animation
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:43 PM
  3. high bouncing ball
    By bouncingball in forum Reviews / Advertising
    Replies: 1
    Last Post: 06-19-2008, 11:21 AM
  4. bouncing ball issue
    By adam405 in forum New To Java
    Replies: 1
    Last Post: 03-18-2008, 03:48 AM
  5. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 09:08 PM

Posting Permissions

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