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

    Default Java applet "game" help

    Hey

    I am trying to do a game in java applet. But i need som help.

    I basicly wanna make a game there you should watch out for the ball/balls in a specefic area.

    1. I want the boll move random in a specefic area.
    2. I want another boll/image on the same screen, but i wanna move the second boll/image with my keyboard.
    3. When everything abow works, i wanna make so "the second image/boll that you can move with the keyboard are gona watch out for the boll, "so he dosen,t touch the first boll that moves randomly.

    Could any one help me plz?




    Here is what i have come so far with:

    import java.awt.Event;

    import java.awt.Graphics;
    import java.applet.*;
    import java.awt.Color;
    import java.util.*;


    public class Uppgift_prog extends Applet {
    int x=80;
    int y=10;
    int riktning=0;
    boolean ner=true;
    Random rnd = new Random();



    public void init () {

    }
    public void paint (Graphics g){
    g.drawRect(10, 10, 150, 150);
    g.setColor(Color.RED);
    g.fillOval(x,y, 10, 10);
    for(int i=0;i<10000000;i++);
    uppdatera();

    }

    public void uppdatera() {
    System.out.println(riktning);
    /*switch(riktning){
    case 1:
    if(x>10)
    x=x-5;
    break;
    case 2:
    if(x<150)
    x=x+5;
    break;
    case 3:
    if(y>5)
    y=y-5;
    break;
    case 4:
    if(y<150)
    y=y+5;
    break;



    */
    if(ner){
    if(y< 150)
    y=y+5;
    else if(y>=150){
    y=y-5;
    ner=false;
    }

    }
    else{
    if(y>0)
    y=y-5;
    else if(y<=0){
    ner=true;
    y=y+5;

    }
    }
    if(ner){
    if(x< 150)
    x=x+5;
    else if(x>=150){
    x=x-5;
    ner=false;


    }
    }
    else{
    if(x>0)
    x=x-5;
    else if(x<=0){
    ner=true;
    x=x+5;
    }
    }




    for(int t=0;t<10000000;t++);
    repaint ();


    }
    private int mathrandom() {
    // TODO Auto-generated method stub
    return 0;
    }
    public boolean keyDown(Event evt, int key){
    switch(key){
    case Event.LEFT:
    riktning=1;
    break;
    case Event.RIGHT:
    riktning=2;
    break;
    case Event.UP:
    riktning=3;
    break;
    case Event.DOWN:
    riktning=4;
    break;


    }
    uppdatera();
    return true;
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You've posted what you want, you've posted code, but you really haven't asked a specific question. No one is going to write this for you or augment your code -- if you need that, check out RentACoder.com, but we'll be more than happy to answer questions you may be having with your code. Best of luck.

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

    Default

    Then my question will be, How does i do so the boll will move randomly?

    at the moment my ball just move a specefi route. I want it to move random?

    can any one help me?

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    depends how you define "randomly"... one thing you can do to start with is this:

    instead of having "x-5", "y+5" everywhere, have "x+deltaX", "y+deltaY". Then, in your paint() method, before calling uppdatera(), have

    deltaX = Math.random (5) + 3; // so that it's random between 3 and 7
    deltaY = Math.random (5) + 3;

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. Java Applet Help - Making "Snake" Game
    By Alphimeda in forum Java Applets
    Replies: 15
    Last Post: 04-04-2010, 05:39 PM
  3. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •