-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
117 lines (97 loc) · 2.42 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
107
108
109
110
111
112
113
114
115
116
117
let frog;
let cars = [];
let logs = [];
let grid = 50;
function resetGame(){
frog = new Frog(width / 2 - grid / 2, height - grid, grid);
frog.attach(null);
}
function setup(){
// (500, 500);
createCanvas(500, 500);
resetGame();
let index = 0;
// ROW 1
for (let i = 0; i < 2; i++) {
let x = i * 300;
cars[index] = new Car(x, height - grid * 2, grid * 2, grid, 2);
index ++;
}
// ROW 2
for (i = 0; i < 2; i++) {
let x = i * 200 + 150;
cars[index] = new Car(x, height - grid * 3, grid, grid, -3.5);
index ++;
}
// ROW 3
for (i = 0; i < 4; i++) {
let x = i * 150 + 25;
cars[index] = new Car(x, height - grid * 4, grid, grid, 1.2);
index ++;
}
// ROW 5
index = 0;
for (i = 0; i < 2; i++) {
let x = i * 250 + 100;
logs[index] = new Log(x, height - grid * 6, grid * 3, grid, 2.3);
index ++;
}
// ROW 6
for (i = 0; i < 3; i++) {
let x = i * 200 + 30;
logs[index] = new Log(x, height - grid * 7, grid * 2, grid, -1.3);
index ++;
}
// ROW 7
for (i = 0; i < 2; i++) {
let x = i * 400 + 10;
logs[index] = new Log(x, height - grid * 8, grid * 4, grid, 0.5);
index ++;
}
}
function draw(){
background(0);
// Safety lines
fill(255, 100);
rect(0, 0, width,grid*2);
rect(0, height-grid,width,grid);
rect(0, height-grid*5,width,grid);
for(let i = 0; i < cars.length; i++){
cars[i].update();
cars[i].show();
if(frog.intersects(cars[i])){
resetGame();
}
}
for(i = 0; i < logs.length; i++){
logs[i].update();
logs[i].show();
}
if (frog.y < height - grid * 5 && frog.y > grid*2) {
let ok = false;
for(i = 0; i<logs.length; i++){
if (frog.intersects(logs[i])) {
ok = true;
frog.attach(logs[i]);
}
}
if(!ok){
resetGame();
}
} else {
frog.attach(null);
}
frog.update();
frog.show();
}
function keyPressed() {
if(keyCode === UP_ARROW){
frog.move(0, -1);
}else if(keyCode === DOWN_ARROW){
frog.move(0, 1);
}else if(keyCode === RIGHT_ARROW){
frog.move(1, 0);
}else if(keyCode === LEFT_ARROW){
frog.move(-1, 0);
}
}