p5.js mouse, keyboard and if’s

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
CS0007: Introduction to Computer Programming
Conditionals – if - else Dr. Sajib Datta
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
ICM Week 2. Structure - statements and blocks of code Any single statement ends with semicolon ; When we want to bunch a few statements together we use.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
COMPSCI 101 Principles of Programming Lecture 27 - Using the Canvas widget to draw rows and columns of shapes.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Decision Structures and Boolean Logic
Keyboard and Events. What about the keyboard? Keyboard inputs can be used in many ways---not just for text The boolean variable keyPressed is true if.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
CSD 340 (Blum)1 Ifs. CSD 340 (Blum)2 Page that asks user for background color and changes fore color from black if user selects black as background color.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
1 2. Program Construction in Java. 2.4 Selection (decisions)
CIS 3.5 Lecture 2.2 More programming with "Processing"
CS – 1P Using Electronic Voting System (EVS) questioning in the Instructional Design for CS-1P.
Algorithms Writing instructions in the order they should execute.
Mouse Inputs in Processing. Interacting with the Mouse mouseX and mouseY: pg mouseXmouseY –The position of the mouse in the canvas pmouseX and.
Lesson Two: Everything You Need to Know
Often being different. Control flow By default Java (and therefore Processing) executes lines of a program one after the other –Doesn’t matter what happened.
G RAPHICS & I NTERACTIVE P ROGRAMMING Lecture 2 More Programming with Processing.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Lesson thirteen Conditional Statement "if- else" ©
Computer Science 1000 Algorithms III. Multiple Inputs suppose I ask you to write a program that computes the area of a rectangle area = length * width.
Review Random numbers mouseX, mouseY setup() & draw() frameRate(), loop(), noLoop() Mouse and Keyboard interaction Arcs, curves, bézier curves, custom.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
 Type Called bool  Bool has only two possible values: True and False.
Computer Science 1000 LOGO II. Boolean Expressions like Excel and Scratch, LOGO supports three Boolean operators less than (
Scratch Programming Cards
Java Programming Fifth Edition
Basic Input and Output CSE 120 Spring 2017
Programming & Scratch.
Scratch for Interactivity
CS0007: Introduction to Computer Programming
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
CMPT 201 if-else statement
Topics The if Statement The if-else Statement Comparing Strings
Microsoft Visual Basic 2005 BASICS
Computation as an Expressive Medium
Basic Input and Output CSE 120 Winter 2018
Topics The if Statement The if-else Statement Comparing Strings
Selection CIS 40 – Introduction to Programming in Python
And now for something completely different . . .
The C++ IF Statement Part 2 Copyright © Curt Hill
Selection (if-then-else)
Mouse Inputs in Processing
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 5, Conditionals Brief Notes
More programming with "Processing"
Pages:51-59 Section: Control1 : decisions
Selection Statements.
Chapter 5: Control Structure
The System.exit() Method
Lecture 6: Conditionals AP Computer Science Principles
IAT 265 Lecture 2: Java Loops, Arrays Processing setup, draw, mouse Shapes Transformations, Push/Pop.
Logic of an if statement
ICT Gaming Lesson 3.
Basic Input and Output CSE 120 Winter 2019
CS2011 Introduction to Programming I Selections (I)
Relational Operators.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
CHAPTER 5: Control Flow Tools (if statement)
Pages:51-59 Section: Control1 : decisions
Barlogik Mathsbee (final)
Presentation transcript:

p5.js mouse, keyboard and if’s

In this variation of an earlier program, the ellipses are red if the mouse is pressed and green otherwise

mouseIsPressed p5.js has a Boolean variable mouseIsPressed Variable means it can change Boolean means it is true or false The structure of if is If(ask some true-false question){ what to do if the answer is true } else { what to do if the answer is false }

mouseX and mouseY mouseX and mouseY are built-in variables in p5.js that indicate The horizontal position of the mouse from the left edge of the canvas: mouseX The vertical position of the mouse from the top of the canvas: mouseY Since there is no background() in the function draw, we “remember” what was drawn earlier and draw on top of it

Nested if’s: within the block of code associated with an if (inside curly brackets), we can ask further (follow-up) questions

Nesting cont. You only execute line 11 if the mouseIsPressed is true and if it is true that the mouseX is less than half the width You only execute line 13 if the mouseIsPressed is true and if it is false that

else if: A different type of follow-up question

Else if Cont. The else-if structure allows one to ask a follow-up question in case the first question produced an answer of false First we ask if the horizontal position is less than 100, when that turns out to be false we ask the follow-up question is it less than 200, when that is false we ask the follow question is it less than 300, etc.

Boolean AND: Asking if two things are true without nesting

ANDED if(200<mouseX && mouseX<400){ fill(255,0,0); } There are two conditions (true-false) questions: 1) is the horizontal position greater than 200; 2) is the horizontal position less than 400 AND (uses &&) mean BOTH must be true for the combination to be true Result horizontal position must be between 200 and 400

Another AND example: the conditions can be on different variables (e.g. horizontal and vertical positions)

Boolean OR: Asking if either of two things is true (uses || between conditions)

Another OR example: either less than 200 or more than 400

Combining AND’s with OR’s

Taking Precedence When combining Boolean AND’s and OR’s, remember that an AND is a bit like multiplication and an OR is a bit like addition. Thus like multiplication and addition, there is a precedence (order of operations). Like multiplication is done before addition, ANDing is done before ORing. Parentheses can force a higher precedence

Without parentheses

With parentheses

Japanese Flag

The function keyPressed handles the “typing” event The function keyPressed handles the “typing” event. If the code (ASCII) for the character typed is an up arrow, change y the vertical position The print statement shows results in the console

UP_ARROW same as 38

The Numpad uparrow has a code of 104, so an OR allows the user to press either up arrow

Else if to allow down arrows

More code to allow horizontal moves