-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw6graphics.rb
111 lines (87 loc) · 2.14 KB
/
hw6graphics.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
# University of Washington, Programming Languages, Homework 6, hw6graphics.rb
# This file provides an interface to a wrapped Tk library. The auto-grader will
# swap it out to use a different, non-Tk backend.
require 'tk'
class TetrisRoot
def initialize
@root = TkRoot.new('height' => 615, 'width' => 205,
'background' => 'lightblue') {title "Tetris"}
end
def bind(char, callback)
@root.bind(char, callback)
end
# Necessary so we can unwrap before passing to Tk in some instances.
# Student code MUST NOT CALL THIS.
attr_reader :root
end
class TetrisTimer
def initialize
@timer = TkTimer.new
end
def stop
@timer.stop
end
def start(delay, callback)
@timer.start(delay, callback)
end
end
class TetrisCanvas
def initialize
@canvas = TkCanvas.new('background' => 'grey')
end
def place(height, width, x, y)
@canvas.place('height' => height, 'width' => width, 'x' => x, 'y' => y)
end
def unplace
@canvas.unplace
end
def delete
@canvas.delete
end
# Necessary so we can unwrap before passing to Tk in some instances.
# Student code MUST NOT CALL THIS.
attr_reader :canvas
end
class TetrisLabel
def initialize(wrapped_root, &options)
unwrapped_root = wrapped_root.root
@label = TkLabel.new(unwrapped_root, &options)
end
def place(height, width, x, y)
@label.place('height' => height, 'width' => width, 'x' => x, 'y' => y)
end
def text(str)
@label.text(str)
end
end
class TetrisButton
def initialize(label, color)
@button = TkButton.new do
text label
background color
command (proc {yield})
end
end
def place(height, width, x, y)
@button.place('height' => height, 'width' => width, 'x' => x, 'y' => y)
end
end
class TetrisRect
def initialize(wrapped_canvas, a, b, c, d, color)
unwrapped_canvas = wrapped_canvas.canvas
@rect = TkcRectangle.new(unwrapped_canvas, a, b, c, d,
'outline' => 'black', 'fill' => color)
end
def remove
@rect.remove
end
def move(dx, dy)
@rect.move(dx, dy)
end
end
def mainLoop
Tk.mainloop
end
def exitProgram
Tk.exit
end