Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help with a third ball in game.

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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:
    PHP 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.0fxPlayer 20.0f;
    float y 20.0fyPlayer 60.0f;
    int horPlayer 0verPlayer 0;

    float hor 1.0fver 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(200200BufferedImage.TYPE_INT_RGB);
    Graphics bufferedGraphics image.getGraphics();

    bufferedGraphics.setColor(Color.WHITE);
    bufferedGraphics.fillRect(00image.getWidth(), image.getHeight());

    bufferedGraphics.setColor(Color.BLACK);
    bufferedGraphics.drawRect(1010150150);

    bufferedGraphics.setColor(Color.RED);
    int x = (int) this.x;
    int y = (int) this.y;
    bufferedGraphics.fillOval(xy1010);

    bufferedGraphics.setColor(Color.BLUE);
    = (int) this.xPlayer;
    = (int) this.yPlayer;
    bufferedGraphics.fillOval(xy1010);
    bufferedGraphics.dispose();
    g.drawImage(image00null);




    try {
    Thread.sleep(15);
    } catch (
    InterruptedException e) {
    System.out.println("EERRRROOOOORRRR");
    }
    uppdatera();
    }

    public 
    void uppdatera() {
    if (
    hor 150.0 || hor 10.0)
    hor = -hor;

    += hor;

    if (
    ver 150.0 || ver 10.0)
    ver = -ver;

    += 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(xy10.0f10.0f);
    Shape circlePlayer = new Ellipse2D.Float(xPlayeryPlayer10.0f10.0f);
    if (
    circlePC.intersects(circlePlayer.getBounds())) {
    hor = -hor;
    ver = -ver;
    do {


    circlePC = new Ellipse2D.Float(xy10.0f10.0f);
    circlePlayer = new Ellipse2D.Float(xPlayeryPlayer10.0f10.0f);

    += hor;
    += 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
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    491
    Thanks
    8
    Thanked 47 Times in 45 Posts

    Default Re: Need help with a third ball in game.


  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Need help with a third ball in game.

    Shape circlePC = new Ellipse2D.Float(x, y, 10.0f, 10.0f);

    Shouldn't it be = new Shape?

Similar Threads

  1. Snake Game
    By Cuju in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 19th, 2011, 07:31 PM
  2. Game 3x3
    By Koren3 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 07:43 PM
  3. Breakout Game
    By Ceasar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 9th, 2009, 12:30 AM
  4. invisible box game
    By new2java in forum Loops & Control Statements
    Replies: 1
    Last Post: September 27th, 2009, 12:46 PM
  5. Job offers to program Hobo Wars
    By MooncakeZ in forum Paid Java Projects
    Replies: 7
    Last Post: September 17th, 2009, 09:41 PM