最初llama.cppをしようとリンク参照で、2時間半程かけてコンパイルしましたが、上手く行かずで、gemma2:2b でです。下の感じで、作成です。修正は必要だと思いますが、分かる方には便利かもです。
> () ollama run gemma2:2b
pulling manifest
pulling 7462734796d6: 100% ▕██████████████████▏ 1.6 GB
pulling e0a42594d802: 100% ▕██████████████████▏ 358 B
pulling 097a36493f71: 100% ▕██████████████████▏ 8.4 KB
pulling 2490e7468436: 100% ▕██████████████████▏ 65 B
pulling e18ad7af7efb: 100% ▕██████████████████▏ 487 B
verifying sha256 digest
writing manifest
success
>>> pythonを使って、スネークゲームを作成して下さい
```python
import pygame
# Initialize Pygame
pygame.init()
# Screen dimensions
screen_width = 600
screen_height = 400
screen = pygame.display.set_mode((screen_width, screen_height))
# Colors
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
# Snake properties
snake_block_size = 10
snake_speed = 15
# Clock for controlling frame rate
clock = pygame.time.Clock()
# Load the snake image
snake_image = pygame.image.load("snake.png") # Replace "snake.png" with
your actual snake image path
snake_x, snake_y = 50, 50 # Initial snake position
class Snake:
def __init__(self, x, y):
self.x = x
self.y = y
self.body = [
(x - snake_block_size / 2, y), # Head of the snake
]
def move(self, direction):
if direction == "up":
self.y -= snake_block_size
elif direction == "down":
self.y += snake_block_size
elif direction == "left":
self.x -= snake_block_size
else: # direction == "right"
self.x += snake_block_size
def draw(self, screen): # Draw the snake on the screen
for x, y in self.body:
pygame.draw.rect(screen, green, (x, y, snake_block_size,
snake_block_size))
# Game loop
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Move the snake
snake.move("right") # Update the snake's movement based on input
# Clear the screen
screen.fill(black)
# Draw the snake and background (add your own background image here)
snake.draw(screen)
# Update the display
pygame.display.update()
# Limit frame rate to 60 FPS
clock.tick(60)
# Quit Pygame
pygame.quit()
```
**Explanation:**
1. **Initialization & Setup:**
- The code starts by importing `pygame` and initializing it
(`pygame.init()`).
- It sets up the game screen (display window), defines screen
dimensions, colors, and initializes variables for snake properties.
2. **Snake Class:**
- A class called `Snake` is defined to represent the snake's logic:
- `__init__(self, x, y)`: Initializes the snake object at a given
position (x, y) with its head initially at that location, and a list of
body parts.
- `move(direction)`: Handles movement based on input. The snake will
move in the specified direction by updating its coordinates.
- `draw(screen)`: Draws each part of the snake as a rectangle (you
can customize this to use different image or styles)
3. **Game Loop:**
- The main game loop runs continuously. It handles events like keyboard
presses and user actions that change the snake's position.
- The `move()` method allows the snake to respond to user input,
causing it to move in a specified direction based on the chosen direction.
4. **Drawing:**
- The `draw()` function calls `pygame.draw.rect` to fill screen with
black color and then draw each part of the snake with green color
5. **Updating & Display:**
- `screen.fill(black)`: Clears the screen.
- `pygame.display.update()`: Updates the entire display, ensuring
what's on screen is shown.
**How to Use:**
1. **Replace "snake.png"**: Put an actual snake image file in your
project folder (e.g., "snake.png") and change the `snake_image` variable
accordingly
2. **Run the code** from your terminal: `python your_script_name.py`
Let me know if you have any specific features or ideas for your snake game!
