# How to make score only go up by one when pressing spacebar?

**URL:** <https://discourse.processing.org/t/how-to-make-score-only-go-up-by-one-when-pressing-spacebar/38247>\
**Category:** Beginners\
**Created:** [August 6, 2022, 2:33pm UTC](https://discourse.processing.org/t/how-to-make-score-only-go-up-by-one-when-pressing-spacebar/38247 "2022-08-06T14:33:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mathamagic](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Mathamagic](https://discourse.processing.org/u/Mathamagic)\
**Post date:** [August 6, 2022, 2:33pm UTC](https://discourse.processing.org/t/how-to-make-score-only-go-up-by-one-when-pressing-spacebar/38247/1 "2022-08-06T14:33:31Z")

</div>

I am trying to make a flappy bird clone and i was woundering how do i make it so that whenever you press the spacebar it increases the score by 1 and doesnt and more than 1 if you hold the space bar?

```auto
let y = 200 ;

let score = 1;

function setup() {
  createCanvas(400, 400);
}

function draw() {

  if (keyIsDown(32)) {
    y -= 10;
    score += 1;
  } else {
    y += 5;
  }

  clear();
  fill(255, 255, 0);
  background(50, 204, 255);
  ellipse(200, y, 50, 50);

  fill(0);
  textAlign(CENTER, CENTER);
  textSize(50);
  text(score, 350, 200);

  
  if (y >= 400) {
    clear();
    background(50, 204, 255)
    fill(0)
    textAlign(CENTER)
    textSize(50)
    text("Game Over", 200, 200)
  }
}

```

P.S sorry for the mess

---

<div class="post-metadata">

**Author:** ![Chi3010](https://avatars.discourse-cdn.com/v4/letter/c/8491ac/32.png) [@Chi3010](https://discourse.processing.org/u/Chi3010)\
**Post date:** [August 15, 2022, 10:47pm UTC](https://discourse.processing.org/t/how-to-make-score-only-go-up-by-one-when-pressing-spacebar/38247/2 "2022-08-15T22:47:16Z")

</div>

Hello! If you want y to increase by pressing and holding and score to increase only when pressed, you can do that by moving score into the keyPressed function. keyPressed is called only once when a key is pressed and m keyPressed is called only once when a key is pressed.

> **[reference | p5.js](https://p5js.org/reference/#/p5/keyPressed)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

```auto
var y = 0;
var score = 0;

function setup() {
  createCanvas(400, 400);
}

function draw() {

  if (keyIsDown(32)) {
    y -= 10;
  } else {
    y += 5;
  }

  clear();
  fill(255, 255, 0);
  background(50, 204, 255);
  ellipse(200, y, 50, 50);

  fill(0);
  textAlign(CENTER, CENTER);
  textSize(50);
  text(score, 350, 200);

  
  if (y >= 400) {
    clear();
    background(50, 204, 255)
    fill(0)
    textAlign(CENTER)
    textSize(50)
    text("Game Over", 200, 200)
  }
}

function keyPressed() {
  score += 1;
}

```
