工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
home  MfgRobots >> 工業製造 >  >> Manufacturing Technology >> 製造プロセス

Pixel Chaser: Arduino Nanoで作るワンタップLEDゲーム

部品と材料

部品 数量 備考
Arduino Nano R3 1 またはIOピン2本以上の任意のマイコン
Adafruit NeoPixelリング: WS2812 5050 RGB LED 1 24ピクセル推奨
ハーフサイズブレッドボード 1
抵抗 10kΩ 1 ボタンプルダウン用
SparkFun プッシュボタンスイッチ 12mm 1 モーメンタリタイプ
ジャンパーワイヤー (汎用) 1

アプリとオンラインサービス

プロジェクト概要

私は元ゲーム開発者ですが、モバイルゲーム開発における「聖杯」の一つは、究極の「ワンタップ」ゲームを作ることです。

マイコン、ボタン、視覚フィードバックだけで、素晴らしいワンタップ「物理」ゲームが作れるか試してみたくなりました。

これがPIXEL CHASERです!

かなり簡単に作れるので、早速始めましょう!

必要なパーツ

Pixel Chaser: Arduino Nanoで作るワンタップLEDゲーム

ステップ1: Nanoの配置

ブレッドボードの一端にNanoを配置。USBコネクタが端に来るように。

Pixel Chaser: Arduino Nanoで作るワンタップLEDゲーム

ステップ2: ボタンの配置

ブレッドボードの反対側、できるだけ端に近い位置にボタンを配置(押しやすくするため)。

Pixel Chaser: Arduino Nanoで作るワンタップLEDゲーム

ステップ3: プルダウン抵抗の接続

ボタンの片側とブレッドボードのGNDレールの間に10kΩ抵抗を接続。これがボタンのデフォルトLOW状態になります。ボタン押下時にVCCとGNDがショートしてマイコンや他部品を破損しないよう、抵抗で保護します。

Pixel Chaser: Arduino Nanoで作るワンタップLEDゲーム

ステップ4: ボタンのVCC接続

ボタンの反対側からVCCレールへジャンパーワイヤーを接続。ボタン押下でHIGHになります。

Pixel Chaser: Arduino Nanoで作るワンタップLEDゲーム

ステップ5: ボタン信号線の接続

ボタンのGND側からNanoのD3へジャンパーワイヤーを接続。D3を読んでボタン状態を判定します。

Pixel Chaser: Arduino Nanoで作るワンタップLEDゲーム

ステップ6: RGBストリップ/リングの配線

VCC線をVCCレール、GND線をGNDレール、DI(Data In)線をNanoのD4に接続。D4でピクセル制御します。

Pixel Chaser: Arduino Nanoで作るワンタップLEDゲーム

ステップ7: 電源供給

Nanoの5VピンからVCCレールへ、GNDピンからGNDレールへジャンパーワイヤーを接続。

Pixel Chaser: Arduino Nanoで作るワンタップLEDゲーム

ステップ8: コード書き込み&プレイ!

Pixel Chaser: Arduino Nanoで作るワンタップLEDゲーム

お楽しみいただけましたか?

このような楽しいプロジェクトを私のYouTubeチャンネルで他にも公開しています。

フォローする

コード

Pixel Chaser Game - Arduino Code (Arduino)

このコードをArduinoに書き込み、組み立て手順に従ってください!

/* ---------------------------------------------------------------------------
Pixel Chaser Game - v1.0.0 - 24/03/2019.
AUTHOR/LICENSE:
Created by Seon Rozenblum - seon@unexpectedmaker.com
Copyright 2016
License: GNU GPL v3
https://www.gnu.org/licenses/gpl-3.0.html
LINKS:
Blog: unexpectedmaker.com
YOUTUBE: youtube.com/c/unexpectedmaker
DISCLAIMER:
This software is furnished "as is", without technical support,
and with no warranty, express or implied, as to its usefulness for any purpose.
PURPOSE:
This controller is the software that runs on the Reflow Master toaster oven controller made by Unexpected Maker
HISTORY:
24/03/2019 v1.0.0 - Initial release.
NOTE: This is a work in progress...
--------------------------------------------------------------------------- */

#include <Adafruit_NeoPixel.h>
#include "OneButton.h"

// ストリップ接続GPIO
#define PIXELPIN 4
// NeoPixel数
#define NUMPIXELS 24
// ボタン接続GPIO
#define BUTTON_TAP 3

// ボタン初期化(デフォルトLOW)
OneButton buttonTAP(BUTTON_TAP, false);

// ピクセルストリップ初期化
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIXELPIN, NEO_GRB + NEO_KHZ800);

// 変数群
int playerIndex = -1;
int playerIndexTrail = -1;
int playerDirection = 1;
float nextMove = 0;
float currentPlayerSpeed = 150;
unsigned long countdown = 0;
int enemyIndex = -1;
int coinIndex = -1;
int score = 0;
int bestScore = 0;
int lastScore = 0;
bool gameOver = false;

void setup() {
  pinMode(BUTTON_TAP, INPUT);
  buttonTAP.attachClick(singleClick);
  buttonTAP.attachLongPressStart(longClick);
  pixels.begin();
  pixels.setBrightness(20);
  countdown = millis() + 2000;
}

void loop() {
  buttonTAP.tick();
  if (gameOver) return;
  SetLevel();
  if (countdown > millis()) {
    pixels.show();
    return;
  }
  DisplayPlayer();
  pixels.show();
}

