write a snake game in 3D & run it
- User
write a snake game in 3D & run it
- Assistant
write a snake game in 3D & run it
Certainly! Below is an example of a simple 3D Snake game written in Python using the Ursina Engine, which is a game engine for Python that makes it easy to create games quickly.
First, make sure you have Python installed on your system (preferably Python 3.6 or later). You will also need to install the ursina package. You can install it using pip:
pip install ursina
Now, here's the code for the 3D Snake game:
from ursina import *
from random import randint
app = Ursina()
window.title = '3D Snake Game'
window.borderless = False
window.fullscreen = False
window.exit_button.visible = False
window.fps_counter.enabled = True
camera.position = (0, 15, -26)
camera.rotation_x = 35
# Create the ground
ground = Entity(model='cube', color=color.light_gray, scale=(20, 1, 20), position=(0, -0.5, 0))
# Snake initial settings
snake = []
initial_length = 3
for i in range(initial_length):
segment = Entity(model='cube', color=color.green, scale=(1, 1, 1), position=(i, 0, 0))
snake.append(segment)
direction = Vec3(1, 0, 0) # Start moving along x-axis
# Generate initial food
food = Entity(model='cube', color=color.red, scale=(1, 1, 1), position=(randint(-9,9), 0, randint(-9,9)))
def update():
global direction
# Snake movement
if held_keys['up arrow']:
if direction != Vec3(0, 0, 1):
direction = Vec3(0, 0, -1)
elif held_keys['down arrow']:
if direction != Vec3(0, 0, -1):
direction = Vec3(0, 0, 1)
elif held_keys['left arrow']:
if direction != Vec3(1, 0, 0):
direction = Vec3(-1, 0, 0)
elif held_keys['right arrow']:
if direction != Vec3(-1, 0, 0):
direction = Vec3(1, 0, 0)
move_snake()
def move_snake():
# Update positions of snake segments
for i in range(len(snake)-1, 0, -1):
snake[i].position = snake[i-1].position
# Move the head
snake[0].position += direction
# Check for collisions with walls
if abs(snake[0].x) > 10 or abs(snake[0].z) > 10:
destroy(snake[0])
invoke(Func(game_over), delay=0.1)
return
# Check for collisions with self
for segment in snake[1:]:
if snake[0].position == segment.position:
invoke(Func(game_over), delay=0.1)
return
# Check for collisions with food
if snake[0].position == food.position:
extend_snake()
relocate_food()
def extend_snake():
# Add new segment to the snake
new_segment = Entity(model='cube', color=color.green, scale=(1, 1, 1))
new_segment.position = snake[-1].position
snake.append(new_segment)
def relocate_food():
# Move the food to a new random position
food.position = (randint(-9,9), 0, randint(-9,9))
# Ensure food doesn't spawn on the snake
for segment in snake:
if food.position == segment.position:
relocate_food()
break
def game_over():
print("Game Over!")
application.quit()
# Set up the game loop to update at regular intervals
from ursina import time
time_interval = 0.2
held_keys = {} # Initialize held_keys to prevent KeyError
def game_loop():
update()
invoke(game_loop, delay=time_interval)
game_loop()
app.run()
Instructions to Run the Game:
Save the Code: Copy the code above into a file and save it with a .py extension, for example, snake3d.py.
Install Ursina: If you haven't installed Ursina yet, open your terminal or command prompt and run:
pip install ursina
Run the Game: In the terminal, navigate to the directory where you saved snake3d.py and run:
python snake3d.py
How to Play:
Game Features:
Customization:
You can adjust various aspects of the game by modifying the code:
time_interval variable to make the snake move faster or slower.randint(-9,9) to change the size of the playing field.color or model properties when creating snake segments for different visuals.Dependencies:
Note:
Feel free to ask if you have any questions or need further assistance with the code!
Not yet included in a published release.