-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (60 loc) · 2.04 KB
/
Copy pathmain.py
File metadata and controls
70 lines (60 loc) · 2.04 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
import pygame
from board import initiate_board, set_tiles
from Snake import Snake
from Target import Target
pygame.init()
screen = pygame.display.set_mode((30*8, 30*9))
tile_surface = pygame.Surface((screen.get_width(), screen.get_height()))
clock = pygame.time.Clock()
dt = 0
game_board = initiate_board(screen)
player_pos = game_board[4][4]
starting_point = pygame.Vector2(player_pos)
snake = Snake(4, 4, starting_point)
target = Target(game_board)
direction = 0
MOVE_PLAYER = pygame.USEREVENT + 1
ADD_SEGMENT = pygame.USEREVENT + 2
GAME_OVER = pygame.USEREVENT + 3
pygame.time.set_timer(MOVE_PLAYER, 250)
pygame.time.set_timer(ADD_SEGMENT, 5000)
running = True
paused = False
ended = False
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
paused = not paused
elif event.type == MOVE_PLAYER:
if not paused and not ended:
success = snake.move(game_board, direction)
if success == "GAME OVER":
pygame.event.post(pygame.event.Event(GAME_OVER))
break
target.check_collision(snake)
elif event.type == GAME_OVER:
print("GAME OVER")
running = False
screen.fill((0, 0, 0))
if not paused and not ended:
set_tiles(tile_surface)
screen.blit(tile_surface, (0,0))
for segment in snake.tails:
pygame.draw.circle(screen, "green", segment['starting_point'], 15)
target_start = pygame.Vector2(game_board[target.x][target.y])
pygame.draw.circle(screen, "yellow", target_start, 15)
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
direction = 1
elif keys[pygame.K_s]:
direction = 2
elif keys[pygame.K_a]:
direction = 3
elif keys[pygame.K_d]:
direction = 4
clock.tick(60)
pygame.display.flip()
pygame.quit()