-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.html
175 lines (162 loc) · 4.8 KB
/
game.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html>
<head>
<title>Phytons Game</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<style type="text/css">
.screen{
width: 1024px;
height: 1024px;
background-image: linear-gradient(to bottom right, #002f3d, #414242, #002f3d) ;
margin : 0 auto;
border-radius: 20px;
box-shadow: 1px 2px 8px rgba(0, 0, 0, 0.65);
}
.gameboard{
padding : 30px;
}
table{
padding-top: 50px;
width: 100%;
}
p,time{
color: white;
font-family: cursive;
font-size: 30px;
}
.title{
font-size: 50px;
text-align: center;
}
.time{
text-align: right;
padding-right: 5px;
}
.time{
text-align: left;
}
.rewind{
margin-left : 40px;
border-style: none;
padding: 2px;
font-size: 30px;
border-radius: 10px;
color: white;
background-color: #0069d9;
font-family: cursive;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
var ruang = $("#ruang")[0];
var ctx = ruang.getContext("2d");
var lebar = $("#ruang").width();
var tinggi = $("#ruang").height();
var c = 20;
var tekan ;
var makanan;
var nilai=6;
var array_ular;
function init() {
tekan = "right";
create_snake();
create_makanan();
nilai = 0;
if (typeof game_loop != "undefined") clearInterval(game_loop);
game_loop = setInterval(paint, 35);
}
init();
function create_snake() {
var length = 6;
array_ular = [];
for (var i = length - 1; i >= 0; i--) {
array_ular.push({ x: i, y: 0 });
}
}
function create_makanan() {
makanan = {
x: Math.round(Math.random() * (lebar - c) / c),
y: Math.round(Math.random() * (tinggi - c) / c)
};
}
function paint() {
ctx.fillStyle = "#ecf0f1";
ctx.fillRect(0, 0, lebar, tinggi);
ctx.strokeStyle = "#2c3e50";
ctx.strokeRect(0, 0, lebar, tinggi);
var nx = array_ular[0].x;
var ny = array_ular[0].y;
if (tekan == "right") nx++;
else if (tekan == "left") nx--;
else if (tekan == "up") ny--;
else if (tekan == "down") ny++;
if (
nx == -1 ||
nx == lebar / c ||
ny == -1 ||
ny == tinggi / c ||
cek_tabrakan(nx, ny, array_ular)
){
init();
return;
}
if(nx == makanan.x && ny == makanan.y){
var ekor = { x: nx, y: ny };
nilai++;
create_makanan();
} else {
var ekor = array_ular.pop();
ekor.x = nx;
ekor.y = ny;
}
array_ular.unshift(ekor);
for (var i = 0; i < array_ular.length; i++) {
var c = array_ular[i];
paint_cell(c.x, c.y);
}
paint_cell(makanan.x, makanan.y);
var nilai_text = "nilai: " + nilai;
ctx.fillText(nilai_text, 5, tinggi - 5);
}
function paint_cell(x, y) {
ctx.fillStyle = "#ff0000";
ctx.fillRect(x * c, y * c, c, c);
ctx.strokeStyle = "#0099bf";
ctx.strokeRect(x * c, y * c, c, c);
}
function cek_tabrakan(x, y, array) {
for (var i = 0; i < array.length; i++) {
if (array[i].x == x && array[i].y == y) return true;
}
return false;
}
$(document).keydown(function(e) {
var key = e.which;
if (key == "37" || key =="65" && tekan != "right") tekan = "left";
else if (key == "38" || key =="87" && tekan != "down") tekan = "up";
else if (key == "39" || key =="68" && tekan != "left") tekan = "right";
else if (key == "40" || key =="83" && tekan != "up") tekan = "down";
});
});
</script>
<body>
<div class="screen">
<table>
<tr>
<td class="score">
<p>Score :</p>
<p>6</p>
</td>
<td><p class="title">PHYTONS</p></td>
<td class="time">
<p>Time :</p>
<time>01</time>
</td>
</tr>
</table>
<canvas class="gameboard" id="ruang" width="960" height="600"></canvas>
<button type="submit" class="rewind">REWIND</button>
</div>
</body>
</html>