-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
106 lines (81 loc) · 1.73 KB
/
sketch.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
let player;
let score = 0;
let asteroids = []
const dist = 5;
const asteroidsize = 10;
let addAsteroids = false;
function setup() {
let cnv = createCanvas(600, 600);
cnv.parent('canvasContainer');
frameRate(60);
player = new Player(15);
restartGame();
}
function draw() {
background(0);
// Get Keyboad Input
if (keyIsPressed === true) {
switch(keyCode) {
case LEFT_ARROW:
case 97: // A key
player.move(dist * -1);
break;
case RIGHT_ARROW:
case 100: // D key
player.move(dist);
break;
}
}
if(touches.length > 0) {
console.log(touches)
if(touches[0].x > width/2) { //Touched on the Right side of the canvas
console.log('right')
player.move(dist);
}
else { // Touched on the left side
player.move(dist * -1);
console.log('left')
}
}
// Update Asteroids
asteroids.forEach( a => {
if(a.checkColl(player.pos, player.radius)) {
gameOver();
}
a.update();
a.draw();
if (a.outOfBounds()) {
a.setRandomPos();
score++;
updateScore();
}
})
if((score % 25 === 0) && score > 0) {
asteroids.push(new Asteroid( updateScore()));
}
player.draw();
}
function updateScore() {
document.getElementById('score').innerText = score;
document.getElementById('asteroidcnt').innerText = asteroids.length;
}
// Called when gameover
function gameOver() {
noLoop();
background(255,0,0,150);
}
function restartGame() {
player.reset();
score = 0;
asteroids = []
for (let i = 0; i < 100; i++) {
asteroids.push(new Asteroid(asteroidsize))
}
updateScore();
loop();
}
function keyPressed() {
if(keyCode === 13) { //EnterKey
restartGame();
}
}