-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
122 lines (101 loc) · 3.86 KB
/
main.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
import pygame, sys
from pygame.math import Vector2
import random
class SNAKE:
def __init__(self):
self.body = [Vector2(5,10), Vector2(4,10), Vector2(3,10)]
self.direction = Vector2(1,0)
self.new_block = False
def draw_snake(self):
for block in self.body:
x_pos = int(block.x * cell_size)
y_pos = int(block.y * cell_size)
block_rect = pygame.Rect(x_pos, y_pos, cell_size, cell_size)
pygame.draw.rect(screen, snake_color, block_rect)
def move_snake(self):
if self.new_block == True:
body_copy = self.body[:]
body_copy.insert(0, body_copy[0] + self.direction)
self.body = body_copy[:]
self.new_block = False
else:
body_copy = self.body[:-1]
body_copy.insert(0, body_copy[0] + self.direction)
self.body = body_copy[:]
def add_block(self):
self.new_block = True
class FRUIT:
def __init__(self):
self.randomize()
def draw_fruit(self):
fruit_rect = pygame.Rect(int(self.pos.x * cell_size), int(self.pos.y * cell_size), cell_size, cell_size)
pygame.draw.rect(screen,fruit_color, fruit_rect)
def randomize(self):
self.x = random.randint(0, cell_number - 1)
self.y = random.randint(0, cell_number - 1)
self.pos = Vector2(self.x, self.y)
class MAIN:
def __init__(self):
self.snake = SNAKE()
self.fruit = FRUIT()
def update(self):
self.snake.move_snake()
self.check_collision()
self.check_fail()
def draw_elements(self):
self.fruit.draw_fruit()
self.snake.draw_snake()
self.draw_score()
def check_collision(self):
if self.fruit.pos == self.snake.body[0]:
self.fruit.randomize()
self.snake.add_block()
def check_fail(self):
if not 0 <= self.snake.body[0].x < cell_number or not 0 <= self.snake.body[0].y < cell_number:
self.game_over()
for block in self.snake.body[1:]:
if block == self.snake.body[0]:
self.game_over()
def game_over(self):
pygame.quit()
sys.exit()
def draw_score(self):
score_text = str(len(self.snake.body) - 3)
score_surface = game_font.render(score_text, True, font_color)
score_x = int(cell_size * cell_number - 100)
score_y = int(cell_size * cell_number - 50)
score_rect = score_surface.get_rect(center=(score_x, score_y))
screen.blit(score_surface, score_rect)
pygame.init()
cell_size = 40
cell_number = 20
screen = pygame.display.set_mode((cell_number * cell_size, cell_number * cell_size))
clock = pygame.time.Clock()
game_font = pygame.font.Font('/Users/sevalkayikci/Desktop/SEVOS/font.ttf', 50)
background_color = '#AFD198'
fruit_color = '#DD5746'
snake_color = '#007F73'
font_color = '#803D3B'
main_game: MAIN = MAIN()
SCREEN_UPDATE = pygame.USEREVENT
pygame.time.set_timer(SCREEN_UPDATE, 150)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == SCREEN_UPDATE:
main_game.update()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and main_game.snake.direction.y != 1:
main_game.snake.direction = Vector2(0, -1)
if event.key == pygame.K_DOWN and main_game.snake.direction.y != -1:
main_game.snake.direction = Vector2(0, 1)
if event.key == pygame.K_RIGHT and main_game.snake.direction.x != -1:
main_game.snake.direction = Vector2(1, 0)
if event.key == pygame.K_LEFT and main_game.snake.direction.x != 1:
main_game.snake.direction = Vector2(-1, 0)
screen.fill(background_color)
main_game.draw_elements()
pygame.display.update()
clock.tick(60)