Napisz grę typu wąż.
- User
Napisz grę typu wąż.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Napisz grę typu wąż.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Oto przykładowa implementacja gry węża w Pythonie z użyciem biblioteki Pygame:
import pygame
import random
# Inicjalizacja Pygame
pygame.init()
# Ustawienia okna gry
WIDTH = 600
HEIGHT = 400
BLOCK_SIZE = 20
FPS = 10
# Kolory
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Tworzenie okna
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Wąż")
clock = pygame.time.Clock()
# Funkcja rysująca węża
def draw_snake(snake):
for segment in snake:
pygame.draw.rect(screen, GREEN, (*segment, BLOCK_SIZE, BLOCK_SIZE))
# Funkcja generująca jedzenie
def generate_food(snake):
while True:
x = random.randint(0, (WIDTH - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE
y = random.randint(0, (HEIGHT - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE
if [x, y] not in snake:
return [x, y]
# Główna funkcja gry
def main():
# Inicjalizacja węża
snake = [[100, 50], [80, 50], [60, 50]]
direction = "RIGHT"
food = generate_food(snake)
score = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Obsługa kierunku ruchu
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and direction != "DOWN":
direction = "UP"
elif event.key == pygame.K_DOWN and direction != "UP":
direction = "DOWN"
elif event.key == pygame.K_LEFT and direction != "RIGHT":
direction = "LEFT"
elif event.key == pygame.K_RIGHT and direction != "LEFT":
direction = "RIGHT"
# Ruch węża
head = snake[0].copy()
if direction == "UP":
head[1] -= BLOCK_SIZE
elif direction == "DOWN":
head[1] += BLOCK_SIZE
elif direction == "LEFT":
head[0] -= BLOCK_SIZE
elif direction == "RIGHT":
head[0] += BLOCK_SIZE
# Sprawdzenie kolizji
if (head in snake or
head[0] < 0 or head[0] >= WIDTH or
head[1] < 0 or head[1] >= HEIGHT):
running = False
continue
snake.insert(0, head)
# Sprawdzenie czy wąż zjadł jedzenie
if head == food:
score += 1
food = generate_food(snake)
else:
snake.pop()
# Rysowanie elementów
screen.fill(BLACK)
draw_snake(snake)
pygame.draw.rect(screen, RED, (*food, BLOCK_SIZE, BLOCK_SIZE))
# Wyświetlanie wyniku
font = pygame.font.SysFont(None, 36)
text = font.render(f"Wynik: {score}", True, WHITE)
screen.blit(text, (10, 10))
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
if __name__ == "__main__":
main()
Aby uruchomić grę:
pip install pygame.pySterowanie:
Gra posiada:
Możliwe rozszerzenia:
Gratulacje! Masz teraz działającą grę węża, którą możesz rozwijać dalej według własnych potrzeb.
Napisz to w html
Oto implementacja gry "Wąż" w HTML/JavaScript z użyciem Canvas:
<!DOCTYPE html>
<html>
<head>
<title>Wąż</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #000;
color: white;
font-family: Arial, sans-serif;
}
canvas {
border: 1px solid #fff;
background-color: #111;
}
#score {
margin: 10px;
font-size: 24px;
}
</style>
</head>
<body>
<div id="score">Wynik: 0</div>
<canvas id="game" width="600" height="400"></canvas>
<script>
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const BLOCK_SIZE = 20;
const FPS = 10;
let score = 0;
let gameOver = false;
// Inicjalizacja węża
let snake = [
{x: 100, y: 50},
{x: 80, y: 50},
{x: 60, y: 50}
];
let direction = 'RIGHT';
let food = generateFood();
// Obsługa klawiatury
document.addEventListener('keydown', changeDirection);
function changeDirection(event) {
const keyPressed = event.keyCode;
const goingUp = direction === 'UP';
const goingDown = direction === 'DOWN';
const goingRight = direction === 'RIGHT';
const goingLeft = direction === 'LEFT';
if (keyPressed === 37 && !goingRight) {
direction = 'LEFT';
}
if (keyPressed === 38 && !goingDown) {
direction = 'UP';
}
if (keyPressed === 39 && !goingLeft) {
direction = 'RIGHT';
}
if (keyPressed === 40 && !goingUp) {
direction = 'DOWN';
}
}
// Generowanie jedzenia
function generateFood() {
let newFood;
while (true) {
newFood = {
x: Math.floor(Math.random() * (canvas.width / BLOCK_SIZE)) * BLOCK_SIZE,
y: Math.floor(Math.random() * (canvas.height / BLOCK_SIZE)) * BLOCK_SIZE
};
if (!snake.some(segment => segment.x === newFood.x && segment.y === newFood.y)) {
return newFood;
}
}
}
// Rysowanie gry
function drawGame() {
if (gameOver) return;
// Przesuwanie węża
const head = {...snake[0]};
switch (direction) {
case 'UP': head.y -= BLOCK_SIZE; break;
case 'DOWN': head.y += BLOCK_SIZE; break;
case 'LEFT': head.x -= BLOCK_SIZE; break;
case 'RIGHT': head.x += BLOCK_SIZE; break;
}
// Sprawdzanie kolizji
if (head.x < 0 || head.x >= canvas.width ||
head.y < 0 || head.y >= canvas.height ||
snake.some(segment => segment.x === head.x && segment.y === head.y)) {
gameOver = true;
alert('Koniec gry! Wynik: ' + score);
return;
}
snake.unshift(head);
// Sprawdzanie czy zjedzono jedzenie
if (head.x === food.x && head.y === food.y) {
score++;
document.getElementById('score').textContent = 'Wynik: ' + score;
food = generateFood();
} else {
snake.pop();
}
// Czyszczenie ekranu
ctx.fillStyle = '#111';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Rysowanie jedzenia
ctx.fillStyle = 'red';
ctx.fillRect(food.x, food.y, BLOCK_SIZE, BLOCK_SIZE);
// Rysowanie węża
ctx.fillStyle = 'lime';
snake.forEach(segment => {
ctx.fillRect(segment.x, segment.y, BLOCK_SIZE, BLOCK_SIZE);
});
}
// Pętla gry
setInterval(drawGame, 1000 / FPS);
</script>
</body>
</html>
.html (np. snake.html)Ta wersja działa bezpośrednio w przeglądarce bez potrzeby instalacji dodatkowych bibliotek.