作品:タイピングゲーム
この回でできるようになること
右から、単語が、流れてきます。左へ消える前に、キーボードで、打ち切る。30 秒で、どれだけ打てるかを競う、タイピングゲームを作ります。文字列を、1文字ずつ扱う方法と、キーボードの入力が、組み合わさります。
let scene = 'title';
let words = ['apple', 'river', 'cloud', 'music', 'tiger', 'green', 'happy', 'light', 'ocean', 'smile', 'bread', 'dance'];
let order = 0;
let word = '';
let wordX = 400;
let typed = 0;
let score = 0;
let misses = 0;
let flash = 0;
let timeLeft = 0;
function setup() {
createCanvas(400, 400);
textFont('monospace');
}
function draw() {
background(30, 30, 60);
if (scene === 'title') {
drawTitle();
} else if (scene === 'play') {
updateGame();
drawGame();
} else if (scene === 'result') {
drawResult();
}
}
function startGame() {
words = shuffle(words);
order = 0;
score = 0;
misses = 0;
flash = 0;
timeLeft = 30 * 60;
nextWord();
}
function nextWord() {
if (order >= words.length) {
words = shuffle(words);
order = 0;
}
word = words[order];
order += 1;
typed = 0;
wordX = 400;
}
function updateGame() {
timeLeft -= 1;
// 単語を流す。スコアが高いほど、速い
wordX -= 1.2 + score * 0.03;
if (wordX < -word.length * 24) {
misses += 1;
flash = 12;
nextWord();
}
if (timeLeft <= 0) {
scene = 'result';
}
}
function drawGame() {
// 間違えた直後は、赤く光らせる
if (flash > 0) {
background(120, 30, 40);
flash -= 1;
}
// 単語を、1文字ずつ描く
textAlign(LEFT, BASELINE);
textSize(40);
for (let i = 0; i < word.length; i++) {
if (i < typed) {
fill(90, 100, 130);
} else if (i === typed) {
fill(255, 220, 80);
} else {
fill(255);
}
text(word[i], wordX + i * 24, 210);
}
// 残り時間の棒
noStroke();
fill(80, 160, 230);
rect(0, 392, 400 * timeLeft / (30 * 60), 8);
fill(255);
textSize(16);
text(`スコア: ${score}`, 16, 28);
text(`ミス: ${misses}`, 160, 28);
text(`残り: ${ceil(timeLeft / 60)}秒`, 300, 28);
}
function drawTitle() {
fill(255);
textAlign(CENTER, CENTER);
textSize(32);
text('タイピングゲーム', 200, 140);
textSize(16);
text('流れてくる単語を、打つ', 200, 200);
text('制限時間は、30秒', 200, 230);
text('スペースキーで、スタート', 200, 290);
}
function drawResult() {
fill(255);
textAlign(CENTER, CENTER);
textSize(32);
text('おしまい', 200, 130);
textSize(22);
text(`スコア: ${score}`, 200, 190);
text(`ミス: ${misses}`, 200, 225);
textSize(16);
text('スペースキーで、タイトルへ', 200, 300);
}
function keyPressed() {
if (scene === 'play') {
if (key.length === 1) {
if (key.toLowerCase() === word[typed]) {
typed += 1;
if (typed === word.length) {
score += word.length;
nextWord();
}
} else {
misses += 1;
flash = 12;
}
}
} else if (key === ' ') {
if (scene === 'title') {
startGame();
scene = 'play';
} else if (scene === 'result') {
scene = 'title';
}
}
}
一度、キャンバスをクリックしてから、スペースキーを押すと、始まります。流れてくる英単語を、キーボードで、打ってください。黄色い文字が、次に打つ文字です。打ち間違えると、画面が赤く光り、ミスが増えます。打ち切る前に、単語が左へ消えても、ミスです。
打てば打つほど、単語の流れが、速くなります。
この回が終わると、次のことができるようになります。
- 文字列を、1文字ずつ取り出して、別々の色で描く
- 押されたキーが、正しい文字かどうかを、判定する
- 「どこまで打ったか」を、数で覚える
- ミスを判定し、画面で知らせる
- 単語を、同じものが続かないように、順番に出す
ゲームを、設計する
タイピングゲームの中心にあるのは、次の問いです。
「いま押されたキーは、次に打つべき文字と、同じか」
これを判定するには、次に打つべき文字が、どれかを、知っている必要があります。そのために、「何文字目まで、打ったか」を、数で覚えておきます。
| 変数 | 意味 | 例(apple の pp まで打ったとき) |
|---|---|---|
word |
いまの単語 | 'apple' |
typed |
打ち終えた文字の数 | 3 |
word[typed] |
次に打つべき文字 | 'l' |
文字列の番号は、0 から始まります。3文字を打ち終えたとき、次に打つのは、番号 3 の文字です。打ち終えた数が、そのまま、次に打つ文字の番号になります。
作る順番は、次のとおりです。
| 順番 | 作るもの |
|---|---|
| 1 | 単語を、右から左へ、流す |
| 2 | 単語を、1文字ずつ描き、打った分の色を変える |
| 3 | キーを判定して、打ち進める |
| 4 | ミスの判定と、赤い光 |
| 5 | 単語を出す順番 |
| 6 | 制限時間と、3つの画面 |
ステップ1:単語を流す
単語を、右の端から、左へ、流します。
let word = 'apple';
let wordX = 400;
function setup() {
createCanvas(400, 400);
textFont('monospace');
}
function draw() {
background(30, 30, 60);
wordX -= 1.5;
if (wordX < -word.length * 24) {
wordX = 400;
}
fill(255);
textSize(40);
text(word, wordX, 210);
}
apple という単語が、右から左へ、流れていきます。左へ消えると、また、右から現れます。
等幅の書体を使う
setup の中で、textFont('monospace'); と、書いています。monospace は、すべての文字が、同じ横幅を持つ書体です。
ふつうの書体では、i は細く、m は太く、文字によって、横幅が違います。等幅の書体なら、どの文字も、同じ幅です。
幅がそろっていると、計算が、簡単になります。この回では、文字の大きさを 40 にして、1文字あたり 24 の幅として、計算します。
| 求めたいもの | 式 |
|---|---|
| 単語の全体の横幅 | word.length * 24 |
i 番目の文字の x 座標 |
wordX + i * 24 |
完全に消えてから、戻す
if (wordX < -word.length * 24) {
wordX = 400;
}
wordX は、単語の左端の x 座標です。単語の右端は、そこから、単語の横幅だけ、右にあります。右端が、キャンバスの左の端(0)より、左へ行ったとき、単語は、完全に、見えなくなっています。それが、wordX < -word.length * 24 です。
ステップ2:1文字ずつ描く
単語を、text(word, ...) で、まとめて描くと、全体が、同じ色になります。打った文字と、これから打つ文字を、色で分けたいので、1文字ずつ、描きます。
for (let i = 0; i < word.length; i++) {
if (i < typed) {
fill(90, 100, 130);
} else if (i === typed) {
fill(255, 220, 80);
} else {
fill(255);
}
text(word[i], wordX + i * 24, 210);
}
word[i] で、i 番目の文字を取り出し、24 ずつ、右へずらして、描きます。
番号と、打ち終えた数を比べる
文字の番号 i と、打ち終えた数 typed を、比べて、色を決めます。
| 条件 | 意味 | 色 |
|---|---|---|
i < typed |
もう打った文字 | 暗い色 |
i === typed |
次に打つ文字 | 黄色 |
| それ以外 | まだ先の文字 | 白 |
if (i < typed) {
fill(90, 100, 130);
} else if (i === typed) {
fill(255, 220, 80);
} else {
fill(255);
}
ためしに、typed を 2 にして、表示してみます。
let word = 'apple';
let wordX = 400;
let typed = 2;
function setup() {
createCanvas(400, 400);
textFont('monospace');
}
function draw() {
background(30, 30, 60);
wordX -= 1.5;
if (wordX < -word.length * 24) {
wordX = 400;
}
textSize(40);
for (let i = 0; i < word.length; i++) {
if (i < typed) {
fill(90, 100, 130);
} else if (i === typed) {
fill(255, 220, 80);
} else {
fill(255);
}
text(word[i], wordX + i * 24, 210);
}
}
ap が暗く、次の p が黄色く、le が白く、表示されます。
試してみよう
3行目の let typed = 2; を、0、4、5 に変えて、色の変わり方を、確かめてください。5 にすると、すべての文字が、暗くなります。黄色い文字は、ありません。i === typed が成り立つ i が、ないからです。
ステップ3:キーを判定する
キーが押されたとき、それが、次に打つべき文字と同じなら、typed を、1 増やします。
function keyPressed() {
if (key === word[typed]) {
typed += 1;
if (typed === word.length) {
score += word.length;
nextWord();
}
}
}
key には、押されたキーの文字が、入っています。word[typed] は、次に打つべき文字です。この2つが等しければ、正解です。
typed が 1 増えると、次のフレームから、黄色い文字が、1つ右へ、移ります。増やしているのは、数1つだけです。色の表示は、draw が、その数を見て、自動で、変えてくれます。
打ち切ったら、次の単語へ
typed が、単語の長さと、等しくなったら、すべての文字を、打ち終えたことになります。
if (typed === word.length) {
score += word.length;
nextWord();
}
スコアに、単語の文字数を足し、次の単語へ、進みます。長い単語ほど、高い点が入ります。
次の単語を出す関数
次の単語へ進むときには、することが、3つ、あります。それを、nextWord という関数に、まとめます。
function nextWord() {
word = random(words);
typed = 0;
wordX = 400;
}
| 行 | すること |
|---|---|
word = random(words); |
単語の配列から、でたらめに、1つ選ぶ |
typed = 0; |
打ち終えた数を、0 に戻す |
wordX = 400; |
位置を、右の端に戻す |
単語が替わったのに、typed を戻し忘れると、新しい単語が、途中まで打たれた状態で、始まってしまいます。単語に関する変数は、まとめて、戻します。
この関数は、2か所から、呼び出されます。打ち切ったときと、左へ消えたときです。関数にしておけば、同じ3行を、2回、書かずに済みます。
let words = ['apple', 'river', 'cloud', 'music', 'tiger', 'green'];
let word = 'apple';
let wordX = 400;
let typed = 0;
let score = 0;
function setup() {
createCanvas(400, 400);
textFont('monospace');
}
function draw() {
background(30, 30, 60);
wordX -= 1.5;
if (wordX < -word.length * 24) {
nextWord();
}
textSize(40);
for (let i = 0; i < word.length; i++) {
if (i < typed) {
fill(90, 100, 130);
} else if (i === typed) {
fill(255, 220, 80);
} else {
fill(255);
}
text(word[i], wordX + i * 24, 210);
}
fill(255);
textSize(16);
text(`スコア: ${score}`, 16, 28);
}
function nextWord() {
word = random(words);
typed = 0;
wordX = 400;
}
function keyPressed() {
if (key === word[typed]) {
typed += 1;
if (typed === word.length) {
score += word.length;
nextWord();
}
}
}
キャンバスをクリックしてから、流れてくる単語を、打ってください。正しいキーを押すと、黄色い文字が、進みます。打ち切ると、スコアが増えて、次の単語が、出てきます。
ステップ4:ミスを判定する
間違ったキーを押しても、いまは、何も起きません。ミスを数えて、画面で、知らせます。
間違いを数える
if に、else を、付けます。
if (key === word[typed]) {
typed += 1;
// ...
} else {
misses += 1;
flash = 12;
}
misses は、ミスの数です。flash は、あとで説明します。
文字ではないキーを、除く
このままでは、困ることが、あります。Shift キーや、矢印キーを押しても、ミスに、数えられてしまいます。
Shift キーを押すと、key には、'Shift' という、5文字の文字列が、入ります。矢印キーなら、'ArrowLeft' です。文字のキーを押したときは、'a' や '5' のように、1文字だけが、入ります。
そこで、key の長さが 1 のときだけ、判定します。
if (key.length === 1) {
// 正しいか、間違いかを、判定する
}
文字のキー以外は、正解でも、ミスでもなく、無視されます。
大文字でも、正解にする
キーボードが、大文字を打つ状態になっていると、a のキーを押しても、key は、'A' になります。'A' と 'a' は、別の文字なので、ミスに、なってしまいます。
押されたキーを、小文字に直してから、比べます。
if (key.toLowerCase() === word[typed]) {
赤く光らせる
ミスをしたことを、画面で、はっきり伝えます。背景を、一瞬、赤くします。
「一瞬」を作るには、残りのフレーム数を、変数で持ちます。
let flash = 0;
| 場所 | すること |
|---|---|
| ミスをしたとき | flash = 12; で、12 フレーム分、光らせると決める |
draw の中 |
flash が 0 より大きければ、背景を赤く塗り、flash を 1 減らす |
if (flash > 0) {
background(120, 30, 40);
flash -= 1;
}
12 フレームは、0.2 秒です。その間だけ、背景が、赤くなります。flash は、1 ずつ減っていき、0 になると、ふつうの背景に、戻ります。
流れ切ったときも、ミス
単語が、打ち切られないまま、左へ消えたときも、ミスにします。
if (wordX < -word.length * 24) {
misses += 1;
flash = 12;
nextWord();
}
let words = ['apple', 'river', 'cloud', 'music', 'tiger', 'green'];
let word = 'apple';
let wordX = 400;
let typed = 0;
let score = 0;
let misses = 0;
let flash = 0;
function setup() {
createCanvas(400, 400);
textFont('monospace');
}
function draw() {
background(30, 30, 60);
wordX -= 1.5;
if (wordX < -word.length * 24) {
misses += 1;
flash = 12;
nextWord();
}
if (flash > 0) {
background(120, 30, 40);
flash -= 1;
}
textSize(40);
for (let i = 0; i < word.length; i++) {
if (i < typed) {
fill(90, 100, 130);
} else if (i === typed) {
fill(255, 220, 80);
} else {
fill(255);
}
text(word[i], wordX + i * 24, 210);
}
fill(255);
textSize(16);
text(`スコア: ${score}`, 16, 28);
text(`ミス: ${misses}`, 160, 28);
}
function nextWord() {
word = random(words);
typed = 0;
wordX = 400;
}
function keyPressed() {
if (key.length === 1) {
if (key.toLowerCase() === word[typed]) {
typed += 1;
if (typed === word.length) {
score += word.length;
nextWord();
}
} else {
misses += 1;
flash = 12;
}
}
}
わざと、違うキーを、押してみてください。画面が赤く光り、ミスの数が、増えます。Shift キーや、矢印キーでは、何も起きません。
クイズ
単語が 'cat' で、typed が 1 のとき、t のキーを押しました。何が起きるでしょう?
▶ 答えを確認する
ミスになります。画面が赤く光り、misses が、1 増えます。typed は、1 のままです。
typed が 1 なので、次に打つべき文字は、word[1]、つまり 'a' です。押された 't' とは、違います。
t は、単語の中に、含まれています。しかし、順番が、違います。判定しているのは、「単語に含まれるか」ではなく、「次の1文字と、同じか」です。
ステップ5:単語を出す順番
random(words) で選ぶと、同じ単語が、続けて出ることが、あります。6つの単語から選ぶなら、6回に1回は、前と同じ単語です。
カードを、よく切って、上から順に、めくっていく方法に、変えます。
words = shuffle(words);
shuffle は、配列の要素を、でたらめな順番に、並べ替えたものを、返します。これを、words に、入れ直します。
何番目まで出したかを、order という変数で、覚えます。
function nextWord() {
if (order >= words.length) {
words = shuffle(words);
order = 0;
}
word = words[order];
order += 1;
typed = 0;
wordX = 400;
}
| 行 | すること |
|---|---|
if (order >= words.length) |
最後まで出し切っていたら、もう一度、混ぜて、最初に戻る |
word = words[order]; |
order 番目の単語を、取り出す |
order += 1; |
次は、その次の単語を、出す |
こうすると、すべての単語が、1回ずつ出るまで、同じ単語は、出ません。
let words = ['apple', 'river', 'cloud', 'music', 'tiger', 'green'];
let order = 0;
let word = '';
let wordX = 400;
let typed = 0;
let score = 0;
let misses = 0;
let flash = 0;
function setup() {
createCanvas(400, 400);
textFont('monospace');
words = shuffle(words);
nextWord();
}
function draw() {
background(30, 30, 60);
wordX -= 1.5;
if (wordX < -word.length * 24) {
misses += 1;
flash = 12;
nextWord();
}
if (flash > 0) {
background(120, 30, 40);
flash -= 1;
}
textSize(40);
for (let i = 0; i < word.length; i++) {
if (i < typed) {
fill(90, 100, 130);
} else if (i === typed) {
fill(255, 220, 80);
} else {
fill(255);
}
text(word[i], wordX + i * 24, 210);
}
fill(255);
textSize(16);
text(`スコア: ${score}`, 16, 28);
text(`ミス: ${misses}`, 160, 28);
}
function nextWord() {
if (order >= words.length) {
words = shuffle(words);
order = 0;
}
word = words[order];
order += 1;
typed = 0;
wordX = 400;
}
function keyPressed() {
if (key.length === 1) {
if (key.toLowerCase() === word[typed]) {
typed += 1;
if (typed === word.length) {
score += word.length;
nextWord();
}
} else {
misses += 1;
flash = 12;
}
}
}
setup の中で、最初に、words = shuffle(words); と、nextWord(); を、呼んでいます。1つ目の単語も、混ぜた配列から、取り出すためです。
| 選び方 | 特徴 |
|---|---|
random(words) |
書くのが簡単。同じものが、続くことがある |
shuffle して、順に出す |
一巡するまで、同じものが出ない。かたよりが、ない |
ステップ6:制限時間と、3つの画面
ゲームとして、まとめます。
残り時間を、フレームで持つ
timeLeft = 30 * 60;
30 秒 × 60 フレームで、1800 です。プレイ中、毎フレーム、1 ずつ減らします。0 になったら、結果の画面へ、進みます。
表示するときは、60 で割って、秒に直します。
text(`残り: ${ceil(timeLeft / 60)}秒`, 300, 28);
ceil は、小数点以下を、切り上げる関数です。残りが 0.5 秒のとき、切り捨てると、0 秒と表示されてしまいます。切り上げれば、最後の1秒の間、「1秒」と表示され、0 になった瞬間に、終わります。
残り時間を、棒で見せる
数字だけでなく、棒の長さでも、残り時間を、見せます。
rect(0, 392, 400 * timeLeft / (30 * 60), 8);
timeLeft / (30 * 60) は、残り時間の割合です。始まったときに 1、終わるときに 0 になります。キャンバスの横幅 400 に、この割合を掛けて、棒の長さにしています。
タイピング中は、目が、流れてくる単語に、向いています。数字を読む余裕は、ありません。棒なら、目の端で、残りの量が、わかります。
シーンと、関数
シーンを、scene という変数で持ち、処理を、関数に分けます。
| 関数 | 役割 |
|---|---|
startGame |
すべての変数を、最初の値に戻し、最初の単語を出す |
nextWord |
次の単語を出す |
updateGame |
時間を減らし、単語を流す |
drawGame |
プレイ中の画面を描く |
drawTitle、drawResult |
タイトルと、結果の画面を描く |
キーの役割が、シーンで変わる
keyPressed の中を、シーンで、分けます。
function keyPressed() {
if (scene === 'play') {
// 文字の判定
} else if (key === ' ') {
// タイトルなら始める。結果なら、タイトルへ戻る
}
}
プレイ中は、すべてのキーが、文字の判定に、使われます。プレイ中でないときだけ、スペースキーが、画面を進める役目を、します。
この分け方をしないと、結果の画面で、キーを打つたびに、ミスが増えたり、プレイ中に、スペースキーで、ゲームが、やり直しになったりします。
だんだん、速くする
wordX -= 1.2 + score * 0.03;
流れる速さを、スコアから、決めます。スコアが 0 のとき、1.2。スコアが 50 のとき、2.7。上手な人ほど、速くなります。
時間ではなく、スコアで速くするのは、その人の腕前に、合わせるためです。ゆっくり打つ人には、ゆっくりのまま。速く打てる人には、どんどん、手ごわくなります。
完成
let scene = 'title';
let words = ['apple', 'river', 'cloud', 'music', 'tiger', 'green', 'happy', 'light', 'ocean', 'smile', 'bread', 'dance'];
let order = 0;
let word = '';
let wordX = 400;
let typed = 0;
let score = 0;
let misses = 0;
let flash = 0;
let timeLeft = 0;
function setup() {
createCanvas(400, 400);
textFont('monospace');
}
function draw() {
background(30, 30, 60);
if (scene === 'title') {
drawTitle();
} else if (scene === 'play') {
updateGame();
drawGame();
} else if (scene === 'result') {
drawResult();
}
}
function startGame() {
words = shuffle(words);
order = 0;
score = 0;
misses = 0;
flash = 0;
timeLeft = 30 * 60;
nextWord();
}
function nextWord() {
if (order >= words.length) {
words = shuffle(words);
order = 0;
}
word = words[order];
order += 1;
typed = 0;
wordX = 400;
}
function updateGame() {
timeLeft -= 1;
// 単語を流す。スコアが高いほど、速い
wordX -= 1.2 + score * 0.03;
if (wordX < -word.length * 24) {
misses += 1;
flash = 12;
nextWord();
}
if (timeLeft <= 0) {
scene = 'result';
}
}
function drawGame() {
// 間違えた直後は、赤く光らせる
if (flash > 0) {
background(120, 30, 40);
flash -= 1;
}
// 単語を、1文字ずつ描く
textAlign(LEFT, BASELINE);
textSize(40);
for (let i = 0; i < word.length; i++) {
if (i < typed) {
fill(90, 100, 130);
} else if (i === typed) {
fill(255, 220, 80);
} else {
fill(255);
}
text(word[i], wordX + i * 24, 210);
}
// 残り時間の棒
noStroke();
fill(80, 160, 230);
rect(0, 392, 400 * timeLeft / (30 * 60), 8);
fill(255);
textSize(16);
text(`スコア: ${score}`, 16, 28);
text(`ミス: ${misses}`, 160, 28);
text(`残り: ${ceil(timeLeft / 60)}秒`, 300, 28);
}
function drawTitle() {
fill(255);
textAlign(CENTER, CENTER);
textSize(32);
text('タイピングゲーム', 200, 140);
textSize(16);
text('流れてくる単語を、打つ', 200, 200);
text('制限時間は、30秒', 200, 230);
text('スペースキーで、スタート', 200, 290);
}
function drawResult() {
fill(255);
textAlign(CENTER, CENTER);
textSize(32);
text('おしまい', 200, 130);
textSize(22);
text(`スコア: ${score}`, 200, 190);
text(`ミス: ${misses}`, 200, 225);
textSize(16);
text('スペースキーで、タイトルへ', 200, 300);
}
function keyPressed() {
if (scene === 'play') {
if (key.length === 1) {
if (key.toLowerCase() === word[typed]) {
typed += 1;
if (typed === word.length) {
score += word.length;
nextWord();
}
} else {
misses += 1;
flash = 12;
}
}
} else if (key === ' ') {
if (scene === 'title') {
startGame();
scene = 'play';
} else if (scene === 'result') {
scene = 'title';
}
}
}
ゲームの状態を表す変数を、まとめます。
| 変数 | 意味 | 変わるとき |
|---|---|---|
scene |
いまの画面 | スペースキー、時間切れ |
word、wordX、typed |
いまの単語、その位置、打ち終えた数 | 打つたび、毎フレーム、単語が替わるとき |
order |
何番目の単語まで、出したか | 単語が替わるとき |
score、misses |
スコアと、ミス | 打ち切ったとき、間違えたとき |
flash |
赤く光らせる、残りのフレーム数 | 間違えたとき、毎フレーム |
timeLeft |
残り時間(フレーム) | 毎フレーム |
遊びやすさを、調整する
| 数字 | 変えると |
|---|---|
| 単語の配列 | 短い単語ばかりにすると、やさしい。長い単語を混ぜると、難しい |
1.2(最初の速さ) |
大きいと、最初から、あわただしい |
0.03(速くなる度合い) |
大きいと、上手な人が、すぐに、追いつけなくなる |
30(制限時間) |
長いと、じっくり。短いと、一気に |
12(赤く光る長さ) |
長いと、ミスが目立つが、次の文字が、見にくくなる |
単語の配列を、取り替えるだけで、まったく別の練習に、なります。英単語、ローマ字の日本語、プログラムで使う言葉。配列の中身は、ゲームの仕組みとは、切り離されています。
やってみよう
次の3つを作ってください。
- 結果の画面に、正確さを、パーセントで表示する。正しく打った数を数えておき、「正しく打った数 ÷(正しく打った数 + ミスの数)× 100」で求める
- 連続で打ち切った回数(コンボ)を数え、点に掛ける。ミスをしたら、0 に戻す。2回以上続いているときは、画面に表示する
- 単語を、ローマ字の日本語に、取り替える
▶ 1. 正確さを表示する
hits という変数で、正しく打った数を、数えます。結果の画面で、割合を求め、round で、小数点以下を、四捨五入します。
let scene = 'title';
let words = ['apple', 'river', 'cloud', 'music', 'tiger', 'green', 'happy', 'light', 'ocean', 'smile', 'bread', 'dance'];
let order = 0;
let word = '';
let wordX = 400;
let typed = 0;
let score = 0;
let misses = 0;
let flash = 0;
let timeLeft = 0;
let hits = 0;
function setup() {
createCanvas(400, 400);
textFont('monospace');
}
function draw() {
background(30, 30, 60);
if (scene === 'title') {
drawTitle();
} else if (scene === 'play') {
updateGame();
drawGame();
} else if (scene === 'result') {
drawResult();
}
}
function startGame() {
words = shuffle(words);
order = 0;
score = 0;
misses = 0;
hits = 0;
flash = 0;
timeLeft = 30 * 60;
nextWord();
}
function nextWord() {
if (order >= words.length) {
words = shuffle(words);
order = 0;
}
word = words[order];
order += 1;
typed = 0;
wordX = 400;
}
function updateGame() {
timeLeft -= 1;
// 単語を流す。スコアが高いほど、速い
wordX -= 1.2 + score * 0.03;
if (wordX < -word.length * 24) {
misses += 1;
flash = 12;
nextWord();
}
if (timeLeft <= 0) {
scene = 'result';
}
}
function drawGame() {
// 間違えた直後は、赤く光らせる
if (flash > 0) {
background(120, 30, 40);
flash -= 1;
}
// 単語を、1文字ずつ描く
textAlign(LEFT, BASELINE);
textSize(40);
for (let i = 0; i < word.length; i++) {
if (i < typed) {
fill(90, 100, 130);
} else if (i === typed) {
fill(255, 220, 80);
} else {
fill(255);
}
text(word[i], wordX + i * 24, 210);
}
// 残り時間の棒
noStroke();
fill(80, 160, 230);
rect(0, 392, 400 * timeLeft / (30 * 60), 8);
fill(255);
textSize(16);
text(`スコア: ${score}`, 16, 28);
text(`ミス: ${misses}`, 160, 28);
text(`残り: ${ceil(timeLeft / 60)}秒`, 300, 28);
}
function drawTitle() {
fill(255);
textAlign(CENTER, CENTER);
textSize(32);
text('タイピングゲーム', 200, 140);
textSize(16);
text('流れてくる単語を、打つ', 200, 200);
text('制限時間は、30秒', 200, 230);
text('スペースキーで、スタート', 200, 290);
}
function drawResult() {
fill(255);
textAlign(CENTER, CENTER);
textSize(32);
text('おしまい', 200, 130);
textSize(22);
text(`スコア: ${score}`, 200, 190);
text(`ミス: ${misses}`, 200, 225);
if (hits + misses > 0) {
let rate = round(hits / (hits + misses) * 100);
text(`正確さ: ${rate}%`, 200, 260);
}
textSize(16);
text('スペースキーで、タイトルへ', 200, 300);
}
function keyPressed() {
if (scene === 'play') {
if (key.length === 1) {
if (key.toLowerCase() === word[typed]) {
typed += 1;
hits += 1;
if (typed === word.length) {
score += word.length;
nextWord();
}
} else {
misses += 1;
flash = 12;
}
}
} else if (key === ' ') {
if (scene === 'title') {
startGame();
scene = 'play';
} else if (scene === 'result') {
scene = 'title';
}
}
}
if (hits + misses > 0) で、1回もキーを押さなかったときを、除いています。0 で割ることになり、おかしな値が、表示されてしまうからです。
▶ 2. コンボを付ける
combo という変数を、足します。打ち切るたびに 1 増やし、点に掛けます。打ち間違えたときと、流れ切ったときに、0 へ戻します。
let scene = 'title';
let words = ['apple', 'river', 'cloud', 'music', 'tiger', 'green', 'happy', 'light', 'ocean', 'smile', 'bread', 'dance'];
let order = 0;
let word = '';
let wordX = 400;
let typed = 0;
let score = 0;
let misses = 0;
let flash = 0;
let timeLeft = 0;
let combo = 0;
function setup() {
createCanvas(400, 400);
textFont('monospace');
}
function draw() {
background(30, 30, 60);
if (scene === 'title') {
drawTitle();
} else if (scene === 'play') {
updateGame();
drawGame();
} else if (scene === 'result') {
drawResult();
}
}
function startGame() {
words = shuffle(words);
order = 0;
score = 0;
misses = 0;
combo = 0;
flash = 0;
timeLeft = 30 * 60;
nextWord();
}
function nextWord() {
if (order >= words.length) {
words = shuffle(words);
order = 0;
}
word = words[order];
order += 1;
typed = 0;
wordX = 400;
}
function updateGame() {
timeLeft -= 1;
// 単語を流す。スコアが高いほど、速い
wordX -= 1.2 + score * 0.03;
if (wordX < -word.length * 24) {
misses += 1;
combo = 0;
flash = 12;
nextWord();
}
if (timeLeft <= 0) {
scene = 'result';
}
}
function drawGame() {
// 間違えた直後は、赤く光らせる
if (flash > 0) {
background(120, 30, 40);
flash -= 1;
}
// 単語を、1文字ずつ描く
textAlign(LEFT, BASELINE);
textSize(40);
for (let i = 0; i < word.length; i++) {
if (i < typed) {
fill(90, 100, 130);
} else if (i === typed) {
fill(255, 220, 80);
} else {
fill(255);
}
text(word[i], wordX + i * 24, 210);
}
// 残り時間の棒
noStroke();
fill(80, 160, 230);
rect(0, 392, 400 * timeLeft / (30 * 60), 8);
fill(255);
textSize(16);
text(`スコア: ${score}`, 16, 28);
text(`ミス: ${misses}`, 160, 28);
text(`残り: ${ceil(timeLeft / 60)}秒`, 300, 28);
if (combo >= 2) {
fill(255, 220, 80);
text(`${combo} 連続!`, 16, 60);
}
}
function drawTitle() {
fill(255);
textAlign(CENTER, CENTER);
textSize(32);
text('タイピングゲーム', 200, 140);
textSize(16);
text('流れてくる単語を、打つ', 200, 200);
text('制限時間は、30秒', 200, 230);
text('スペースキーで、スタート', 200, 290);
}
function drawResult() {
fill(255);
textAlign(CENTER, CENTER);
textSize(32);
text('おしまい', 200, 130);
textSize(22);
text(`スコア: ${score}`, 200, 190);
text(`ミス: ${misses}`, 200, 225);
textSize(16);
text('スペースキーで、タイトルへ', 200, 300);
}
function keyPressed() {
if (scene === 'play') {
if (key.length === 1) {
if (key.toLowerCase() === word[typed]) {
typed += 1;
if (typed === word.length) {
combo += 1;
score += word.length * combo;
nextWord();
}
} else {
misses += 1;
combo = 0;
flash = 12;
}
}
} else if (key === ' ') {
if (scene === 'title') {
startGame();
scene = 'play';
} else if (scene === 'result') {
scene = 'title';
}
}
}
3回続けて打ち切ると、3つ目の単語は、3倍の点になります。ミスなく打ち続けることに、大きな見返りが、生まれます。あわてて打つか、確実に打つか。打ち方に、かけひきが、出てきます。
▶ 3. ローマ字の日本語にする
配列の中身を、取り替えるだけです。
let scene = 'title';
let words = ['sakura', 'neko', 'yama', 'umi', 'sora', 'hoshi', 'tsuki', 'kaze', 'ame', 'hana', 'tori', 'mori'];
let order = 0;
let word = '';
let wordX = 400;
let typed = 0;
let score = 0;
let misses = 0;
let flash = 0;
let timeLeft = 0;
function setup() {
createCanvas(400, 400);
textFont('monospace');
}
function draw() {
background(30, 30, 60);
if (scene === 'title') {
drawTitle();
} else if (scene === 'play') {
updateGame();
drawGame();
} else if (scene === 'result') {
drawResult();
}
}
function startGame() {
words = shuffle(words);
order = 0;
score = 0;
misses = 0;
flash = 0;
timeLeft = 30 * 60;
nextWord();
}
function nextWord() {
if (order >= words.length) {
words = shuffle(words);
order = 0;
}
word = words[order];
order += 1;
typed = 0;
wordX = 400;
}
function updateGame() {
timeLeft -= 1;
// 単語を流す。スコアが高いほど、速い
wordX -= 1.2 + score * 0.03;
if (wordX < -word.length * 24) {
misses += 1;
flash = 12;
nextWord();
}
if (timeLeft <= 0) {
scene = 'result';
}
}
function drawGame() {
// 間違えた直後は、赤く光らせる
if (flash > 0) {
background(120, 30, 40);
flash -= 1;
}
// 単語を、1文字ずつ描く
textAlign(LEFT, BASELINE);
textSize(40);
for (let i = 0; i < word.length; i++) {
if (i < typed) {
fill(90, 100, 130);
} else if (i === typed) {
fill(255, 220, 80);
} else {
fill(255);
}
text(word[i], wordX + i * 24, 210);
}
// 残り時間の棒
noStroke();
fill(80, 160, 230);
rect(0, 392, 400 * timeLeft / (30 * 60), 8);
fill(255);
textSize(16);
text(`スコア: ${score}`, 16, 28);
text(`ミス: ${misses}`, 160, 28);
text(`残り: ${ceil(timeLeft / 60)}秒`, 300, 28);
}
function drawTitle() {
fill(255);
textAlign(CENTER, CENTER);
textSize(32);
text('タイピングゲーム', 200, 140);
textSize(16);
text('流れてくる単語を、打つ', 200, 200);
text('制限時間は、30秒', 200, 230);
text('スペースキーで、スタート', 200, 290);
}
function drawResult() {
fill(255);
textAlign(CENTER, CENTER);
textSize(32);
text('おしまい', 200, 130);
textSize(22);
text(`スコア: ${score}`, 200, 190);
text(`ミス: ${misses}`, 200, 225);
textSize(16);
text('スペースキーで、タイトルへ', 200, 300);
}
function keyPressed() {
if (scene === 'play') {
if (key.length === 1) {
if (key.toLowerCase() === word[typed]) {
typed += 1;
if (typed === word.length) {
score += word.length;
nextWord();
}
} else {
misses += 1;
flash = 12;
}
}
} else if (key === ' ') {
if (scene === 'title') {
startGame();
scene = 'play';
} else if (scene === 'result') {
scene = 'title';
}
}
}
ゲームの仕組みには、手を付けていません。単語は、長さが、ばらばらでも、かまいません。位置や、判定は、すべて、word.length を使って、計算しているからです。
まとめ
- 「何文字目まで打ったか」を、数で覚える。その数が、次に打つ文字の番号になる
- 文字列を、ループで1文字ずつ描くと、文字ごとに、色を変えられる。等幅の書体なら、位置の計算が、簡単
- 押されたキーと、
word[typed]を比べて、正解かどうかを、判定する key.length === 1で、文字のキーだけを、判定の対象にする。toLowerCase()で、大文字と小文字の違いを、なくす- 単語が替わるときに戻す変数は、1つの関数に、まとめる
- 「一瞬だけ」の効果は、残りのフレーム数を、変数で持って、作る
shuffleで混ぜて、順に取り出すと、一巡するまで、同じものが出ない- キーの役割は、シーンによって、分ける
- 残りの量は、数字だけでなく、棒の長さでも見せると、目の端で、わかる