-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrace.js
205 lines (191 loc) · 6.4 KB
/
race.js
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
const COLOR_AMBER = '#FFBF00'
const COLOR_GREEN = '#33ff33'
kontra.init();
var sprites = []
let tileSize = 48
const map = [
['x','x','x','x','x','x','x','x','x','x'],
['x',' ',' ',' ',' ',' ',' ',' ','x','x'],
['x',' ','x','x','x','x','x',' ',' ','x'],
['x',' ',' ',' ','x',' ','x',' ',' ','x'],
['x','x','x',' ','x',' ','x',' ',' ','x'],
['x',' ',' ',' ','x',' ','x','x',' ','x'],
['x',' ',' ','x','x',' ',' ','x',' ','x'],
['x',' ','x','x','x','x','x','x',' ','x'],
['x',' ',' ',' ',' ','s',' ',' ',' ','x'],
['x','x','x','x','x','x','x','x','x','x']
]
var degreesToRadians = function (deg) {
return deg * Math.PI / 180;
}
var score = kontra.sprite({
x:0,
y:480,
laps:0,
color: COLOR_GREEN,
render: function () {
// Make a progress bar
this.context.fillStyle = '#333';
this.context.fillRect(this.x, this.y, kontra.canvas.width, kontra.canvas.height - this.y)
this.context.fillStyle = '#666';
let percent = this.laps / 5.0;
let percentWidth = percent * kontra.canvas.width
this.context.fillRect(this.x, this.y, percentWidth, kontra.canvas.height - this.y)
// Write the laps
this.context.fillStyle = this.color;
this.context.font = "48px Courier New"
this.context.textBaseline = 'top'
this.context.fillText(this.laps + '/5', this.x, this.y)
}
})
sprites.unshift(score)
var car = kontra.sprite({
x: 100, // starting x,y position of the sprite
y: 80,
color: COLOR_GREEN, // fill color of the sprite rectangle
width: 20, // width and height of the sprite rectangle
height: 40,
speed: 1.5, // move the sprite 2px to the right every frame
rotation: 0,
crashFrames: 0,
update: function () {
this.color = (this.crashFrames > 0) ? COLOR_AMBER : COLOR_GREEN
if (this.crashFrames > 0) {
// this.rotation -= 6
this.crashFrames--;
} else {
const cos = Math.cos(degreesToRadians(this.rotation));
const sin = Math.sin(degreesToRadians(this.rotation));
this.x += cos*this.speed;
this.y -= sin* this.speed;
let tileX = Math.floor(this.x / tileSize)
let tileY = Math.floor(this.y / tileSize)
if (map[tileY][tileX] === 'x') {
// Crash!
this.speed = 1.5;
this.crashFrames = 90;
this.x = this.tileX * tileSize + tileSize/2;
this.y = this.tileY * tileSize + tileSize /2;
}
if (map[tileY][tileX] === 's') {
// Only allow 0 degrees rotation on s tile
this.rotation = 180
// The previous tile was before it, increment a lap
if (this.tileX > tileX) {
score.laps++;
}
}
this.tileX = tileX;
this.tileY = tileY;
this.speed += 1/60/30
}
},
render: function () {
kontra.context.save()
kontra.context.strokeStyle = this.color
kontra.context.fillStyle = 'black'
kontra.context.translate(this.x, this.y)
kontra.context.rotate(-1 * degreesToRadians(this.rotation))
kontra.context.fillRect(-20, -10, 40, 20)
kontra.context.strokeRect(-20, -10, 40, 20)
kontra.context.strokeRect(0, -8, 10, 16) // windshield
kontra.context.strokeRect(6, -14, 8, 4) // wheel
kontra.context.strokeRect(6, 10, 8, 4) // wheel
kontra.context.strokeRect(-16, -14, 8, 4) // wheel
kontra.context.strokeRect(-16, 10, 8, 4) // wheel
kontra.context.restore()
}
});
sprites.push(car)
var grass = {
width:48,
height:48,
color: COLOR_GREEN,
render: function () {
kontra.context.strokeStyle = this.color
kontra.context.strokeRect(this.x, this.y, tileSize, tileSize)
kontra.context.strokeRect(this.x+4, this.y+4, tileSize-8, tileSize-8)
}
}
var start = {
width:48,
height:48,
color: COLOR_AMBER,
render: function () {
kontra.context.strokeStyle = this.color
kontra.context.strokeRect(this.x, this.y, tileSize, tileSize)
kontra.context.strokeRect(this.x+4, this.y+4, tileSize-8, tileSize-8)
}
}
let reset = function() {
for (let y = 0; y < map.length; y++) {
for (let x = 0; x < map[y].length; x++) {
if (map[y][x] === 'x') {
let s = kontra.sprite(grass)
s.x = x*tileSize;
s.y = y*tileSize;
sprites.unshift(s)
} if (map[y][x] === 's') {
// Start the player there
car.x = x*tileSize + car.width/2
car.y = y*tileSize + car.height/2
car.rotation = 180;
let s = kontra.sprite(start )
s.x = x*tileSize;
s.y = y*tileSize;
sprites.unshift(s)
}
}
}
}
kontra.keys.bind(['space','right'], function() {
car.rotation = (car.rotation-90+360)%360
})
let rightButton = kontra.sprite({
x: 240,
y: 500,
width:80,
height: 80,
color: COLOR_GREEN,
onDown: function () {
car.rotation = (car.rotation-90+360)%360
},
render: function () {
this.context.save();
this.context.strokeStyle = this.color;
this.context.translate(this.x, this.y)
this.context.beginPath()
this.context.moveTo(0,20)
this.context.lineTo(40, 20)
this.context.lineTo(40, 0)
this.context.lineTo(80, 40)
this.context.lineTo(40, 80)
this.context.lineTo(40, 60)
this.context.lineTo(0, 60)
this.context.closePath()
this.context.stroke()
this.context.restore();
}
})
kontra.pointer.track(rightButton)
sprites.push(rightButton)
var loop = kontra.gameLoop({ // create the main game loop
update() { // update the game state
sprites.forEach(sprite => {
sprite.update()
if (sprite.x > kontra.canvas.width) {
sprite.x = -sprite.width;
}
})
},
render() { // render the game state
kontra.context.fillStyle = "#000000";
kontra.context.fillRect(0,0,kontra.canvas.width,kontra.canvas.height);
sprites.forEach(sprite => {
sprite.render();
})
}
});
reset()
loop.start();
this.loop = loop