Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
127 lines (103 loc) · 3.43 KB
/
Copy pathmain.js
File metadata and controls
127 lines (103 loc) · 3.43 KB
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
const canvas = document.getElementById("game_screen");
const ctx = canvas.getContext("2d");
import { Player } from "./player.js"
import { draw as Draw } from "./draw.js"
import { Block } from "./block.js"
export class main {
player = new Player()
draw = new Draw()
block = new Array()
menu = true
Score = 0
highScore = 0
async score(){
while(true){
await this.sleep(1000)
if (!this.menu){
this.Score++
}
}
}
async start(){
this.player.Input()
this.Input()
this.blockInit()
this.score()
while(true){
this.draw.Game(this.player, this.block, this.menu, this.Score, this.highScore)
if (!this.menu){
this.player.update()
this.BlockUpdate()
this.MiscChecks()
}
await this.sleep(1000/60)
}
}
async sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
blockInit() {
this.block[0] = new Block()
this.block[1] = new Block()
this.block[2] = new Block()
this.block[3] = new Block()
this.block[4] = new Block()
}
BlockUpdate() {
this.block[0].update()
this.block[1].update()
this.block[2].update()
this.block[3].update()
this.block[4].update()
}
MiscChecks(){
if (this.player.y > 1000) {this.Die()}
for(let i = 0; i <= 4; i++) {
console.log(this.player.x, ">=", this.block[i].x - this.block[i].width/2)
console.log(this.player.x, "<=", this.block[i].x + this.block[i].width/2 )
console.log(this.player.y, ">=", this.block[i].y - this.block[i].height/2 )
console.log(this.player.y, "<=", this.block[i].y + this.block[i].height/2 )
if(
( this.player.x + this.player.r >= this.block[i].x - this.block[i].width/2&&
this.player.x + this.player.r <= this.block[i].x + this.block[i].width/2 &&
this.player.y >= this.block[i].y - this.block[i].height/2&&
this.player.y <= this.block[i].y + this.block[i].height/2)||
( this.player.x - this.player.r >= this.block[i].x - this.block[i].width/2&&
this.player.x - this.player.r <= this.block[i].x + this.block[i].width/2 &&
this.player.y >= this.block[i].y - this.block[i].height/2&&
this.player.y <= this.block[i].y + this.block[i].height/2) ||
( this.player.x >= this.block[i].x - this.block[i].width/2&&
this.player.x <= this.block[i].x + this.block[i].width/2 &&
this.player.y + this.player.r >= this.block[i].y - this.block[i].height/2&&
this.player.y + this.player.r <= this.block[i].y + this.block[i].height/2) ||
( this.player.x >= this.block[i].x - this.block[i].width/2&&
this.player.x <= this.block[i].x + this.block[i].width/2 &&
this.player.y - this.player.r >= this.block[i].y - this.block[i].height/2&&
this.player.y - this.player.r <= this.block[i].y + this.block[i].height/2)
){this.Die()}
}
}
Input() {
document.addEventListener('keydown', (event) => {
var code = event.code;
if(this.menu == true && code == "KeyW") {
this.menu = false
this.Score = 0
}
}, false)
}
Die(){
this.menu = true
this.player.velX = 0
this.player.velY = 0
this.player.row = 3
this.player.x = 838
this.player.y = 700
if (this.Score > this.highScore){
this.highScore = this.Score
}
this.blockInit()
}
}
var game = new main()
game.start()