Creating a Scoreboard for Simple Game

Hi guys, I’m new to this forum so I’m not really sure how to post my code properly. I’m trying to make a game where a character, Steve, runs around and collects food. Whenever he runs over a piece of food, the food reappears somewhere else. How do I make a scoreboard that goes up by 1 every time he runs over the food? Thank you for your help.

import processing.serial.*;
Serial myPort;

int SteveX = 500;
int SteveY = 500;
int SteveWidth = 30;
int SteveHeight = 30;
int foodWidth = 10;
int foodHeight = 10;
float foodX = random(1000-foodWidth);
float foodY = random(1000-foodHeight);
int buttonVal = 0;
int speed = 5;
color bgcolor = color(255, 204, 100);

void setup(){
  size(1000,1000);
  strokeWeight(4);
  println("Available serial ports:");
  println(Serial.list());
  
  myPort = new Serial(this, "COM13", 9600);
}

void draw(){
  background(bgcolor);
  
  if(myPort.available() > 0){
    buttonVal = myPort.read();
    println(buttonVal);
  }
rect(SteveX, SteveY, SteveWidth, SteveHeight);

if(buttonVal >= 254){
  SteveX = SteveX-speed;
}
else if((buttonVal >= 249) && (buttonVal <= 251)){
  SteveY = SteveY-speed;
}
else if((buttonVal >= 126) && (buttonVal <= 129)){
  SteveX = SteveX+speed;
}
else if((buttonVal < 3) && (buttonVal > 0)){
  SteveY = SteveY+speed;
}

rect(foodX, foodY, foodWidth, foodHeight);

if((foodX > SteveX-foodWidth) && (foodX < SteveX+SteveWidth) && (foodY < SteveY+SteveHeight) && (foodY > SteveY-foodHeight)){
  foodX = random(1000-foodWidth);
  foodY = random(1000-foodHeight);
}

if(SteveX<0){
  SteveX=0;
}
if(SteveX>1000-SteveWidth){
  SteveX=1000-SteveWidth;
}
if(SteveY<0){
  SteveY=0;
}
if(SteveY>1000-SteveHeight){
  SteveY = 1000-SteveHeight;
}
}

Hi
Format your code using preformatted text

Hello @honeydew

This previous forum topic may give you some ideas on how to proceed implementing a scoreboard:

Hope this helps!
:nerd_face:

2 Likes

First you need an int variable to keep track of the score

Where you increase the score say score++; once

To make a high score you need a separate state to
Display the High Score

You also need a was to enter a player Name
(which is also a new state of the program)

When you want to see the old high score when you start the program again/ on the next day, you need to save it on your hard drive (look at saveStrings() command in the reference)

To use it over the web, you need to store it in the web

2 Likes

Here apparently you want to say
score++;

Use fill(150); text(score,17,17); in the main section (not in the if clause obviously) to display the text throughout

Before setup () ::: int score=0;

1 Like