-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnek.py
161 lines (124 loc) · 5.02 KB
/
snek.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from operator import ge
from click import password_option
import pygame
from scipy import rand
import Snake as sna
import random
import time
pygame.font.init()
pygame.init()
pygame.mixer.init()
WIDTH, HEIGHT = 1000, 600
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snek")
WHITE = (255, 255, 255)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
FPS = 30
VEL = 1
# load spaceship image
HEALTH_FONT = pygame.font.SysFont('comicsans', 20)
LOSE_SOUND = pygame.mixer.Sound('Assets/JKL83NH-video-game-win.mp3')
x_coor = [x for x in range(0, WIDTH - 20 + 1, 20)]
y_coor = [y for y in range(0, HEIGHT - 20 + 1, 20)]
snakes = []
for i in range(800):
snakes.append(sna.Snake(x_coor, y_coor))
dead_snakes = []
def draw():
window.fill(BLACK)
for snake in snakes:
for i in range(len(snake.snek_body)):
pygame.draw.rect(window, GREEN, snake.snek_body[i])
# Draw food rectangle
pygame.draw.rect(window, RED, snake.food)
# yellow_text = HEALTH_FONT.render(f'Score: {snake.score}', True, YELLOW)
# window.blit(yellow_text, (10, 10))
'''
if snake.snek_body[0].x + snake.snek_body[0].width > WIDTH or snake.snek_body[0].x < 0 or \
snake.snek_body[0].y + snake.snek_body[0].height > HEIGHT or snake.snek_body[0].y < 0 or \
snake.snek_body[0].collidelist(snake.snek_body[1:]) != -1:
win_text = HEALTH_FONT.render('YOU LOSE', True, YELLOW)
window.blit(win_text, (WIDTH/2 - win_text.get_width()//2, HEIGHT/2 - win_text.get_height()//2))
LOSE_SOUND.play()
'''
pygame.display.update()
def sort_score(e):
return e.score
def main():
run = True
clock = pygame.time.Clock()
gen = 1
record = -1
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
'''
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
snakes[0].direction = sna.UP
if event.key == pygame.K_d:
snakes[0].direction = sna.RIGHT
if event.key == pygame.K_s:
snakes[0].direction = sna.DOWN
if event.key == pygame.K_a:
snakes[0].direction = sna.LEFT '''
for snake in snakes:
next_move = snake.predict(snake.get_input())
if next_move == sna.UP and snake.direction != sna.DOWN:
snake.direction = sna.UP
if next_move == sna.DOWN and snake.direction != sna.UP:
snake.direction = sna.DOWN
if next_move == sna.LEFT and snake.direction != sna.RIGHT:
snake.direction = sna.LEFT
if next_move == sna.RIGHT and snake.direction != sna.LEFT:
snake.direction = sna.RIGHT
snake.grow()
if snake.snek_body[0].colliderect(snake.food):
snake.score += 3000
snake.movecountdown = 500
x, y = snake.food_create()
snake.food.x = x
snake.food.y = y
else:
snake.snek_body.pop()
snake.movecountdown -= 1
snake.ttl += 1
draw()
for snake in snakes:
if snake.snek_body[0].x + snake.snek_body[0].width > WIDTH or \
snake.snek_body[0].x < 0 or snake.snek_body[0].y + snake.snek_body[0].height > HEIGHT or \
snake.snek_body[0].y < 0 or snake.snek_body[0].collidelist(snake.snek_body[1:]) != -1 or snake.movecountdown <= 0:
if snake.movecountdown <= 0:
snake.score -= snake.ttl * 50
else:
snake.score -= 10000
#print('dc cong them ne')
snake.score = snake.score + snake.ttl * 10
dead_snakes.insert(0, snake)
snakes.remove(snake)
if len(snakes) == 0 and dead_snakes[0].score < 10000000:
dead_snakes.sort(key=sort_score, reverse=True)
print(f'Gen {gen}: {dead_snakes[0].score} : {dead_snakes[1].score} : {len(dead_snakes[0].snek_body)} : {len(dead_snakes)}')
gen += 1
for i in range(200):
snakes.append(dead_snakes[0].crossover(dead_snakes[1]).mutate(0.05))
for i in range(200):
snakes.append(dead_snakes[2].crossover(dead_snakes[3]).mutate(0.05))
for i in range(200):
snakes.append(dead_snakes[4].crossover(dead_snakes[5]).mutate(0.05))
for snake in dead_snakes[0:200]:
s = sna.Snake( x_coor, y_coor)
s.brain = snake.brain
snakes.append(s)
dead_snakes.clear()
elif len(dead_snakes) > 0 and dead_snakes[0].score >= 10000000:
break
print(dead_snakes[0].brain.weights)
pygame.quit()
if __name__ == '__main__':
main()