-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.py
More file actions
133 lines (105 loc) · 3.32 KB
/
Copy pathsnake.py
File metadata and controls
133 lines (105 loc) · 3.32 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import pygame
import time
from datetime import datetime
from datetime import timedelta
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 400
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
rect = pygame.Rect((0,0), (SCREEN_WIDTH, SCREEN_HEIGHT))
RED = 255, 0, 0
GREEN = 0, 255, 0
BLUE = 0, 0, 255
PURPLE = 127, 0, 127
BLACK = 0, 0, 0
GRAY = 127, 127, 127
WHITE = 255, 255, 255
# # time.sleep(4)
# pygame.draw.rect(screen, WHITE, rect)
# rect = pygame.Rect((0,0),(40,40))
# pygame.draw.rect(screen, GREEN, rect)
# rect = pygame.Rect((340,60), (60, 120))
# pygame.draw.rect(screen, RED, rect)
# pygame.display.update()
# SCREEN_WIDTH = 400
# SCREEN_HEIGHT = 400
# BLOCK_SIZE = 20
# def draw_background(screen):
# background = pygame.Rect((0, 0), (SCREEN_WIDTH, SCREEN_HEIGHT))
# pygame.draw.rect(screen, WHITE, background)
# def draw_block(screen, color, position):
# block = pygame.Rect((position[1] * BLOCK_SIZE, position[0] * BLOCK_SIZE), (BLOCK_SIZE, BLOCK_SIZE))
# pygame.draw.rect(screen, color, block)
# draw_background(screen)
# draw_block(screen, RED, (1, 1))
# draw_block(screen, RED, (3, 1))
# draw_block(screen, RED, (5, 1))
# draw_block(screen, RED, (7, 1))
# draw_block(screen, GREEN, (12, 10))
# draw_block(screen, GREEN, (12, 11))
# draw_block(screen, GREEN, (12, 12))
# draw_block(screen, GREEN, (12, 13))
# pygame.display.update()
# DIRECTION_ON_KEY = {
# pygame.K_UP:'north',
# pygame.K_DOWN:'south',
# pygame.K_LEFT:'west',
# pygame.K_RIGHT:'east',
# }
# block_direction = 'east'
# block_position = [0, 0]
# last_moved_time = datetime.now()
# while True:
# events = pygame.event.get()
# for event in events:
# if event.type == pygame.QUIT:
# exit()
# if event.type == pygame.KEYDOWN:
# if event.key in DIRECTION_ON_KEY:
# block_direction = DIRECTION_ON_KEY[event.key]
# if timedelta(seconds=1) <= datetime.now() - last_moved_time:
# # block_position[1] += 1
# if block_direction == 'north':
# block_position[0] -= 1
# elif block_direction == 'south':
# block_position[0] += 1
# elif block_direction == 'west':
# block_position[1] -= 1
# elif block_direction == 'east':
# block_position[1] += 1
# last_moved_time = datetime.now()
# draw_background(screen)
# draw_block(screen, GREEN, block_position)
# pygame.display.update()
class Snake:
color = GREEN
def __init__(self):
self.positions = [(9,6), (9,7), (9,8), (9,9)]
self.direction = 'north'
def draw(self, screen):
for position in self.positions:
draw_block(screen, self.color, position)
class Apple:
color = RED
def __init__(self, position=(5,5)):
self.position = position
def draw(self, screen):
draw_block(screen, self.color, self.position)
class GameBoard:
width = 20
height = 20
def __init__(self):
self.snake = Snake()
self.apple = Apple()
def draw(self, screen):
self.apple.draw(screen)
self.snake.draw(screen)
game_board = GameBoard()
while True:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
exit()
draw_background(screen)
game_board.draw(screen)
pygame.display.update()