-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtetris_frame.rb
More file actions
120 lines (91 loc) · 2.6 KB
/
tetris_frame.rb
File metadata and controls
120 lines (91 loc) · 2.6 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
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
require 'java'
require './tetris_data'
require './block'
import java.awt.Color
import java.awt.image.BufferedImage
import java.awt.event.KeyListener
import java.awt.event.KeyEvent
import java.awt.event.ActionListener
import java.io.File
import javax.swing.JFrame
import javax.swing.Timer
import javax.imageio.ImageIO
class TetrisFrame < JFrame
include KeyListener
def initialize(title, x, y)
super title
set_default_close_operation JFrame::EXIT_ON_CLOSE
add_key_listener self
init_frame x, y
@x_size = x
@y_size = y
@read_image = ImageIO.read File.new("block.bmp")
@td = TetrisData.new x, y
@block_datas = Array.new
@block_datas << [1, 1, [[0, -1], [1, 0], [0, 1]], 4] # T
@block_datas << [1, 1, [[0, -1], [0, 1], [1, 1]], 4] # L
@block_datas << [1, 1, [[0, -1], [0, 1], [-1, 1]], 4] # reverse L
@block_datas << [1, 1, [[0, -1], [1, -1], [1, 0]], 0] # square
@block_datas << [1, 1, [[0, -1], [0, 1], [0, 2]], 2] # tetris
@block_datas << [1, 1, [[0, -1], [1, 0], [1, 1]], 2] # key
@block_datas << [1, 1, [[0, -1], [-1, 0], [-1, 1]], 2] # reverse key
@down_count = 100
next_block
down_timer = Timer.new(10, nil)
down_timer.add_action_listener do |e|
@down_count = @down_count - 1
if @down_count <= 0
if !@td.down_block @block
@td.vanish_down_proc
next_block
end
repaint
@down_count = 100
end
end
down_timer.start
end
def init_frame(x, y)
set_bounds 200, 200, x * 20, y * 20
end
def keyPressed(e)
case e.get_key_code
when KeyEvent::VK_RIGHT
@td.right_block @block
when KeyEvent::VK_LEFT
@td.left_block @block
when KeyEvent::VK_DOWN
@down_count = 0
when KeyEvent::VK_UP
@td.rotate_block @block
end
repaint
end
def keyReleased(e)
end
def next_block
block_data = @block_datas[rand(@block_datas.size)]
@block = Block.new(block_data[0], block_data[1], block_data[2], block_data[3])
@td.set_block @block
end
def update(g)
paint(g)
end
def paint(g)
off_i = create_image(@x_size*20, @y_size*20)
off_g = off_i.get_graphics
off_g.set_background Color::PINK
off_g.clear_rect 0, 0, get_width, get_height
@td.image_table.each_with_index do |ex, x|
ex.each_with_index do |ey, y|
if x == 0 or x == @x_size + 1 or y == @y_size + 1
next
end
if ey == TetrisData::BLOCK_UNFIXED
off_g.draw_image @read_image, (x-1) * 20, y * 20, self
end
end
end
g.draw_image off_i, 0, 0, self
end
end