-
Notifications
You must be signed in to change notification settings - Fork 1
/
main01.js
152 lines (125 loc) · 3.36 KB
/
main01.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
const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
var screen = {
width: 30,
height: 30,
zoom: 24
};
canvas.width = screen.width;
canvas.height = screen.height;
canvas.style.width = (screen.width*screen.zoom) + 'px';
canvas.style.height = (screen.height*screen.zoom) + 'px';
canvas.style['image-rendering'] = 'pixelated';
const context = canvas.getContext('2d');
function clear() {
context.fillStyle='black';
context.fillRect(0, 0, screen.width, screen.height);
}
function pixel(x, y, color = 'red') {
context.fillStyle=color;
context.fillRect(x, y, 1, 1);
}
let mapNumbers = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
];
class Entity {
constructor() {
this.x = 0;
this.y = 0;
this.color = 'gray';
}
render() {
pixel(this.x, this.y, this.color);
}
update() {
}
}
class Pacman extends Entity {
constructor() {
super();
this.color = 'yellow';
}
update() {
if (this.y >= mapNumbers.length || mapNumbers[this.y][this.x+1] !== 1) {
this.x++;
}
}
}
class Ghost extends Entity {
constructor() {
super();
this.color = 'red';
}
update() {
if (this.y >= mapNumbers.length || mapNumbers[this.y][this.x+1] !== 1) {
this.x++;
}
}
}
class Map extends Entity {
render() {
const rows = mapNumbers.length;
const cols = mapNumbers[0].length;
for (let row = 0; row<rows; row++) {
for (let col = 0; col<cols; col++) {
if (mapNumbers[row][col] !== 0) {
pixel(this.x + col, this.y + row, this.color);
}
}
}
}
}
let entities = [];
function add(e) {
entities.push(e);
}
function remove(e) {
const ind = entities.indexOf(e);
if (ind>=0) {
entities.splice(ind, 1);
}
}
let map = new Map();
add(map);
let pacman = new Pacman();
add(pacman);
pacman.x =1;
pacman.y =2;
for (let i=0;i<20;i++){
let x = Math.floor(Math.random()*screen.width);
let y = Math.floor(Math.random()*screen.height);
if (y < mapNumbers.length && mapNumbers[y][x] === 1) {
continue;
}
let ghost = new Ghost();
ghost.x = x;
ghost.y = y;
add(ghost);
}
function frame() {
for (let i =0;i<entities.length;i++) {
entities[i].update();
}
clear();
for (let i =0;i<entities.length;i++) {
entities[i].render();
}
}
frame();
setInterval(frame, 1000);