Browse Source

Added levels

Arnaud Vergnet 4 years ago
parent
commit
07d8fb8d15
2 changed files with 63 additions and 5 deletions
  1. 43
    3
      screens/Tetris/GameLogic.js
  2. 20
    2
      screens/Tetris/TetrisScreen.js

+ 43
- 3
screens/Tetris/GameLogic.js View File

@@ -4,6 +4,32 @@ import Tetromino from "./Tetromino";
4 4
 
5 5
 export default class GameLogic {
6 6
 
7
+    static levelTicks = {
8
+        '1': 1000,
9
+        '2': 900,
10
+        '3': 800,
11
+        '4': 700,
12
+        '5': 600,
13
+        '6': 500,
14
+        '7': 400,
15
+        '8': 300,
16
+        '9': 200,
17
+        '10': 150,
18
+    };
19
+
20
+    static levelThresholds = {
21
+        '1': 100,
22
+        '2': 300,
23
+        '3': 500,
24
+        '4': 700,
25
+        '5': 1000,
26
+        '7': 1500,
27
+        '8': 2000,
28
+        '9': 3000,
29
+        '10': 4000,
30
+        '11': 5000,
31
+    };
32
+
7 33
     currentGrid: Array<Array<Object>>;
8 34
 
9 35
     height: number;
@@ -13,6 +39,7 @@ export default class GameLogic {
13 39
     gamePaused: boolean;
14 40
     gameTime: number;
15 41
     score: number;
42
+    level: number;
16 43
 
17 44
     currentObject: Tetromino;
18 45
 
@@ -31,7 +58,6 @@ export default class GameLogic {
31 58
         this.width = width;
32 59
         this.gameRunning = false;
33 60
         this.gamePaused = false;
34
-        this.gameTick = 250;
35 61
         this.colors = colors;
36 62
     }
37 63
 
@@ -165,9 +191,21 @@ export default class GameLogic {
165 191
             this.currentObject.rotate(false);
166 192
     }
167 193
 
194
+    setNewGameTick(level: number) {
195
+        if (level > 10)
196
+            return;
197
+        this.gameTick = GameLogic.levelTicks[level];
198
+        clearInterval(this.gameTickInterval);
199
+        this.gameTickInterval = setInterval(this.onTick, this.gameTick);
200
+    }
201
+
168 202
     onTick(callback: Function) {
169 203
         this.tryMoveTetromino(0, 1);
170
-        callback(this.score, this.getFinalGrid());
204
+        callback(this.score, this.level, this.getFinalGrid());
205
+        if (this.level <= 10 && this.score > GameLogic.levelThresholds[this.level]) {
206
+            this.level++;
207
+            this.setNewGameTick(this.level);
208
+        }
171 209
     }
172 210
 
173 211
     onClock(callback: Function) {
@@ -248,9 +286,11 @@ export default class GameLogic {
248 286
         this.gamePaused = false;
249 287
         this.gameTime = 0;
250 288
         this.score = 0;
289
+        this.level = 1;
290
+        this.gameTick = GameLogic.levelTicks[this.level];
251 291
         this.currentGrid = this.getEmptyGrid();
252 292
         this.createTetromino();
253
-        tickCallback(this.score, this.getFinalGrid());
293
+        tickCallback(this.score, this.level, this.getFinalGrid());
254 294
         clockCallback(this.gameTime);
255 295
         this.onTick = this.onTick.bind(this, tickCallback);
256 296
         this.onClock = this.onClock.bind(this, clockCallback);

+ 20
- 2
screens/Tetris/TetrisScreen.js View File

@@ -16,7 +16,8 @@ type State = {
16 16
     grid: Array<Array<Object>>,
17 17
     gameRunning: boolean,
18 18
     gameTime: number,
19
-    gameScore: number
19
+    gameScore: number,
20
+    gameLevel: number,
20 21
 }
21 22
 
22 23
 class TetrisScreen extends React.Component<Props, State> {
@@ -39,6 +40,7 @@ class TetrisScreen extends React.Component<Props, State> {
39 40
             gameRunning: false,
40 41
             gameTime: 0,
41 42
             gameScore: 0,
43
+            gameLevel: 0,
42 44
         };
43 45
         this.onTick = this.onTick.bind(this);
44 46
         this.onClock = this.onClock.bind(this);
@@ -83,9 +85,10 @@ class TetrisScreen extends React.Component<Props, State> {
83 85
             this.showPausePopup();
84 86
     }
85 87
 
86
-    onTick(score: number, newGrid: Array<Array<Object>>) {
88
+    onTick(score: number, level: number, newGrid: Array<Array<Object>>) {
87 89
         this.setState({
88 90
             gameScore: score,
91
+            gameLevel: level,
89 92
             grid: newGrid,
90 93
         });
91 94
     }
@@ -141,6 +144,7 @@ class TetrisScreen extends React.Component<Props, State> {
141 144
 
142 145
     showGameOverConfirm() {
143 146
         let message = 'SCORE: ' + this.state.gameScore + '\n';
147
+        message += 'LEVEL: ' + this.state.gameLevel + '\n';
144 148
         message += 'TIME: ' + this.state.gameTime + '\n';
145 149
         Alert.alert(
146 150
             'GAME OVER',
@@ -193,6 +197,20 @@ class TetrisScreen extends React.Component<Props, State> {
193 197
                 </View>
194 198
                 <View style={{
195 199
                     flexDirection: 'row',
200
+                    position: 'absolute',
201
+                    top: 50,
202
+                    left: 10,
203
+                }}>
204
+                    <MaterialCommunityIcons
205
+                        name={'gamepad'}
206
+                        color={this.colors.text}
207
+                        size={20}/>
208
+                    <Text style={{
209
+                        marginLeft: 5
210
+                    }}>{this.state.gameLevel}</Text>
211
+                </View>
212
+                <View style={{
213
+                    flexDirection: 'row',
196 214
                     marginRight: 'auto',
197 215
                     marginLeft: 'auto',
198 216
                 }}>

Loading…
Cancel
Save