-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_game.py
112 lines (92 loc) · 3.45 KB
/
snake_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import pygame
import random
import numpy as np
# Oyun ayarları
WIDTH, HEIGHT = 500, 500
GRID_SIZE = 20
GRID_WIDTH = WIDTH // GRID_SIZE
GRID_HEIGHT = HEIGHT // GRID_SIZE
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLACK = (0, 0, 0)
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)
ACTIONS = [UP, DOWN, LEFT, RIGHT]
class SnakeGame:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.clock = pygame.time.Clock()
self.reset()
def reset(self):
self.snake = [(GRID_WIDTH // 2, GRID_HEIGHT // 2)]
self.direction = random.choice(ACTIONS)
self.food = self.generate_food()
self.score = 0
self.game_over = False
self.previous_distance = self.calculate_distance(self.snake[0], self.food)
def generate_food(self):
while True:
food = (random.randint(0, GRID_WIDTH - 1), random.randint(0, GRID_HEIGHT - 1))
if food not in self.snake:
return food
def move_snake(self):
head_x, head_y = self.snake[0]
new_head = (head_x + self.direction[0], head_y + self.direction[1])
self.snake.insert(0, new_head)
if self.snake[0] == self.food:
self.score += 1
self.food = self.generate_food()
else:
self.snake.pop()
def is_collision(self):
head = self.snake[0]
return (
head[0] < 0 or head[0] >= GRID_WIDTH or
head[1] < 0 or head[1] >= GRID_HEIGHT or
head in self.snake[1:]
)
def calculate_distance(self, point1, point2):
# Manhattan mesafesi hesaplama
return abs(point1[0] - point2[0]) + abs(point1[1] - point2[1])
def step(self, action):
self.direction = action
self.move_snake()
# Yılanın önceki ve şimdiki yemeğe uzaklığını hesapla
current_distance = self.calculate_distance(self.snake[0], self.food)
reward = 0
if self.is_collision():
reward = -200
self.game_over = True
elif self.snake[0] == self.food:
reward = 300
self.previous_distance = self.calculate_distance(self.snake[0], self.food) # Yemeği yedikten sonra sıfırla
else:
if current_distance < self.previous_distance:
reward = 10 # Yaklaştığı için pozitif ödül
else:
reward = 0 # Uzaklaştığı için negatif ödül
self.previous_distance = current_distance
return self.get_state(), reward, self.game_over, self.score
def get_state(self):
head = self.snake[0]
return np.array([head[0], head[1], self.food[0], self.food[1]])
def render(self):
self.screen.fill(BLACK)
for segment in self.snake:
pygame.draw.rect(self.screen, GREEN, (segment[0] * GRID_SIZE, segment[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE))
pygame.draw.rect(self.screen, RED, (self.food[0] * GRID_SIZE, self.food[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE))
pygame.display.flip()
def close(self):
pygame.quit()
if __name__ == "__main__":
game = SnakeGame()
while True:
game.render()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game.close()
sys.exit()