o1-preview-2024-09-12· openai
from WildChat-4.8M
write a program in javascript that play snake
- User
write a program in javascript that play snake
- Assistant
Certainly! Below is a simple implementation of the classic Snake game using JavaScript and HTML5 canvas. This code will allow you to play Snake directly in your web browser.
Step 1: Create the HTML File
Create an
index.htmlfile with the following content:html<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Snake Game</title> <style> body { background-color: #000; } canvas { display: block; margin: 50px auto; background-color: #eee; border: 1px solid #ccc; } h1, p { text-align: center; color: #fff; } </style> </head> <body> <h1>Snake Game</h1> <canvas id="game" width="400" height="400"></canvas> <p>Use the arrow keys to move the snake.</p> <script src="script.js"></script> </body> </html>
Step 2: Create the JavaScript File
Create a
script.jsfile with the following content:javascript