

boolean and float are primitive data types, boolean refers to a value that can be either true or false, and float refers to a number that unlike an integer can specify a whole number and a decimal part. Section 2.2.1, adding properties, introduces a couple of data types that may be unfamiliar to beginners. The boolean variable gameover is used to check if the player has lost the game and the float variable s is a general size specifying the size of the rectangles representing the objective and the snake.įigure 4: Shows the property of the objective. The first two properties to add is gameover and s (see figure 2). The first step is to add global properties that can store information about size, positions, etc. To keep the article simple, the following section is divided into different steps of coding the game. The score increases whenever the snake collides with the objective.The game ends if the snake’s head collides with its tail.The objective gets a new random position when it collides with the snake’s head.When the snake reaches the screen’s border, it is teleported to the opposite position.The first rectangle of the snake represents the head of the snake and it is only the head that can trigger collision events with the objective and tail.The snake and objective are drawn using rectangles.Section 2.1, therefore, describes the requirements of the game which is followed by a section describing the code. 2 Building the gameĪs with every project, it can be a great idea to establish a couple of requirements to set the direction and outline an overview of necessary features. Find more 2D primitive methods at /reference.įigure 1: Display the direction of the horizontal axis (x) and vertical axis (y) within Processing’s graphical interface. Drawing shapes within Processing is very simple because the library provides the 2D primitive methods such as rect(x, y, w, h), circle(x, y, r), or line(x1, y1, x2, y2). Processing also provides a graphical interface where the point (x, y) = (0, 0) is at the left top corner (see figure 1). keyPressed() executes when a key is pressed, and the key is stored in a variable named key (Processing, 2022c).draw() executes forever and is called a number of times matching the framerate (Processing, 2022b).setup() is called one time when the application starts (Processing, 2022a).Another built-in method, the article is introducing later is keyPressed(). Two important methods to know to get started with Processing are the methods setup() and draw(). Processing provides many methods to control the flow of an application, for example, to control key events, initialization, or continuous behavior. To download and install Processing, go to /download and download the installable matching the preferred platform. The library is built on Java and provides additional classes and simplifications (Wikipedia, 2022). Wikipedia (2022) describes Processing as a graphical library and integrated development environment (IDE). To learn more about how and for what purposes Amazon uses personal information (such as Amazon Store order history), please visit our Privacy Notice.A GIF displaying the Snake game from the article.ĭo you know the basics of Java or Processing and want to start developing your own games or maybe just recreate one of the classics? This article describes the process of building the classic Snake game using Processing.

You can change your choices at any time by visiting Cookie Preferences, as described in the Cookie Notice.

Click ‘Customise Cookies’ to decline these cookies, make more detailed choices, or learn more. Third parties use cookies for their purposes of displaying and measuring personalised ads, generating audience insights, and developing and improving products. This includes using first- and third-party cookies, which store or access standard device information such as a unique identifier. If you agree, we’ll also use cookies to complement your shopping experience across the Amazon stores as described in our Cookie Notice. We also use these cookies to understand how customers use our services (for example, by measuring site visits) so we can make improvements. We use cookies and similar tools that are necessary to enable you to make purchases, to enhance your shopping experiences and to provide our services, as detailed in our Cookie Notice.