void ClearLevel() {
  for (int i = 0; i < NUMPIXELS; i++)
    pixels.setPixelColor(i, pixels.Color(0, 0, 0));
  pixels.show();
}

void BestScore() {
  for (int i = 0; i < NUMPIXELS; i++) {
    if (i < bestScore) pixels.setPixelColor(i, pixels.Color(255, 155, 0));
    else pixels.setPixelColor(i, pixels.Color(0, 0, 0));
  }
  if (lastScore < bestScore) {
    for (int i = 0; i < lastScore; i++)
      pixels.setPixelColor(i, pixels.Color(255, 0, 0));
  }
  pixels.show();
}

void GameOver() {
  int a = enemyIndex;
  int b = enemyIndex;
  for (int i = 0; i < NUMPIXELS / 2; i++) {
    pixels.setPixelColor(a, pixels.Color(255, 0, 0));
    pixels.setPixelColor(b, pixels.Color(255, 0, 0));
    a = (a + 1) % NUMPIXELS;
    b--;
    if (b == -1) b = NUMPIXELS;
    pixels.show();
    delay(20);
  }
  delay(100);
  a = enemyIndex;
  b = enemyIndex;
  for (int i = 0; i < NUMPIXELS / 2; i++) {
    pixels.setPixelColor(a, pixels.Color(0, 0, 0));
    pixels.setPixelColor(b, pixels.Color(0, 0, 0));
    a = (a + 1) % NUMPIXELS;
    b--;
    if (b == -1) b = NUMPIXELS;
    pixels.show();
    delay(20);
  }
  delay(100);
  BestScore();
}

void SetLevel() {
  if (enemyIndex < 0) {
    if (playerIndex < 0) {
      enemyIndex = NUMPIXELS / 2;
    } else {
      enemyIndex = random(0, NUMPIXELS);
      while (abs(enemyIndex - playerIndex) < (NUMPIXELS / 4))
        enemyIndex = random(0, NUMPIXELS);
    }
  }
  if (coinIndex < 0) {
    coinIndex = random(0, NUMPIXELS);
    while (abs(coinIndex - playerIndex) < 7 || abs(coinIndex - enemyIndex) < 7)
      coinIndex = random(0, NUMPIXELS);
  }
  pixels.setPixelColor(enemyIndex, pixels.Color(255, 0, 0));
  pixels.setPixelColor(coinIndex, pixels.Color(255, 255, 0));
}

void DisplayPlayer() {
  if (nextMove < millis()) {
    nextMove = millis() + currentPlayerSpeed;
    if (playerIndexTrail >= 0) pixels.setPixelColor(playerIndexTrail, pixels.Color(0, 0, 0));
    if (playerIndex >= 0) {
      pixels.setPixelColor(playerIndex, pixels.Color(0, 100, 0));
      playerIndexTrail = playerIndex;
    }
    playerIndex += playerDirection;
    if (playerIndex < 0) playerIndex = NUMPIXELS - 1;
    else if (playerIndex == NUMPIXELS) playerIndex = 0;
    pixels.setPixelColor(playerIndex, pixels.Color(0, 255, 0));
    
    // コイン取得判定
    if (playerIndex == coinIndex) {
      enemyIndex = -1;
      coinIndex = -1;
      score++;
      currentPlayerSpeed = constrain(currentPlayerSpeed - 10, 50, 150);
      ClearLevel();
      pixels.setPixelColor(playerIndex, pixels.Color(0, 255, 0));
    }
    // 敵衝突判定
    else if (playerIndex == enemyIndex) {
      lastScore = score;
      if (score >= bestScore) bestScore = score;
      GameOver();
      gameOver = true;
      enemyIndex = -1;
      coinIndex = -1;
      playerIndex = -1;
    }
  }
}

void singleClick() {
  if (countdown > millis()) return;
  playerDirection = -playerDirection;
}

void longClick() {
  gameOver = !gameOver;
  if (gameOver) {
    enemyIndex = -1;
    coinIndex = -1;
    playerIndex = -1;
    currentPlayerSpeed = 150;
    ClearLevel();
  } else {
    ClearLevel();
    score = 0;
    currentPlayerSpeed = 150;
    countdown = millis() + 2000;
  }
}

製造プロセス

  1. Arduino NanoとMPU-6050で作るリアルタイムジャイロスコープゲーム
  2. Arduinoデジタルダイス: LCD表示の乱数ジェネレーター自作
  3. Arduino UNOとOLEDディスプレイでクラシックPongゲームを自作:ステップバイステップチュートリアル
  4. Arduino Nanoで37LEDルーレットゲームを自作:ヨーロピアンスタイル完全再現
  5. DIY Arduino USBゲームコントローラー - 高性能ゲームパッドを自作
  6. Arduinoタッチブレークアウトゲーム:2.4インチTFT LCDでクラシックアーケードを再現
  7. 自作16×8 LEDマトリックスでテトリスゲーム:Arduino Nano実装
  8. Arduinoで作る斥力式磁気浮上装置: シンプル回路とDIYガイド
  9. Arduinoで自動化するChrome恐竜ゲーム – LDRとPythonで障害物を検知してジャンプ
  10. Alexa対応Arduinoホームコントローラー:音声でスマートホーム自動化
  11. Arduino Nanoで作るNeoPixelマトリックス版Pongゲーム