# When button pressed, load a game

**URL:** <https://discourse.processing.org/t/when-button-pressed-load-a-game/11100>\
**Category:** Coding Questions\
**Created:** [May 10, 2019, 9:18am UTC](https://discourse.processing.org/t/when-button-pressed-load-a-game/11100 "2019-05-10T09:18:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nliegeard](https://avatars.discourse-cdn.com/v4/letter/n/a8b319/32.png) [@nliegeard](https://discourse.processing.org/u/nliegeard)\
**Post date:** [May 10, 2019, 9:18am UTC](https://discourse.processing.org/t/when-button-pressed-load-a-game/11100/1 "2019-05-10T09:18:51Z")

</div>

Hello guys !  
i started a programme where i did a home page with a start button, the button work’s good but i have no idea on how can i code that when button pressed then loadfile “test.pde”,  
if you help me i’l be thankfull

---

<div class="post-metadata">

**Author:** ![nliegeard](https://avatars.discourse-cdn.com/v4/letter/n/a8b319/32.png) [@nliegeard](https://discourse.processing.org/u/nliegeard)\
**Post date:** [May 10, 2019, 9:20am UTC](https://discourse.processing.org/t/when-button-pressed-load-a-game/11100/2 "2019-05-10T09:20:31Z")

</div>

```auto
float bx;//position de la boite sur l'axe x
float by;//position de la boite sur l'axe y
int boxSize = 80;//taille de la boite 
int couleurBox = 255;
boolean addlb = false;//Au Dessus De La Boite (addlb)
boolean fermer = false;//fermer = clique de l'utilisateur
float xdeplacement = 0.0; 
float ydeplacement = 0.0;

void setup() {
  size(1200, 800);
  bx = (600);
  by = (400);
  rectMode(RADIUS);//radius = rayon, on prend le rayon du rectangle pour que ca soit centrer donc pas de corner
  background(0, 0, 0);
  PFont mapolice;// On déclare notre police
  mapolice = loadFont("ArialMT.vlw");//On choisit la police ArialMT qu'on a inserer sinon cela ne fonctionne pas
  textFont(mapolice, 50);//le texte aura comme police ArialMT et comme taille 50
}
void draw() { 
  background(0);//fond noir
  // savoir si les souris est sur le rectangle
  if (mouseX > bx-boxSize && mouseX < bx+boxSize && //&& = et
  mouseY > by-boxSize && mouseY < by+boxSize) {
    addlb = true;//donc addlb est vrai quand la souris est sur le rectangle
    if (!fermer)//ne pas fermer on attend que l'utilisateur clique pour activer le bouton
{
     
      fill(couleurBox, 0, 0);//couleur de la boite
    }
  } else {
    fill(140);//le rectangle est gris quand la souris est pas dessus
    addlb = false;//donc on n'est pas sur la boite
  }

  rect(bx, by, boxSize, boxSize);
  fill(0, 255, 0);
  //début des textes
  text("Space invaders", 450, 50);
  text("Start", 550, 300);
  text("LEFT=gauche", 0, 50);
  text("RIGHT=droite", 0, 100);
  text("espace=tirer", 0, 150);
  //fin des textes
}

void mousePressed() {//si la souris est cliqué alors on active fermer sinon on n'active pas fermer
  if (addlb) { 
    fermer = true; 
    fill(255, 0, 255);
  } else {
    fermer = false;
  }
}

```

here is my code, the comment are in french btw

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 10, 2019, 9:56am UTC](https://discourse.processing.org/t/when-button-pressed-load-a-game/11100/3 "2019-05-10T09:56:39Z")

</div>

hi,  
please format your code using in forum post editor the

```auto
</> code tag

```

* * *

if you have in your sketchbook path your project and also a file

> test.pde

( mans you see it in the processing IDE )  
it is already loaded  
and if that file contains functions  
you can call that directly from you “main”

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 10, 2019, 10:24am UTC](https://discourse.processing.org/t/when-button-pressed-load-a-game/11100/4 "2019-05-10T10:24:04Z")

</div>

You can‘t start another complete sketch this way.

Instead you need to unify both sketches.

When fermer is true then don’t display the button but draw the content of the unified 2nd sketch

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 10, 2019, 11:08am UTC](https://discourse.processing.org/t/when-button-pressed-load-a-game/11100/5 "2019-05-10T11:08:13Z")

</div>

Example

```auto

float bx;//position de la boite sur l’axe x
float by;//position de la boite sur l’axe y
int boxSize = 80;//taille de la boite
int couleurBox = 255;
boolean addlb = false;//Au Dessus De La Boite (addlb)
boolean fermer = false;//fermer = clique de l’utilisateur
float xdeplacement = 0.0;
float ydeplacement = 0.0;

void setup() {
  size(1200, 800);
  bx = (600);
  by = (400);
  rectMode(RADIUS);//radius = rayon, on prend le rayon du rectangle pour que ca soit centrer donc pas de corner
  background(0, 0, 0);
  PFont mapolice;// On déclare notre police
  // mapolice = loadFont("ArialMT.vlw");//On choisit la police ArialMT qu’on a inserer sinon cela ne fonctionne pas
  // textFont(mapolice, 50);//le texte aura comme police ArialMT et comme taille 50
}

void draw() {
  background(0);//fond noir

  if (fermer) {
    fill(255, 0, 0);
    text("GAME", 111, 111);
  } else {

    // savoir si les souris est sur le rectangle
    if (mouseX > bx-boxSize && mouseX < bx+boxSize && //&& = et
      mouseY > by-boxSize && mouseY < by+boxSize) {
      addlb = true;//donc addlb est vrai quand la souris est sur le rectangle
      if (!fermer)//ne pas fermer on attend que l’utilisateur clique pour activer le bouton
      {

        fill(couleurBox, 0, 0);//couleur de la boite
      }
    } else {
      fill(140);//le rectangle est gris quand la souris est pas dessus
      addlb = false;//donc on n’est pas sur la boite
    }

    rect(bx, by, boxSize, boxSize);
    fill(0, 255, 0);
    //début des textes
    text("Space invaders", 450, 50);
    text("Start", 550, 300);
    text("LEFT=gauche", 0, 50);
    text("RIGHT=droite", 0, 100);
    text("espace=tirer", 0, 150);
    //fin des textes
    //
  }
}

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

void mousePressed() {//si la souris est cliqué alors on active fermer sinon on n’active pas fermer
  if (fermer) {
    // GAME
  } else {
    // MENU
    if (addlb) {
      fermer = true;
      fill(255, 0, 255);
    } else {
      fermer = false;
    }
  }
}
//

```
