浏览代码

Allow changing speed

master
Dylan Baker 4 年前
父节点
当前提交
baa0543774
共有 3 个文件被更改,包括 69 次插入7 次删除
  1. 20
    5
      src/Board.tsx
  2. 20
    1
      src/Controls.tsx
  3. 29
    1
      src/index.css

+ 20
- 5
src/Board.tsx 查看文件

12
 interface State {
12
 interface State {
13
   grid: boolean[][];
13
   grid: boolean[][];
14
   running: boolean;
14
   running: boolean;
15
+  speed: number;
15
 }
16
 }
16
 
17
 
17
 class Board extends React.Component<Props, State> {
18
 class Board extends React.Component<Props, State> {
19
 
20
 
20
   constructor(props: Props) {
21
   constructor(props: Props) {
21
     super(props);
22
     super(props);
22
-    const height = props.height ?? 5;
23
-    const width = props.height ?? 5;
23
+    const height = props.height ?? 10;
24
+    const width = props.height ?? 20;
24
     this.state = {
25
     this.state = {
25
       grid: Array(height).fill(Array(width).fill(false)),
26
       grid: Array(height).fill(Array(width).fill(false)),
26
       running: false,
27
       running: false,
28
+      speed: 500,
27
     };
29
     };
28
     this.timer = null;
30
     this.timer = null;
29
   }
31
   }
56
       });
58
       });
57
 
59
 
58
       this.setState({ grid: newGrid });
60
       this.setState({ grid: newGrid });
59
-    }, 500);
61
+    }, this.state.speed);
60
   };
62
   };
61
 
63
 
62
   stop = () => {
64
   stop = () => {
77
     this.setState({ running: !this.state.running });
79
     this.setState({ running: !this.state.running });
78
   };
80
   };
79
 
81
 
82
+  updateSpeed = (speed: number) => {
83
+    this.setState({ speed });
84
+    if (this.state.running) {
85
+      this.stop();
86
+      this.start();
87
+    }
88
+  };
89
+
80
   render() {
90
   render() {
81
-    const { grid, running } = this.state;
91
+    const { grid, running, speed } = this.state;
82
 
92
 
83
     return (
93
     return (
84
       <div className="board">
94
       <div className="board">
85
-        <Controls running={running} toggle={this.toggleRunning} />
95
+        <Controls
96
+          running={running}
97
+          speed={speed}
98
+          toggle={this.toggleRunning}
99
+          updateSpeed={this.updateSpeed}
100
+        />
86
         {grid.map((row, y) => (
101
         {grid.map((row, y) => (
87
           <div className="row" key={y}>
102
           <div className="row" key={y}>
88
             {row.map((cell, x) => (
103
             {row.map((cell, x) => (

+ 20
- 1
src/Controls.tsx 查看文件

2
 
2
 
3
 interface Props {
3
 interface Props {
4
   running: boolean;
4
   running: boolean;
5
+  speed: number;
5
   toggle: () => void;
6
   toggle: () => void;
7
+  updateSpeed: (speed: number) => void;
6
 }
8
 }
7
 
9
 
8
 const Controls = (props: Props) => {
10
 const Controls = (props: Props) => {
9
-  const { running, toggle } = props;
11
+  const { running, speed, toggle, updateSpeed } = props;
12
+
13
+  const goSlower = () => {
14
+    updateSpeed(speed + 100);
15
+  };
16
+
17
+  const goFaster = () => {
18
+    if (speed > 100) {
19
+      updateSpeed(speed - 100);
20
+    }
21
+  };
10
 
22
 
11
   return (
23
   return (
12
     <div className="controls">
24
     <div className="controls">
13
       <button onClick={toggle}>{running ? 'Pause' : 'Play'}</button>
25
       <button onClick={toggle}>{running ? 'Pause' : 'Play'}</button>
26
+      <div className="speed">
27
+        <button onClick={goSlower}>Slower</button>
28
+        <button disabled={speed <= 100} onClick={goFaster}>
29
+          Faster
30
+        </button>
31
+        <span>{speed}ms</span>
32
+      </div>
14
     </div>
33
     </div>
15
   );
34
   );
16
 };
35
 };

+ 29
- 1
src/index.css 查看文件

63
 .controls button {
63
 .controls button {
64
   background: transparent;
64
   background: transparent;
65
   border: 1px solid black;
65
   border: 1px solid black;
66
+  border-right: none;
66
   cursor: pointer;
67
   cursor: pointer;
67
   flex: 1;
68
   flex: 1;
68
   font-size: 18px;
69
   font-size: 18px;
69
   padding: 0.25em 0;
70
   padding: 0.25em 0;
70
 }
71
 }
71
 
72
 
72
-.controls button:hover {
73
+.controls button:last-child,
74
+.controls .speed button:last-of-type {
75
+  border-right: 1px solid black;
76
+}
77
+
78
+.controls button:not([disabled]):hover {
73
   background: black;
79
   background: black;
74
   color: white;
80
   color: white;
75
 }
81
 }
82
+
83
+.controls .speed {
84
+  align-items: center;
85
+  display: flex;
86
+  flex: 1;
87
+  justify-content: center;
88
+}
89
+
90
+.controls .speed button,
91
+.controls .speed span {
92
+  flex: 1;
93
+}
94
+
95
+.controls .speed span {
96
+  font-weight: bold;
97
+  text-align: center;
98
+}
99
+
100
+.controls .speed button[disabled] {
101
+  cursor: not-allowed;
102
+  opacity: 0.6;
103
+}

正在加载...
取消
保存