So as an elective, I took Game design in Java in my school and I have to make a really simple game for it. Basically, what I'm trying to do is get the bullets (titled as 'bullet) to shoot out from the character. This is what I have and I'm having trouble seeing what's wrong with it. Thanks in advanced!
Code is below, here's a link to the project file: http://mediafire.com/?q6ysy1hbkyasnqt
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class MyGame extends Applet implements Runnable {
Image background;
Image figure;
AudioClip Ninja;
AudioClip bang;
// Variables for application
int width = 700;
int height = 300;
int tilesx = 1;
int tilesy = 1;
int lives = 3;
// Variables for ninja;
int Ninja_pos = 20;
int NinjaY = 150;
// Variables for bullet:
boolean bullet = false;
int bullet_x = Ninja_pos;
int bullet_y = NinjaY + 50;
int bullet_speed = 2;
public void init() {
bang = getAudioClip(getCodeBase(), "bang.au");
Ninja = getAudioClip(getCodeBase(), "Ninja.au");
Ninja.play();
for (int y = 0; y < tilesy; y++) {
for (int x = 0; x < tilesx; x++) {
}
background = getImage(getCodeBase(), "Background.gif");
figure = getImage(getCodeBase(), "Figure.png");
setSize(700, 300);
}
}
public void start() {
Thread th = new Thread(this);
th.start();
}
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);
while(true)
{
draw_bullet();
try
{
Thread.sleep(5);
}
catch(InterruptedException x)
{}
Thread.currentThread().setPriority(Thread.MAX_PRIO RITY);
}
}
private void draw_bullet()
{
if(bullet)
{
bullet_y = bullet_y - bullet_speed;
bullet_x = Ninja_pos;
}
}
public void paint(Graphics g) {
g.drawImage(background, 0, 0, this);
g.drawImage(figure, Ninja_pos, NinjaY, this);
g.setFont(new Font("TimesRoman", Font.PLAIN, 18));
g.setColor(Color.WHITE);
g.drawString("Lives: " + lives, 25, 25);
if(bullet)
{
g.setColor(Color.BLACK);
g.fillRect(bullet_x, bullet_y, 150, 150);
}
}
// Keyboard
// ************************ KeyDown *************************************
public boolean keyDown(Event evt, int key) {
if (key == Event.LEFT) {
figure = getImage(getCodeBase(), "Figure2.png");
Ninja_pos = Ninja_pos - 5;
if(Ninja_pos<=30){
figure = getImage(getCodeBase(), "Figure.png");
Ninja_pos=30;
}
repaint();
}
if (Ninja_pos <= 0) {
Ninja_pos = 0;
}
if (key == Event.RIGHT) {
figure = getImage(getCodeBase(), "Figure.png");
Ninja_pos += 5;
if (Ninja_pos >= 640) {
figure = getImage(getCodeBase(), "Figure2.png");
Ninja_pos = 640;
}
repaint();
}
if (key == 32){
bang.play();
}
return false;
}
// Double buffering
private Image dbImage;
private Graphics dbg;
/** Update - Method, implements double buffering */
public void update(Graphics g) {
// initialize buffer
if (dbImage == null) {
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}
// clear screen in background
dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
dbg.setColor(getForeground());
paint(dbg);
// draw image on the screen
g.drawImage(dbImage, 0, 0, this);
} }