-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpole.rb
160 lines (122 loc) · 3.9 KB
/
pole.rb
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
DELAY = 100
require './2048'
class Pole < Gtk::DrawingArea
def width
@x*2+(@width+@interval)*4
end
def height
(@y*2+(@height+@interval)*4 + @pole_rezultat)
end
def initialize
super
@game2048 = Game2048.new
@width = 55
@height = 55
@interval = 2
@pole_rezultat = 15
@x = 10
@y = 10
@colors_rgb = {
0 => {"r" => (161/256.0), "g" => (167/256.0), "b" => (161/256.0)},
2 => {"r" => (142/256.0), "g" => (232/256.0), "b" => (121/256.0)},
4 => {"r" => (90/256.0), "g" => (198/256.0), "b" => (217/256.0)},
8 => {"r" => (210/256.0), "g" => (90/256.0), "b" => (217/256.0)},
16 => {"r" => (231/256.0), "g" => (241/256.0), "b" => (65/256.0)},
32 => {"r" => (234/256.0), "g" => (140/256.0), "b" => (133/256.0)},
64 => {"r" => (26/256.0), "g" => (169/256.0), "b" => (11/256.0)},
128 => {"r" => (140/256.0), "g" => (12/256.0), "b" => (177/256.0)},
256 => {"r" => (38/256.0), "g" => (86/256.0), "b" => (241/256.0)},
512 => {"r" => (237/256.0), "g" => (18/256.0), "b" => (193/256.0)},
1024 => {"r" => (236/256.0), "g" => (119/256.0), "b" => (15/256.0)},
2048 => {"r" => (239/256.0), "g" => (17/256.0), "b" => (22/256.0)},
2049 => {"r" => (1.0), "g" => (1.0), "b" => (1.0)}
}
signal_connect "draw" do
on_draw
end
init_game
end
def on_timer
# if @inGame
queue_draw
@inGame = [email protected]_game
true
# else
# false
# end
end
def init_game
@inGame = true
GLib::Timeout.add(DELAY) { on_timer }
end
def on_draw
cr = window.create_cairo_context
if @inGame
draw_objects cr
else
game_over cr
end
end
def draw_objects cr
pole = @game2048.pole
xx = [0, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048]
0.upto(3){|i|
0.upto(3){|j|
kx = @x + (@width + @interval) * j
ky = @y + @pole_rezultat + (@height + @interval) * i
color_cell = pole[i][j] > 2048 ? 2049 : pole[i][j]
cr.set_source_rgb @colors_rgb[color_cell]["r"], @colors_rgb[color_cell]["g"], @colors_rgb[color_cell]["b"]
cr.rectangle kx, ky, @width, @height
cr.fill
if pole[i][j] != 0
cr.set_source_rgb 0.1, 0.1, 0.1
cr.select_font_face "Purisa", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL
cr.set_font_size 25
str = pole[i][j].to_s
extents = cr.text_extents str
cr.move_to (kx + @width/2 - extents.width/2), (ky + @height/2 + extents.height/2)
cr.show_text str
end
}
}
cr.select_font_face "Arial", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL
cr.set_font_size 17
cr.set_source_rgb 0.1, 0.1, 0.1
cr.move_to @x, (@y + @pole_rezultat - 5)
cr.show_text "Счет: #{@game2048.schet}"
end
def game_over cr
draw_objects cr
w = allocation.width / 2
h = allocation.height / 2
cr.set_font_size 15
te = cr.text_extents "Game Over"
cr.set_source_rgb 114/256.0, 121/256.0, 115/256.0
cr.rectangle te.width, h-20, te.width+10, 25
cr.fill
cr.set_source_rgb 0, 0, 0
cr.move_to w - te.width/2, h
cr.show_text "Game Over"
end
def on_key_down event
key = event.keyval
napravlenie = case key
when 65361 #Gdk::Keyval::GDK_KEY_Left
1
when 65363 #Gdk::Keyval::GDK_KEY_Right
3
when 65362 #Gdk::Keyval::GDK_KEY_Up
5
when 65364 #Gdk::Keyval::GDK_KEY_Down
2
when 78, 110 #Gdk::Keyval::GDK_KEY_N, Gdk::Keyval::GDK_KEY_n
@game2048.new_game
0
else
0
end
if napravlenie != 0
@game2048.gtk_dvigenie_add_znachenie napravlenie
end
end
end