Scoring System

by Alex VanDenburgh in Circuits > Raspberry Pi

499 Views, 3 Favorites, 0 Comments

Scoring System

IMG_0548.jpg
IMG_0715.jpg

My friends and I made a skee ball table by semester, Wooden Skeeball Game : 18 Steps (with Pictures) - Instructables. We decided to add our own scoring system to it. If you are looking to recreate exactly what we built you will first have to follow the aforementioned skee ball table Instructable. On the other hand, if you wish to just make the scoring system for something else then the skee ball table is not required. However the code was written in regards to scoring a skee ball table, so the code may need to be edited to fit other projects. This was a lot of fun to make and we hope that you will be able to enjoy it as much as we did!

Supplies

  1. Raspberry Pi 4 Model B
  2. ME-91011NC+1NO Flexible Coil Spring Rod Momentary Limit Switch
  3. Copper Wires
  4. Connector Kit
  5. Amazon.com: Glarks 1191Pcs 2.54mm Pitch 1 2 3 4 5 6 7 8 9 10 Pin Housing Connector and Male Female Crimp Pins with Pin Header and 10 Wire Rainbow Color Flat Ribbon IDC Cable Compatible with Dupont Connector Kit : Electronics
  6. Monitor (With HDMI)
  7. Cables:
  8. Micro HDMI to HDMI
  9. USB to USBC power cable
  10. Scratch 3 downloaded onto Raspberry Pi
  11. Basic understanding of Scratch.
  12. Basic understanding of Raspbian (Raspberry Pi OS)

Raspberry Pi Simple Electronics

IMG_0101.jpg

First, within Raspberrry Pi's Scratch 3, enable the Raspberry Pi Simple Electronics expansion. This will allow us to connect our code to whatever is connected to the Raspberry Pi's pins

Variables

sas.PNG

We are going to need 4 variables for this project: Ball Count, High Score, Index, and Score. Ball Count, Score, and High Score are self-explanatory, but Index will be used to make our score a certain number a digits long - SOmething that will be explained in a later step

Scoring

IMG_0099.jpg
IMG_0102.jpg
IMG_0719.jpg

This is the first chunk of code that will be used to track when the switches are activated. It first sets the score to 0 then checks to see if the internal ball count is higher than 0, which will be important later on. Then it checks if the correct switch is pressed, and if it is, changes our score variable by the specified amount and waits for a full second (half a second for 1000, in case 2 balls go through one after the other). Since we are making ours with respect to a skee ball table, our scores are in increments of 1000, 2000, 3000, 5000, and two 10000s. These scores can easily change by modifying the number in the current code and in a later chunk of code. Having a wait block after changing the score is essential because if it was missing, the score would keep increasing by the specified amount every millisecond the switch was pressed. Create each of these chunks for every switch that will count score, changing the desired score earned and button pressed on the Raspberry Pi as needed.

We will explain how the Raspberry Pi pins and buttons work in the switches step.

Ball Counter

aDS.PNG

This chunk of code is used to keep track of the ball count while playing the game. When the scratch game is started, the ball count is set to nine (Change this number if you have a different number of tries in mind). Once the ball count has reached 0 balls, it broadcasts the "stop increase" message. This broadcast will be explained in the next step. It then waits 5 seconds to give the player time to see and share their score. Afterward, the score is reset back to 0 and the ball count back to 9.


Checking Results

a.PNG

This next section is what will happen when the broadcast is called, which happens when the ball count reaches 0. As to not keep increasing points when the game is over, this code stops the adding of points to our score. Once the ball count reaches zero, it waits for 2/5 of a second and then checks to see if any switches are pressed. If one gets pressed in the 5 seconds the broadcast is active, the game removes the specific number of points a switch would give immediately after it is added. For example, if you ended the game with 20,000 points and rolled a tenth ball into the 10,000 point hole, the game gives you 10,000 points but immediately takes it away before one can notice. This code is needed for every button, and the numbers should be the same as you get for scoring except they are negative, since Scratch does not have the ability to subtract. At the very end, once the ball count is reset back to 9, this script is suspended. This suspension is needed to allow the game to continue normally in the next round. If it were not here, the game would never award the player points.

Tracking High Score

asdads.PNG

Once the game is started, the scoring system is immediately checking if the current score is higher than the high score. if the score is in fact higher than the current high score, the high score count will be changed alongside the score. The wait block in the if statement is used to make sure the high score does not change when the ball count is 0 and another switch is turned on, which was explained in the previous step.


Tracking Ball Count

fghjf.PNG

