-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtetris_auto_controller.e
157 lines (141 loc) · 5.15 KB
/
tetris_auto_controller.e
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
note
description : "Automatic playing."
author : "Louis Marchand"
date : "July 19 2012"
revision : "1.0"
class
TETRIS_AUTO_CONTROLLER
inherit
TETRIS_CONTROLLER
rename
make as make_tetris
export
{MENU_CONTROLLER} screen_surface
undefine
update_screen
redefine
on_iteration,
change_current_tetromino,
game_over
end
create
make
feature {NONE} -- Initialization
make(l_init_ctrl:INIT_CONTROLLER; l_theme_ctrl:THEME_CONTROLLER; l_lib_ctrl:GAME_LIB_CONTROLLER;alpha:NATURAL_8)
-- Initialization for `Current' loading configuration from `l_init_ctrl',
-- theme from `l_heme_ctrl', using `l_lib_ctrl' as game library and
-- `alpha' as global transparency value.
local
ghost_alpha:INTEGER
do
init_ctrl:=l_init_ctrl
theme_ctrl:=l_theme_ctrl
lib_ctrl:=l_lib_ctrl
event_controller:=l_lib_ctrl.event_controller
create blocks_surface.make (l_theme_ctrl.blocks_file_name)
create screen_surface.make (lib_ctrl.screen_surface.width, lib_ctrl.screen_surface.height)
screen_surface.set_overall_alpha_value (255)
create bg_surface.make_with_alpha (theme_ctrl.bg_file_name)
create tetrominos_fact.make_with_alpha (blocks_surface, theme_ctrl.block_width, theme_ctrl.block_height,theme_ctrl.block_rotation,alpha)
if init_ctrl.is_ghost_show then
ghost_alpha:=(theme_ctrl.ghost_alpha//(255//alpha.to_integer_32)).to_natural_8
create tetrominos_fact_ghost.make_with_alpha (blocks_surface, theme_ctrl.block_width, theme_ctrl.block_height,theme_ctrl.block_rotation,ghost_alpha.to_natural_8)
end
create pfield.make (theme_ctrl.playfield_x.to_integer_32, theme_ctrl.playfield_y.to_integer_32, theme_ctrl.block_width, theme_ctrl.block_height)
create rnd_bag.make(lib_ctrl,1,7)
init_currents_tetrominos
is_init:=false
is_game_over:=false
create mem
bg_surface.set_overall_alpha_value (alpha)
event_controller.on_iteration.extend(agent on_iteration)
down_delay:=200
auto_move_delay:=100
auto_move_tick:=lib_ctrl.get_ticks
lib_ctrl.generate_new_random
auto_next_tetromino_position:=lib_ctrl.last_random_integer_between (1, 7)
lib_ctrl.generate_new_random
auto_left_tetromino_rotation:=lib_ctrl.last_random_integer_between (1, 7)
lib_ctrl.generate_new_random
auto_right_tetromino_rotation:=lib_ctrl.last_random_integer_between (1, 7)
end
feature -- Access
print_screen
-- Show `Current'
do
screen_surface.fill_rect (create {GAME_COLOR}.make_rgb(0,0,0), 0, 0, lib_ctrl.screen_surface.width, lib_ctrl.screen_surface.height)
screen_surface.draw_surface (bg_surface, 0, 0)
if init_ctrl.is_ghost_show then
update_ghost
pfield.print_playfield_with_tetromino_and_ghost (currents_tetrominos.first,current_tetromino_ghost, screen_surface)
else
pfield.print_playfield_with_tetromino (currents_tetrominos.first,screen_surface)
end
if theme_ctrl.hold_field_show and holded_tetromino/=Void then
holded_tetromino.print_on_surface (screen_surface, theme_ctrl.hold_field_x, theme_ctrl.hold_field_y)
end
if theme_ctrl.next_field_show then
print_next_field
end
end
feature {NONE} -- Implementation - Routines
on_iteration
-- <Precursor>
do
if lib_ctrl.get_ticks>auto_move_tick+auto_move_delay then
if auto_left_tetromino_rotation>0 then
rotate_left
auto_left_tetromino_rotation:=auto_left_tetromino_rotation-1
elseif auto_right_tetromino_rotation>0 then
rotate_right
auto_right_tetromino_rotation:=auto_right_tetromino_rotation-1
elseif not right_pressed and then auto_next_tetromino_position>currents_tetrominos.first.x then
start_move_right
elseif not left_pressed and then auto_next_tetromino_position<currents_tetrominos.first.x then
start_move_left
elseif right_pressed and then auto_next_tetromino_position<=currents_tetrominos.first.x then
right_pressed:=false
elseif left_pressed and then auto_next_tetromino_position>=currents_tetrominos.first.x then
left_pressed:=false
else
down_pressed:=true
end
auto_move_tick:=lib_ctrl.get_ticks
end
precursor
end
change_current_tetromino
-- <Precursor>
do
precursor
lib_ctrl.generate_new_random
auto_next_tetromino_position:=lib_ctrl.last_random_integer_between (1, 7)
lib_ctrl.generate_new_random
auto_left_tetromino_rotation:=lib_ctrl.last_random_integer_between (0, 3)
lib_ctrl.generate_new_random
auto_right_tetromino_rotation:=lib_ctrl.last_random_integer_between (0, 3)
end
update_screen
-- Do nothing
do
end
game_over
-- <Precursor>
do
create pfield.make (theme_ctrl.playfield_x.to_integer_32, theme_ctrl.playfield_y.to_integer_32, theme_ctrl.block_width, theme_ctrl.block_height)
mem.full_collect
end
feature {NONE} -- Implementation - Variables
auto_next_tetromino_position:INTEGER
-- Next position to place the {TETROMINO}
auto_left_tetromino_rotation:INTEGER
-- Left position to place the {TETROMINO}
auto_right_tetromino_rotation:INTEGER
-- Right position to place the {TETROMINO}
auto_down_delay:NATURAL
-- Delay of the falling {TETROMINO}
auto_move_delay:NATURAL
-- Delay of the automatic user movement
auto_move_tick:NATURAL
-- FPS of the automatic user movement
end