# Button press to pause game

**URL:** https://forum.code.org/t/button-press-to-pause-game/31630
**Category:** CS Discoveries
**Tags:** csd-unit-3
**Created:** [November 5, 2019, 9:18pm UTC](https://forum.code.org/t/button-press-to-pause-game/31630 "2019-11-05T21:18:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![filiere.ann](https://avatars.discourse-cdn.com/v4/letter/f/4af34b/32.png) [@filiere.ann](https://forum.code.org/u/filiere.ann)
#### Post date: [November 5, 2019, 9:18pm UTC](https://forum.code.org/t/button-press-to-pause-game/31630/1 "2019-11-05T21:18:00Z")

</div>

I have a student who wants to code a space bar press to pause the game and then press it again to restart it.

---

<div class="post-metadata">

### Author: ![mmathes](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@mmathes](https://forum.code.org/u/mmathes)
#### Post date: [November 6, 2019, 4:18am UTC](https://forum.code.org/t/button-press-to-pause-game/31630/2 "2019-11-06T04:18:11Z")

</div>

One idea would be to use a modulus inside of an if statement. Create a variable to count the number of times you press space bar and set the value to 0.

var counter = 0;

Create an if statement to count the number of times you press the space bar.

if(keyWentDown(“space”)) {  
counter = counter + 1;  
}

Create another if statement to pause the game (stop whatever actions you are doing) if counter mod 2 is not equal to 0 . So every odd number of space bar presses should pause the game.

SAMPLE CODE FOR THE FLYER GAME

if(counter % 2 != 0) {  
character.velocityX = 0;  
character.velocityY = 0;  
fill(“black”);  
textSize(“50”);  
textAlign(“center”, “center”);  
text(“PAUSED”,200,200);  
}

Hope this helps!

---

<div class="post-metadata">

### Author: ![mkmietowicz](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mkmietowicz/32/6322_2.png) [@mkmietowicz](https://forum.code.org/u/mkmietowicz)
#### Post date: [November 6, 2019, 4:28am UTC](https://forum.code.org/t/button-press-to-pause-game/31630/3 "2019-11-06T04:28:25Z")

</div>

I was trying to work out some kind of clever workaround using booleans, but @mmathes beat me to the punch. That looks like it should work.

–Michael K.

---

<div class="post-metadata">

### Author: ![kookooloopdaking](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/kookooloopdaking/32/5224_2.png) [@kookooloopdaking](https://forum.code.org/u/kookooloopdaking)
#### Post date: [November 15, 2019, 8:37pm UTC](https://forum.code.org/t/button-press-to-pause-game/31630/4 "2019-11-15T20:37:47Z")

</div>

you could have the code in the draw loop that effects the game ran if an if/then statement reads true. ex:

var paused = false;

function draw() {

if (keyWentDown(“space”)) {

paused = !paused;

}

if (!paused) {

//game code

}  
}

---

<div class="post-metadata">

### Author: ![filiere.ann](https://avatars.discourse-cdn.com/v4/letter/f/4af34b/32.png) [@filiere.ann](https://forum.code.org/u/filiere.ann)
#### Post date: [November 15, 2019, 9:26pm UTC](https://forum.code.org/t/button-press-to-pause-game/31630/5 "2019-11-15T21:26:46Z")

</div>

Thank you! The suggestions are very helpful!
