-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_classes.py
79 lines (62 loc) · 1.92 KB
/
snake_classes.py
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
from turtle import Turtle
import random
# Creating the wall around the window
MAX_WIDTH_X = 415
MIN_WIDTH_X = -415
MAX_HEIGHT_Y = 355
MIN_HEIGHT_Y = -355
class Wall(Turtle):
def __init__(self):
super().__init__()
self.pencolor("#B31312")
self.hideturtle()
self.pensize(15)
self.penup()
self.setpos(MIN_WIDTH_X, MAX_HEIGHT_Y)
self.create_wall()
def create_wall(self):
self.pendown()
self.goto(MAX_WIDTH_X, MAX_HEIGHT_Y)
self.goto(MAX_WIDTH_X, MIN_HEIGHT_Y)
self.goto(MIN_WIDTH_X, MIN_HEIGHT_Y)
self.goto(MIN_WIDTH_X, MAX_HEIGHT_Y)
# Creating foods in random places
class Food(Turtle):
def __init__(self):
super().__init__()
self.shape("circle")
self.speed("fastest")
self.color("#FFD700")
self.penup()
self.shapesize(.5, .5)
self.refresh()
def refresh(self):
X_COORDINATES = random.randint(-380, 380)
Y_COORDINATES = random.randint(-330, 330)
self.setpos(X_COORDINATES, Y_COORDINATES)
# Creating Score bord
FONT = ('Courier', 18, 'normal')
TEXT_ALIGN = 'center'
class ScoreBoard(Turtle):
def __init__(self):
super().__init__()
self.score = 0
self.hideturtle()
self.penup()
self.setpos(x=0, y=360)
self.color("white")
self.update_score()
def update_score(self):
self.clear()
self.write(f"Score = {self.score} ", align=TEXT_ALIGN, font=FONT)
def count_score(self, game_mode):
if game_mode == 'e' or game_mode == 'easy':
self.score += 1
elif game_mode == 'm' or game_mode == 'medium':
self.score += 2
elif game_mode == 'h' or game_mode == 'hard':
self.score += 3
self.update_score()
def game_over(self):
self.setpos(0, 300)
self.write(f"Oopss..Game Over", align=TEXT_ALIGN, font=FONT)