-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.py
More file actions
295 lines (219 loc) · 7.53 KB
/
objects.py
File metadata and controls
295 lines (219 loc) · 7.53 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import random
import time
import pygame
from pygame.rect import Rect
class GameObject:
"""This is a basic class.
Attributes:
x_coordinate(int), y_coordinate(int): object coordinates
width(int), height(int): size of object
bounds(RectType): object
color(tuple is long 3): object color
"""
def __init__(self, x_coordinate, y_coordinate, width, height):
"""
Args:
x_coordinate(int): x coordinate
y_coordinate(int): y coordinate
width(int): width of object
height(int): height of object
"""
self.x_coordinate = x_coordinate
self.y_coordinate = y_coordinate
self.width = width
self.height = height
self.bounds = Rect(x_coordinate, y_coordinate, width, height)
self.color = (0, 0, 0)
def draw(self, surface):
"""Draw object on transmitted surface
Args:
surface(pygame.Surface): surface where we will draw object
Returns:
nothing
"""
pygame.draw.rect(surface, self.color, self.bounds)
class Square(GameObject):
"""This is special square for drawing of snake.
"""
def __init__(self, x_coordinate, y_coordinate, side_of_square, color):
"""
Args:
x_coordinate(int): x coordinate
y_coordinate(int): y coordinate
side_of_square(int): size of square
color(tuple is a long 3): color of square
"""
GameObject.__init__(self, x_coordinate, y_coordinate, side_of_square, side_of_square)
self.color = color
class Apple(GameObject):
"""
Attributes:
w(int), h(int): size of surface for correct choose coordinates for drawing apple.
side_of_square(int):
"""
def __init__(self, x_coordinate, y_coordinate, side_of_square, color, width_of_field, height_of_field):
"""
Args:
x_coordinate(int): x coordinate
y_coordinate(int): y coordinate
side_of_square(int):
color(tuple is a long 3):
width_of_field(int):
height_of_field(int):
"""
GameObject.__init__(self, x_coordinate, y_coordinate, side_of_square, side_of_square)
self.color = color
self.side_of_square = side_of_square
self.w = width_of_field
self.h = height_of_field
def update(self):
"""Update coordinates.
When snake eat an apple we choose new coordinates.
Returns:
nothing
"""
self.x_coordinate = random.randrange(0, self.w//self.side_of_square)*self.side_of_square
self.y_coordinate = random.randrange(0, self.h//self.side_of_square)*self.side_of_square
self.bounds = Rect(self.x_coordinate, self.y_coordinate, self.side_of_square, self.side_of_square)
class Bonus(Apple):
"""
Like an apple but bonus disappears after max_time seconds.
Attributes:
time(time): time when bonus born
max_time(int): max time of life
"""
def __init__(self, x_coordinate, y_coordinate, side_of_square, color, width_of_field, height_of_field):
"""
Args:
x_coordinate(int): x coordinate
y_coordinate(int): y coordinate
side_of_square(int):
color(tuple is a long 3):
width_of_field(int):
height_of_field(int):
"""
Apple.__init__(self, x_coordinate, y_coordinate, side_of_square, color, width_of_field, height_of_field)
self.time = time.time()
self.max_time = 5
class Snake:
"""
Snake consists of Squares.
Attributes:
head(tuple): head coordinates
body(list with tuple): body coordinates
side_of_square(int):
direction(str): snake move this direction
color(tuple is long 3): color of snake
"""
def __init__(self, list_of_pos, side_of_square, direction, color):
"""
Args:
list_of_pos(list with tuple): body coordinates
side_of_square(int):
direction(str):
color(tuple is a long 3):
"""
self.head = list_of_pos[0]
self.body = []
self.side_of_square = side_of_square
self.color = color
for pos in list_of_pos:
self.body.append(Square(pos[0], pos[1], side_of_square, self.color))
self.direction = direction
def draw(self, surface):
"""
Draw all squares on surface.
Args:
surface(pygame.Surface): surface where we will draw snake
Returns:
nothing
"""
for body_square in self.body:
body_square.draw(surface)
def update(self):
"""
Add new head coordinates in the right place that depends on the direction
Returns:
nothing
"""
if self.direction == 'RIGHT':
self.head = self.head[0] + self.side_of_square, self.head[1]
elif self.direction == 'LEFT':
self.head = self.head[0] - self.side_of_square, self.head[1]
elif self.direction == 'UP':
self.head = self.head[0], self.head[1] - self.side_of_square
elif self.direction == 'DOWN':
self.head = self.head[0], self.head[1] + self.side_of_square
self.body.insert(0, Square(self.head[0], self.head[1], self.side_of_square, self.color))
def delete_tail(self):
"""
Delete last element of body if snake did not eat an apple.
Returns:
nothing
"""
self.body.pop()
def new_direction(self, command):
"""
Choose new direction that user wants if new direction is correct for snake.
Args:
command(str): new direction that user wants
Returns:
nothing
"""
if command == 'LEFT' and self.direction != 'RIGHT':
self.direction = command
if command == 'RIGHT' and self.direction != 'LEFT':
self.direction = command
if command == 'UP' and self.direction != 'DOWN':
self.direction = command
if command == 'DOWN' and self.direction != 'UP':
self.direction = command
class TextObject:
"""
Attributes:
x(int), y(int): coordinates
color(tuple is long 3): color of text
font_name(str): font of text
font_size(int): size of text
"""
def __init__(self, x, y, color, text, font_name, font_size):
"""
Args:
x(int): x coordinate
y(int): y coordinate
color(tuple is a long 3):
text(str):
font_name(str): font of text
font_size(int): size of text
"""
self.pos = (x, y)
self.text = text
self.color = color
self.font = pygame.font.SysFont(font_name, font_size)
def get_surface(self, text):
"""
Args:
text(str): text that we draw
Returns:
render of text
"""
text_surface = self.font.render(text, False, self.color)
return text_surface
def draw(self, surface):
"""Draw text on surface.
Args:
surface(pygame.Surface): surface where we will draw text
Returns:
nothing
"""
text_surface = self.get_surface(self.text)
pos = self.pos
surface.blit(text_surface, pos)
def get_update(self, text):
"""Update text.
Args:
text(str): new text
Returns:
nothing
"""
self.text = text