Processing: How to Control Game Screens with Mouse Clicks

Game Logic

Back to my Spanish language Processing project. The original version is scripted to operate as flashcards, but what I really want is a game that’ll keep me more engaged. I’m a millennial with a raging case of undiagnosed ADD. I need game screens.

So let’s map out the structure.

Screen Map

When the sketch initially runs, I want a screen that says Go. Then I want the game to continue for a specific amount of mouse clicks. Once that’s done, the player should hit a Game Over screen.

You can do this by breaking the Game and End Game processes into their own functions which can be called on within the void MousePressed() block. Using a global variable to track the number of mouse clicks, you’ll be able to let Processing know which screen to display throughout the course of the sketch.

The Functions

Making functions in Processing is pretty darn easy. Here’s the template:

void FunctionName() {
//Code to run
}

That probably looks familiar because you generally have at least a setup() and draw() function within each sketch. Void setup() runs once when the user presses play. That’s where I’ve put the start screen since it only needs to show at the beginning.

Quick note: The void draw() block doesn’t have code yet, but when I update this sketch to have a timer, that’s where I’ll be putting it.

Below the draw() block are the StartGame() and EndGame() functions. Those functions won’t run until the mouse is clicked. If our global variable storing the click count is below 10, the StartGame() function runs. Once it hits 10, it switches to EndGame(). Take a look below for the syntax within the MousePressed block.

A Quick Breakdown

Game Logic in Processing

And that’s it. Next I’ll be working on a way to get Processing to generate three wrong answers with the right one so that we can start keeping score.

References:
Ultimate Guide to the Processing Language Part II: Building a Simple Game by Oguz Gelal

Advertisements

5 thoughts on “Processing: How to Control Game Screens with Mouse Clicks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s