0% found this document useful (0 votes)
7 views2 pages

Import Pygame

This document is a Python script for a simple Snake game using the Pygame library. It includes game settings, functions to draw the snake and food, move the snake, check for collisions, and grow the snake when it eats food. The main game loop handles user input, updates the game state, and renders the graphics until the game ends.

Uploaded by

emir587206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Import Pygame

This document is a Python script for a simple Snake game using the Pygame library. It includes game settings, functions to draw the snake and food, move the snake, check for collisions, and grow the snake when it eats food. The main game loop handles user input, updates the game state, and renders the graphics until the game ends.

Uploaded by

emir587206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pygame

import random

# Oyun ayarları
WIDTH, HEIGHT = 600, 400
CELL_SIZE = 20
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

# Pygame başlat
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Yılan Oyunu")
clock = pygame.time.Clock()

# Yılan ve yemek
snake = [(100, 100)]
snake_dir = "RIGHT"
food = (random.randint(0, (WIDTH - CELL_SIZE) // CELL_SIZE) * CELL_SIZE,
random.randint(0, (HEIGHT - CELL_SIZE) // CELL_SIZE) * CELL_SIZE)
speed = 5

def draw_snake(snake):
for segment in snake:
pygame.draw.rect(screen, GREEN, (segment[0], segment[1], CELL_SIZE,
CELL_SIZE))

def draw_food(food):
pygame.draw.rect(screen, RED, (food[0], food[1], CELL_SIZE, CELL_SIZE))

def move_snake(snake, direction):


head_x, head_y = snake[0]
if direction == "UP":
head_y -= CELL_SIZE
elif direction == "DOWN":
head_y += CELL_SIZE
elif direction == "LEFT":
head_x -= CELL_SIZE
elif direction == "RIGHT":
head_x += CELL_SIZE
new_head = (head_x, head_y)
snake = [new_head] + snake[:-1]
return snake

def check_collision(snake):
head = snake[0]
# Duvara çarptı mı?
if head[0] < 0 or head[0] >= WIDTH or head[1] < 0 or head[1] >= HEIGHT:
return True
# Kendine çarptı mı?
if head in snake[1:]:
return True
return False
def grow_snake(snake):
snake.append(snake[-1])

# Ana oyun döngüsü


running = True
while running:
# Olayları işle
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Yön değişikliği
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and snake_dir != "DOWN":
snake_dir = "UP"
if keys[pygame.K_DOWN] and snake_dir != "UP":
snake_dir = "DOWN"
if keys[pygame.K_LEFT] and snake_dir != "RIGHT":
snake_dir = "LEFT"
if keys[pygame.K_RIGHT] and snake_dir != "LEFT":
snake_dir = "RIGHT"

# Yılanı hareket ettir


snake = move_snake(snake, snake_dir)

# Çarpışma kontrolü
if check_collision(snake):
print("Oyun Bitti!")
running = False

# Yemek yeme kontrolü


if snake[0] == food:
grow_snake(snake)
food = (random.randint(0, (WIDTH - CELL_SIZE) // CELL_SIZE) * CELL_SIZE,
random.randint(0, (HEIGHT - CELL_SIZE) // CELL_SIZE) * CELL_SIZE)

# Çizimleri güncelle
screen.fill(BLACK)
draw_snake(snake)
draw_food(food)
pygame.display.flip()

# FPS ayarı
clock.tick(speed)

# Pygame kapat
pygame.quit()

You might also like