forked from CodingTrain/website-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54e6e44
commit 34f8f99
Showing
17 changed files
with
804 additions
and
0 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
CodingChallenges/CC_72_Frogger/CC_72_Frogger_original/CC_72_Frogger_original.pde
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,124 @@ | ||
// Daniel Shiffman | ||
// http://codingtra.in | ||
// http://patreon.com/codingtrain | ||
|
||
// Frogger | ||
// Part 1: https://youtu.be/giXV6xErw0Y | ||
// Part 2: https://youtu.be/06-ZvYmSeus | ||
// Part 3: https://youtu.be/hk326ZHlENQ | ||
// Part 4: https://youtu.be/c6WdJltqEtM | ||
|
||
Frog frog; | ||
Car[] cars; | ||
Log[] logs; | ||
|
||
float grid = 50; | ||
|
||
void resetGame() { | ||
frog = new Frog(width/2-grid/2, height-grid, grid); | ||
frog.attach(null); | ||
} | ||
|
||
void setup() { | ||
size(500, 500); | ||
//frog = new Frog(width/2-grid/2, height-grid, grid); | ||
resetGame(); | ||
|
||
int index = 0; | ||
cars = new Car[8]; | ||
|
||
// ROW 1 | ||
for (int i = 0; i < 2; i++) { | ||
float x = i * 300; | ||
cars[index] = new Car(x, height-grid*2, grid*2, grid, 2); | ||
index++; | ||
} | ||
|
||
// ROW 2 | ||
for (int i = 0; i < 2; i++) { | ||
float x = i * 200 + 150; | ||
cars[index] = new Car(x, height-grid*3, grid, grid, -3.5); | ||
index++; | ||
} | ||
|
||
// ROW 3 | ||
for (int i = 0; i < 4; i++) { | ||
float x = i * 150 + 25; | ||
cars[index] = new Car(x, height-grid*4, grid, grid, 1.2); | ||
index++; | ||
} | ||
|
||
logs = new Log[7]; | ||
|
||
// ROW 5 | ||
index = 0; | ||
for (int i = 0; i < 2; i++) { | ||
float x = i * 250 + 100; | ||
logs[index] = new Log(x, height-grid*6, grid*3, grid, 2.3); | ||
index++; | ||
} | ||
// ROW 6 | ||
for (int i = 0; i < 3; i++) { | ||
float x = i * 200 + 30; | ||
logs[index] = new Log(x, height-grid*7, grid*2, grid, -1.3); | ||
index++; | ||
} | ||
// ROW 7 | ||
for (int i = 0; i < 2; i++) { | ||
float x = i * 400 + 10; | ||
logs[index] = new Log(x, height-grid*8, grid*4, grid, 0.5); | ||
index++; | ||
} | ||
} | ||
|
||
void draw() { | ||
background(0); | ||
fill(255, 100); | ||
rect(0, 0, width, grid*2); | ||
rect(0, height-grid, width, grid); | ||
rect(0, height-grid*5, width, grid); | ||
for (Car car : cars) { | ||
car.show(); | ||
car.update(); | ||
if (frog.intersects(car)) { | ||
resetGame(); | ||
//println("GAME OVER"); | ||
} | ||
} | ||
|
||
for (Log log : logs) { | ||
log.show(); | ||
log.update(); | ||
} | ||
|
||
if (frog.y < height-grid*5 && frog.y > grid*2) { | ||
boolean ok = false; | ||
for (Log log : logs) { | ||
if (frog.intersects(log)) { | ||
ok = true; | ||
frog.attach(log); | ||
} | ||
} | ||
if (!ok) { | ||
resetGame(); | ||
} | ||
} else { | ||
frog.attach(null); | ||
} | ||
|
||
frog.update(); | ||
frog.show(); | ||
} | ||
|
||
void keyPressed() { | ||
|
||
if (keyCode == UP) { | ||
frog.move(0, -1); | ||
} else if (keyCode == DOWN) { | ||
frog.move(0, 1); | ||
} else if (keyCode == RIGHT) { | ||
frog.move(1, 0); | ||
} else if (keyCode == LEFT) { | ||
frog.move(-1, 0); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
CodingChallenges/CC_72_Frogger/CC_72_Frogger_original/Car.pde
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,32 @@ | ||
// Daniel Shiffman | ||
// http://codingtra.in | ||
// http://patreon.com/codingtrain | ||
|
||
// Frogger | ||
// Part 1: https://youtu.be/giXV6xErw0Y | ||
// Part 2: https://youtu.be/06-ZvYmSeus | ||
// Part 3: https://youtu.be/hk326ZHlENQ | ||
// Part 4: https://youtu.be/c6WdJltqEtM | ||
|
||
class Car extends Rectangle { | ||
float speed; | ||
|
||
Car(float x, float y, float w, float h, float s) { | ||
super(x, y, w, h); | ||
speed = s; | ||
} | ||
|
||
void update() { | ||
x = x + speed; | ||
if (speed > 0 && x > width+grid) { | ||
x = -w-grid; | ||
} else if (speed < 0 && x < -w-grid) { | ||
x = width+grid; | ||
} | ||
} | ||
|
||
void show() { | ||
fill(200); | ||
rect(x, y, w, h); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
CodingChallenges/CC_72_Frogger/CC_72_Frogger_original/Frog.pde
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,40 @@ | ||
// Daniel Shiffman | ||
// http://codingtra.in | ||
// http://patreon.com/codingtrain | ||
|
||
// Frogger | ||
// Part 1: https://youtu.be/giXV6xErw0Y | ||
// Part 2: https://youtu.be/06-ZvYmSeus | ||
// Part 3: https://youtu.be/hk326ZHlENQ | ||
// Part 4: https://youtu.be/c6WdJltqEtM | ||
|
||
class Frog extends Rectangle { | ||
|
||
Log attached = null; | ||
|
||
Frog(float x, float y, float w) { | ||
super(x, y, w, w); | ||
} | ||
|
||
void attach(Log log) { | ||
attached = log; | ||
} | ||
|
||
void update() { | ||
if (attached != null) { | ||
x += attached.speed; | ||
} | ||
|
||
x = constrain(x, 0, width-w); | ||
} | ||
|
||
void show() { | ||
fill(0, 255, 0, 200); | ||
rect(x, y, w, w); | ||
} | ||
|
||
void move(float xdir, float ydir) { | ||
x += xdir * grid; | ||
y += ydir * grid; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
CodingChallenges/CC_72_Frogger/CC_72_Frogger_original/Log.pde
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,16 @@ | ||
// Daniel Shiffman | ||
// http://codingtra.in | ||
// http://patreon.com/codingtrain | ||
|
||
// Frogger | ||
// Part 1: https://youtu.be/giXV6xErw0Y | ||
// Part 2: https://youtu.be/06-ZvYmSeus | ||
// Part 3: https://youtu.be/hk326ZHlENQ | ||
// Part 4: https://youtu.be/c6WdJltqEtM | ||
|
||
class Log extends Car { | ||
|
||
Log(float x, float y, float w, float h, float s) { | ||
super(x, y, w, h, s); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
CodingChallenges/CC_72_Frogger/CC_72_Frogger_original/Rectangle.pde
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,40 @@ | ||
// Daniel Shiffman | ||
// http://codingtra.in | ||
// http://patreon.com/codingtrain | ||
|
||
// Frogger | ||
// Part 1: https://youtu.be/giXV6xErw0Y | ||
// Part 2: https://youtu.be/06-ZvYmSeus | ||
// Part 3: https://youtu.be/hk326ZHlENQ | ||
// Part 4: https://youtu.be/c6WdJltqEtM | ||
|
||
class Rectangle { | ||
float x; | ||
float y; | ||
float w; | ||
float h; | ||
|
||
Rectangle(float x, float y, float w, float h) { | ||
this.x = x; | ||
this.w = w; | ||
this.y = y; | ||
this.h = h; | ||
} | ||
|
||
boolean intersects(Rectangle other) { | ||
float left = x; | ||
float right = x + w; | ||
float top = y; | ||
float bottom = y + h; | ||
|
||
float oleft = other.x; | ||
float oright = other.x + other.w; | ||
float otop = other.y; | ||
float obottom = other.y + other.h; | ||
|
||
return !(left >= oright || | ||
right <= oleft || | ||
top >= obottom || | ||
bottom <= otop); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
CodingChallenges/CC_72_Frogger/CC_72_Frogger_refactored/CC_72_Frogger_refactored.pde
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,66 @@ | ||
// Daniel Shiffman | ||
// http://codingtra.in | ||
// http://patreon.com/codingtrain | ||
|
||
// Frogger | ||
// Part 1: https://youtu.be/giXV6xErw0Y | ||
// Part 2: https://youtu.be/06-ZvYmSeus | ||
// Part 3: https://youtu.be/hk326ZHlENQ | ||
// Part 4: https://youtu.be/c6WdJltqEtM | ||
|
||
Frog frog; | ||
Lane[] lanes; | ||
|
||
int SAFETY = 0; | ||
int CAR = 1; | ||
int LOG = 2; | ||
|
||
float grid = 50; | ||
|
||
void resetGame() { | ||
frog = new Frog(width/2-grid/2, height-grid, grid); | ||
frog.attach(null); | ||
} | ||
|
||
void setup() { | ||
size(500, 550); | ||
//frog = new Frog(width/2-grid/2, height-grid, grid); | ||
resetGame(); | ||
int totalLanes = int(height / grid); | ||
lanes = new Lane[totalLanes]; | ||
lanes[0] = new Lane(0, color(100)); | ||
lanes[1] = new Lane(1, LOG, 3, 1, 150, 3); | ||
lanes[2] = new Lane(2, LOG, 2, 3, 350, -2.5); | ||
lanes[3] = new Lane(3, LOG, 4, 1, 200, 1); | ||
lanes[4] = new Lane(4, LOG, 3, 2, 250, -1.7); | ||
lanes[5] = new Lane(5, color(100)); | ||
lanes[6] = new Lane(6, CAR, 3, 1, 150, 2.4); | ||
lanes[7] = new Lane(7, CAR, 2, 2, 150, -3.6); | ||
lanes[8] = new Lane(8, CAR, 1, 3, 150, 2.3); | ||
lanes[9] = new Lane(9, CAR, 4, 1, 150, -1); | ||
lanes[10] = new Lane(10, color(100)); | ||
} | ||
|
||
void draw() { | ||
background(0); | ||
for (Lane lane : lanes) { | ||
lane.run(); | ||
} | ||
int laneIndex = int(frog.y / grid); | ||
lanes[laneIndex].check(frog); | ||
frog.update(); | ||
frog.show(); | ||
} | ||
|
||
void keyPressed() { | ||
|
||
if (keyCode == UP) { | ||
frog.move(0, -1); | ||
} else if (keyCode == DOWN) { | ||
frog.move(0, 1); | ||
} else if (keyCode == RIGHT) { | ||
frog.move(1, 0); | ||
} else if (keyCode == LEFT) { | ||
frog.move(-1, 0); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
CodingChallenges/CC_72_Frogger/CC_72_Frogger_refactored/Frog.pde
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,41 @@ | ||
// Daniel Shiffman | ||
// http://codingtra.in | ||
// http://patreon.com/codingtrain | ||
|
||
// Frogger | ||
// Part 1: https://youtu.be/giXV6xErw0Y | ||
// Part 2: https://youtu.be/06-ZvYmSeus | ||
// Part 3: https://youtu.be/hk326ZHlENQ | ||
// Part 4: https://youtu.be/c6WdJltqEtM | ||
|
||
class Frog extends Rectangle { | ||
|
||
Obstacle attached = null; | ||
|
||
Frog(float x, float y, float w) { | ||
super(x, y, w, w); | ||
} | ||
|
||
void attach(Obstacle log) { | ||
attached = log; | ||
} | ||
|
||
void update() { | ||
if (attached != null) { | ||
x += attached.speed; | ||
} | ||
|
||
x = constrain(x, 0, width-w); | ||
} | ||
|
||
void show() { | ||
fill(0, 255, 0, 200); | ||
rect(x, y, w, w); | ||
} | ||
|
||
void move(float xdir, float ydir) { | ||
x += xdir * grid; | ||
y += ydir * grid; | ||
attach(null); | ||
} | ||
} |
Oops, something went wrong.