You are on page 1of 20

MR3009

Diseño Mecatrónico

Weekdays 13:00 – 15:00

Ricardo Roberts ricardo.roberts@tec.mx


Eduardo Piña pinha@tec.mx
Processing
Intro
Snake Game

The snake “head”:


-Moves continuously.
-Direction responsive to buttons.
-Can eat food items.
-Dies if it eats the body.
-Can jump across screen edges.

The snake “body”:


-Follows the head.
-Increases in size every time food is eaten.

The “food”:
-Is placed randomly in the screen.
-Can be eaten by the snake.
-Reappears at a new location every time is eaten.
Go to:
processing.org

Click on:
“Download Processing”
Download the Processing version that suits
your Operating System.
Download the Processing version that suits
your Operating Systemc
Open Processing, you should see a blank
program.
Start your program creating the “setup” and
“draw” functions. Note the position of the
parenthesis and brackets, they all have a
function. Don’t forget to add “void” before
each function.
Use the “size” function to determine the
workspace dimensions. The two numbers
represent the horizontal and vertical lengths
of the workspace. Press “Run” button to test
the code. A grey rectangle should appear.
Close the new window to continue working.
Use the “background” function to define the
color.
The three numbers correspond to the Red,
Green, and Blue components of the color
(RGB). Define the background color of your
preference.
Add two variables of the type integer to
encode the position of the snake. Call them
however you want. In this example they are
named “posX” and “posY”. Use the function
“rect” to place a rectangle on said position.
This will be our snake head.
Add two more variables to encode the
snake movement.
Use those values to update the snake
position every cycle. Use the “delay”
function to define the game update rate.
At the end of the program create a new
function named “keyPressed()”. This
function will run every time you use your
keyboard. You can add “if” conditionals to
change the snake movement when a key is
pressed.
To convert the square to a true snake.
Change the code that defines the position
as shown.
Try your program.
P.S.:
You can add comments that do not affect
the program functionality. Note the
difference between “line comment” and
“paragraph comment”.
Continue adding code until you have a fully functional game. Then add your own modification. It can be some
change in functionality, playability, or aesthetic. Be creative!
Describe how your modification changes the player experience (less tan 200 words).
Paste said description in the program as a paragraph comment.

Compress the folder of your program as a zip file


Upload the file to Canvas:
Activities/4 Processing

Due date: January 28, 2021

Individual activity
Minimalistic
Arduino +
Processing
DAQ
Processing Code
Arduino Code

You might also like