This repository has been archived by the owner on Aug 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processing.pde
231 lines (207 loc) · 7.53 KB
/
processing.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
Ground ground;
Dino dino;
Obstacle obstacle;
int groundY;
int score;
int speed;
boolean isJumping;
boolean isMoonMode;
PFont font;
void settings() {
size(1200, 750);
}
void setup() {
groundY = height / 2;
score = 0;
speed = 10;
isJumping = false;
isMoonMode = false;
ground = new Ground(10);
dino = new Dino(50);
obstacle = new Obstacle(width);
font = loadFont("HanziPenSC-W5-48.vlw");
textFont(font);
}
void draw() {
background(255);
ground.show();
dino.show();
dino.jump(300, 50);
obstacle.show();
obstacle.move();
collision();
fill(95, 99, 104);
textSize(32);
textAlign(RIGHT, TOP);
text("Score: " + score + " Speed: " + speed, width - 20, groundY - 300);
}
class Ground {
int offsetY;
Ground(int offsetY) {
this.offsetY = offsetY;
}
void show() {
stroke(95, 99, 104);
strokeWeight(3);
line(0, groundY - this.offsetY, width, groundY - this.offsetY);
}
}
class Dino extends Entity {
float velocityY;
float gravity = 0.98;
int jumpPower = 25;
Dino(int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, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0} ,
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0} ,
{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
} , originX);
}
void jump(int maxHeight, int hangTime) {
if (isMoonMode) {
gravity = 0.162;
jumpPower = 10;
}
if (isJumping) {
if (originY > initOriginY()) {
velocityY = 0;
originY = initOriginY();
isJumping = false;
} else {
velocityY += gravity;
originY += velocityY;
}
}
}
}
class Obstacle extends Entity {
Obstacle(int originX) {
super(new int[][]{
{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0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 0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
} , originX);
}
void move() {
this.originX -= speed;
if (this.originX + pixels[0].length * pixelSize < 0) {
this.originX = width;
score++;
if (score % 5 == 0) {
speed++;
}
}
}
}
abstract class Entity {
int[][] pixels;
int pixelSize;
int originX;
int originY;
Entity(int[][] pixels, int originX) {
this.pixels = pixels;
this.pixelSize = 4;
this.originX = originX;
this.originY = initOriginY();
}
void show() {
noStroke();
for (int i = 0; i < pixels.length; i++) {
for (int j = 0; j < pixels[0].length; j++) {
if (pixels[i][j] == 1) {
fill(95, 99, 104);
rect(this.originX + j * pixelSize, originY + i * pixelSize, pixelSize, pixelSize);
}
}
}
}
int initOriginY() {
return groundY - pixels.length * pixelSize;
}
}
void collision() {
int dinoWidth = dino.pixels[0].length * dino.pixelSize;
int dinoHeight = dino.pixels.length * dino.pixelSize;
int obstacleWidth = obstacle.pixels[0].length * obstacle.pixelSize;
int obstacleHeight = obstacle.pixels.length * obstacle.pixelSize;
if (dino.originX < obstacle.originX + obstacleWidth && dino.originX + dinoWidth > obstacle.originX &&
dino.originY < obstacle.originY + obstacleHeight && dino.originY + dinoHeight > obstacle.originY) {
for (int i = 0; i < dino.pixels.length; i++) {
for (int j = 0; j < dino.pixels[i].length; j++) {
if (dino.pixels[i][j] == 1) {
int dinoPixelX = dino.originX + j * dino.pixelSize;
int dinoPixelY = dino.originY + i * dino.pixelSize;
for (int k = 0; k < obstacle.pixels.length; k++) {
for (int l = 0; l < obstacle.pixels[k].length; l++) {
if (obstacle.pixels[k][l] == 1) {
int obstaclePixelX = obstacle.originX + l * obstacle.pixelSize;
int obstaclePixelY = obstacle.originY + k * obstacle.pixelSize;
if (dinoPixelX == obstaclePixelX && dinoPixelY == obstaclePixelY) {
noLoop();
fill(95, 99, 104);
textSize(64);
textAlign(CENTER, CENTER);
text("GAME OVER!!", width / 2, groundY - 100);
return;
}
}
}
}
}
}
}
}
}
void keyPressed() {
if (key == ' ' && !isJumping) {
isJumping = true;
dino.velocityY = -dino.jumpPower;
}
if (key == 'm' && !isJumping) {
isMoonMode = true;
}
}