-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.rb
More file actions
executable file
·88 lines (71 loc) · 2.29 KB
/
gui.rb
File metadata and controls
executable file
·88 lines (71 loc) · 2.29 KB
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
#! /usr/bin/env ruby
require "gosu"
require_relative "chess"
# http://ixian.com/chess/jin-piece-sets/
class GUIChess < Gosu::Window
def initialize
super(640, 640, false)
@game = Chess.new
@background = Gosu::Image.new(self, './assets/background.png', false)
@white_king = Gosu::Image.new(self, './assets/white_king.png', false)
@white_queen = Gosu::Image.new(self, './assets/white_queen.png', false)
@white_bishop = Gosu::Image.new(self, './assets/white_bishop.png', false)
@white_knight = Gosu::Image.new(self, './assets/white_knight.png', false)
@white_rook = Gosu::Image.new(self, './assets/white_rook.png', false)
@white_pawn = Gosu::Image.new(self, './assets/white_pawn.png', false)
@black_king = Gosu::Image.new(self, './assets/black_king.png', false)
@black_queen = Gosu::Image.new(self, './assets/black_queen.png', false)
@black_bishop = Gosu::Image.new(self, './assets/black_bishop.png', false)
@black_knight = Gosu::Image.new(self, './assets/black_knight.png', false)
@black_rook = Gosu::Image.new(self, './assets/black_rook.png', false)
@black_pawn = Gosu::Image.new(self, './assets/black_pawn.png', false)
end
def coords_to_pixels(pos)
x, y = pos
[x * 80, 640-((y+1) * 80)]
end
def pixels_to_coords(pixel)
x, y = pixel
[(x/80).to_i, ((640-y)/80).to_i]
end
def draw
@background.draw(0, 0, 0)
@game.board.pieces.each do |piece|
sym = ('@'+piece.color.to_s+'_'+piece.class.to_s.downcase).to_sym
image = self.instance_variable_get(sym)
x, y = coords_to_pixels(piece.pos)
image.draw(x, y, 1)
end
end
def needs_cursor?
true
end
def update
# exit if over?
end
def button_down(id)
@start_pos = get_coords # use an ivar to persist state until button_up
end
def button_up(id)
end_pos = get_coords
begin
@game.GUIplay(@start_pos, end_pos)
rescue GameError
end
end
def get_coords
pixels_to_coords([mouse_x, mouse_y])
end
end
class Chess
def GUIplay(start_pos, end_pos)
piece = @board[start_pos]
validate_moving_piece(piece)
validate_destination(piece, end_pos)
@board.move(piece, end_pos)
change_players
end
end
if __FILE__ == $PROGRAM_NAME
GUIChess.new.show
end