-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (89 loc) · 3.52 KB
/
main.py
File metadata and controls
110 lines (89 loc) · 3.52 KB
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
import pygame
import random
pygame.init()
WIDTH, HEIGHT = 600, 450
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game")
GameOverText = pygame.image.load('game_over.png').convert_alpha()
game_over_title = pygame.transform.scale(GameOverText, (350, 250))
RestartTitleText = pygame.image.load('restart.png').convert_alpha()
restart_title = pygame.transform.scale(RestartTitleText, (368, 25))
BLACK = (0, 0, 0)
GREEN = (24, 163, 11)
RED = (237, 81, 81)
WHITE = (255, 255, 255)
snake_size = 25
snake_pos = [WIDTH // 2, HEIGHT // 2]
snake_body = [[WIDTH // 2, HEIGHT // 2]]
apple_pos = [
random.randint(0, (WIDTH - snake_size) // snake_size) * snake_size,
random.randint(0, (HEIGHT - snake_size) // snake_size) * snake_size
]
apple_size = snake_size
direction = 'RIGHT'
change_to = direction
score = 0
game_over = False
font = pygame.font.SysFont('Arial', 25)
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r and game_over:
game_over = False
snake_pos = [WIDTH // 2, HEIGHT // 2]
snake_body = [[WIDTH // 2, HEIGHT // 2]]
direction = 'RIGHT'
change_to = direction
score = 0
apple_pos = [
random.randint(0, (WIDTH - snake_size) // snake_size) * snake_size,
random.randint(0, (HEIGHT - snake_size) // snake_size) * snake_size
]
if not game_over:
if event.key == pygame.K_a and direction != 'RIGHT':
change_to = 'LEFT'
if event.key == pygame.K_d and direction != 'LEFT':
change_to = 'RIGHT'
if event.key == pygame.K_w and direction != 'DOWN':
change_to = 'UP'
if event.key == pygame.K_s and direction != 'UP':
change_to = 'DOWN'
if not game_over:
direction = change_to
if direction == 'RIGHT':
snake_pos[0] += snake_size
if direction == 'LEFT':
snake_pos[0] -= snake_size
if direction == 'UP':
snake_pos[1] -= snake_size
if direction == 'DOWN':
snake_pos[1] += snake_size
snake_body.insert(0, list(snake_pos))
if snake_pos[0] == apple_pos[0] and snake_pos[1] == apple_pos[1]:
apple_pos = [
random.randint(0, (WIDTH - snake_size) // snake_size) * snake_size,
random.randint(0, (HEIGHT - snake_size) // snake_size) * snake_size
]
score += 1
else:
snake_body.pop()
if (snake_pos[0] >= WIDTH or snake_pos[0] < 0 or
snake_pos[1] >= HEIGHT or snake_pos[1] < 0 or
snake_pos in snake_body[1:]):
game_over = True
screen.fill(BLACK)
pygame.draw.rect(screen, RED, (apple_pos[0], apple_pos[1], apple_size, apple_size))
for pos in snake_body:
pygame.draw.rect(screen, GREEN, (pos[0], pos[1], snake_size, snake_size))
score_text = font.render(f'score: {score}', True, WHITE)
screen.blit(score_text, (10, 10))
if game_over:
screen.blit(game_over_title, (WIDTH // 2 - 180, HEIGHT // 2 - 122.5))
screen.blit(restart_title, (WIDTH // 2 - 180, HEIGHT // 2 + 50))
pygame.display.update()
clock.tick(12)
pygame.quit()