0

I am trying to code up a game where a rectangle moves according to keyboard inputs on the bottom of the screen, and when spacebar is pressed, it releases a bomb implemented by another rectangle being shot up. I have a Bomb class that implements the spacebar initiated bombs. However, whenever I run, I get a nullPointerException error. Code for the two classes is below. Help would be much appreciated.

SuperLeaperPro4000 Class:

package SuperLeaperPro4000;

//need to import this;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class SuperLeaperPro4000 extends Canvas implements ActionListener, KeyListener{
//Declare and initialize timer
Timer animator = new Timer(1, this);
//Global variable declarations
//coordinates of first rectangle
int x= 0;
int y= 900;
//velocity variables of first rectangle
int velX= 0;
int velY= 0;
Graphics g;

public SuperLeaperPro4000() {
    //add key listener
    addKeyListener(this);
    //enable key listener
    setFocusable(true);
}

public static void main(String[] args){

    //create a JFrame named Animation;
    JFrame frame= new JFrame("Animation");
    //create a new instance of the scribble class
    Canvas canvas= new SuperLeaperPro4000();
    //set the size of the canvas
    canvas.setSize(1000, 1000);
    frame.setResizable(false);
    //add the canvas
    frame.add(canvas);
    //make sure window is maxed
    frame.pack();
    //stop program when closed
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //let the frame be seen
    frame.setVisible(true);
}

public void paint (Graphics g) {

    //draw the rectangle that will move according to keyboard inputs
    g.drawRect(x, y, 50, 50);
    animator.start();
}

public void actionPerformed(ActionEvent e) {
    //update x
    if (x+velX>=0 && x+velX<=950) {
        x= x + velX;
    }
    //update y
    if (y+velY>=0 && y+velY<=900) {
        y= y + velY;
    }
    repaint();
}

//method that gets called whenever user presses keyboard key
public void keyPressed(KeyEvent key) {

    //if user presses left arrow key, update left velocity;
    if(key.getKeyCode()==KeyEvent.VK_LEFT) {
        velX= -1;
    }
    //if user presses right arrow key, update right velocity;
    else if (key.getKeyCode()==KeyEvent.VK_RIGHT) {
        velX= 1;
    }
    //if user presses space, go up;
    if (key.getKeyCode()==KeyEvent.VK_SPACE){
        Bomb haymaker= new Bomb(x, y, 10, 10, 0, -5);
        haymaker.paint(g);
    }
}

//method that gets called whenever user releases keyboard key
public void keyReleased(KeyEvent key) {
    //when the user releases the keys, set the velocities back to 0
    if(key.getKeyCode()==KeyEvent.VK_LEFT || key.getKeyCode()==KeyEvent.VK_RIGHT) {
        velX= 0;
    }
}

public void keyTyped(KeyEvent key) {
}
}

Bomb Class:

package SuperLeaperPro4000;

//need to import this;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Bomb extends Canvas {

//field values
int x;
int y;
int height;
int width;
int velX;
int velY;

//constructor for a bomb
public Bomb(int x, int y, int height, int width, int velX, int velY) {
    this.x= x;
    this.y= y;
    this.height= height;
    this.width= width;
    this.velX= velX;
    this.velY= velY;
}
public void paint(Graphics gs) {
    gs.drawRect(this.x, this.y, this.height, this.width);
}

public void fire(int velocity) {
    this.velX= velocity;
}

public int getX() {
    return this.x;
}

public int getY() {
    return this.y;
}

public int getWidth() {
    return this.width;
}

public int getHeight() {
    return this.height;
}

public int getVelX() {
    return this.velX;
}

public int getVelY() {
    return this.velY;
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1943827
  • 113
  • 2
  • 8

0 Answers0