o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Make snake game with random generated quests using HTML + JS (one file), make game smooth
- User
Make snake game with random generated quests using HTML + JS (one file), make game smooth
- Assistant
Make snake game with random generated quests using HTML + JS (one file), make game smooth
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Snake Game with Random Quests</title>
<style>
body {
margin: 0;
background-color: #000;
overflow: hidden;
}
canvas {
display: block;
margin: 0 auto;
background-color: #111;
}
#quest {
color: #fff;
text-align: center;
font-size: 24px;
margin-top: 10px;
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<canvas id="game" width="600" height="600"></canvas>
<div id="quest"></div>
<script>
/*** Game Variables ***/
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let framesPerSecond = 10;
let snake = [{ x: 10, y: 10 }];
let velocity = { x: 0, y: 0 };
let food = { x: 15, y: 15 };
let specialFood = null;
let quest = {
type: '',
target: 0,
progress: 0,
};
let score = 0;
let questCompleted = false;
/*** Initialization ***/
function init() {
generateQuest();
document.addEventListener('keydown', keyDown);
requestAnimationFrame(gameLoop);
}
/*** Game Loop ***/
let lastRenderTime = 0;
function gameLoop(currentTime) {
const secondsSinceLastRender = (currentTime - lastRenderTime) / 1000;
if (secondsSinceLastRender < 1 / framesPerSecond) {
requestAnimationFrame(gameLoop);
return;
}
lastRenderTime = currentTime;
update();
draw();
if (questCompleted) {
generateQuest();
questCompleted = false;
}
requestAnimationFrame(gameLoop);
}
/*** Update Game State ***/
function update() {
// Move snake
const head = { x: snake[0].x + velocity.x, y: snake[0].y + velocity.y };
// Wall collision (wrap around)
head.x = (head.x + tileCount) % tileCount;
head.y = (head.y + tileCount) % tileCount;
// Self collision
for (let segment of snake) {
if (head.x === segment.x && head.y === segment.y) {
resetGame();
return;
}
}
snake.unshift(head);
// Food collision
if (head.x === food.x && head.y === food.y) {
score++;
updateQuestProgress('Collect');
placeFood();
} else {
snake.pop();
}
// Special food collision
if (specialFood && head.x === specialFood.x && head.y === specialFood.y) {
score += 5;
updateQuestProgress('CollectSpecial');
specialFood = null;
}
// Update 'Survive' quest
updateQuestProgress('Survive');
}
/*** Draw Game ***/
function draw() {
// Clear canvas
ctx.fillStyle = '#111';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw snake
ctx.fillStyle = '#0f0';
snake.forEach((segment, index) => {
ctx.fillStyle = index === 0 ? '#0ff' : '#0f0';
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize - 2, gridSize - 2);
});
// Draw food
ctx.fillStyle = '#f00';
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize - 2, gridSize - 2);
// Draw special food
if (specialFood) {
ctx.fillStyle = '#ff0';
ctx.fillRect(specialFood.x * gridSize, specialFood.y * gridSize, gridSize - 2, gridSize - 2);
}
// Draw quest
displayQuest();
}
/*** Handle Input ***/
function keyDown(e) {
switch (e.keyCode) {
case 37: // left arrow
case 65: // 'A' key
if (velocity.x !== 1) velocity = { x: -1, y: 0 };
break;
case 38: // up arrow
case 87: // 'W' key
if (velocity.y !== 1) velocity = { x: 0, y: -1 };
break;
case 39: // right arrow
case 68: // 'D' key
if (velocity.x !== -1) velocity = { x: 1, y: 0 };
break;
case 40: // down arrow
case 83: // 'S' key
if (velocity.y !== -1) velocity = { x: 0, y: 1 };
break;
}
}
/*** Place Food ***/
function placeFood() {
food = randomPosition();
// Ensure food doesn't spawn on the snake
while (snake.some(segment => segment.x === food.x && segment.y === food.y)) {
food = randomPosition();
}
// Chance to spawn special food
if (Math.random() < 0.1) { // 10% chance
placeSpecialFood();
}
}
/*** Place Special Food ***/
function placeSpecialFood() {
specialFood = randomPosition();
while (
snake.some(segment => segment.x === specialFood.x && segment.y === specialFood.y) ||
(food.x === specialFood.x && food.y === specialFood.y)
) {
specialFood = randomPosition();
}
}
/*** Generate Random Position ***/
function randomPosition() {
return {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
}
/*** Generate Quest ***/
function generateQuest() {
const questTypes = ['Collect', 'Survive', 'CollectSpecial'];
quest.type = questTypes[Math.floor(Math.random() * questTypes.length)];
switch (quest.type) {
case 'Collect':
quest.target = Math.floor(Math.random() * 5) + 3; // 3 to 7
break;
case 'Survive':
quest.target = Math.floor(Math.random() * 10) + 5; // 5 to 14 seconds
quest.progress = 0;
break;
case 'CollectSpecial':
quest.target = 1;
specialFood = null;
placeSpecialFood();
break;
}
quest.progress = 0;
}
/*** Update Quest Progress ***/
function updateQuestProgress(type) {
if (!questCompleted && quest.type === type) {
if (type === 'Survive') {
quest.progress += 1 / framesPerSecond;
if (quest.progress >= quest.target) {
questCompleted = true;
}
} else {
quest.progress++;
if (quest.progress >= quest.target) {
questCompleted = true;
}
}
}
}
/*** Display Quest ***/
function displayQuest() {
const questDiv = document.getElementById('quest');
if (!questCompleted) {
switch (quest.type) {
case 'Collect':
questDiv.textContent = `Quest: Collect ${quest.target} apples (${quest.progress}/${quest.target})`;
break;
case 'Survive':
questDiv.textContent = `Quest: Survive for ${quest.target.toFixed(1)} seconds (${quest.progress.toFixed(1)}s)`;
break;
case 'CollectSpecial':
questDiv.textContent = `Quest: Collect the special item (${quest.progress}/${quest.target})`;
break;
}
} else {
questDiv.textContent = `Quest Completed! A new quest will start shortly.`;
}
}
/*** Reset Game ***/
function resetGame() {
snake = [{ x: 10, y: 10 }];
velocity = { x: 0, y: 0 };
placeFood();
questCompleted = false;
generateQuest();
score = 0;
specialFood = null;
}
/*** Start Game ***/
init();
</script>
</body>
</html>
Snake Game with Random Quests
This is a self-contained Snake game implemented in HTML and JavaScript. It features randomly generated quests to keep the gameplay engaging.
Features:
Instructions:
Controls:
Objective:
Quests:
Starting the Game:
snake_game.html.Customization:
framesPerSecond variable in the script to change the game's speed.questTypes array and implement new quest logic in generateQuest() and updateQuestProgress() functions.Enjoy the Game!
This game is designed to be easily understandable and modifiable. Feel free to delve into the code, experiment, and enhance it according to your preferences.
Not yet included in a published release.