You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+43-25Lines changed: 43 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Angular Tetris
2
2
3
-
A child-hood memory Tetris game built with Angular 10 and Akita.
3
+
A childhood memory Tetris game built with Angular 10 and Akita.
4
4
5
5
## Working Game
6
6
@@ -21,47 +21,47 @@ Thanks a bunch for stopping by and supporting me!
21
21
22
22
## Why?
23
23
24
-
Tetris was the first game that my dad bought to me and It costed about 1$ US at that time. It didn't sound a lot today. But 20 years ago, 1$ can feed my family for at least few days. Put it that way, with 1\$ you can buy 2 dozens of eggs.
25
-
This is the only one gaming "machine" that I ever had until my first computer arrived. I have never had a SNES or PS1 at home.
24
+
Tetris was the first game that my dad bought for me and It cost about 1$ US at that time. It didn't sound a lot today. But 20 years ago, 1$ can feed my family for at least a few days. Put it that way, with 1\$ you can buy 2 dozens eggs.
25
+
This is the only gaming "machine" that I ever had until my first computer arrived. I have never had a SNES or PS1 at home.
26
26
27
27
My Tetris was exactly in the same yellow color and it was so big, running on 2 AA battery. It is how it looks.
28
28
29
29
![Retro Tetris][tetris]
30
30
31
31
After showing my wife the [Tetris game built with Vue][vue]. She told me why didn't I build the same <u>Tetris with Angular</u>? And here you go.
32
32
33
-
> I designed the game to hold maximum score of 999999 (one million minus one 😂) and I have never reached to that very end. Please [tweet][tweetmax] your screenshot together with hashtag `#angulartetris` and tag my name as well `@tuantrungvo`.
33
+
> I designed the game to hold a maximum score of 999999 (one million minus one 😂) and I have never reached that very end. Please [tweet][tweetmax] your screenshot together with hashtag `#angulartetris` and tag my name as well `@tuantrungvo`.
34
34
>
35
-
> The **first five** amazing gamer that reached to 999999 points will received a <u>free gift</u>
35
+
> The **first five** amazing gamer that reached to 999999 points will receive a <u>free gift</u>
- For anyone that grew up with Tetris as a part of your memory. It was definitely my childhood memory and I hope you enjoy the game as well.
44
-
- For Angular developer community, I have never really see a game that built with Angular and that's my answer. Using Akita as the underlying state management really helps me to see all of the data flow, it is great for debugging. I wanted to see more Angular game from you guys 💪💪💪
43
+
- For anyone that grew up with Tetris as a part of your memory. It was my childhood memory and I hope you enjoy the game as well.
44
+
- For the Angular developer community, I have never really seen a game that built with Angular and that's my answer. Using Akita as the underlying state management helps me to see all of the data flow, it is great for debugging. I wanted to see more Angular game from you guys 💪💪💪
45
45
46
46
## How to play
47
47
48
48
### Before playing
49
49
50
50
- You can use both keyboard and mouse to play. But prefer to use <u>keyboard</u>
51
51
- Press arrow left and right to change the speed of the game **(1 - 6)**. The higher the number, the faster the piece will fall
52
-
- Press arrow up and down to change how many of lines has been filled before starting the game **(1 - 10)**
52
+
- Press arrow up and down to change how many of lines have been filled before starting the game **(1 - 10)**
53
53
- Press `Space` to start the game
54
54
- Press `P` for pause/resume game
55
-
- Press `R` for reset the game
56
-
- Press `S` for the turn on/off music
55
+
- Press `R` for resetting the game
56
+
- Press `S` for the turn on/off the sounds
57
57
58
58
### Playing game
59
59
60
60
- Press `Space` make the piece drop quickly
61
61
- Press `Arrow left` and `right` for moving left and right
62
62
- Press `Arrow up` to rotate the piece
63
63
- Press `Arrow down` to move a piece faster
64
-
- When clearing lines, you will received a point - 100 points for 1 line, 300 points for 2 lines, 700 points for 3 lines, 1500 points for 4 lines
64
+
- When clearing lines, you will receive a point - 100 points for 1 line, 300 points for 2 lines, 700 points for 3 lines, 1500 points for 4 lines
65
65
- The drop speed of the pieces increases with the number of rows eliminated (one level up for every 20 lines cleared)
66
66
67
67
## Techstack
@@ -72,25 +72,25 @@ I built it barely with Angular and Akita, no additional UI framework/library was
72
72
73
73
## Development Challenge
74
74
75
-
I got the inspiration from the same same but different [Tetris game built with Vue][vue]. To not reinvented the wheel, I started to look at Vue code and thought it would be very identical to Angular. But later one, I realized a few catches:
75
+
I got the inspiration from the same but different [Tetris game built with Vue][vue]. To not reinvented the wheel, I started to look at Vue code and thought it would be very identical to Angular. But later one, I realized a few catches:
76
76
77
-
- The Vue source code was written few years ago with pure JS. I could find several problems that the complier didn't tell you. Such as giving `parseInt` a number. It is still working though, but I don't like it.
78
-
- There was an extensively uses of `setTimeout` and `setInterval` for making animations. I rewrote all of the animation logic using RxJS. You will see the detail below.
79
-
- The brain of the game also used `setTimeout` for the game loop. It was not a problem, but I was having a <u>hard time</u> understanding the code on some essential elements: how to render the piece to the UI, how the calculation make senses with xy axis. In the end, I changed all of the logic to a proper OOP way using TypeScript class, based on [@chrum/ngx-tetris][ngx-tetris].
77
+
- The Vue source code was written a few years ago with pure JS. I could find several problems that the compiler didn't tell you. Such as giving `parseInt` a number. It is still working though, but I don't like it.
78
+
- There was extensive use of `setTimeout` and `setInterval` for making animations. I rewrote all of the animation logic using RxJS. You will see the detail below.
79
+
- The brain of the game also used `setTimeout` for the game loop. It was not a problem, but I was having a <u>hard time</u> understanding the code on some essential elements: how to render the piece to the UI, how the calculation makes sense with XY axis. In the end, I changed all of the logic to a proper OOP way using TypeScript class, based on [@chrum/ngx-tetris][ngx-tetris].
80
80
81
81
### Tetris Core
82
82
83
-
It is the most important part of the game. As I am following the Vue source code, It is getting harder to understand what was the developer's intention. The Vue version gave me the inspiration but I think I have to write the core tetris differently.
83
+
It is the most important part of the game. As I am following the Vue source code, It is getting harder to understand what was the developer's intention. The Vue version inspired me but I think I have to write the core Tetris differently.
84
84
85
-
Take a look at the two block of code below which do the same rendering piece on the screen and you will understand what I am talking about. The left side was rewritten with Angular and TypeScript and the right side was the JS version.
85
+
Take a look at the two blocks of code below which do the same rendering piece on the screen and you will understand what I am talking about. The left side was rewritten with Angular and TypeScript and the right side was the JS version.
86
86
87
87
![Angular Tetris][compare01]
88
88
89
-
I always think that your code must be written as you talk to people, without explaining a word. Otherwise when someone come in and read your code and maintain it, they will be struggling.
89
+
I always think that your code must be written as you talk to people, without explaining a word. Otherwise, when someone comes in and reads your code and maintains it, they will be struggling.
90
90
91
91
> “ Code is like humor. When you have to explain it, it’s bad.” – Cory House
92
92
93
-
And let me emphasize it again, I didn't write the brain of the game from the scratch. I adapted the well-written source by [@chrum/ngx-tetris][ngx-tetris] for tetris core. I did refactor some parts to support Akita and wrote some new functionality as well.
93
+
And let me emphasize it again, I didn't write the brain of the game from scratch. I adapted the well-written source by [@chrum/ngx-tetris][ngx-tetris] for Tetris core. I did refactor some parts to support Akita and wrote some new functionality as well.
94
94
95
95
### Akita state management + dev tool support
96
96
@@ -108,7 +108,7 @@ I turn it on all the time on [tetris.trungk18.com][angular-tetris], you can open
108
108
109
109
### Web Audio API
110
110
111
-
There are many sound effects in the game such as when you press space, or left, right. In reality, all of the sound was reference to a single file [assets/tetris-sound.mp3][sounds].
111
+
There are many sound effects in the game such as when you press space, or left, right. In reality, all of the sounds were a reference to a single file [assets/tetris-sound.mp3][sounds].
112
112
113
113
I don't have much experience working with audio before but the Web Audio API looks very promising. You could do more with it.
114
114
@@ -127,9 +127,21 @@ The actual result doesn't look very identical but it is good enough in my standa
127
127
128
128
![Angular Tetris][compare02-result]
129
129
130
+
### Animation
131
+
132
+
I rewrote the animation with RxJS. See the comparison below for the simple dinosaurs running animation at the beginning of the game.
133
+
134
+
You could do a lot of stuff if you know RxJS well enough :) I think I need to strengthen my RxJS knowledge soon enough as well. Super powerful.
135
+
136
+
![Angular Tetris][compare02]
137
+
138
+
The actual result doesn't look very identical but it is good enough in my standard.
139
+
140
+
![Angular Tetris][compare02-result]
141
+
130
142
### Keyboard handling
131
143
132
-
I planned to use [@ngneat/hotkeys][hotkeys] but I decided to use `@HostListener` instead. A simple implementation could looks like:
144
+
I planned to use [@ngneat/hotkeys][hotkeys] but I decided to use `@HostListener` instead. A simple implementation could look like:
@@ -170,12 +182,14 @@ See more at [containers/angular-tetris/angular-tetris.component.ts][hotkeys-impl
170
182
171
183
## Time spending
172
184
173
-
I was actually still working with [Chau Tran][chautran] on phase two of [Angular Jira clone][jira-clone]and I saw a Tetris game. My wife really want to have a version that I built so that I decided to finish the Angular Tetris first before completing Jira clone phase two.
185
+
I was still working with [Chau Tran][chautran] on phase two of [Angular Jira clone][jira-clone]when I saw that Tetris game built with Vue. My wife wanted to have a version that I built so that I decided to finish the Angular Tetris first before completing Jira clone phase two.
174
186
175
187
According to waka time report, I have spent about 30 hours working on this project. Which is equal to [run a marathon five times][marathon] at my current speed 😩
176
188
177
189
![Angular Tetris][timespending]
178
190
191
+
The flow was easy. I designed a simple [to do list][todolist], then start reading the code in Vue. And start working on the Angular at the same time. Halfway, I start to read [@chrum/ngx-tetris][ngx-tetris] instead of the Vue source. And keep building until I have the final result. 30 hours was a good number. It would take me longer, or lesser. But I enjoyed the experience working on the first-ever game I have built.
|[@Binaryify/vue-tetris][vue]| Vue Tetris, I reused part of HTML, CSS and static assets from that project |
198
212
|[@chrum/ngx-tetris][ngx-tetris]| A comprehensive core Tetris written with Angular, I reused part of that for the brain of the game. |
199
-
|[Game Development: Tetris in Angular][medium]| A detailed excellent article about how to built a complete Tetris game. I didn't check the code but I learned much more from the article |
200
-
|[Super Rotation System][srs]| A standard for how the piece behave. I didn't follow everything but it is good to know as wells|
213
+
|[Game Development: Tetris in Angular][medium]| A detailed excellent article about how to build a complete Tetris game. I didn't check the code but I learned much more from the article |
214
+
|[Super Rotation System][srs]| A standard for how the piece behaves. I didn't follow everything but it is good to know as wells |
201
215
202
216
## Contributing
203
217
@@ -211,19 +225,22 @@ Feel free to use my code on your project. It would be great if you put a referen
0 commit comments