Skip to content

Commit

Permalink
Added chapter 8 exercise solutions, tweaks to lecture slides
Browse files Browse the repository at this point in the history
  • Loading branch information
pippinbarr committed Oct 9, 2017
1 parent dda81f5 commit fb57766
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 1 deletion.
24 changes: 24 additions & 0 deletions learning-processing-exercise-solutions/08-objects/exercise-8-1.md
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 learning-processing-exercise-solutions/08-objects/exercise-8-2.md
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;
}
}
```
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 learning-processing-exercise-solutions/08-objects/exercise-8-4.md
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 learning-processing-exercise-solutions/08-objects/exercise-8-5.md
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 learning-processing-exercise-solutions/08-objects/exercise-8-6.md
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);
}
}
```
6 changes: 5 additions & 1 deletion slides/03-Loops-and-functions/03-loops-and-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ rect(5*boardWidth,height-boardHeight,boardWidth,boardHeight);
--
And then you die of boredom.

???

__Note:__ We use `height-boardHeight` as the y coordinate of each board to make it align with the bottom of the screen. If we drew it at `height` its top-left corner would be at the bottom (and we wouldn't see it), so we subtract `boardHeight` to draw it perfectly aligned.

---

class: middle
Expand Down Expand Up @@ -242,7 +246,7 @@ class: middle
```java
void draw() {
int x = 0;
while (mouseX < width) {
while (mouseX < 50) {
rect(x,mouseY,10,10);
x += 20;
}
Expand Down
8 changes: 8 additions & 0 deletions slides/04-Objects-and-style/04-objects-and-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ void setup() {
- This would be a new _type_ of thing, so we'd have a _variable_ of _type_ Ball
- But... how do we make it?

???

__Note:__ Calling our _type_ Ball and our variable _ball_ shows us the important different between type (aka. class) names and variable names. When we do OOP the new types of things we make have a capital letter, but the variable will have a lowercase letter.

It's a bit like `int number;` in a way. The variable is named after the type.

---

# Let there be Ball!
Expand Down Expand Up @@ -312,6 +318,8 @@ This is what a _class_ definition looks like.
- And Processing now knows what a Ball is
- ... admittedly, though, it doesn't _do_ anything

__Note:__ The ball _does exist_, it just has no representation on the screen and doesn't do anything!

---

# Ball...
Expand Down

0 comments on commit fb57766

Please sign in to comment.