Skip to content

Commit ed2472d

Browse files
author
Mike Barkmin
committed
Add color hit game
1 parent 01f4815 commit ed2472d

File tree

14 files changed

+366
-0
lines changed

14 files changed

+366
-0
lines changed

examples/ColorHit/ColorHit.pde

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import eu.barkmin.processing.scratch.*;
2+
3+
ScratchStage stage;
4+
Wheel wheel;
5+
Dart dart;
6+
ArrayList<Dart> dartLifes;
7+
8+
int[] colors = {50, 180};
9+
int level = 1;
10+
int score = 0;
11+
int lifes = 3;
12+
int speed = 1;
13+
14+
enum States {
15+
THROW, HIT, INIT, LOST
16+
}
17+
States state = States.INIT;
18+
19+
void setup() {
20+
size(400, 600, P2D);
21+
ScratchStage.init(this);
22+
stage = ScratchStage.getInstance();
23+
stage.addBackdrop("bg", "sprites/bg.png");
24+
stage.setTint(120);
25+
wheel = new Wheel(8);
26+
dart = new Dart();
27+
dart.setPosition(width / 2, height - 100);
28+
dart.newColor();
29+
updateLifes();
30+
}
31+
32+
void updateLifes() {
33+
dartLifes = new ArrayList();
34+
for(int i = 0; i < lifes; i++) {
35+
Dart life = new Dart();
36+
life.setSize(20);
37+
life.setPosition(20 + i * 20, height - 40);
38+
dartLifes.add(life);
39+
}
40+
}
41+
42+
void checkDart() {
43+
if (dart.currentColorIndex == wheel.currentColorIndex) {
44+
score++;
45+
} else {
46+
lifes--;
47+
updateLifes();
48+
}
49+
50+
if (lifes < 0) {
51+
state = States.LOST;
52+
return;
53+
}
54+
55+
if(score > level * 10) {
56+
speed++;
57+
level++;
58+
lifes++;
59+
updateLifes();
60+
}
61+
62+
// next dart
63+
dart.newColor();
64+
state = States.INIT;
65+
dart.setPosition(width / 2, height - 100);
66+
}
67+
68+
void keyPressed() {
69+
if (keyCode == 32 && state == States.INIT) {
70+
state = States.THROW;
71+
}
72+
if (keyCode == ENTER && state == States.LOST) {
73+
state = States.INIT;
74+
score = 0;
75+
lifes = 3;
76+
speed = 1;
77+
dart.setPosition(width / 2, height - 100);
78+
}
79+
}
80+
81+
void draw() {
82+
// draw score
83+
fill(255);
84+
text(score, width - 40, height - 20);
85+
86+
// draw lifes
87+
for(Dart life : dartLifes) {
88+
life.draw();
89+
}
90+
91+
// draw wheel and dart depending on state
92+
switch (state) {
93+
case INIT:
94+
dart.draw();
95+
wheel.draw();
96+
wheel.rotate(speed);
97+
break;
98+
case THROW:
99+
dart.draw();
100+
wheel.draw();
101+
wheel.rotate(speed);
102+
dart.move(10);
103+
break;
104+
case HIT:
105+
checkDart();
106+
dart.draw();
107+
wheel.draw();
108+
delay(1000);
109+
break;
110+
case LOST:
111+
background(0);
112+
textAlign(CENTER);
113+
text("You scored " + score + " Points! Great try!\n Press Enter to play again.", width / 2, height / 2);
114+
break;
115+
}
116+
117+
// change state if dart has hit wheel
118+
if (dart.getY() <= 300 && state != States.LOST) {
119+
state = States.HIT;
120+
}
121+
}

examples/ColorHit/Dart.pde

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Dart extends ScratchSprite {
2+
3+
DartFlight flight;
4+
DartShaft shaft;
5+
6+
int flightColor = 255;
7+
8+
int currentColorIndex;
9+
10+
Dart() {
11+
flight = new DartFlight();
12+
shaft = new DartShaft();
13+
this.setSize(50);
14+
this.setRotation(-90);
15+
}
16+
17+
void newColor() {
18+
currentColorIndex = (int) random(0, colors.length);
19+
flightColor = colors[currentColorIndex];
20+
}
21+
22+
void draw() {
23+
this.getX();
24+
flight.setPosition(this.getX(), this.getY() + 60 * this.getSize() / 100.0);
25+
flight.setSize(this.getSize());
26+
flight.setTint(flightColor);
27+
flight.draw();
28+
shaft.setPosition(this.getX(), this.getY() - 10 * this.getSize() / 100.0);
29+
shaft.setSize(this.getSize());
30+
shaft.draw();
31+
}
32+
}

examples/ColorHit/DartFlight.pde

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class DartFlight extends ScratchSprite {
2+
DartFlight() {
3+
super("flight", "sprites/dart_flight.png");
4+
}
5+
}

examples/ColorHit/DartShaft.pde

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class DartShaft extends ScratchSprite {
2+
DartShaft() {
3+
super("shaft", "sprites/dart_shaft.png");
4+
}
5+
}

examples/ColorHit/Wheel.pde

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Wheel extends ScratchSprite {
2+
3+
WheelPart[] parts;
4+
5+
6+
int currentColorIndex;
7+
8+
Wheel(int parts) {
9+
this.parts = new WheelPart[parts];
10+
11+
currentColorIndex = 1;
12+
13+
for (int i = 0; i < this.parts.length; i++) {
14+
this.parts[i] = new WheelPart(parts);
15+
this.parts[i].setRotation(i * 360 / this.parts.length);
16+
this.parts[i].setTint(colors[i % colors.length]);
17+
}
18+
19+
this.setPosition(width / 2, 150);
20+
}
21+
22+
void rotate(int degrees) {
23+
this.turnLeft(degrees);
24+
for (WheelPart part : parts) {
25+
part.turnLeft(degrees);
26+
}
27+
}
28+
29+
void draw() {
30+
super.draw();
31+
32+
// check which color is currently at the bottom
33+
for(int i = 0; i < this.parts.length; i++) {
34+
float limit = 360 - i * 360.0 / this.parts.length;
35+
if (this.getRotation() < limit) {
36+
currentColorIndex = i % colors.length;
37+
}
38+
}
39+
40+
for (WheelPart part : parts) {
41+
part.setPosition(this.getX(), this.getY());
42+
part.draw();
43+
}
44+
}
45+
}

examples/ColorHit/WheelPart.pde

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class WheelPart extends ScratchSprite {
2+
WheelPart(int parts) {
3+
super("full", "sprites/full.png");
4+
this.addCostume("half", "sprites/half.png");
5+
this.addCostume("quarter", "sprites/quarter.png");
6+
this.addCostume("eighth", "sprites/eighth.png");
7+
this.setSize(50);
8+
9+
switch (parts) {
10+
case 1:
11+
this.switchCostume("full");
12+
break;
13+
case 2:
14+
this.switchCostume("half");
15+
break;
16+
case 4:
17+
this.switchCostume("quarter");
18+
break;
19+
case 8:
20+
this.switchCostume("eighth");
21+
break;
22+
}
23+
}
24+
}

examples/ColorHit/sprites/bg.png

4.39 KB
Loading
1.29 KB
Loading
1.25 KB
Loading

examples/ColorHit/sprites/eighth.png

3.01 KB
Loading

0 commit comments

Comments
 (0)