-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspriteTest.js
83 lines (71 loc) · 2.1 KB
/
spriteTest.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
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
canvas.width = '1000';
canvas.height = '500';
canvas.style.border ='1px solid black';
canvas.style.position = 'absolute';
canvas.style.margin = 'auto';
canvas.style.left = '0';
canvas.style.right = '0';
class Game{
constructor(canvasWidth, canvasHeight){
this.canvas = getElementById('canvas');
this.ctx = this.canvas.getContext('2d');
this.canvas.width = canvasWidth;
this.canvas.height = canvasHeight;
//styling and positioning canvas
this.canvas.style.position = 'absolute';
this.canvas.style.margin = 'auto';
this.canvas.style.left = '0';
this.canas.style.right = '0';
this.canvas.style.border = '1px solid black';
}
}
let sprite = new Image();
sprite.src = 'assets/images/red-brick.png';
let blank = new Image();
blank.src = 'assets/images/empty-space.png';
let spriteHeight = 64;
let spriteWidth = 72;
let imageCounter = 0;
let clipImageX = 0;
let clipImageY = 0;
let map = [
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,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,0,0,0,0,0,0,0,1,
1,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,1,1,1,1,0,0,0,1,1,1,1,1,1,1,0,1,
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
]
// document.addEventListener('keydown', event =>{
// if(event.code === 'ArrowRight'){
// console.log('here');
// ctx.drawImage(sprite,clipImageX ,clipImageY,72,64, 100 , 100, 72,64); //, 72, 64, 100, 100, 72 , 64);
// }
// clipImageX += 60;
// imageCounter++;
// });
function animate(){
drawMap();
window.requestAnimationFrame(animate);
}
let count = 0;
let startPosX = 0;
let startPosY = 0;
function drawMap(){
for(let i = 0 ; i < 10 ; i++){
for(let j = 0 ; j< 20 ; j++){
if(map[((i*20) + j)] === 1){
ctx.drawImage(sprite , j*50 , i*50 , 50,50 );
}else if(map[((i*20) + j)] === 0){
ctx.drawImage(blank , j*50 , i*50 , 50, 50);
}
}
}
}
animate();