Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

My First Java Game - I need some help..

PengwenPengwen Posts: 3Member
Hello this is my first java game and is very simple and easy. I have written the code below and was wondering if anyone would help me by making some suggestions on how I could clean up some inefficiences in the program or recode some of it to make it run smoother and more efficiently.
Any and all help is greatly appreciated and thanks in advance.
Here is the code (NB:The code is quite short so please take time to read it if you can help me)

import java.awt.*;
import java.applet.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;


public class Hunt extends Applet implements MouseListener{
Image background, bird;
int spike;
int birdyX, birdyY;
int x,y;
int score, darts;


public void init() {
darts = 10;
background = getImage(getCodeBase(), "graphics/background.gif");
bird = getImage(getCodeBase(), "graphics/bird.gif");
addMouseListener(this);

}
public void mouseClicked(MouseEvent e) {
x = e.getX();
y = e.getY();
darts = darts-1;
if (((x < birdyX+45) && (x > birdyX)) && ((y < birdyY+45) && (y > birdyY)))
score = score + 1;
repaint();
}

public void mouseReleased (MouseEvent e) {};
public void mouseEntered (MouseEvent e) {};
public void mouseExited (MouseEvent e) {};
public void mousePressed (MouseEvent e) {};


public void paint (Graphics g) {
g.drawImage(background,0,0, this);
spike = (int)(Math.random()*10);
switch (spike) {
case 0: {
g.drawImage(bird, 130, 250, 60, 45, this);
birdyX = 130;
birdyY = 250;
break;
}
case 1: {
g.drawImage(bird, 10, 25, 60, 45, this);
birdyX = 10;
birdyY = 25;
break;
}
case 2: {
g.drawImage(bird, 450, 275, 60, 45, this);
birdyX = 450;
birdyY = 275;
break;
}
case 3: {
g.drawImage(bird, 123, 450, 60, 45, this);
birdyX = 123;
birdyY = 450;
break;
}
case 4: {
g.drawImage(bird, 541, 310, 60, 45, this);
birdyX = 541;
birdyY = 310;
break;
}
case 5: {
g.drawImage(bird, 61, 345, 60, 45, this);
birdyX = 61;
birdyY = 345;
break;
}
case 6: {
g.drawImage(bird, 480, 360, 60, 45, this);
birdyX = 480;
birdyY = 360;
break;
}
case 7: {
g.drawImage(bird, 465, 329, 60, 45, this);
birdyX = 465;
birdyY = 329;
break;
}
case 8: {
g.drawImage(bird, 100, 200, 60, 45, this);
birdyX = 100;
birdyY = 200;
break;
}
case 9: {
g.drawImage(bird, 365, 220, 60, 45, this);
birdyX = 365;
birdyY = 220;
break;
}
}

if (darts == 0) {
g.setFont(new Font("Goudy Stoout", Font.BOLD, 14));
g.setColor(new Color(255,0,0));
g.drawString("Darts Remaining "+darts, 20, 20);
g.drawString("Your score is "+ score, 150, 20);
}
g.setFont(new Font("serif", Font.BOLD,14));
g.setColor(new Color(255,0,0));
g.drawString("Darts remaining "+darts, 20 ,20);
g.drawString("Your score is "+score,150, 20);


}
}

Comments

  • arb123arb123 Posts: 696Member
    Couple things stand out from a brief look:

    1) do not instantiate objects in paint where possible
    -where you create a Font or Color, to that in init as a member variable as you do with your Images.
    -the String concatenation is the same; store the String and only create a new one if the value has changed.

    2) avoid using custom fonts in Applets; the target user probably won't have them.

    3) [b]new Color(255, 0, 0)[/b] is the same as using [b]Color.red[/b]

    ---------------------------------
    [size=1](Its just my sig)
    HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

  • PengwenPengwen Posts: 3Member
    QUOTE:
    "Where you create a Font or Color, to that in init as a member variable as you do with your Images.

    The String concatenation is the same; store the String and only create a new one if the value has changed."

    How would I go about doing these two things ?

  • arb123arb123 Posts: 696Member
    : "Where you create a Font or Color, to that in init as a member variable as you do with your Images.

    You can see from the code below that it uses the same pattern you use for images.
    [code]
    Font serif = null; //this is a member variable
    public void init() {
    serif = new Font("serif", Font.BOLD, 14);
    }
    public void paint(Graphics g) {
    g.setFont(serif);
    }
    [/code]

    :
    : The String concatenation is the same; store the String and only create a new one if the value has changed."

    Use [b]getScoreAsString()[/b] when you want to draw the string.
    [code]
    int score, darts;
    int lastdrawnscore = 0;
    String scoreDisplay = "Your score is 0";
    private String getScoreAsString() {
    if (lastdrawnscore != score) {
    lastdrawnscore = score;
    scoreDisplay = "Your score is " + score;
    }
    return scoreDisplay;
    }
    [/code]

    You can scale images with the [b]Image.getScaledInstance[/b] method. If you want to keep track of where your targets have hit, consider creating a [b]java.util.Set[/b] of [b]java.awt.Point[/b] objects.

    ---------------------------------
    [size=1](Its just my sig)
    HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

  • PengwenPengwen Posts: 3Member
    Ok thanks your help is greatly appreciated, ill try using those methods you suggested to try and make the image decrease by 3 pixels for every 2 succesive hits and make it so the bird does not appear in the same location twice.
    If you don't mind ill write the code up and post it up on here and see what you think ?
    You might have some more pointers for me.
    Thanks Heaps.
Sign In or Register to comment.