napisz grę w węża
- User
napisz grę w węża
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ę w węża
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Oczywiście! Oto prosty przykład gry w "Węża" (Snake) napisany w Pythonie z użyciem biblioteki pygame. Jeśli nie masz jeszcze pygame zainstalowanego, możesz go dodać za pomocą komendy:
pip install pygame
import pygame
import time
import random
# Inicjalizacja pygame
pygame.init()
# Ustawienia okna gry
WIDTH = 600
HEIGHT = 400
CELL_SIZE = 20
# 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("Gra W Węża")
# Zegar do kontroli FPS
clock = pygame.time.Clock()
FPS = 10
# Font
font = pygame.font.SysFont('arial', 25)
def draw_snake(snake_list):
for x in snake_list:
pygame.draw.rect(screen, GREEN, [x[0], x[1], CELL_SIZE, CELL_SIZE])
def message(msg, color, y_offset=0):
text = font.render(msg, True, color)
rect = text.get_rect(center=(WIDTH//2, HEIGHT//2 + y_offset))
screen.blit(text, rect)
def game_loop():
game_over = False
game_close = False
# Początkowa pozycja węża
x = WIDTH // 2
y = HEIGHT // 2
x_change = 0
y_change = 0
# Ciało węża
snake_list = []
length_of_snake = 1
# Pozycja jedzenia
food_x = round(random.randrange(0, WIDTH - CELL_SIZE) / CELL_SIZE) * CELL_SIZE
food_y = round(random.randrange(0, HEIGHT - CELL_SIZE) / CELL_SIZE) * CELL_SIZE
while not game_over:
while game_close:
screen.fill(BLACK)
message("Przegrałeś! Naciśnij Q-wyjście lub C-zagraj ponownie", RED, y_offset=0)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
game_loop()
# Obsługa klawiatury
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and x_change == 0:
x_change = -CELL_SIZE
y_change = 0
elif event.key == pygame.K_RIGHT and x_change == 0:
x_change = CELL_SIZE
y_change = 0
elif event.key == pygame.K_UP and y_change == 0:
x_change = 0
y_change = -CELL_SIZE
elif event.key == pygame.K_DOWN and y_change == 0:
x_change = 0
y_change = CELL_SIZE
# Sprawdzenie kolizji z granicą ekranu
if x >= WIDTH or x < 0 or y >= HEIGHT or y < 0:
game_close = True
x += x_change
y += y_change
screen.fill(BLACK)
pygame.draw.rect(screen, RED, [food_x, food_y, CELL_SIZE, CELL_SIZE])
snake_head = [x, y]
snake_list.append(snake_head)
if len(snake_list) > length_of_snake:
del snake_list[0]
# Sprawdzenie kolizji z ciałem
for segment in snake_list[:-1]:
if segment == snake_head:
game_close = True
draw_snake(snake_list)
pygame.display.update()
# Sprawdzenie czy wąż zjadł jedzenie
if x == food_x and y == food_y:
food_x = round(random.randrange(0, WIDTH - CELL_SIZE) / CELL_SIZE) * CELL_SIZE
food_y = round(random.randrange(0, HEIGHT - CELL_SIZE) / CELL_SIZE) * CELL_SIZE
length_of_snake += 1
FPS += 0.5 # przyspieszenie gry
clock.tick(FPS)
pygame.quit()
quit()
# Uruchomienie gry
game_loop()
C) lub wyjścia (Q).Jeśli chcesz bardziej rozbudowaną wersję (np. z punktacją, menu startowym, muzyką, grafikami), daj znać — mogę pomóc ją stworzyć!
Chcesz też, żebym opisał działanie tego kodu?