-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbananawars.js
175 lines (130 loc) · 3.68 KB
/
bananawars.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
var keys = {};
var keyText;
window.onkeyup = function(e) {
keys[e.keyCode] = false;
}
window.onkeydown = function(e) {
keys[e.keyCode] = true;
keyText.innerHTML = e.keyCode;
}
window.onload = function(){
keyText = document.getElementById("key");
var canvas = document.getElementById("bananascene");
var heightText = document.getElementById("height");
var ctx = canvas.getContext("2d");
var buildingHeight = 200;
var buildingWidth = 100;
var canvasHeight = 600;
var canvasWidth = 1024;
var buildingSpace = 100;
var left_y = 400;
var right_y = 400;
var left_x = buildingSpace - 23;
var right_x = canvasWidth - buildingSpace - 8;
var left_bx = buildingSpace;
var left_by = 400;
var right_bx = canvasWidth - buildingSpace - 60;
var right_by = 400;
var left_bananaThrown = false;
var right_bananaThrown = false;
var left_death = false;
var right_death = false;
var left_gorilla = new Image();
var right_gorilla = new Image();
var left_banana = new Image();
var right_banana = new Image();
function draw() {
ctx.beginPath();
ctx.rect(0, 0, canvasWidth, canvasHeight);
ctx.fillStyle = "blue";
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.rect(buildingSpace, buildingHeight, buildingWidth, canvasHeight-buildingHeight);
ctx.fillStyle = "grey";
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.rect(canvasWidth-buildingWidth-buildingSpace, buildingHeight, buildingWidth, canvasHeight-buildingHeight);
ctx.fillStyle = "grey";
ctx.fill();
ctx.closePath();
left_gorilla.onload = function () {
ctx.drawImage(left_gorilla, left_x, left_y);
};
right_gorilla.onload = function () {
ctx.drawImage(right_gorilla, right_x, right_y);
};
left_banana.onload = function () {
ctx.drawImage(left_banana, left_bx, left_by);
};
right_banana.onload = function () {
ctx.drawImage(right_banana, right_bx, right_by);
};
left_gorilla.src = 'gorilla_left.png';
right_gorilla.src = 'gorilla_right.png';
left_banana.src = 'banana_left.png';
right_banana.src = 'banana_right.png';
// Banana follows monkey
if(left_bananaThrown == false) {
left_by = left_y;
}
// Up
if(keys[87] == true) {
if (left_y > 174) { left_y = left_y - 1; }
heightText.innerHTML = left_y;
}
// Down
if(keys[83] == true) {
if (left_y < 550) { left_y = left_y + 1; }
heightText.innerHTML = left_y;
}
// throw banana!!!
if(keys[68] == true) {
left_bananaThrown = true;
}
if(left_bananaThrown == true) {
left_bx = left_bx + 10;
if(left_bx > canvasWidth) {
left_bx = buildingSpace;
left_bananaThrown = false;
}
}
// if left gorilla is hit by right banana, left death!!!
if((right_bx == left_x) && (right_by == right_y))
{
left_death = true;
}
if(left_death) {
left_y = left_y + 5;
left_x = left_x - 1.4;
}
// right gorilla
// Banana follows monkey
if(right_bananaThrown == false) {
right_by = right_y;
}
// Up
if(keys[38] == true) {
if (right_y > 174) { right_y = right_y - 1; }
heightText.innerHTML = right_y;
}
// Down
if(keys[40] == true) {
if (right_y < 550) { right_y = right_y + 1; }
heightText.innerHTML = right_y;
}
// throw banana!!!
if(keys[37] == true) {
right_bananaThrown = true;
}
if(right_bananaThrown == true) {
right_bx = right_bx - 10;
if(right_bx < 0) {
right_bx = canvasWidth - buildingSpace - 60;
right_bananaThrown = false;
}
}
};
setInterval(draw, 10);
}