├── Passo a Passo - Snake.pdf
├── README.md
├── Solução - Snake.pdf
├── snake.py
└── snake_final.py
/Passo a Passo - Snake.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filhoweuler/Pygame-Snake/5166de162a3a31cc9aface71f72fd8b98a5b6c74/Passo a Passo - Snake.pdf
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Pygame-Snake
2 | Simples jogo de Snake feito com PyGame.
3 |
4 | Para rodar o projeto:
5 |
6 | - Execute
pip install pygame
7 | - Rode o jogo com
python snake.py
8 |
--------------------------------------------------------------------------------
/Solução - Snake.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filhoweuler/Pygame-Snake/5166de162a3a31cc9aface71f72fd8b98a5b6c74/Solução - Snake.pdf
--------------------------------------------------------------------------------
/snake.py:
--------------------------------------------------------------------------------
1 | import pygame, random
2 | from pygame.locals import *
3 |
4 | def on_grid_random():
5 | x = random.randint(0,590)
6 | y = random.randint(0,590)
7 | return (x//10 * 10, y//10 * 10)
8 |
9 | def collision(c1, c2):
10 | return (c1[0] == c2[0]) and (c1[1] == c2[1])
11 |
12 | UP = 0
13 | RIGHT = 1
14 | DOWN = 2
15 | LEFT = 3
16 |
17 | pygame.init()
18 | screen = pygame.display.set_mode((600,600))
19 | pygame.display.set_caption('Snake')
20 |
21 | snake = [(200, 200), (210, 200), (220,200)]
22 | snake_skin = pygame.Surface((10,10))
23 | snake_skin.fill((255,255,255))
24 |
25 | apple_pos = on_grid_random()
26 | apple = pygame.Surface((10,10))
27 | apple.fill((255,0,0))
28 |
29 | my_direction = LEFT
30 |
31 | clock = pygame.time.Clock()
32 |
33 | while True:
34 | clock.tick(10)
35 | for event in pygame.event.get():
36 | if event.type == QUIT:
37 | pygame.quit()
38 |
39 | if event.type == KEYDOWN:
40 | if event.key == K_UP:
41 | my_direction = UP
42 | if event.key == K_DOWN:
43 | my_direction = DOWN
44 | if event.key == K_LEFT:
45 | my_direction = LEFT
46 | if event.key == K_RIGHT:
47 | my_direction = RIGHT
48 |
49 | if collision(snake[0], apple_pos):
50 | apple_pos = on_grid_random()
51 | snake.append((0,0))
52 |
53 | for i in range(len(snake) - 1, 0, -1):
54 | snake[i] = (snake[i-1][0], snake[i-1][1])
55 |
56 | if my_direction == UP:
57 | snake[0] = (snake[0][0], snake[0][1] - 10)
58 | if my_direction == DOWN:
59 | snake[0] = (snake[0][0], snake[0][1] + 10)
60 | if my_direction == RIGHT:
61 | snake[0] = (snake[0][0] + 10, snake[0][1])
62 | if my_direction == LEFT:
63 | snake[0] = (snake[0][0] - 10, snake[0][1])
64 |
65 | screen.fill((0,0,0))
66 | screen.blit(apple, apple_pos)
67 | for pos in snake:
68 | screen.blit(snake_skin,pos)
69 |
70 | pygame.display.update()
--------------------------------------------------------------------------------
/snake_final.py:
--------------------------------------------------------------------------------
1 | ##### 10 - Game over ####
2 | import pygame, random
3 | from pygame.locals import *
4 |
5 | # Helper functions
6 | def on_grid_random():
7 | x = random.randint(0,59)
8 | y = random.randint(0,59)
9 | return (x * 10, y * 10)
10 |
11 | def collision(c1, c2):
12 | return (c1[0] == c2[0]) and (c1[1] == c2[1])
13 |
14 | # Macro definition for snake movement.
15 | UP = 0
16 | RIGHT = 1
17 | DOWN = 2
18 | LEFT = 3
19 |
20 | pygame.init()
21 | screen = pygame.display.set_mode((600, 600))
22 | pygame.display.set_caption('Snake')
23 |
24 | snake = [(200, 200), (210, 200), (220,200)]
25 | snake_skin = pygame.Surface((10,10))
26 | snake_skin.fill((255,255,255)) #White
27 |
28 | apple_pos = on_grid_random()
29 | apple = pygame.Surface((10,10))
30 | apple.fill((255,0,0))
31 |
32 | my_direction = LEFT
33 |
34 | clock = pygame.time.Clock()
35 |
36 | font = pygame.font.Font('freesansbold.ttf', 18)
37 | score = 0
38 |
39 | game_over = False
40 | while not game_over:
41 | clock.tick(10)
42 | for event in pygame.event.get():
43 | if event.type == QUIT:
44 | pygame.quit()
45 | exit()
46 |
47 | if event.type == KEYDOWN:
48 | if event.key == K_UP and my_direction != DOWN:
49 | my_direction = UP
50 | if event.key == K_DOWN and my_direction != UP:
51 | my_direction = DOWN
52 | if event.key == K_LEFT and my_direction != RIGHT:
53 | my_direction = LEFT
54 | if event.key == K_RIGHT and my_direction != LEFT:
55 | my_direction = RIGHT
56 |
57 | if collision(snake[0], apple_pos):
58 | apple_pos = on_grid_random()
59 | snake.append((0,0))
60 | score = score + 1
61 |
62 | # Check if snake collided with boundaries
63 | if snake[0][0] == 600 or snake[0][1] == 600 or snake[0][0] < 0 or snake[0][1] < 0:
64 | game_over = True
65 | break
66 |
67 | # Check if the snake has hit itself
68 | for i in range(1, len(snake) - 1):
69 | if snake[0][0] == snake[i][0] and snake[0][1] == snake[i][1]:
70 | game_over = True
71 | break
72 |
73 | if game_over:
74 | break
75 |
76 | for i in range(len(snake) - 1, 0, -1):
77 | snake[i] = (snake[i-1][0], snake[i-1][1])
78 |
79 | # Actually make the snake move.
80 | if my_direction == UP:
81 | snake[0] = (snake[0][0], snake[0][1] - 10)
82 | if my_direction == DOWN:
83 | snake[0] = (snake[0][0], snake[0][1] + 10)
84 | if my_direction == RIGHT:
85 | snake[0] = (snake[0][0] + 10, snake[0][1])
86 | if my_direction == LEFT:
87 | snake[0] = (snake[0][0] - 10, snake[0][1])
88 |
89 | screen.fill((0,0,0))
90 | screen.blit(apple, apple_pos)
91 |
92 | for x in range(0, 600, 10): # Draw vertical lines
93 | pygame.draw.line(screen, (40, 40, 40), (x, 0), (x, 600))
94 | for y in range(0, 600, 10): # Draw vertical lines
95 | pygame.draw.line(screen, (40, 40, 40), (0, y), (600, y))
96 |
97 | score_font = font.render('Score: %s' % (score), True, (255, 255, 255))
98 | score_rect = score_font.get_rect()
99 | score_rect.topleft = (600 - 120, 10)
100 | screen.blit(score_font, score_rect)
101 |
102 | for pos in snake:
103 | screen.blit(snake_skin,pos)
104 |
105 | pygame.display.update()
106 |
107 | while True:
108 | game_over_font = pygame.font.Font('freesansbold.ttf', 75)
109 | game_over_screen = game_over_font.render('Game Over', True, (255, 255, 255))
110 | game_over_rect = game_over_screen.get_rect()
111 | game_over_rect.midtop = (600 / 2, 10)
112 | screen.blit(game_over_screen, game_over_rect)
113 | pygame.display.update()
114 | pygame.time.wait(500)
115 | while True:
116 | for event in pygame.event.get():
117 | if event.type == QUIT:
118 | pygame.quit()
119 | exit()
120 |
--------------------------------------------------------------------------------