-
Notifications
You must be signed in to change notification settings - Fork 1
/
tetris_controller.e
1018 lines (936 loc) · 31.4 KB
/
tetris_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
note
description : "The game engine."
author : "Louis Marchand"
date : "July 19 2012"
revision : "1.0"
class
TETRIS_CONTROLLER
create
make
feature {NONE} -- Initialization
make(l_init_ctrl:INIT_CONTROLLER; l_theme_ctrl:THEME_CONTROLLER; l_lib_ctrl:GAME_LIB_CONTROLLER;l_audio_ctrl:AUDIO_CONTROLLER)
-- Initialization for `Current'.
local
anim_surface:GAME_SURFACE_IMG_FILE
music:AUDIO_SOUND_SND_FILE
do
init_ctrl:=l_init_ctrl
theme_ctrl:=l_theme_ctrl
lib_ctrl:=l_lib_ctrl
audio_ctrl:=l_audio_ctrl
event_controller:=l_lib_ctrl.event_controller
create blocks_surface.make (l_theme_ctrl.blocks_file_name)
screen_surface:=lib_ctrl.screen_surface
create bg_surface.make (theme_ctrl.bg_file_name)
create tetrominos_fact.make (blocks_surface, theme_ctrl.block_width, theme_ctrl.block_height,theme_ctrl.block_rotation)
if init_ctrl.is_ghost_show then
create tetrominos_fact_ghost.make_with_alpha (blocks_surface, theme_ctrl.block_width, theme_ctrl.block_height,theme_ctrl.block_rotation,theme_ctrl.ghost_alpha)
end
if theme_ctrl.lines_anim_show then
create anim_surface.make (theme_ctrl.lines_anim_file_name)
create pfield.make_with_anim (theme_ctrl.playfield_x.to_integer_32, theme_ctrl.playfield_y.to_integer_32, theme_ctrl.block_width, theme_ctrl.block_height,anim_surface,theme_ctrl.lines_anim_delay)
else
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)
end
create rnd_bag.make(lib_ctrl,1,7)
init_currents_tetrominos
is_init:=false
create font.make (theme_ctrl.font_file_name, theme_ctrl.font_size)
is_game_over:=false
create mem
create text_color.make_from_hex_string_rgb (theme_ctrl.font_color)
if init_ctrl.custom_control_enable then
if init_ctrl.custom_control_ctrl.keyboard_enable then
event_controller.on_key_down.extend (agent custom_keyboard_press)
event_controller.on_key_up.extend (agent custom_keyboard_released)
end
if init_ctrl.custom_control_ctrl.joystick_enable then
event_controller.enable_joystick_event
if init_ctrl.custom_control_ctrl.joystick_buttons_enable then
event_controller.on_joystick_button_pressed.extend (agent custom_joystick_button_press)
event_controller.on_joystick_button_released.extend (agent custom_joystick_button_release)
end
if init_ctrl.custom_control_ctrl.joystick_axis_enable then
event_controller.on_joystick_axis_change.extend (agent custom_joystick_axis_change)
end
end
else
event_controller.on_key_down.extend (agent default_key_press)
event_controller.on_key_up.extend (agent default_key_released)
end
event_controller.on_iteration.extend(agent on_iteration)
event_controller.on_quit_signal.extend(agent on_quit)
play_sound:=theme_ctrl.is_sound_enable
if play_sound then
audio_ctrl.add_source
sound_source:=audio_ctrl.last_source
if theme_ctrl.is_sound_game_drop then
create {AUDIO_SOUND_SND_FILE} sound_drop.make(theme_ctrl.sound_game_drop_file)
end
if theme_ctrl.is_sound_game_rotation then
create {AUDIO_SOUND_SND_FILE} sound_rotation.make(theme_ctrl.sound_game_rotation_file)
end
if theme_ctrl.is_sound_game_move then
create {AUDIO_SOUND_SND_FILE} sound_move.make(theme_ctrl.sound_game_move_file)
end
if theme_ctrl.is_sound_game_down then
create {AUDIO_SOUND_SND_FILE} sound_down.make(theme_ctrl.sound_game_down_file)
end
if theme_ctrl.is_sound_game_anim then
create {AUDIO_SOUND_SND_FILE} sound_anim.make(theme_ctrl.sound_game_anim_file)
end
if theme_ctrl.is_sound_game_collapse then
create {AUDIO_SOUND_SND_FILE} sound_collapse.make(theme_ctrl.sound_game_collapse_file)
end
if theme_ctrl.is_music_game then
audio_ctrl.add_source
music_source:=audio_ctrl.last_source
if theme_ctrl.is_music_game_intro then
create music.make (theme_ctrl.music_game_intro_file)
music_source.queue_sound (music)
end
create music.make (theme_ctrl.music_game_loop_file)
music_source.queue_sound_infinite_loop (music)
music_source.play
end
end
down_delay:=1000
is_hold_used:=false
nb_lines:=0
points:=0
anim_in_progress:=false
if theme_ctrl.score_show then
create score_surface.make_blended (points.out, font, text_color)
end
if theme_ctrl.lines_show then
create lines_surface.make_blended (nb_lines.out, font, text_color)
end
if theme_ctrl.level_show then
create level_surface.make_blended (level.out, font, text_color)
end
end
feature -- Access
launch
-- Start the engine of `Current'
do
lib_ctrl.launch
end
resume
-- Resme a paused game
do
lib_ctrl.replace_event_controller (event_controller)
left_pressed:=false
right_pressed:=false
down_pressed:=false
rotate_left_pressed:=false
rotate_right_pressed:=false
drop_pressed:=false
pause_pressed:=false
hold_pressed:=false
if play_sound then
if sound_source.is_pause then
sound_source.play
end
music_source.play
end
end
is_game_over:BOOLEAN
-- `True' when the game finished
last_image_surface:GAME_SURFACE
-- The las image to show
feature {NONE} -- Implementation - Routines
on_iteration
-- Main loop method
do
if anim_in_progress then
cont_anim
else
if not is_init then
down_tick_number:=lib_ctrl.get_ticks
is_init:=true
update_screen
elseif down_pressed and then lib_ctrl.get_ticks>down_tick_number+30 then
down_tick_number:=lib_ctrl.get_ticks
go_down(true)
update_screen
elseif lib_ctrl.get_ticks>down_tick_number+down_delay then
down_tick_number:=lib_ctrl.get_ticks
go_down(true)
update_screen
elseif left_pressed and then lib_ctrl.get_ticks>move_tick_number+move_delay then
move_tick_number:=lib_ctrl.get_ticks
move_delay:=30
move_left
update_screen
elseif right_pressed and then lib_ctrl.get_ticks>move_tick_number+move_delay then
move_tick_number:=lib_ctrl.get_ticks
move_delay:=30
move_right
update_screen
end
end
end
on_quit
-- When the user quit the game.
do
if play_sound then
if sound_source.is_playing then
sound_source.pause
end
music_source.pause
end
lib_ctrl.stop
create last_image_surface.make (screen_surface.width, screen_surface.height)
last_image_surface.draw_surface (bg_surface, 0, 0)
if theme_ctrl.score_show then
print_score(last_image_surface)
end
if theme_ctrl.lines_show then
print_lines(last_image_surface)
end
if theme_ctrl.level_show then
print_level(last_image_surface)
end
end
default_key_press(keyboard_event:GAME_KEYBOARD_EVENT)
-- Manage the keyboard key pressed specified by `keyboard_event'
-- Default hardcoded management
do
if keyboard_event.is_left_key then
start_move_left
elseif keyboard_event.is_right_key then
start_move_right
elseif keyboard_event.is_down_key then
down_pressed:=true
elseif keyboard_event.is_a_key then
rotate_left
elseif keyboard_event.is_s_key then
rotate_right
elseif keyboard_event.is_up_key then
drop
elseif keyboard_event.is_escape_key then
on_quit
elseif keyboard_event.is_tab_key then
hold
end
update_screen
end
custom_keyboard_press(keyboard_event:GAME_KEYBOARD_EVENT)
-- Manage the keyboard key pressed specified by `keyboard_event'
-- Custom input management specified in `init_ctrl'.`custom_control_ctrl'
do
if not left_pressed and keyboard_event.scancode = init_ctrl.custom_control_ctrl.keyboard_game_left then
start_move_left
update_screen
elseif not right_pressed and keyboard_event.scancode = init_ctrl.custom_control_ctrl.keyboard_game_right then
start_move_right
update_screen
elseif not down_pressed and keyboard_event.scancode = init_ctrl.custom_control_ctrl.keyboard_game_down then
down_pressed:=true
elseif not rotate_left_pressed and keyboard_event.scancode = init_ctrl.custom_control_ctrl.keyboard_game_rotate_left then
rotate_left
update_screen
rotate_left_pressed:=true
elseif not rotate_right_pressed and keyboard_event.scancode = init_ctrl.custom_control_ctrl.keyboard_game_rotate_right then
rotate_right
update_screen
rotate_right_pressed:=true
elseif not drop_pressed and keyboard_event.scancode = init_ctrl.custom_control_ctrl.keyboard_game_drop then
drop
update_screen
drop_pressed:=true
elseif not pause_pressed and keyboard_event.scancode = init_ctrl.custom_control_ctrl.keyboard_game_pause then
on_quit
pause_pressed:=true
elseif not hold_pressed and keyboard_event.scancode = init_ctrl.custom_control_ctrl.keyboard_game_hold then
hold
update_screen
hold_pressed:=true
end
end
custom_joystick_button_press(button_id,device_id:NATURAL_8)
-- Manage the joystick button pressed specified by `device_id' and `button_id'
-- Custom input management specified in `init_ctrl'.`custom_control_ctrl'
do
if device_id=init_ctrl.custom_control_ctrl.joystick_device_id then
if not left_pressed and button_id = init_ctrl.custom_control_ctrl.joystick_button_game_left then
start_move_left
update_screen
elseif not right_pressed and button_id = init_ctrl.custom_control_ctrl.joystick_button_game_right then
start_move_right
update_screen
elseif not down_pressed and button_id = init_ctrl.custom_control_ctrl.joystick_button_game_down then
down_pressed:=true
elseif not rotate_left_pressed and button_id = init_ctrl.custom_control_ctrl.joystick_button_game_rotate_left then
rotate_left
update_screen
rotate_left_pressed:=true
elseif not rotate_right_pressed and button_id = init_ctrl.custom_control_ctrl.joystick_button_game_rotate_right then
rotate_right
update_screen
rotate_right_pressed:=true
elseif not drop_pressed and button_id = init_ctrl.custom_control_ctrl.joystick_button_game_drop then
drop
update_screen
drop_pressed:=true
elseif not pause_pressed and button_id = init_ctrl.custom_control_ctrl.joystick_button_game_pause then
on_quit
pause_pressed:=true
elseif not hold_pressed and button_id = init_ctrl.custom_control_ctrl.joystick_button_game_hold then
hold
update_screen
hold_pressed:=true
end
end
end
default_key_released(keyboard_event:GAME_KEYBOARD_EVENT)
-- Manage the keyboard key released specified by `keyboard_event'
-- Default hardcoded management
do
if keyboard_event.is_left_key then
left_pressed:=false
elseif keyboard_event.is_right_key then
right_pressed:=false
elseif keyboard_event.is_down_key then
down_pressed:=false
end
end
custom_keyboard_released(keyboard_event:GAME_KEYBOARD_EVENT)
-- Manage the keyboard key released specified by `keyboard_event'
-- Custom input management specified in `init_ctrl'.`custom_control_ctrl'
do
if keyboard_event.scancode = init_ctrl.custom_control_ctrl.keyboard_game_left then
left_pressed:=false
elseif keyboard_event.scancode = init_ctrl.custom_control_ctrl.keyboard_game_right then
right_pressed:=false
elseif keyboard_event.scancode = init_ctrl.custom_control_ctrl.keyboard_game_down then
down_pressed:=false
elseif keyboard_event.scancode = init_ctrl.custom_control_ctrl.keyboard_game_rotate_left then
rotate_left_pressed:=false
elseif keyboard_event.scancode = init_ctrl.custom_control_ctrl.keyboard_game_rotate_right then
rotate_right_pressed:=false
elseif keyboard_event.scancode = init_ctrl.custom_control_ctrl.keyboard_game_drop then
drop_pressed:=false
elseif keyboard_event.scancode = init_ctrl.custom_control_ctrl.keyboard_game_pause then
pause_pressed:=false
elseif keyboard_event.scancode = init_ctrl.custom_control_ctrl.keyboard_game_hold then
hold_pressed:=false
end
end
custom_joystick_button_release(button_id,device_id:NATURAL_8)
-- Manage the joystick button released specified by `device_id' and `button_id'
-- Custom input management specified in `init_ctrl'.`custom_control_ctrl'
do
if device_id=init_ctrl.custom_control_ctrl.joystick_device_id then
if button_id = init_ctrl.custom_control_ctrl.joystick_button_game_left then
left_pressed:=false
elseif button_id = init_ctrl.custom_control_ctrl.joystick_button_game_right then
right_pressed:=false
elseif button_id = init_ctrl.custom_control_ctrl.joystick_button_game_down then
down_pressed:=false
elseif button_id = init_ctrl.custom_control_ctrl.joystick_button_game_rotate_left then
rotate_left_pressed:=false
elseif button_id = init_ctrl.custom_control_ctrl.joystick_button_game_rotate_right then
rotate_right_pressed:=false
elseif button_id = init_ctrl.custom_control_ctrl.joystick_button_game_drop then
drop_pressed:=false
elseif button_id = init_ctrl.custom_control_ctrl.joystick_button_game_pause then
pause_pressed:=false
elseif button_id = init_ctrl.custom_control_ctrl.joystick_button_game_hold then
hold_pressed:=false
end
end
end
custom_joystick_axis_change(value:INTEGER_16;axis_id,device_id:NATURAL_8)
-- When a user change the joystick axis `value' specified by `device_id' and `axis_id'
-- Custom input management specified in `init_ctrl'.`custom_control_ctrl'
local
active_state:BOOLEAN
do
if device_id=init_ctrl.custom_control_ctrl.joystick_device_id then
if axis_id=init_ctrl.custom_control_ctrl.joystick_axis_game_left.axis_id then
active_state:= value>init_ctrl.custom_control_ctrl.joystick_axis_game_left.upper_than or
value<init_ctrl.custom_control_ctrl.joystick_axis_game_left.lower_than
if not left_pressed and active_state then
start_move_left
update_screen
else
left_pressed:=active_state
end
end
if axis_id=init_ctrl.custom_control_ctrl.joystick_axis_game_right.axis_id then
active_state:= value>init_ctrl.custom_control_ctrl.joystick_axis_game_right.upper_than or
value<init_ctrl.custom_control_ctrl.joystick_axis_game_right.lower_than
if not right_pressed and active_state then
start_move_right
update_screen
else
right_pressed:=active_state
end
end
if axis_id=init_ctrl.custom_control_ctrl.joystick_axis_game_down.axis_id then
active_state:= value>init_ctrl.custom_control_ctrl.joystick_axis_game_down.upper_than or
value<init_ctrl.custom_control_ctrl.joystick_axis_game_down.lower_than
down_pressed:=active_state
end
if axis_id=init_ctrl.custom_control_ctrl.joystick_axis_game_rotate_left.axis_id then
active_state:= value>init_ctrl.custom_control_ctrl.joystick_axis_game_rotate_left.upper_than or
value<init_ctrl.custom_control_ctrl.joystick_axis_game_rotate_left.lower_than
if not rotate_left_pressed and active_state then
rotate_left
update_screen
rotate_left_pressed:=true
else
rotate_left_pressed:=active_state
end
end
if axis_id=init_ctrl.custom_control_ctrl.joystick_axis_game_rotate_right.axis_id then
active_state:= value>init_ctrl.custom_control_ctrl.joystick_axis_game_rotate_right.upper_than or
value<init_ctrl.custom_control_ctrl.joystick_axis_game_rotate_right.lower_than
if not rotate_right_pressed and active_state then
rotate_right
update_screen
rotate_right_pressed:=true
else
rotate_right_pressed:=active_state
end
end
if axis_id=init_ctrl.custom_control_ctrl.joystick_axis_game_drop.axis_id then
active_state:= value>init_ctrl.custom_control_ctrl.joystick_axis_game_drop.upper_than or
value<init_ctrl.custom_control_ctrl.joystick_axis_game_drop.lower_than
if not drop_pressed and active_state then
drop
update_screen
drop_pressed:=true
else
drop_pressed:=active_state
end
end
if axis_id=init_ctrl.custom_control_ctrl.joystick_axis_game_pause.axis_id then
active_state:= value>init_ctrl.custom_control_ctrl.joystick_axis_game_pause.upper_than or
value<init_ctrl.custom_control_ctrl.joystick_axis_game_pause.lower_than
if not pause_pressed and active_state then
on_quit
pause_pressed:=true
else
pause_pressed:=active_state
end
end
if axis_id=init_ctrl.custom_control_ctrl.joystick_axis_game_hold.axis_id then
active_state:= value>init_ctrl.custom_control_ctrl.joystick_axis_game_hold.upper_than or
value<init_ctrl.custom_control_ctrl.joystick_axis_game_hold.lower_than
if not hold_pressed and active_state then
hold
update_screen
hold_pressed:=true
else
hold_pressed:=active_state
end
end
end
end
rotate_left
-- Rotate left the `currents_tetrominos'
do
if not anim_in_progress then
currents_tetrominos.first.rotate_left
finish_rotation
end
end
rotate_right
-- Rotate right the `currents_tetrominos'
do
if not anim_in_progress then
currents_tetrominos.first.rotate_right
finish_rotation
end
end
start_move_left
-- Begin the left movement of the `currents_tetrominos'
do
if not anim_in_progress then
move_left
move_delay:=300
move_tick_number:=lib_ctrl.get_ticks
left_pressed:=true
end
end
start_move_right
-- move the `currents_tetrominos' right
do
if not anim_in_progress then
move_right
move_delay:=300
move_tick_number:=lib_ctrl.get_ticks
right_pressed:=true
end
end
drop
-- Move to the bottom the `currents_tetrominos'
local
old_y:INTEGER
do
if not anim_in_progress then
from
old_y:=currents_tetrominos.first.y+1
until
not (old_y>currents_tetrominos.first.y)
loop
old_y:=currents_tetrominos.first.y
go_down(false)
end
end
end
hold
-- Validate the `hold_tetromino' execution.
do
if theme_ctrl.hold_field_show and not anim_in_progress then
hold_tetromino
end
end
finish_rotation
-- Manage exception when a rotaion has been used.
do
from
currents_tetrominos.first.wall_kick
until
not pfield.detect_collision (currents_tetrominos.first) or
currents_tetrominos.first.wall_kick_exhausted
loop
currents_tetrominos.first.wall_kick
end
if pfield.detect_collision (currents_tetrominos.first) then
currents_tetrominos.first.cancel_last_move
end
update_ghost
if play_sound and then theme_ctrl.is_sound_game_rotation then
sound_source.stop
sound_rotation.restart
sound_source.queue_sound (sound_rotation)
sound_source.play
end
end
move_left
-- Move the `currents_tetrominos' left
do
if not anim_in_progress then
currents_tetrominos.first.move_left
finish_move
end
end
move_right
-- Move the `currents_tetrominos' right
do
if not anim_in_progress then
currents_tetrominos.first.move_right
finish_move
end
end
finish_move
-- Manage exception when a moving has been used.
do
if pfield.detect_collision (currents_tetrominos.first) then
currents_tetrominos.first.cancel_last_move
else
update_ghost
if play_sound and then theme_ctrl.is_sound_game_move then
sound_source.stop
sound_move.restart
sound_source.queue_sound (sound_move)
sound_source.play
end
end
end
go_down(down_sound:BOOLEAN)
-- Move one block down the `currents_tetrominos'
do
if not anim_in_progress then
currents_tetrominos.first.go_down
if pfield.detect_collision (currents_tetrominos.first) then
currents_tetrominos.first.cancel_last_move
pfield.freeze_tetromino (currents_tetrominos.first)
valid_lines
if not anim_in_progress then
change_current_tetromino
down_tick_number:=lib_ctrl.get_ticks
end
else
if play_sound and then down_sound and theme_ctrl.is_sound_game_down then
sound_source.stop
sound_down.restart
sound_source.queue_sound (sound_down)
sound_source.play
end
end
end
end
valid_lines
-- Look for full line
local
new_delay:INTEGER
do
pfield.check_full_lines
if pfield.nb_full_lines/=0 then
if pfield.nb_full_lines=1 then
points:=points+100*level
elseif pfield.nb_full_lines=2 then
points:=points+300*level
elseif pfield.nb_full_lines=3 then
points:=points+500*level
else
points:=points+800*level
end
if points>999999999 then
points:=999999999
end
nb_lines:=nb_lines+pfield.nb_full_lines
if nb_lines>999999999 then
nb_lines:=999999999
end
if theme_ctrl.score_show then
create score_surface.make_blended (points.out, font, text_color)
end
if theme_ctrl.lines_show then
create lines_surface.make_blended (nb_lines.out, font, text_color)
end
if theme_ctrl.level_show then
create level_surface.make_blended (level.out, font, text_color)
end
new_delay:=1050-(50*level)
if new_delay<=0 then
new_delay:=50
end
down_delay:=new_delay.to_natural_32
if theme_ctrl.lines_anim_show then
pfield.prepare_anim
anim_in_progress:=true
anim_start:=lib_ctrl.get_ticks
if play_sound and then theme_ctrl.is_sound_game_anim then
sound_source.stop
sound_anim.restart
sound_source.queue_sound (sound_anim)
sound_source.play
end
else
if play_sound and then theme_ctrl.is_sound_game_collapse then
sound_source.stop
sound_collapse.restart
sound_source.queue_sound (sound_collapse)
sound_source.play
end
pfield.delete_full_line
mem.full_collect
end
else
if play_sound and then theme_ctrl.is_sound_game_drop then
sound_source.stop
sound_drop.restart
sound_source.queue_sound (sound_drop)
sound_source.play
end
end
end
cont_anim
-- Next step of the current animation.
local
old_value:NATURAL
l_ticks:NATURAL
do
l_ticks:=lib_ctrl.get_ticks
if l_ticks>=anim_start+(theme_ctrl.lines_anim_delay*theme_ctrl.lines_anim_step) then
anim_current_value:=theme_ctrl.lines_anim_delay
update_screen
anim_in_progress:=false
pfield.delete_full_line
mem.full_collect
if play_sound and then theme_ctrl.is_sound_game_collapse then
sound_source.stop
sound_collapse.restart
sound_source.queue_sound (sound_collapse)
sound_source.play
end
change_current_tetromino
update_screen
down_tick_number:=lib_ctrl.get_ticks
else
old_value:=anim_current_value
anim_current_value:=(l_ticks-anim_start)//theme_ctrl.lines_anim_step
if old_value<=pfield.med_anim_value and anim_current_value>pfield.med_anim_value then
pfield.remove_full_lines_block
end
update_screen
end
end
update_ghost
-- Move the {TETRONIMO} ghost to adapt to the new position of the `currents_tetrominos'
do
if init_ctrl.is_ghost_show then
from
current_tetromino_ghost.copy_state_from_other (currents_tetrominos.first)
until
pfield.detect_collision (current_tetromino_ghost)
loop
current_tetromino_ghost.go_down
end
current_tetromino_ghost.cancel_last_move
end
end
hold_tetromino
-- Swap the `currents_tetrominos' and the `holded_tetromino'.
-- If there is no `holded_tetromino', put the `holded_tetromino'
-- on the `holded_tetromino' and activate the next {TETROMINO}.
local
swap_tetromino:TETROMINO
do
if not is_hold_used then
if holded_tetromino=Void then
holded_tetromino:=currents_tetrominos.first
holded_tetromino.default_state
change_current_tetromino
else
down_pressed:=false
swap_tetromino:=holded_tetromino
holded_tetromino:=currents_tetrominos.first
currents_tetrominos.start
currents_tetrominos.replace (swap_tetromino)
holded_tetromino.default_state
if init_ctrl.is_ghost_show then
current_tetromino_ghost:=tetrominos_fact_ghost.get_tetromino_by_index (currents_tetrominos.first.index)
end
end
end
is_hold_used:=true
end
change_current_tetromino
-- Switch `currents_tetrominos' to the next {TETROMINO}
local
new_tetromino_index:INTEGER
do
is_hold_used:=false
down_pressed:=false
rnd_bag.pick
new_tetromino_index:=rnd_bag.last_pick
currents_tetrominos.start
currents_tetrominos.remove
currents_tetrominos.extend (tetrominos_fact.get_tetromino_by_index (new_tetromino_index))
if init_ctrl.is_ghost_show then
current_tetromino_ghost:=tetrominos_fact_ghost.get_tetromino_by_index (currents_tetrominos.first.index)
end
if pfield.detect_collision (currents_tetrominos.first) then
game_over
end
end
init_currents_tetrominos
-- Start the `currents_tetrominos' by placing every tetromino on it
-- randomized by a {RANDOM_BAG}
local
new_tetromino_index,i,nb_next:INTEGER
do
if not theme_ctrl.next_field_show then
nb_next:=1
else
nb_next:=theme_ctrl.next_field_nb+1
end
create {LINKED_LIST[TETROMINO]} currents_tetrominos.make
from
i:=1
until
i>nb_next
loop
rnd_bag.pick
new_tetromino_index:=rnd_bag.last_pick
currents_tetrominos.extend (tetrominos_fact.get_tetromino_by_index (new_tetromino_index))
i:=i+1
end
if init_ctrl.is_ghost_show then
current_tetromino_ghost:=tetrominos_fact_ghost.get_tetromino_by_index (currents_tetrominos.first.index)
end
end
update_screen
-- Show the scene of `Current'
do
screen_surface.draw_surface (bg_surface, 0, 0)
if not anim_in_progress then
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
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
if theme_ctrl.score_show then
print_score(screen_surface)
end
if theme_ctrl.lines_show then
print_lines(screen_surface)
end
if theme_ctrl.level_show then
print_level(screen_surface)
end
if anim_in_progress then
pfield.print_playfield_with_anim (screen_surface, anim_current_value)
end
lib_ctrl.flip_screen
end
print_level(target_surface:GAME_SURFACE)
-- Show the level text on `target_surface'.
do
target_surface.draw_surface (level_surface, theme_ctrl.level_x, theme_ctrl.level_y)
end
print_lines(target_surface:GAME_SURFACE)
-- Show the number of lines text on `target_surface'.
do
target_surface.draw_surface (lines_surface, theme_ctrl.lines_x+(theme_ctrl.lines_w - lines_surface.width)//2, theme_ctrl.lines_y)
end
print_score(target_surface:GAME_SURFACE)
-- Show the score on `target_surface'.
do
target_surface.draw_surface (score_surface, theme_ctrl.score_x+(theme_ctrl.score_w - score_surface.width)//2, theme_ctrl.score_y)
end
print_next_field
-- Show the next {BOLCK} on `target_surface'.
local
pos:INTEGER
do
from
pos:=0
currents_tetrominos.start
currents_tetrominos.forth
until currents_tetrominos.exhausted
loop
if theme_ctrl.next_field_vertical then
currents_tetrominos.item.print_on_surface (screen_surface, theme_ctrl.next_field_x, theme_ctrl.next_field_y+pos)
pos:=pos+theme_ctrl.block_width.to_integer_32*2+theme_ctrl.next_field_padding
currents_tetrominos.forth
else
currents_tetrominos.item.print_on_surface (screen_surface, theme_ctrl.next_field_x+pos, theme_ctrl.next_field_y)
if currents_tetrominos.item.index=1 then
pos:=pos+theme_ctrl.block_width.to_integer_32*4+theme_ctrl.next_field_padding
else
pos:=pos+theme_ctrl.block_width.to_integer_32*3+theme_ctrl.next_field_padding
end
currents_tetrominos.forth
end
end
currents_tetrominos.start
end
game_over
-- The game is finished. Stop the engin and clear ressources.
do
update_screen
lib_ctrl.stop
is_game_over:=true
create last_image_surface.make (screen_surface.width, screen_surface.height)
last_image_surface.draw_surface (screen_surface, 0, 0)
end
level:INTEGER
-- Manage current level of the player.
do
Result:=(nb_lines//10)+1
end
feature {NONE} -- Implementation - Variables
mem:MEMORY
-- Manage automatic memory management for optimisation
init_ctrl:INIT_CONTROLLER
-- The loaded configuration of the game
theme_ctrl:THEME_CONTROLLER
-- The visual theme of the game
lib_ctrl:GAME_LIB_CONTROLLER
-- The game lbrary
audio_ctrl:AUDIO_CONTROLLER
-- the audio library
event_controller:GAME_EVENT_CONTROLLER
-- Used to manage input event
rnd_bag:RANDOM_BAG
-- The bag used to randomize {TETROMINO}
blocks_surface:GAME_SURFACE_IMG_FILE
-- A surface used th show a {BLOCK}
bg_surface:GAME_SURFACE_IMG_FILE
-- the image of the background
screen_surface:GAME_SURFACE
-- The output surface of the screen
tetrominos_fact:TETROMINOS_FACTORY
-- A factory used to generate {TETROMINO}
tetrominos_fact_ghost:TETROMINOS_FACTORY
-- A factory used to generate {TETROMINO} ghost
pfield:PLAYFIELD
-- the game field
is_init:BOOLEAN
-- `Current' has been initialised
is_hold_used:BOOLEAN
-- An `hold_tetromino' has been set
current_tetromino_ghost:TETROMINO
-- The ghost of the `currents_tetrominos'
currents_tetrominos:LINKED_LIST[TETROMINO]
-- Every {TETROMINO} to play (firts is the current).
holded_tetromino:TETROMINO
-- The {TETROMINO} that has been put on hold
left_pressed:BOOLEAN
-- Left key has been pressed
right_pressed:BOOLEAN
-- Right key has been pressed
down_pressed:BOOLEAN
-- Down key has been pressed
drop_pressed:BOOLEAN
-- Drop key has been pressed
hold_pressed:BOOLEAN
-- Hold key has been pressed
pause_pressed:BOOLEAN
-- Pause key has been pressed
rotate_left_pressed:BOOLEAN
-- Rotate Left key has been pressed
rotate_right_pressed:BOOLEAN
-- Rotate right key has been pressed
down_tick_number:NATURAL
-- the number of main loop iteration between each down movement.
move_tick_number:NATURAL
-- the number of main loop iteration between each left and right movement.
down_delay:NATURAL
-- The delay to use between each down movement.
move_delay:NATURAL
-- The delay to use between each left and right movement.
nb_lines:INTEGER
-- Number of full line detected
points:INTEGER
-- Number of points (score) of the player.
font:GAME_FONT
-- The font used to draw text on the scene.
text_color:GAME_COLOR
-- The color of every tex to show on the scene
level_surface:GAME_SURFACE_TEXT
-- The text image of the level number
score_surface:GAME_SURFACE_TEXT
-- The text image of the player's score
lines_surface:GAME_SURFACE_TEXT
-- The text image of the number of lines
anim_in_progress:BOOLEAN
-- `True' if there is an animation in progress.
anim_start:NATURAL
-- The first `tick' of the animation
anim_current_value:NATURAL
-- The current `tick' of the animation
sound_source:AUDIO_SOURCE
-- The {AUDIO_SOURCE} used to play individual sound in `Current'
sound_drop:AUDIO_SOUND
-- {AUDIO_SOUND} to play when a drop is used
sound_move:AUDIO_SOUND
-- {AUDIO_SOUND} to play when a move is used