-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
191 lines (173 loc) · 4.09 KB
/
main.ts
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
import { EventType, Rect, Surface, Texture, WindowBuilder } from "deno_sdl2";
import { Dino } from "./Dino.ts";
import { Cacti } from "./Cactus.ts";
import getImage from "./utils/sprites.ts";
import checkCollision from "./utils/checkCollision.ts";
import sleepSync from "./utils/sleepSync.ts";
const canvasWidth = 600;
const canvasHeight = 150;
/** Game window */
const window = new WindowBuilder("Dino Game", canvasWidth, canvasHeight)
.build();
export const canvas = window.canvas();
const dino = new Dino();
const cacti = new Cacti();
const sleepSyncValue = 10;
const gravity = 2;
cacti.generateCactus();
// const mainFont = canvas.loadFont("./fonts/mainfont.ttf", 128);
// Key press handlers
let isSpace = false;
// Other handlers
let gameOver = false;
let intro = true;
let trackX = 20;
let score = 0;
let trackSpeed = 4;
let playing = false;
const creator = canvas.textureCreator();
const trackImg = getImage("./sprites/track.png", creator);
function cactus(c: {
texture: Texture;
width: number;
height: number;
x: number;
y: number;
}) {
canvas.copy(
c.texture,
new Rect(0, 0, c.width, c.height),
new Rect(c.x, c.y, 34, 34),
);
}
function gameLoop() {
canvas.setDrawColor(255, 255, 255, 255);
canvas.present();
canvas.clear();
if (intro) {
dino.canvas.copy(
dino.playerImgJump,
new Rect(0, 0, dino.width, dino.height),
new Rect(dino.x, dino.y, 42, 42),
);
return;
}
canvas.clear();
dino.render();
for (let i = 0; i < cacti.cactusList.length; i++) {
cactus(cacti.cactusList[i]);
const { x, y, width, height } = cacti.cactusList[i];
if (x <= 0 - cacti.width) {
const loc = () => Math.round(Math.random()) * canvasWidth;
let gap = loc();
while (gap > canvasWidth) gap = loc();
cacti.cactusList[i].x = canvasWidth + gap;
cacti.cactusList[i].texture = cacti.cactusTextures[
Math.floor(Math.random() * (cacti.cactusTextures.length - 1))
];
}
cacti.cactusList[i].x -= isSpace ? trackSpeed + 10 : trackSpeed;
if (
checkCollision({
x1: dino.x,
y1: dino.y,
w1: 14,
h1: 14,
x2: x,
y2: y,
w2: width,
h2: height,
})
) {
// canvas.playMusic("./audio/game_over.wav");
intro = true;
trackSpeed = 0;
gameOver = true;
score = 0;
return;
}
}
// Check if space bar is pressed and player is on the ground.
if (isSpace && dino.y == 100 - 28) {
dino.y -= 70;
isSpace = false;
// canvas.playMusic("./audio/jump.wav");
} else {
// Give player downwards acceleration
dino.y += gravity;
}
// Reset space state
isSpace = false;
canvas.copy(
trackImg,
new Rect(0, 0, 600 * 2, 28),
new Rect(
trackX,
130 - 28,
600 * 2,
28,
),
);
trackX -= trackSpeed;
if (trackX <= -130) {
trackX = 0;
}
if (dino.y >= 100 - 28) {
dino.y = 100 - 28;
}
if (score >= 100) {
if (trackSpeed < 5) {
// canvas.playMusic("./audio/score.wav");
}
trackSpeed = 5;
}
score += 0.2;
// canvas.renderFont(
// mainFont,
// Math.round(score).toString(),
// {
// blended: { color: { r: 4, g: 0, b: 0, a: 255 } },
// },
// {
// x: 540,
// y: 0,
// width: 44,
// height: 40,
// },
// );
canvas.present();
sleepSync(sleepSyncValue);
}
canvas.present();
for await (const event of window.events()) {
switch (event.type) {
case EventType.Draw:
gameLoop();
break;
case EventType.Quit:
Deno.exit(0);
break;
case EventType.KeyDown:
// Space
console.log(event);
if (event.keysym.sym == 32 && !gameOver) {
console.log(event);
intro = false;
if (!playing) {
playing = true;
// canvas.playMusic("./audio/click.wav");
}
if (!isSpace) isSpace = true;
}
break;
case EventType.MouseButtonDown:
// Left click
if (event.button == 1 && !gameOver) {
intro = false;
if (!isSpace) isSpace = true;
}
break;
default:
break;
}
}