|
| 1 | +# University of Washington, Programming Languages, Homework 6, hw6graphics.rb |
| 2 | + |
| 3 | +# This file provides an interface to a wrapped Tk library. The auto-grader will |
| 4 | +# swap it out to use a different, non-Tk backend. |
| 5 | + |
| 6 | +require 'tk' |
| 7 | + |
| 8 | +class TetrisRoot |
| 9 | + def initialize |
| 10 | + @root = TkRoot.new('height' => 615, 'width' => 205, |
| 11 | + 'background' => 'lightblue') {title "Tetris"} |
| 12 | + end |
| 13 | + |
| 14 | + def bind(char, callback) |
| 15 | + @root.bind(char, callback) |
| 16 | + end |
| 17 | + |
| 18 | + # Necessary so we can unwrap before passing to Tk in some instances. |
| 19 | + # Student code MUST NOT CALL THIS. |
| 20 | + attr_reader :root |
| 21 | +end |
| 22 | + |
| 23 | +class TetrisTimer |
| 24 | + def initialize |
| 25 | + @timer = TkTimer.new |
| 26 | + end |
| 27 | + |
| 28 | + def stop |
| 29 | + @timer.stop |
| 30 | + end |
| 31 | + |
| 32 | + def start(delay, callback) |
| 33 | + @timer.start(delay, callback) |
| 34 | + end |
| 35 | +end |
| 36 | + |
| 37 | +class TetrisCanvas |
| 38 | + def initialize |
| 39 | + @canvas = TkCanvas.new('background' => 'grey') |
| 40 | + end |
| 41 | + |
| 42 | + def place(height, width, x, y) |
| 43 | + @canvas.place('height' => height, 'width' => width, 'x' => x, 'y' => y) |
| 44 | + end |
| 45 | + |
| 46 | + def unplace |
| 47 | + @canvas.unplace |
| 48 | + end |
| 49 | + |
| 50 | + def delete |
| 51 | + @canvas.delete |
| 52 | + end |
| 53 | + |
| 54 | + # Necessary so we can unwrap before passing to Tk in some instances. |
| 55 | + # Student code MUST NOT CALL THIS. |
| 56 | + attr_reader :canvas |
| 57 | +end |
| 58 | + |
| 59 | +class TetrisLabel |
| 60 | + def initialize(wrapped_root, &options) |
| 61 | + unwrapped_root = wrapped_root.root |
| 62 | + @label = TkLabel.new(unwrapped_root, &options) |
| 63 | + end |
| 64 | + |
| 65 | + def place(height, width, x, y) |
| 66 | + @label.place('height' => height, 'width' => width, 'x' => x, 'y' => y) |
| 67 | + end |
| 68 | + |
| 69 | + def text(str) |
| 70 | + @label.text(str) |
| 71 | + end |
| 72 | +end |
| 73 | + |
| 74 | +class TetrisButton |
| 75 | + def initialize(label, color) |
| 76 | + @button = TkButton.new do |
| 77 | + text label |
| 78 | + background color |
| 79 | + command (proc {yield}) |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + def place(height, width, x, y) |
| 84 | + @button.place('height' => height, 'width' => width, 'x' => x, 'y' => y) |
| 85 | + end |
| 86 | +end |
| 87 | + |
| 88 | +class TetrisRect |
| 89 | + def initialize(wrapped_canvas, a, b, c, d, color) |
| 90 | + unwrapped_canvas = wrapped_canvas.canvas |
| 91 | + @rect = TkcRectangle.new(unwrapped_canvas, a, b, c, d, |
| 92 | + 'outline' => 'black', 'fill' => color) |
| 93 | + end |
| 94 | + |
| 95 | + def remove |
| 96 | + @rect.remove |
| 97 | + end |
| 98 | + |
| 99 | + def move(dx, dy) |
| 100 | + @rect.move(dx, dy) |
| 101 | + end |
| 102 | + |
| 103 | +end |
| 104 | + |
| 105 | +def mainLoop |
| 106 | + Tk.mainloop |
| 107 | +end |
| 108 | + |
| 109 | +def exitProgram |
| 110 | + Tk.exit |
| 111 | +end |
0 commit comments