-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakey.py
148 lines (116 loc) · 4.38 KB
/
snakey.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
import pygame
import sys
import random
from pygame.math import Vector2
class Dot:
def __init__(self):
self.rad = 10
self.eaten = True
def location(self):
self.x = random.randint(0, board_x)
self.y = random.randint(0, board_y)
self.locate = Vector2((self.x * cell), (self.y * cell))
def draw_dot(self):
if self.eaten == True:
self.location()
pygame.draw.circle(screen, "green", self.locate, self.rad)
self.eaten = False
elif self.locate.x <= (0 + cell) or self.locate.x >= (500 - cell):
self.location()
pygame.draw.circle(screen, "green", self.locate, self.rad)
elif self.locate.y <= (0 + cell) or self.locate.y >= (800 - cell):
self.location()
pygame.draw.circle(screen, "green", self.locate, self.rad)
else:
pygame.draw.circle(screen, "green", self.locate, self.rad)
class Snake:
def __init__(self):
self.body = [Vector2(board_x, board_y), Vector2((board_x * cell), (board_y * cell)), Vector2((board_x * (2 * cell)), (board_y * (2 * cell)))]
self.rad = 10
self.direction = Vector2(cell, 0)
self.growing = False
self.dead = False
def draw_snake(self):
if self.dead == False:
for segment in self.body:
pygame.draw.circle(screen, "red", segment, self.rad)
if self.dead == True:
for segment in self.body:
pygame.draw.circle(screen, "black", segment, self.rad)
def move_snake(self):
if self.dead == True:
None
else:
if self.growing == True:
snake_copy = self.body[:]
snake_copy.insert(0, snake_copy[0] + self.direction)
self.body = snake_copy[:]
self.growing = False
else:
snake_copy = self.body[:-1]
snake_copy.insert(0, snake_copy[0] + self.direction)
self.body = snake_copy[:]
def grow(self):
self.growing = True
def death(self):
for segment in self.body[1:]:
if self.body[0] == segment:
self.dead = True
if self.body[0].x <= 15 or self.body[0].x >= 500:
self.dead = True
if self.body[0].y <= 15 or self.body[0].y >= 785:
self.dead = True
class Main:
def __init__(self):
self.snake = Snake()
self.dot = Dot()
def update(self):
self.snake.move_snake()
self.eat()
self.snake.death()
def draw_game(self):
self.dot.draw_dot()
self.snake.draw_snake()
pygame.draw.line(screen, "black", (0, 0), (510, 0), 10)
pygame.draw.line(screen, "black", (0, 800), (510, 800), 10)
pygame.draw.line(screen, "black", (0, 0), (0, 800), 10)
pygame.draw.line(screen, "black", (510, 0), (510, 800), 5)
def eat(self):
if self.dot.locate == self.snake.body[0]:
self.snake.grow()
self.dot.eaten = True
self.dot.draw_dot()
for segment in self.snake.body[:]:
if segment == self.dot.locate:
self.dot.eaten = True
self.dot.draw_dot()
pygame.init()
board_x = 40
board_y = 40
cell = 20
pygame.display.set_caption("Snakey")
screen = pygame.display.set_mode((800,800))
clock = pygame.time.Clock()
update = pygame.USEREVENT
pygame.time.set_timer(update, 150)
game = Main()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == update:
game.update()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
game.snake.direction = Vector2(0, -cell)
if event.key == pygame.K_DOWN:
game.snake.direction = Vector2(0, cell)
if event.key == pygame.K_LEFT:
game.snake.direction = Vector2(-cell, 0)
if event.key == pygame.K_RIGHT:
game.snake.direction = Vector2(cell, 0)
screen.fill("white")
game.draw_game()
pygame.display.update()
clock.tick(60)