/
tile.py
132 lines (110 loc) · 5.12 KB
/
tile.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
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
import stddraw # the stddraw module is used as a basic graphics library
from color import Color # used for coloring the tile and the number on it
from point import Point # used for representing the position of the tile
import copy as cp # the copy module is used for copying tile positions
from random import randint
import math # math module that provides mathematical functions
# Class used for representing numbered tiles as in 2048
class Tile:
# Class attributes shared among all Tile objects
# ---------------------------------------------------------------------------
# value used for the thickness of the boxes (boundaries) around the tiles
boundary_thickness = 0.004
# font family and size used for displaying the tile number
font_family, font_size = "Arial", 14
# Constructor that creates a tile at a given position with 2 as its number
def __init__(self, position = Point(0, 0)): # (0, 0) is the default position
# assign the number on the tile
self.number = 2**randint(1,3)
# set the colors of the tile
self.foreground_color = Color(167, 153, 140) # foreground (number) color
self.boundary_color = Color(167, 153, 140) # boundary (box) color
# set the position of the tile as the given position
self.position = Point(position.x, position.y)
def __repr__(self):
return str(self.number)
# Setter method for the position of the tile
def set_position(self, position):
# set the position of the tile as the given position
self.position = cp.copy(position)
# Getter method for the position of the tile
def get_position(self):
# return the position of the tile
return cp.copy(self.position)
def double(self):
self.number *= 2
# Method for moving the tile by dx along the x axis and by dy along the y axis
def move(self, dx, dy):
self.position.translate(dx, dy)
# Method for drawing the tile
def draw(self):
# draw the tile as a filled square
if (self.number == 2):
self.background_color = Color(238, 228, 218) # background (tile) color
if (self.number == 4):
self.background_color = Color(237, 224, 200) # background (tile) color
if (self.number == 8):
self.background_color = Color(242, 177, 121) # background (tile) color
if (self.number == 16):
self.background_color = Color(245, 149, 99) # background (tile) color
if (self.number == 32):
self.background_color = Color(246, 124, 95) # background (tile) color
if (self.number == 64):
self.background_color = Color(246, 94, 59) # background (tile) color
if (self.number == 128):
self.background_color = Color(237, 207, 114) # background (tile) color
if (self.number == 256):
self.background_color = Color(237, 204, 97) # background (tile) color
if (self.number == 512):
self.background_color = Color(237, 200, 80) # background (tile) color
if (self.number == 1024):
self.background_color = Color(237, 197, 63) # background (tile) color
if (self.number == 2048):
self.background_color = Color(237, 194, 46) # background (tile) color
#used proper colors until 2048
#used remainder in order to get a proper color for every number
#with this the value will never exceed 255 however better solution may be proposed
if (self.number > 2048):
self.background_color= Color((self.number%255),(self.number%20),(self.number%255))
stddraw.setPenColor(self.background_color)
stddraw.filledSquare(self.position.x, self.position.y, 0.5)
# draw the bounding box of the tile as a square
stddraw.setPenColor(self.boundary_color)
stddraw.setPenRadius(Tile.boundary_thickness)
stddraw.square(self.position.x, self.position.y, 0.5)
stddraw.setPenRadius() # reset the pen radius to its default value
# draw inner lines
x = self.position.x
y = self.position.y
stddraw.setPenColor(self.foreground_color)
stddraw.square(x,y,0.3)
stddraw.setPenRadius(Tile.boundary_thickness)
stddraw.line(x-0.5,y-0.5,x+0.5,y+0.5)
stddraw.line(x-0.5,y+0.5,x+0.5,y-0.5)
#draw inner box
stddraw.setPenRadius()
stddraw.setPenColor(self.background_color)
stddraw.filledSquare(self.position.x, self.position.y, 0.3)
###############################
#stddraw.setPenColor(stddraw.RED)
#stddraw.filledCircle(0.5, 0.5, 0.3)
#stddraw.filledCircle(-0.5,0.5,0.3)
# draw the number on the tile
stddraw.setPenColor(self.foreground_color)
stddraw.setFontFamily(Tile.font_family)
stddraw.setFontSize(Tile.font_size)
stddraw.boldText(self.position.x, self.position.y, str(self.number))
def main(): # Test method
grid_h, grid_w = 18, 12
# set the size of the drawing canvas
canvas_h, canvas_w = 40 * grid_h, 60 * grid_w
stddraw.setCanvasSize(canvas_w, canvas_h)
# set the scale of the coordinate system
stddraw.setXscale(-0.5, grid_w + 4.5)
stddraw.setYscale(-0.5, grid_h - 0.5)
obj = Tile(Point(2,4))
obj.draw()
stddraw.show()
pass
if __name__ == '__main__':
main()