# Mario game help!

**URL:** https://discourse.processing.org/t/mario-game-help/21979
**Category:** Coding Questions
**Tags:** homework
**Created:** [June 18, 2020, 5:14pm UTC](https://discourse.processing.org/t/mario-game-help/21979 "2020-06-18T17:14:22Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![jasmine](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@jasmine](https://discourse.processing.org/u/jasmine)
#### Post date: [June 18, 2020, 5:14pm UTC](https://discourse.processing.org/t/mario-game-help/21979/1 "2020-06-18T17:14:22Z")

</div>

How can I add enemies that the player needs to avoid

```auto
int gameMode=0;//0 - menu, 1 - play game, 2 - instructions, 3 - character selection
int selChar=1;//default character is “character 1”
int level=1;
color red=color(255, 0, 0);
color green=color(0, 255, 0);
color blue=color(0, 0, 255);

boolean moveRight=false;
boolean moveLeft=false;
boolean moveDown=false;
boolean moveUp=false;

//int x=50;
boolean[] keys=new boolean[128];

//int[] v={4, 0};//horizontal and vertical velocity

int[] g1={0, 0, -968};
int[] g2={968, 0, -968};

PImage pic1;
PImage pic2;

//int[] g3={0, 0, -108};
//int[] g4={108, 0, -108};

PImage pic3;
PImage pic4;

PImage a;
PImage b;
PImage c;

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
float marioX, marioY;
boolean jumping=false; 
int moveY=0; 

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
boolean leftBorder=false;
boolean rightBorder=false;

import processing.sound.*;
SoundFile file;

void setup() {
  size(800, 600);

  file = new SoundFile(this, "supermario.mp3");
  // file.play();

  a = loadImage("mario.png");
  b = loadImage("luigi.png");
  c = loadImage("Yoshi.png");

  pic1=loadImage("back.png");
  pic1.resize(0, height);
  pic2=loadImage("back1.png");
  pic3=loadImage("ground1.png");
  pic4=loadImage("ground2.png");

  marioX=width/2; 
  marioY=height-59;
}

void draw() {
  //runs forever
  background(red);
  if (gameMode==0) {
    mainMenu();
  } else if (gameMode==1) {
    playL1();
  } else if (gameMode==2)
    controls();
  else if (gameMode==3)
    select();
}//end draw

// ------------------------------------------------------------------------

void playL1() {

  // backg!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  image(pic1, g1[0], g1[1]);

  // more images right of the screen !!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
  if (g1[0] + pic1.width < width) {
    image(pic1, g1[0] + pic1.width, g1[1]);
    fill(255);
    text("seam 1!", g1[0] + pic1.width, g1[1]+55);
  }//if

  if (g1[0] + pic1.width + pic1.width < width) {
    image(pic1, g1[0] + pic1.width + pic1.width, g1[1]);
    fill(255);
    text("seam 2!", g1[0] + pic1.width + pic1.width, g1[1]+55);
  }//if

  if (g1[0] + pic1.width + pic1.width + pic1.width < width+7) {
    println("Here2");
    rightBorder=true;
  }//if
  else {
    rightBorder=false;
  }

  //// more images LEFT of the screen !!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
  //if (g1[0] > 0) {
  // image(pic1, g1[0] - pic1.width, g1[1]);
  // fill(255);
  // text("seam -1!", g1[0], g1[1]+55);
  //}//if

  //if (g1[0] - pic1.width > 0 ) {
  // image(pic1, g1[0] - pic1.width - pic1.width, g1[1]);
  // fill(255);
  // text("seam -2!", g1[0] - pic1.width, g1[1]+55);
  //}//if

  // if (g1[0] - pic1.width - pic1.width > 0 ) {
  // image(pic1, g1[0] - pic1.width - pic1.width - pic1.width, g1[1]);
  // fill(255);
  // text("seam -3!", g1[0] - pic1.width - pic1.width, g1[1]+55);
  // // text("seam -4!", g1[0] - pic1.width - pic1.width - pic1.width, g1[1]+55);
  // }//if

  if (g1[0] - pic1.width - pic1.width - pic1.width > -7 ) {
    leftBorder=true;
  } else {
    leftBorder=false;
  }

  text("pic2", g2[0], g2[1]);
  text("Mario", marioX, marioY);

  if (keys[LEFT]) {
    //
    //marioX += -3;
    if ( ! leftBorder)  
      g1[0] += 3;
  }

  if (keys[RIGHT]) {
    //
    //marioX += +3;
    if ( ! rightBorder) 
      g1[0] += -3;
  }

  //marioX += moveX; 
  if (jumping) {
    marioY += moveY;
    moveY++; 
    if (marioY>=height-59) {
      marioY= height-59; 
      moveY=0;
      jumping=false;
    }
  }
} //end playL1

void controls() {
  textSize(35);
  fill(255);
  text("press spacebar to jump", 150, 150);
  text("move you characters", 150, 250);
  text("collect coins", 150, 350);
  text("avoid the enemies", 150, 450);
  backToMain();
}

void select() {//character select
  textAlign(LEFT);
  textSize(20);
  fill(0);//black
  //displaying all characters
  rect(200, 50, 100, 100);
  rect(350, 50, 100, 100);
  rect(500, 50, 100, 100);

  if (mouseIn(200, 50, 100, 100)) {
    fill(blue);
    rect(200, 50, 100, 100);
    if (mousePressed) {
      selChar=1;//1 could mean Mario
      gameMode=0;//main menu
    }
  }

  if (mouseIn(350, 50, 100, 100)) {
    fill(blue);
    rect(350, 50, 100, 100);
    if (mousePressed) {
      selChar=2;//2 could mean Luigi
      gameMode=0;
    }
  }

  if (mouseIn(500, 50, 100, 100)) {
    fill(blue);
    rect(500, 50, 100, 100);
    if (mousePressed) {
      selChar=3;//3 could be Yoshi
      gameMode=0;
    }
  }

  fill(255);
  text("Char 1", 210, 100);
  text("Char 2", 360, 100);
  text("Char 3", 510, 100);

  backToMain();
}//end select

void mainMenu() {
  textAlign(LEFT);
  textSize(35);
  fill(blue);
  text("START", 350, 430);
  text("INSTRUCTIONS", 290, 490);
  text("CHARACTERS", 295, 555);

  image(a, 200, 0, 150, 200);
  image(b, 80, 250, 200, 250);
  image(c, 600, 200, 150, 200);

  if (mouseIn(330, 400, 150, 35)) {//“play”
    fill(0);
    text("START", 350, 430);
    if (mousePressed) {
      gameMode=1;
    }
  }
  if (mouseIn(280, 460, 270, 35)) {//“instructions”
    fill(0);
    text("INSTRUCTIONS", 290, 490);
    if (mousePressed) {
      gameMode=2;
    }
  }
  if (mouseIn(285, 525, 240, 35)) {//“characters”
    fill(0);
    text("CHARACTERS", 295, 555);
    if (mousePressed) {
      gameMode=3;
    }
  }
  fill(255);
  textSize(60);
  text("Super Mario ", 230, 300);
}//end mainMenu

void backToMain() {
  textAlign(LEFT);
  textSize(20);
  fill(green);
  rect(20, 550, 120, 40);
  fill(blue);
  text("Menu", 50, 580);

  if (mouseIn(20, 550, 120, 40)) {//“back to menu”
    fill(0);
    text("Menu", 50, 580);
    if (mousePressed) {
      gameMode=0;
    }
  }
}//end backToMain

void keyPressed() {
  //println(keyCode+" was pressed");
  keys[keyCode]=true;

  if (key==' ' && ! jumping) {
    jumping=true; 
    moveY=-21;
  }
}

void keyReleased() {
  //println(keyCode+" was released");
  keys[keyCode]=false;
}

//The function returns true if the mouse pointer is INSIDE the rectangle, otherwise returns false
boolean mouseIn(int left, int top, int w, int h) {
  return (mouseX > left && mouseX < left+w && mouseY > top && mouseY < top+h);
}//end mouseIn
//

```

---

<div class="post-metadata">

### Author: ![jasmine](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@jasmine](https://discourse.processing.org/u/jasmine)
#### Post date: [June 18, 2020, 6:20pm UTC](https://discourse.processing.org/t/mario-game-help/21979/2 "2020-06-18T18:20:34Z")

</div>

Help me please. I need to have this done today

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [June 18, 2020, 6:25pm UTC](https://discourse.processing.org/t/mario-game-help/21979/3 "2020-06-18T18:25:26Z")

</div>

Can you be more specific about what want to happen?

Two questions:

1. Do you want to add a predetermined number of enemies? Or the ability to have a variable number of enemies?
2. What is the deterrent to the enemies’ interaction? For example, when the enemies come within range or overlap something of consequence happens? Like the game stops or you lose points (if there is a scoring component)?

---

<div class="post-metadata">

### Author: ![jasmine](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@jasmine](https://discourse.processing.org/u/jasmine)
#### Post date: [June 18, 2020, 6:36pm UTC](https://discourse.processing.org/t/mario-game-help/21979/4 "2020-06-18T18:36:10Z")

</div>

so the enemies don’t need to be a variable and if the player comes to close to them, the player loses a life. The enemies move left and right but stay around the same area.

---

<div class="post-metadata">

### Author: ![jasmine](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@jasmine](https://discourse.processing.org/u/jasmine)
#### Post date: [June 18, 2020, 6:36pm UTC](https://discourse.processing.org/t/mario-game-help/21979/5 "2020-06-18T18:36:19Z")

</div>

I hope that makes sense

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [June 18, 2020, 6:43pm UTC](https://discourse.processing.org/t/mario-game-help/21979/6 "2020-06-18T18:43:18Z")

</div>

> [@jasmine](#):
>
> so the enemies don’t need to be a variable

You mean there is a set number of enemies at the outset of the game?

---

<div class="post-metadata">

### Author: ![jasmine](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@jasmine](https://discourse.processing.org/u/jasmine)
#### Post date: [June 18, 2020, 6:43pm UTC](https://discourse.processing.org/t/mario-game-help/21979/7 "2020-06-18T18:43:53Z")

</div>

yes that’s what I mean

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [June 18, 2020, 6:47pm UTC](https://discourse.processing.org/t/mario-game-help/21979/8 "2020-06-18T18:47:48Z")

</div>

Since it sounds like you want multiple enemies that all have the same appearance and functionality, have you studied OOP yet? Classes and objects?

---

<div class="post-metadata">

### Author: ![jasmine](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@jasmine](https://discourse.processing.org/u/jasmine)
#### Post date: [June 18, 2020, 6:49pm UTC](https://discourse.processing.org/t/mario-game-help/21979/9 "2020-06-18T18:49:33Z")

</div>

no we haven’t done that

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [June 18, 2020, 7:02pm UTC](https://discourse.processing.org/t/mario-game-help/21979/10 "2020-06-18T19:02:13Z")

</div>

Ok, so basically to set up one enemy you can:

1. set up a simple primitive shape as the enemy. In this case, an ellipse will work.
2. use the dist() function to determine when one of your characters comes near the enemy
3. use if-statement construct to outline the consequence when character nears or touches the enemy

**Here is an excellent tutorial by Abe Pazos on how to use the dist() function:**

[![]( " - YouTube") ](https://www.youtube.com/watch?v=N5Ka4C3RVmw)
