-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
97 lines (81 loc) · 1.64 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Game</title>
</head>
<body onKeyPress="check(event)">
<style>
canvas {
border : 1px solid black;
}
button {
padding: 50 px;
}
.button1 {
background-color: #ff0000;
}
.button2 {
background-color: #00ff00;
}
</style>
<canvas width="200" height="200" id="gsc"></canvas>
<br>
<br>
<script>
let canvas = document.getElementById("gsc");
let ctx = canvas.getContext('2d');
this.position = {x: 0, y: 0};
this.position2 = {x: 190, y: 0};
let state = "start";
function stop() {
state = "stop";
}
function check(event) {
if (event.key === "g"){
start();
}
elif (event.key === "s") {
stop();
}
}
function start() {
state = "start";
}
function gameLoop() {
ctx.clearRect(0, 0, 200, 200);
ctx.fillStyle = "#000000"
ctx.fillRect(this.position.x, this.position.y, 10, 10);
ctx.fillRect(this.position2.x, this.position2.y, 10, 10);
if (state === "stop") {
this.position.x += 0;
this.position.y += 0;
this.position2.x += 0;
this.position2.y += 0;
}
if (state === "start") {
if (this.position.x !== 200 || this.position2.x !== 0){
this.position.x += 1;
this.position2.x -= 1;
}
if (this.position.y !== 190 || this.position2.y !== 190)
{
this.position.y += 1
this.position2.y += 1;
}
if (this.position.x === 200 || this.position2.x === 0) {
this.position.x = 0;
this.position2.x = 190;
}
if (this.position.y === 190 || this.position2.y === 190) {
this.position.y = 0;
this.position2.y = 0;
}
}
requestAnimationFrame(gameLoop);
}
gameLoop();
requestAnimationFrame(gameLoop);
</script>
<button onclick="stop()" class="button button1">Stop</button>
<button onclick="start()"class="button button2">Start</button>
</body>