# How do I loop the methods in my game?

**URL:** https://discourse.processing.org/t/how-do-i-loop-the-methods-in-my-game/30821
**Category:** Coding Questions
**Created:** [June 20, 2021, 6:45pm UTC](https://discourse.processing.org/t/how-do-i-loop-the-methods-in-my-game/30821 "2021-06-20T18:45:58Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Metin](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@Metin](https://discourse.processing.org/u/Metin)
#### Post date: [June 20, 2021, 6:45pm UTC](https://discourse.processing.org/t/how-do-i-loop-the-methods-in-my-game/30821/1 "2021-06-20T18:45:59Z")

</div>

Hello guys!  
I am in need of your help. I am currently working on a game that is similar to Pac-Man without the walls and stuff.  
Doing it while being a beginner in coding and Processing.  
You will also see that I am still not finished with my work and that there are some mistakes there and there…  
The biggest obstacle for me right now is on how I can loop the methods, so it won’t start the methods just once. I tried to do it with while and do-while, but got a lot of eh errors and stuff 😬  
Can you guys help me? Thank you!

Here is the code:

```auto
//Team: Medina, --, -- ; 
//Class: TG13I
//Date: 23.06.2021
//Project: PacMan-AmongUs

import processing.sound.*;
SoundFile music;

PImage Hintergrund; //Allgemein: Hintergrund
PImage start; //Starthintergrund
PImage spiel; //Spielhintergrund nach dem man "1" gedrückt hat
PImage victory; //Hintergrund für Sieg
PImage defeat; //Hintergrund für Verloren
PImage muenze; //Bild Münze

//Bewegung PAC
boolean PAC;
float pacX;
float pacY;

//Münzen
boolean M;
int muenzen = 100; //Anzahl der Münzen
float [] mPoX = new float[muenzen]; //Position von Münzen x
float [] mPoY = new float[muenzen]; //Position von Münzen y

//Bewegung Geister
int geister = 4; //Anzahl Geister 
float geist_ge_x; //Position des Geistes x-richtung
float geist_ge_y; //Position des Geistes y-richtung
float geist_ge_vx; //Geschwindigkeit des Geistes x-richtung
float geist_ge_vy; //Geschwindigkeit des Geistes y-richtung

//Anfang SETUP

void setup() {
  size(640,640); //Größe des Fensters
  start = loadImage("Hintergrund 1.png"); //Laden des Startbildes
  spiel = loadImage("spielHG.png"); //Laden des Spielhintergrundes
  victory = loadImage("victory.png"); //Laden des Spielhintergrundes
  defeat = loadImage("defeat.png"); //Laden des Spielhintergrundes
  muenze = loadImage("Münze.png"); //Laden des Spielhintergrundes  
  
  Hintergrund = start; //Startbild wird angezeigt
  background(Hintergrund); //Hier deklariert
  
  music = new SoundFile (this, "main.wav"); //Laden der Musik
  music.play(); //einmalige Abspielung der Musik von "main.wav"
  
  //music = new SoundFile (this, "victory.wav"); //Laden der Musik
  //music = new SoundFile (this, "defeat.wav"); //Laden der Musik
  
  
  //Geister
    geist_ge_x=400; //Position des Geistes x-richtung
    geist_ge_y=300; //Position des Geistes y-richtung
    geist_ge_vx =-4;//Geschwindigkeit in x - Richtung
    geist_ge_vy =0;//Geschwindigkeit in y - Richtung
  
   
  //Münze
   for (int i=0; i<muenzen;i++){ //for-Schleife für Münzen -> Initalisieren
    mPoX [i] = random(0); //Position in x - Richtung
    mPoY [i] = random(0); //Position in y - Richtung
    
    switch (muenzen) //Abfrage: sobald je 25 münzen eingesammelt worden sind, verschnellern sich die Geister
    {
        case 75:
          -- geist_ge_x; //Geschwindigkeit in x - Richtung
          break;
        case 50:
         -- geist_ge_x; //Geschwindigkeit in x - Richtung
          break;
        case 25:
         -- geist_ge_x; //Geschwindigkeit in x - Richtung
          break;
    }
  }
  

  
  //Pac
  pacX = 20;
  pacY = 60;
  

rectMode(CENTER); 
}

//Ende von SETUP

//Anfang DRAW

void draw() {
  background (Hintergrund); //Anzeige Hintergrund, hier von "start"

    //Spiel wird gestartet 
    Start();   
  } 

//Ende DRAW

void muenze(){
  
M=true;

//Münzen aktualisierung
for (int i=0; i<muenzen;i++) 
{  
  mPoX[i] = mPoX[i]+ mPoY[i]; //Aktualisierung der Positionen
  mPoY[i] = mPoY[i]+ mPoY[i];
  
  //sollte Pac über Münze sein, verschwindet diese und wird abgezählt
  if((pacY < mPoY[i]) && (mPoY[i] > pacY)||(pacX < mPoX[i]) && (mPoX[i] > pacX))
  {
    muenzen--; //Minus Münze
    mPoX[i] = mPoX [muenzen]; //Position in x - Richtung
    mPoY[i] = mPoY [muenzen]; //Position in y - Richtung
    i--;
    continue;
  }
   if (muenzen==0){ //Sollte Pac alle münzen haben -> Pac win
   Hintergrund = victory; //Anzeige Hintergrund
   background(Hintergrund); //Hintergrund gewechselt
   }
  
  image(muenze, mPoX[i],mPoY[i],15,15);
  
 }
}

void Start(){
  
if (keyPressed)
  {
    if (keyCode == '1')
    {
       Hintergrund = spiel; //Anzeige Starthintergrund auf Spielhintergrund
       background(Hintergrund); //Bei Tastendruck der "1" wird der Hintergrund gewechselt
       music.stop(); //Musik wird gestoppt
       Pac();
       Geist();
       muenze();
    }
    if (keyCode == '2')
    {
      Hintergrund = start; //Anzeige Spielhintergrund auf Starthintergrund
      background(Hintergrund); //Bei Tastendruck der "2" wird der Hintergrund gewechselt
      music.pause(); //Musik wird pausiert
      music.play(); //Musik wird wieder gespielt   
    }
  }
  
}

void Geist(){
  
fill(random(255), random(255), random(255), random(255));
rect(geist_ge_x,geist_ge_y,25, 25);

geist_ge_x = geist_ge_x + geist_ge_vx;
geist_ge_y = geist_ge_y + geist_ge_vy;

  if((geist_ge_y < pacY) && (geist_ge_y > pacY)||(geist_ge_x < pacX) && (geist_ge_x > pacX)){ //Sollte Geist auf Pac treffen -> Pac lose
     PAC = false;
     Hintergrund = defeat; //Anzeige Hintergrund
     background(Hintergrund); //Hintergrund gewechselt
      
   }
 

// Abprall an den Wänden
if ( geist_ge_y > 50 || geist_ge_y < 590)
  {
    geist_ge_vy = -geist_ge_vy;
  }
if ( geist_ge_x > 635) 
  {
    geist_ge_vx = -geist_ge_vx;
  }
}

void Pac(){

PAC = true;
fill(255,255,0);
rect(pacX,pacY,20, 20);

if (keyPressed) {
      if (keyCode == LEFT) {
         if(pacX > 50)
           {
             pacX = pacX - 5;
           }
        }
      else if (keyCode == RIGHT) {
          if(pacX < 590)
           {
             pacX = pacX + 5;
           }
        }
      else if (keyCode == UP) {
          if(pacY > 50)
            {
              pacY = pacY - 5;
            }
        }
      else if (keyCode == DOWN) {
         if(pacY < 590)
           {
             pacY = pacY + 5;
           }
        }
    
  }
 
}

```

---

<div class="post-metadata">

### Author: ![Th3cG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/th3cg/32/12780_2.png) [@Th3cG](https://discourse.processing.org/u/Th3cG)
#### Post date: [June 20, 2021, 8:35pm UTC](https://discourse.processing.org/t/how-do-i-loop-the-methods-in-my-game/30821/2 "2021-06-20T20:35:28Z")

</div>

Hello,  
maybe I’m wrong but I think that you should call the methods inside the draw function as shown here:

[https://processing.org/examples/functions.html](https://processing.org/examples/functions.html)

Let me know if this solves your issue ![slight_smile](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6a2402c9644770fc4771f101871fdc8c6cb09e1d.png)

---

<div class="post-metadata">

### Author: ![Metin](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@Metin](https://discourse.processing.org/u/Metin)
#### Post date: [June 20, 2021, 8:39pm UTC](https://discourse.processing.org/t/how-do-i-loop-the-methods-in-my-game/30821/3 "2021-06-20T20:39:44Z")

</div>

Thank you for responding!  
They are in the draw.

```auto

void Start(){
  
if (keyPressed)
  {
    if (keyCode == '1')
    {
       Hintergrund = spiel; //Anzeige Starthintergrund auf Spielhintergrund
       background(Hintergrund); //Bei Tastendruck der "1" wird der Hintergrund gewechselt
       music.stop(); //Musik wird gestoppt
       Pac();
       Geist();
       muenze();
    }
    if (keyCode == '2')
    {
      Hintergrund = start; //Anzeige Spielhintergrund auf Starthintergrund
      background(Hintergrund); //Bei Tastendruck der "2" wird der Hintergrund gewechselt
      music.pause(); //Musik wird pausiert
      music.play(); //Musik wird wieder gespielt   
    }
  }
  
}

```

Right there when the key „1“ is pressed.

---

<div class="post-metadata">

### Author: ![Th3cG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/th3cg/32/12780_2.png) [@Th3cG](https://discourse.processing.org/u/Th3cG)
#### Post date: [June 20, 2021, 8:46pm UTC](https://discourse.processing.org/t/how-do-i-loop-the-methods-in-my-game/30821/4 "2021-06-20T20:46:07Z")

</div>

> [@Metin](#):
>
> They are in the draw.

Oops, sorry about that!

Try

`if (keyPressed == true)`

---

<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: [June 20, 2021, 10:19pm UTC](https://discourse.processing.org/t/how-do-i-loop-the-methods-in-my-game/30821/5 "2021-06-20T22:19:08Z")

</div>

Hello Metin!

What you have in Start() now should be in a function keyPressed ()

But instead of running the game (move this part into draw()) set a variable A to true or false here

In draw instead of checking keyPressed and key just check A to run the game

---

<div class="post-metadata">

### Author: ![Metin](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@Metin](https://discourse.processing.org/u/Metin)
#### Post date: [June 21, 2021, 9:55am UTC](https://discourse.processing.org/t/how-do-i-loop-the-methods-in-my-game/30821/6 "2021-06-21T09:55:54Z")

</div>

Hello! So here is what I tried.

```auto
void draw() {
  background (Hintergrund); //Anzeige Hintergrund, hier von "start"

    //Spiel wird gestartet 
    Start();   
    if (s==true){ 
      Pac();
      Geist();
      muenze();}
  } 
void Start(){
  
if (keyPressed==true)
  {
    if (keyCode == '1')
    {
       Hintergrund = spiel; //Anzeige Starthintergrund auf Spielhintergrund
       background(Hintergrund); //Bei Tastendruck der "1" wird der Hintergrund gewechselt
       music.stop(); //Musik wird gestoppt
       s = true;
      
    }
    if (keyCode == '2')
    {
      Hintergrund = start; //Anzeige Spielhintergrund auf Starthintergrund
      background(Hintergrund); //Bei Tastendruck der "2" wird der Hintergrund gewechselt
      music.pause(); //Musik wird pausiert
      music.play(); //Musik wird wieder gespielt   
    }
  }
  
}

```

---

<div class="post-metadata">

### Author: ![Metin](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@Metin](https://discourse.processing.org/u/Metin)
#### Post date: [June 21, 2021, 9:56am UTC](https://discourse.processing.org/t/how-do-i-loop-the-methods-in-my-game/30821/7 "2021-06-21T09:56:42Z")

</div>

omg it functions omg

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 21, 2021, 10:16am UTC](https://discourse.processing.org/t/how-do-i-loop-the-methods-in-my-game/30821/8 "2021-06-21T10:16:35Z")

</div>

Hello,

There are resources (tutorials, references, examples, etc.) here:  
[https://processing.org/](https://processing.org/)

Take a look at the Keyboard references.  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/7c82a5bcfe2a19d5153f661a20fea51c32968c63.png)

Consider using the keyPressed() function outside of draw().

`:)`

---

<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: [June 21, 2021, 11:23am UTC](https://discourse.processing.org/t/how-do-i-loop-the-methods-in-my-game/30821/9 "2021-06-21T11:23:53Z")

</div>

It works!

Congratulations!

---

<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: [June 21, 2021, 1:48pm UTC](https://discourse.processing.org/t/how-do-i-loop-the-methods-in-my-game/30821/10 "2021-06-21T13:48:21Z")

</div>

> [@Metin](#):
>
> `void Start(){`

Just replace with

void keyPressed(){

It gets called automatically (so no need to call it explicitly)

---

<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: [June 21, 2021, 1:50pm UTC](https://discourse.processing.org/t/how-do-i-loop-the-methods-in-my-game/30821/11 "2021-06-21T13:50:12Z")

</div>

> [@Metin](#):
>
> `if (s==true){ `

technically if(s) { is enough here, the == true is not needed
