Pacman
in
Programming Questions
•
4 months ago
Hello I am trying to create Pacman the game and I am stuck. I am starting to work on the consumables but I keep getting a null pointer error. I am using a PSape function with an array to create the two different kinds of eatables. It seems when ever the shape function tries to call on the array eats[] it returns a NPR. There is a good chance this is relatively simple but I figured I would ask.
Also, apologies if I am not posting to a specific layout, I am relatively knew to coding and this forum.
- //declare varibales
- GameControl gc;
- Map map;
- void setup() {
- size(640, 480);
- gc= new GameControl();
- map= new Map();
- imageMode(CENTER);
- }//setup
- void draw() {
- println("x-axis "+gc.plyr.x);
- println("y-axis "+gc.plyr.y);
- map.display();
- gc.runGame();
- }//draw
- void keyPressed() {
- if (keyCode==LEFT) {
- if (map.collideMap(gc.plyr.x-20, gc.plyr.y)) {
- gc.plyr.move(0);
- }//checking true or false boolean
- }//move left
- if (keyCode==RIGHT) {
- if (map.collideMap(gc.plyr.x+20, gc.plyr.y)) {
- gc.plyr.move(1);
- }
- }//move right
- if (keyCode==UP) {
- if (map.collideMap(gc.plyr.x, gc.plyr.y-20)) {
- gc.plyr.move(2);
- }
- }//move up
- if (keyCode==DOWN) {
- if (map.collideMap(gc.plyr.x, gc.plyr.y+20)) {
- gc.plyr.move(3);
- }
- }//move down
- }//keypressed
- class Collectables {
- int xpos, ypos, numofcirc, circsize, circ2size, spacing;
- int startx, starty;
- PShape[] eats;
- Collectables (int x, int y) {
- startx= x;
- starty= y;
- numofcirc= 2;
- circsize= 5;
- circ2size=15;
- spacing= 10;
- eats = new PShape[2];
- eats[0] = createShape (ELLIPSE, 118, 54, circ2size, circ2size);
- eats[0] = createShape (ELLIPSE, 518, 54, circ2size, circ2size);
- eats[0] = createShape (ELLIPSE, 118, 334, circ2size, circ2size);
- eats[0] = createShape (ELLIPSE, 518, 334, circ2size, circ2size);
- eats[1] = createShape (ELLIPSE, 100, 50, circsize, circsize);
- }//constructor
- void display () {
- /*for (int i=1; i<=numofcirc; i++) {
- for (int j=1; j<=numofcirc; j++) {
- xpos=startx+(i*spacing);
- ypos=starty+(j*spacing);
- fill(255);
- stroke(0);
- ellipse(xpos, ypos, circsize, circsize);
- }//j loop
- }//i loop */
- noStroke();
- shape(eats[1]);
- shape(eats[2]);
- }//display
- }//class end
- class GameControl {
- Player plyr;
- Collectables clct;
- PImage pacman;
- ArrayList collItems;
- int score;
- PFont scoreText;
- GameControl () {
- startGame();
- }//constructor
- void runGame() {
- /*for (int j=0; j<collItems.size();j++) {
- checkCollision(plyr, (Collectables)collItems.get(j), j);
- }
- for (int i=0; i<collItems.size();i++) {
- Collectables tempC;
- tempC=(Collectables)collItems.get(i);
- tempC.display();
- } */
- plyr.display();
- this.displayScore();
- for (int i=0; i<collItems.size();i++) {
- clct=(Collectables)collItems.get(i);
- clct.display();
- }
- }//runGame
- void startGame() {
- plyr= new Player(318, 254, 10);
- pacman= loadImage("pacman_LEFT_open.png");
- imageMode(CENTER);
- score=0;
- scoreText = loadFont("Arial-BoldMT-15.vlw");
- collItems = new ArrayList();
- for (int i=0; i<20;i++) {
- collItems.add(new Collectables(120, 20));
- }
- }//startGame
- void adjScore (int amount) {
- score+=amount;
- }
- void displayScore () {
- textFont(scoreText);
- text(score, 20, 80);
- }
- /* void checkCollision(Player pl, Collectables cl, int index) {
- if (keyPressed==LEFT) {
- if (map.collideMap(gc.plyr.x-20, gc.plyr.y)) {
- gc.plyr.move(0);
- }//checking true or false boolean
- }//move left
- if (keyPressed==RIGHT) {
- if (map.collideMap(gc.plyr.x+20, gc.plyr.y)) {
- gc.plyr.move(1);
- }
- }//move right
- if (keyPressed==UP) {
- if (map.collideMap(gc.plyr.x, gc.plyr.y-20)) {
- gc.plyr.move(2);
- }
- }//move up
- if (keyPressed==DOWN) {
- if (map.collideMap(gc.plyr.x, gc.plyr.y+20)) {
- gc.plyr.move(3);
- }
- }//move down
- }//checkCollision */
- }//class end
- class Map {
- boolean[][] collisionMap;
- PImage colMap= loadImage("pacman_collisionmap.gif");
- PImage blankMap;
- Map () {
- blankMap= loadImage("pacman_blankmap.png");
- }//constructor
- void display() {
- image(blankMap, width/2, height/2);
- imageMode(CENTER);
- }//display
- boolean collideMap(float x, float y) {
- color black= color(0);
- color white= color(255);
- color c=colMap.get((int)x, (int)y);
- //this if statement decides the state when the collision map is black or white
- if (c==black) {
- return false;
- }
- else {
- return true;
- }
- }//end ColMap
- }//class end
- class Player {
- int frames;
- float x, y, speed;
- int direction;
- PImage[] pacman;
- int state;
- Player (float _x, float _y, float _s) {
- x= _x;
- y= _y;
- speed= _s;
- frames=8;
- direction=3;
- state=1;
- pacman = new PImage[frames];
- pacman[0]= loadImage("pacman_LEFT_open.png");
- pacman[1]= loadImage("pacman_LEFT_closed.png");
- pacman[2]= loadImage("pacman_RIGHT_open.png");
- pacman[3]= loadImage("pacman_RIGHT_closed.png");
- pacman[4]= loadImage("pacman_UP_open.png");
- pacman[5]= loadImage("pacman_UP_closed.png");
- pacman[6]= loadImage("pacman_DOWN_open.png");
- pacman[7]= loadImage("pacman_DOWN_closed.png");
- }//constructor
- void display () {
- imageMode(CENTER);
- if (state>0) {
- image(pacman[direction*2], x, y);
- }
- else {
- image(pacman[(direction*2)+1], x, y);
- }
- if (x<100) {
- x=526;
- }//portal
- if (x>540) {
- x=110;
- }//portal
- }//display
- void move (int pdirection) {
- direction=pdirection;
- state*=-1;
- if (pdirection==0) {
- x-=speed;
- }
- if (pdirection==1) {
- x-=-speed;
- }
- if (pdirection==2) {
- y-=speed;
- }
- if (pdirection==3) {
- y-=-speed;
- }
- }//move
- }//class end
1