forked from csce-331-ritchey-2023a/project-1-m-ruiz21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.html
223 lines (213 loc) · 6.74 KB
/
ai.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Chat GPT</title>
<link rel="icon" type="image/png" href="assets/common/logo.svg"/>
<meta name="Chat GPT" content="Chat GPT page for Mateo Ruiz's personal website, containing a website using only Chat GPT">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles/app.css">
<link rel="icon" type="image/png" href="assets/common/logo.svg"/>
<!-- fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto+Flex:opsz,wght@8..144,300&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto+Flex:opsz,wght@8..144,300;8..144,600&display=swap" rel="stylesheet">
<!-- only for the navbar -->
<script src="scripts/common.js"></script>
<style>
#game-board {
width: 400px;
height: 400px;
border: 2px solid black;
position: relative;
margin: 0 auto;
margin-top: 3rem;
margin-bottom: 3rem;
}
.snake-unit {
width: 10px;
height: 10px;
position: absolute;
background-color: navy;
border-radius: 5px;
}
.food-unit {
width: 10px;
height: 10px;
position: absolute;
background-color: red;
border-radius: 5px;
}
* {
font-family: 'Roboto', sans-serif;
}
h1, h2, h3, p, ol, li {
margin-left: 1rem;
margin-right: 1rem;
}
h1 {
text-align: left;
font-weight: normal;
}
h2 {
text-align: left;
margin-top: 20px;
font-weight: normal;
}
p {
text-align: left;
margin: 20px;
font-weight: normal;
}
ol {
text-align: left;
margin: 20px;
font-weight: normal;
}
li {
list-style: decimal;
}
</style>
</head>
<body onload="startGame()">
<script>navbar();</script>
<h1>Chat GPT</h1>
<h3>This page was made soley with Chat GPT</h3>
<h2>Instructions</h2>
<ol>
<li>Create an HTML page with a snake game using JavaScript, HTML and CSS.</li>
<li>Use the CSS to style the appearance of the snake and food.</li>
<li>Use JavaScript to create the logic for the game, including a start function that sets up the initial state of the game, creates the snake and food, starts the game loop, and adds an event listener for movement control.</li>
<li>Make the movement use the wasd or ijkl keys to ensure the game doesn't move while playing.</li>
<li>The game should have a game loop that updates the position of the snake and checks for collisions with the food, borders, and itself.</li>
<li>If the snake collides with the borders or itself, the game should stop and display an "Game Over" message.</li>
<li>At the top of the HTML file, add the title "Chat GPT". As a subtitle put "this page was made solely with Chat GPT"</li>
<li>List all of the instructions that I gave you (including this one).</li>
<li>Make this content Roboto flex, and format it however you see fit.</li>
<li>Add some 1rem margin to the left and right for all the text elements, and 3rem margin to the top and bottom of the game.</li>
</ol>
<div id="game-board"></div>
<script>
const scoreBoard = document.getElementById("score-board");
const board = document.getElementById("game-board");
const snake = [];
let food;
let gameInterval;
let direction = "right";
function startGame() {
// Create initial snake
for (let i = 4; i >= 0; i--) {
snake.push({ x: i, y: 0 });
}
// Create initial food
createFood();
// Start game loop
gameInterval = setInterval(update, 100);
// Add event listener for movement control
document.addEventListener("keydown", e => {
switch (e.key) {
case "w":
direction = "up";
break;
case "s":
direction = "down";
break;
case "a":
direction = "left";
break;
case "d":
direction = "right";
break;
case "i":
direction = "up";
break;
case "k":
direction = "down";
break;
case "j":
direction = "left";
break;
case "l":
direction = "right";
break;
}
});
}
function createFood() {
food = {
x: Math.floor(Math.random() * 39),
y: Math.floor(Math.random() * 39)
};
// Check if food overlaps with snake
for (let i = 0; i < snake.length; i++) {
if (food.x === snake[i].x && food.y === snake[i].y) {
createFood();
}
}
}
function update() {
// Move snake
let head;
switch (direction) {
case "up":
head = { x: snake[0].x, y: snake[0].y - 1 };
break;
case "down":
head = { x: snake[0].x, y: snake[0].y + 1 };
break;
case "left":
head = { x: snake[0].x - 1, y: snake[0].y };
break;
case "right":
head = { x: snake[0].x + 1, y: snake[0].y };
break;
}
snake.unshift(head);
// Check for collision with food
if (head.x === food.x && head.y === food.y) {
createFood();
} else {
snake.pop();
}
// Check for collision with borders
if (head.x < 0 || head.x > 39 || head.y < 0 || head.y > 39) {
clearInterval(gameInterval);
alert("Game Over");
restart();
}
// Check for collision with self
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
clearInterval(gameInterval);
alert("Game Over");
restart();
}
}
// Clear board
while (board.firstChild) {
board.removeChild(board.firstChild);
}
// Draw food
const foodUnit = document.createElement("div");
foodUnit.className = "food-unit";
foodUnit.style.left = `${food.x * 10}px`;
foodUnit.style.top = `${food.y * 10}px`;
board.appendChild(foodUnit);
// Draw snake
for (let i = 0; i < snake.length; i++) {
const snakeUnit = document.createElement("div");
snakeUnit.className = "snake-unit";
snakeUnit.style.left = `${snake[i].x * 10}px`;
snakeUnit.style.top = `${snake[i].y * 10}px`;
board.appendChild(snakeUnit);
}
}
function restart() {
if (confirm("Restart game?")) {
window.location.reload();
}
}
</script>
</body>
</html>