-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainPage
123 lines (112 loc) · 3.93 KB
/
MainPage
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>About Me</title>
<style>
html {
overflow: hidden;
}
#gamec {
width: 100vw;
}
</style>
</head>
<body>
<h1>About Me</h1>
<p>Hello, my name is Mitelk. I am a student at Columbia Basin College.</p>
<p>I am currently working on my Software Development Bachelors Degree.</p>
<p>I am in my second year of my degree and currently doing an assignment for my network fundamentals class.<p>
<p>I am trying to make my own web page.<p>
<p>While I don’t know much about HTML at this current time this is the best I can do as far as making a web page.<p>
<h2>My Game</h2>
<canvas id="gamec" width="1920" height="1000"></canvas>
<script>
var gamec = document.getElementById("gamec");
var ctx = gamec.getContext("2d");
var player = {
x: 1920 / 2 - 10,
y: 1000 - 20,
w: 20,
h: 20,
dead: false
};
function Obstacle(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.vl = [Math.random() * 10 - 5, Math.random() * 10 - 5];
this.out = false;
}
Obstacle.prototype.draw = function () {
ctx.fillStyle = "red";
ctx.fillRect(this.x, this.y, this.w, this.h);
};
Obstacle.prototype.run = function () {
if ((
(this.x + this.w > player.x && this.x + this.w < player.x + player.w) ||
(player.x + player.w > this.x && player.x + player.w < this.x + this.w)
) && (
(this.y + this.h > player.y && this.y + this.h < player.y + player.h) ||
(player.y + player.h > this.y && player.y + player.h < this.y + this.h)
)) player.dead = true;
this.x += this.vl[0];
this.y += this.vl[1];
if (this.x > 1920 || this.x + this.w < 0 || this.y > 1000 || this.y + this.h < 0) this.out = true;
};
Obstacle.obstacles = [];
Obstacle.run = function () {
for (let i = 0; i < Obstacle.obstacles.length; i++) {
let that = Obstacle.obstacles[i];
that.draw();
that.run();
if (that.out) {
Obstacle.obstacles.splice(i, 1);
i--;
}
}
};
var keys = {};
document.body.addEventListener("keydown", (e) => {
keys[e.code] = true;
});
document.body.addEventListener("keyup", (e) => {
keys[e.code] = false;
});
var frameCount = 0;
function draw() {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, 1920, 1000);
ctx.fillStyle = "blue";
ctx.fillRect(player.x, player.y, player.w, player.h);
if (keys.KeyW) player.y -= 3;
if (keys.KeyS) player.y += 3;
if (keys.KeyA) player.x -= 3;
if (keys.KeyD) player.x += 3;
Obstacle.run();
if (frameCount % 10 === 0) {
let w = Math.random() * 10 + 5;
let h = Math.random() * 10 + 5;
Obstacle.obstacles.push(new Obstacle((1920 - w) / 2, (1000 - h) / 2, w, h));
}
if (player.dead) {
restartGame();
} else {
window.requestAnimationFrame(draw);
}
frameCount++;
}
function restartGame() {
player.x = 1920 / 2 - 10;
player.y = 1000 - 20;
player.dead = false;
Obstacle.obstacles = [];
frameCount = 0;
window.requestAnimationFrame(draw);
}
window.requestAnimationFrame(draw);
</script>
</body>
</html>