-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
128 lines (98 loc) · 3.48 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
import time
import random
#INIT THE GAME
pygame.init()
#COLOR
color_snake = (45, 114, 20)
color_background = (73, 71, 71)
color_food = (228, 202, 183)
color_font = (255, 255, 255)
#SETTING SNAKE
size_snake = 10
speed_snake = 13
#SCENE SETTING
width = 600
height = 400
message_font = pygame.font.SysFont('Aerial', 25)
score_font = pygame.font.SysFont('Aerial', 25)
game_display = pygame.display.set_mode((width, height))
time = pygame.time.Clock()
pygame.display.set_caption("Python Game")
#FUNCTION PRINT THE SCORE
def print_score(score):
text = score_font.render("Score : " + str(score), True, color_font)
game_display.blit(text, [0, 0])
#FUNCTION CREATE SNAKE
def print_snake(size_snake, pixels_snake):
for pixel in pixels_snake:
pygame.draw.rect(game_display, color_snake, [pixel[0], pixel[1], size_snake, size_snake])
#FUNCTION DEFINE AND START THE GAME
def game_start():
finish = False
close = False
x = width/2
y = height/2
speed_x = 0
speed_y = 0
pixels_snake = []
length_snake = 1
target_x = round(random.randrange(0, int((width-size_snake) / 10))) * 10
target_y = round(random.randrange(0, int((height-size_snake) / 10))) * 10
#ALGO of the Game
while not finish:
while close:
game_display.fill(color_background)
finish_message = message_font.render("Tap space to retry", True, color_font)
game_display.blit(finish_message, [width / 2.7, height / 5])
print_score(length_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
finish = True
close = False
if event.key == pygame.K_SPACE:
game_start()
if event.type == pygame.QUIT:
finish = True
close = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
finish = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
speed_y = -size_snake
speed_x = 0
if event.key == pygame.K_LEFT:
speed_x = -size_snake
speed_y = 0
if event.key == pygame.K_RIGHT:
speed_x = size_snake
speed_y = 0
if event.key == pygame.K_DOWN:
speed_y = size_snake
speed_x = 0
if x >= width or x < 0 or y >= height or y < 0:
close = True
x += speed_x
y += speed_y
game_display.fill(color_background)
pygame.draw.rect(game_display, color_food, [target_x, target_y, size_snake, size_snake])
pixels_snake.append([x, y])
if len(pixels_snake) > length_snake:
del pixels_snake[0]
for pixel in pixels_snake[:-1]:
if pixel == [x, y]:
close = True
print_snake(size_snake, pixels_snake)
print_score((length_snake - 1))
pygame.display.update()
if x == target_x and y == target_y:
target_x = round(random.randrange(0, int((width-size_snake) / 10))) * 10
target_y = round(random.randrange(0, int((height-size_snake) / 10))) * 10
length_snake += 1
time.tick(speed_snake)
pygame.quit()
quit()
game_start()