-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added chapter 8 exercise solutions, tweaks to lecture slides
- Loading branch information
1 parent
dda81f5
commit fb57766
Showing
8 changed files
with
249 additions
and
1 deletion.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
learning-processing-exercise-solutions/08-objects/exercise-8-1.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
## Car data | ||
|
||
* `color` | ||
* `transmission` | ||
* `maxSpeed` | ||
* `acceleration` | ||
* `fuelUsage` | ||
* `fuelCapacity` | ||
* `currentFuel` | ||
* `engineType` | ||
* `tireType` | ||
* ... | ||
|
||
## Car functions | ||
|
||
* `start()` | ||
* `steer()` | ||
* `changeToGear()` | ||
* `brake()` | ||
* `accelerate()` | ||
* `indicate()` | ||
* `toggleHeadlights()` | ||
* `toggleWindshieldWipers()` | ||
* ... |
21 changes: 21 additions & 0 deletions
21
learning-processing-exercise-solutions/08-objects/exercise-8-2.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
```java | ||
class Human { | ||
|
||
color hairColor; | ||
float height; | ||
boolean awake; | ||
boolean eyesOpen; | ||
|
||
Human () { | ||
hairColor = color(100,100,100); | ||
height = 1 + random(1); | ||
awake = true; | ||
eyesOpen = true; | ||
} | ||
|
||
void sleep () { | ||
eyesOpen = false; | ||
awake = false; | ||
} | ||
} | ||
``` |
7 changes: 7 additions & 0 deletions
7
learning-processing-exercise-solutions/08-objects/exercise-8-3.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
## Declare and initialise | ||
|
||
`Human h = new Human();` | ||
|
||
## Call the sleep function | ||
|
||
`h.sleep();` |
50 changes: 50 additions & 0 deletions
50
learning-processing-exercise-solutions/08-objects/exercise-8-4.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Main file | ||
|
||
```java | ||
Car myCar; | ||
|
||
void setup() { | ||
size(200, 200); | ||
// Initialize Car object | ||
myCar = new Car(); | ||
} | ||
|
||
void draw() { | ||
background(255); | ||
// Operate Car object. | ||
myCar.move(); | ||
myCar.display(); | ||
} | ||
``` | ||
|
||
# `Car.pde` | ||
|
||
```java | ||
class Car { | ||
color c; | ||
float xpos; | ||
float ypos; | ||
float xspeed; | ||
|
||
Car() { | ||
c = color(255); | ||
xpos = width/2; | ||
ypos = height/2; | ||
xspeed = 1; | ||
} | ||
|
||
void display() { | ||
// The car is just a square | ||
rectMode(CENTER); | ||
fill(c); | ||
rect(xpos, ypos, 20, 10); | ||
} | ||
|
||
void move() { | ||
xpos = xpos + xspeed; | ||
if (xpos > width) { | ||
xpos = 0; | ||
} | ||
} | ||
} | ||
``` |
51 changes: 51 additions & 0 deletions
51
learning-processing-exercise-solutions/08-objects/exercise-8-5.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Main file | ||
|
||
```java | ||
Ball ball; | ||
float gravity = 0.1; | ||
void setup() { | ||
size(200, 200); | ||
ball = new Ball(50, 0); | ||
} | ||
void draw() { | ||
background(255); | ||
ball.display(); | ||
ball.move(); | ||
} | ||
``` | ||
|
||
# `Ball.pde` | ||
|
||
```java | ||
class Ball { | ||
float x; | ||
float y; | ||
float speed; | ||
|
||
Ball(float _x, float _y) { | ||
x = _x; | ||
y = _y; | ||
speed = 0; | ||
} | ||
|
||
void display () { | ||
// Display the circle | ||
fill(175); | ||
stroke(0); | ||
ellipse(x, y, 10, 10); | ||
} | ||
|
||
void move () { | ||
// Add speed to y location | ||
y = y + speed; | ||
// Add gravity to speed | ||
speed = speed + gravity; | ||
// If square reaches the bottom | ||
// Reverse speed | ||
if (y > height) { | ||
speed = speed * -0.95; | ||
y = height; | ||
} | ||
} | ||
} | ||
``` |
83 changes: 83 additions & 0 deletions
83
learning-processing-exercise-solutions/08-objects/exercise-8-6.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
## Main file | ||
|
||
```java | ||
Zoog zoog; | ||
Zoog zoog2; | ||
|
||
void setup() { | ||
size(200, 200); | ||
zoog = new Zoog(100, 125, 60, 60, 16, color(255,0,0),0,width/2); | ||
zoog2 = new Zoog(80, 150, 60, 60, 16, color(0,255,0),width/2,width); | ||
} | ||
|
||
void draw() { | ||
background(255); | ||
// mouseX position determines speed factor | ||
float factor = constrain(mouseX/10, 0, 5); | ||
zoog.jiggle(factor); | ||
zoog2.jiggle(factor); | ||
zoog.display(); | ||
zoog2.display(); | ||
} | ||
``` | ||
|
||
## `Zoog.pde` | ||
|
||
```java | ||
class Zoog { | ||
// Zoog's variables | ||
float x, y, w, h, eyeSize, constrainLeft, constrainRight; | ||
color bodyColor; | ||
|
||
|
||
// Zoog constructor | ||
Zoog(float tempX, float tempY, float tempW, float tempH, float tempEyeSize, color tempBodyColor, float tempConstrainLeft, float tempConstrainRight) { | ||
x = tempX; | ||
y = tempY; | ||
w = tempW; | ||
h = tempH; | ||
eyeSize = tempEyeSize; | ||
bodyColor = tempBodyColor; | ||
constrainLeft = tempConstrainLeft; | ||
constrainRight = tempConstrainRight; | ||
} | ||
|
||
// Move Zoog | ||
void jiggle(float speed) { | ||
// Change the location of Zoog randomly | ||
x = x + random(-1, 1)*speed; | ||
y = y + random(-1, 1)*speed; | ||
// Constrain Zoog to window | ||
x = constrain(x, constrainLeft, constrainRight); | ||
y = constrain(y, 0, height); | ||
} | ||
|
||
// Display Zoog | ||
void display() { | ||
// Set ellipses and rects to CENTER mode | ||
ellipseMode(CENTER); | ||
rectMode(CENTER); | ||
// Draw Zoog's arms with a for loop | ||
for (float i = y - h/3; i < y + h/2; i += 10) { | ||
stroke(0); | ||
line(x - w/4, i, x + w/4, i); | ||
} | ||
// Draw Zoog's body | ||
stroke(0); | ||
fill(bodyColor); | ||
rect(x, y, w/6, h); | ||
// Draw Zoog's head | ||
stroke(0); | ||
fill(255); | ||
ellipse(x, y - h, w, h); | ||
// Draw Zoog's eyes | ||
fill(0); | ||
ellipse(x - w/3, y - h, eyeSize, eyeSize*2); | ||
ellipse(x + w/3, y - h, eyeSize, eyeSize*2); | ||
// Draw Zoog's legs | ||
stroke(0); | ||
line(x - w/12, y + h/2, x - w/4, y + h/2 + 10); | ||
line(x + w/12, y + h/2, x + w/4, y + h/2 + 10); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters