-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplay.php
More file actions
103 lines (91 loc) · 2.32 KB
/
play.php
File metadata and controls
103 lines (91 loc) · 2.32 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
<?php
use Tetris\Game;
use Tetris\Matrix;
use Tetris\Vector;
use Tetris\SevenBag;
use Tetris\Direction;
use Tetris\Time\FrameTimer;
use Tetris\Time\SystemClock;
use Tetris\Time\NonBlockingTimer;
use Tetris\Processes\RenderWithCanvas;
use Tetris\Processes\SpawnNewTetrimino;
use Tetris\EventDispatch\DispatchEvents;
use Tetris\Processes\CloseProgramAtGameOver;
use Tetris\UI\Input\NonBlockingKeyboardPlayerInput;
require 'vendor/autoload.php';
/*
* system clock
*/
$clock = new SystemClock();
/*
* start the game
*/
$game = Game::start(
Matrix::withDimensions(
10, 20,
Vector::fromInt(5, 0)
),
new SevenBag(),
new NonBlockingTimer($clock, .8)
);
/*
* frame timer
*/
$frameTimer = new FrameTimer($clock, 20);
$frameTimer->start();
/*
* processes
*/
$events = new DispatchEvents(
[
new RenderWithCanvas(),
new SpawnNewTetrimino($game),
new CloseProgramAtGameOver(),
]
);
/*
* player input
*/
$playerInput = new NonBlockingKeyboardPlayerInput($events);
while (true) {
/*
* WASD = left / right controls
*
* ;' keys (to the right of L = rotate left / right
*/
$pressedKey = $playerInput->pressedKey();
if ($pressedKey) {
switch ($pressedKey) {
case 'a': // dvorak and qwerty left arrow, A in wasd
$game->movePiece(Direction::left());
break;
case 'e': // dvorak right arrow, D in wasd
case 'd': // qwerty right arrow
$game->movePiece(Direction::right());
break;
case 's': // dvorak rotate left
case ';': // qwerty rotate left
$game->rotatePiece(Direction::left());
break;
case '-': // dvorak rotate right
case "'": // qwerty rotate right
$game->rotatePiece(Direction::right());
break;
case ",": // dvorak hard drop
case "w": // qwerty hard drop
$game->hardDrop();
break;
case 'q': // dvorak
case 'x': // qwerty exit
die('exit');
}
}
// process game time
$game->tick();
// dispatch state changes
$events->dispatch(
$game->flushEvents()
);
// sleep until next frame
$frameTimer->waitForNextFrame();
}