-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathblob.html
184 lines (163 loc) · 5.32 KB
/
blob.html
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
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: Plum;}
canvas{ border: 2px solid black;}
</style>
</head>
<body>
<img id="link1" src="link1 (2).png" alt="Link1" style="display:none">
<img id="link2" src="link2 (2).png" alt="Link2" style="display:none">
<img id="grass1" src="grass1.png" alt="Link2" style="display:none">
<img id="grass2" src="grass2.png" alt="Link2" style="display:none">
<img id="grass3" src="grass3.png" alt="Link2" style="display:none">
<img id="grass4" src="grass4.png" alt="Link2" style="display:none">
<img id="rock1" src="rock1.png" alt="Link2" style="display:none">
<canvas id = "game"></canvas>
<script>
var Key = {
_pressed: {},
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
isDown: function(keyCode){
return this._pressed[keyCode];
},
onKeydown: function(event) {
this._pressed[event.keyCode] = true;
},
onKeyup: function(event) {
delete this._pressed[event.keyCode];
}
};
window.addEventListener('keyup', function(event) {Key.onKeyup(event); }, false);
window.addEventListener('keydown', function(event) {Key.onKeydown(event); }, false);
var OBSTACLES = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,1,1,0,0,0,0,0,0,0,0,0,0],
[0,1,0,1,0,0,1,0,0,0,0,0,0,0],
[0,1,1,1,0,1,0,1,0,0,0,0,0,0],
[0,1,0,1,0,1,1,1,0,0,0,0,0,0],
[0,1,1,1,0,1,0,1,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1]
];
//REALLY IMPORTANT CHANGE
var canvas = document.getElementById('game');
canvas.width = 1400; //window.innerWidth;
canvas.height =900; //window.innerHeight;
var grid = 100;
var player_width = document.getElementById("link1").width;
var player_height = document.getElementById("link1").height;
//var player_width = document.getElementById("rock1").width;
//var player_height = document.getElementById("rock1").height;
console.log(player_width, player_height);
function isCollidedOb(x,y,width,height,obarray){
upperleft = [[Math.floor(y/grid)],[Math.floor(x/grid)]];
lowerleft = [[Math.floor((y+height)/grid)],[Math.floor((x)/grid)]];
upperright = [[Math.floor((y)/grid)],[Math.floor((x+width)/grid)]];
lowerright = [[Math.floor((y+height)/grid)],[Math.floor((x+width)/grid)]];
console.log(upperleft[0],lowerleft,upperright[0],lowerright);
if(obarray[upperleft[0]][upperleft[1]] || obarray[lowerleft[0]][lowerleft[1]] || obarray[upperright[0]][upperright[1]] || obarray[lowerright[0]][lowerright[1]]){
return true;
}
return false;
}
function drawObs(obarray,grid){
for (let i = 0; i < obarray.length; i++) {
for (let j = 0; j < obarray[i].length; j++) {
if (obarray[i][j]){
ctx.drawImage(rock1,j*grid,i*grid);
}
}
}
}
var x = 50;
var y = 50;
var toggle = 1;
var grass1 = document.getElementById("grass1");
var rock1 = document.getElementById("rock1");
var ctx = canvas.getContext('2d');
var goalX = Math.random() * window.innerWidth;
var goalY = Math.random() * window.innerHeight;
var playerSize = 50;
var goalSize = 15;
var speed = 3;
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "rgb(24, 255, 0)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
drawObs(OBSTACLES,grid);
ctx.drawImage(grass1, 200, 200);
//MOVE UP
if(Key.isDown(Key.UP)){
toggle += 1;
if(y < 0){
y= canvas.height-player_height;
}
if(!isCollidedOb(x,Math.abs(y-speed),player_width,player_height,OBSTACLES)){
y-=speed;
}
}
//MOVE DOWN
if(Key.isDown(Key.DOWN)){
toggle += 1;
if(y > canvas.height){
y= y%canvas.height;
}
if(!isCollidedOb(x,y+speed,player_width,player_height,OBSTACLES)){
y+=speed;
}
}
//MOVE LEFT
if(Key.isDown(Key.LEFT)){
toggle += 1;
if(x < 0){
x = canvas.width-player_width;
}
if(!isCollidedOb(x-speed,y,player_width,player_height,OBSTACLES)){
x-=speed;
}
}
//MOVE RIGHT
if(Key.isDown(Key.RIGHT)){
toggle += 1;
if(x > canvas.width){
x= x%canvas.width;
}
if(!isCollidedOb(x+speed,y,player_width,player_height,OBSTACLES)){
x+=speed;
}
}
if ((Math.abs(x-goalX))**2 + (Math.abs(y-goalY))**2 < (playerSize+goalSize)**2){
//playerSize += 5;
goalX = Math.random() * canvas.width;
goalY = Math.random() * canvas.height;
}
//var character = new Path2D();
////////////////////////////////////////////////////////////////////
//var character = new Image();
//toggle += 1;
toggle %= 100;
if(toggle > 50){
var character = document.getElementById("link1");
//character.src = document.getElementById("link1");
} else {
//character.src = document.getElementById("link2");}
var character = document.getElementById("link2");}
//character.addEventListener('load', function() {
// execute drawImage statements here
ctx.drawImage(character, x, y);
var goal = new Path2D();
goal.arc(goalX, goalY, goalSize, 0, 2 * Math.PI);
ctx.fillStyle = "#FF0000"
//ctx.fill(character);
ctx.fill(goal)
}
setInterval(draw, 10);
</script>
</body>
</html>