The code for this section is too long to fit into a cohesive picture, so our apologies upfront. This code will be used to count how many balls have been played in a game. For the if statement, 5 "or" blocks from the operators tab are needed. Placing each of the switches that are being used into the long "or" string will make sure the computer is checking all the switches at the same time. When one of them is pressed, the ball count decreases by one and waits one second, as to not accidentally count one ball as two.


Reset Button

Capture.PNG

This last piece of code within the scene allows the user to do a full reset of the code. if a certain switch is pressed, the ball count goes back to nine, the score goes back the zero, and the high score also resets back to zero. Along with this, every script written thus far is stopped, meaning if this switch is pressed, a manual start is needed to begin the game again.


Sprites

1c.PNG
1b.PNG
1a.PNG

The next step can be done at any point in the coding process. Decorating the score screen is necessary to keep people entertained while playing. Decorate it however you want - the only sprites you need to have are "Score Tile", "High Score Tile", and "Ball Count Tile". The rest is up for you to choose!

Costumes Within Sprites

2a.PNG
2b.PNG

Within the aforementioned 3 required sprites, you need 11 costumes, 10 of which have a different single-digit number in the middle of them 0-9. Each of the costumes should be named "n_", with the blank being the number in the costume. So, if our costume has the number 6, the costume should be called "n6". The last of the 11 costumes should have nothing in it and be called "n". This costume will be used in cases where the score can have more than 1 digit (more on this later) and will replace the extra digits with nothing.


Changing Score Sprite

asdf.PNG
asdasd.PNG
hh.PNG

These 3 pictures of code are from different sprites within the game. The first one is inside "Score Tile", the second one is inside "High Score Tile", and the third is within "Ball Count Tile". Each of them does the same thing with relatively the same code but with some small changes to fit their respective variable.


For the regular scoring, the game immediately hides the sprite, moves it to the correct place on the screen, and sets its index to zero. We hide the sprite in order to not have the clones we are about to create overlapping with the original sprite. The index variable is how we tell the game what place in the score a certain number is. For example, if our score is 51924, the 4 is in the 0 index, the 2 in the 1 index, the 9 in the 2 index, and so on. The next step of the code is to create a clone of the sprite, move it to the right by changing the x by 50, and increase the index by 1, since it is now a new digit in the score. This process is repeated 6 times since the highest score someone can get in our game is 6 digits long (90000). The next block of code explains what the clones should do once they are created. They are shown and, if the score is a certain number, change their costume to that number - something explained in a previous step.


For the high score tile, the code is exactly the same, with the only change being the score variable in the last chunk changing to a high score variable.


As for the ball count, a bigger change is needed. Since our max ball count per game only goes up to 9, we do not need to display more than 1 digit, so repeating the code multiple times is not needed. Instead, we just need to create a single clone of our sprite. We also need to change the variable to "Ball Count" in the last chunk.

Connecting Switches

0ADFE7ED-6B17-4477-BBB5-A8E90765786C.jpg
34FE1604-7987-4831-8AFE-D7034C451C52.jpg

When first connecting your wires to the switches you need to locate the NO (normally open) and NC (normally closed) this determines how the switch sends its pulse when activated. For this application, we will be using NO so when the switch is activated it will tell the raspberry pi it's been activated. You will need to unscrew the corresponding screws for No and feed the wire underneath the screw and tighten it to lock the wire in place. You will have to do this twice one wire will be for the GPIO (general purpose input and output) and one for the round. Next, we will have to connect the wire to the raspberry pi's pins. we will need to cut the length of the smaller wire that comes with the connector kit and mesh it with the existing copper wire already on the wires. after that's done we need to connect the metal pin holder. you will need to expose the wire and feed it into the housing and crimp the two pointed prongs over so that it holds the wire firmly in place. after that is done you need to insert it into the black plastic housing until you hear a click.


Where to Connect Switches

CA6A0802-6C27-49B0-BF91-130F5874C7CB.png
655A7748-0F72-4284-9724-AA9FC9EEAACE.jpg
IMG_0133.jpg
F52D2EF8-0A95-4194-8F57-3B72687EC33E.jpg
28813303-6EF3-4EA2-A7B1-E87DF25D29E2.jpg

When connecting the switches make sure that one wire is always connected to a ground this is essential so that the raspberry pi won't crash. on the pin diagram above all the GPIO labels are your inputs and the corresponding number of the GPIO will correspond with the switch number in the code. when connecting the switches one wire will connect to a GPIO and another to the ground (triangle on diagram). The GPIO number will define the number the switch is connected to. So for example, if switch A is plugged into a ground and GPIO 2 you will have to change the code so that switch two is used. Therefore when the switch connected to GPIO2 is trigged it should activate the code linked to switch two.

Display

9k.png

To connect the Raspberry Pi to the monitor use the Micro HDMI to HDMI cable along with using the USB to USBC power cable to power the Raspberry Pi.