We know setup( ) happens once and draw( ) loops forever. In order to learn how to have something happen when the mouse is clicked, we need to return to the flow of our program. A crucial form of interaction, however, is missing-clicking the mouse! We are well on our way to creating dynamic, interactive Processing sketches through the use the setup( ) and draw( ) framework and the mouseX and mouseY keywords. Size(200,200) // Set the size of the windowīackground(255) // Draw a white backgroundĬhange the code so that his eyes and legs move with the rest of the body.Īlter the code of your own character to do the same. The following example only moves Zoog’s body and head. Zoog’s head, forĮxample, is located at ( mouseX, mouseY-30 ). Zoog’s body are drawn relative to the mouse. Note that my Zoog’s body is located at the exact location of the mouse ( mouseX, mouseY ), however, other parts of For example, we can rewrite your character to follow the mouse. We could push this idea a bit further and create an example where a more complex pattern (multiple shapes and colors) is controlled by mouseX and mouseY position. For now, we only need to look at two simple blocks: setup( ) and draw( ). Will do this for you via the Auto-Format option (Tools → Auto-Format).īlocks of code will reveal themselves to be crucial in developing more complex logic, in terms of variables, conditionals, iteration, objects , and functions, as discussed later. A programming convention is to indent the lines of code within each block to make the code more readable. This is an important construct as it allows us to separate and manage our code as individual pieces of a larger puzzle. Technically speaking setup( ) and draw( ) are functions.Ī block of code is any code enclosed within curly brackets.īlocks of code can be nested within each other, too. This is accomplished by writing two “ blocks of code ” setup( ) and draw(). This program will draw to the screen continuously (i.e., until the Now that we are good and exhausted from running marathons in order to better learn programming, we can take this newfound knowledge and apply it to our first “dynamic ” Processing sketch.
0 Comments
Leave a Reply. |