Jump to content

Java applet "game" help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
vlan

vlan

    Newbie

  • Members
  • Pip
  • 5 posts
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;
    }
}

Edited by ZekeDragon, 14 April 2010 - 02:03 AM.
Please use [code] tags (the # button) when posting code.


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Take a look at this tutorial. You'll learn a lot from that one i think: http://forum.codecal...-detection.html
I also implemented double buffering yet the screen may sometimes flicker.
public boolean keyDown(Event evt, int key) This method is depricated and should not be used anymore.

I made it quite a bit different than your original setup but just take a look at it.
It's not 100% good. if my ball catches the computer's ball from behind, the computer ball will change direction backwards so go trough my ball and continue his path 180° reversed.

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 Uppgift_prog extends Applet implements KeyListener {
    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) {
        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("EERRRROOOORRRR");
        }
        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;
    }
}

Oh, this also isn't very object oriented programming. You should've made a class Ball, or Circle or something to start with. If you did that it will be very easy to implement multiple balls.

#3
vlan

vlan

    Newbie

  • Members
  • Pip
  • 5 posts
thanks:)
It was extremly helpful

Maybe you also know how to do so it will pup up some text like "You Won" when you maybe catches the ball from behind?. IF i refer to your game.

Just so i can see an example

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Doable, if hor= positive & ver = positive, the computer's ball is going in a direction between south & west. dependant on the values given to those.
So if horPlayer = positive & verPlayer = positive AND
circlePC.intersects(circlePlayer.getBounds()) ==true
, the player got the ball from behind i think. Didn't test this, but something like that will be very easy to implement and test. (i switched computer now, with no Java installed)

Note that, if the computer's ball is faster than the player's ball, it's the computer's ball that will be catching the player's ball ;)
Speed is probably calculated with pythagoras in this case:
player speed = sqrt(horPlayer² + verPlayer²)

To show the text, the
 bufferedGraphics .drawString(String str, int x, int y)
will probably do.

#5
vlan

vlan

    Newbie

  • Members
  • Pip
  • 5 posts
Do you know how to do any kind of score board? If i refer to you code

I wanna make a score board so you can get points if for exampel the blue cirkel touch the first cirkel, then you get one point and so on. or something easier.

#6
GMVResources

GMVResources

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
vlan i think if you need this much help on Java you should get a book if you're a beginner i recommend "Java in 24 hours" By: Rogers Cadenhead