大家好!!!欢迎来到基于 Arduino 的新项目。我们一生中都玩过飞扬的小鸟游戏。如果我们在我们的 PC 上播放它并使用我们的 Arduino 控制它怎么办?按照下面给出的所有步骤操作,到本教程结束时,您将使用微控制器控制游戏。
我们在这里使用超声波传感器的原因是为了获取我们的手和传感器之间的距离数据,并使用这些值来调整移动的鸟的高度。游戏在 Processing 中创建,Arduino 使用串行端口与其通信。我已经链接了上面游戏的几张图片,所以请看一下它们以便对这个项目有所了解。
赞助商链接 - UTSource.net。
首先将 SR-04 传感器连接到 Arduino 板上。由于只有一个传感器接口,我不会为这个项目添加电路图。连接如下 -
SR-04 >> Arduino Uno
电源 >> 5V
接地 >> 接地
触发针 >> 数字针 11
Echo Pin >> 数字引脚 10
就是这样,连接就完成了。
现在是时候将代码上传到您的 Arduino 开发板了。
从下面复制代码。
在上传代码之前,请确保选择正确的 com 端口和波特率,因为我们将使用它向游戏发送数据。
const int trigPin=11; //DECLARE TRIG PIN AT D11
int echoPin=10; //DECLARE ECHO PIN AT D10
int safezone=10;
void setup()
{
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
Serial.begin(9600);
}
void loop()
{
long duration,cm; //DECLARE VARIABLES TO STORE SENSOR O/P
digitalWrite(trigPin,LOW); //MAKE THE TRIG PIN LOW
delayMicroseconds(2); //WAIT FOR FEW MICROSECONDS
digitalWrite(trigPin,HIGH); //NOW SET THE TRIG PIN
delayMicroseconds(5); //WAIT FOR FEW MICROSECONDS UNTIL THE TRIG PULSE IS SENT
digitalWrite(trigPin,LOW); //MAKE THE TRIG PIN LOW AGAIN
duration=pulseIn(echoPin,HIGH); //MAKE ECHO PIN HIGH AND STORE THE BOUNCED PULSE IN VARIABLE DURATION
cm=microsecondsToCentimeters(duration);
long inch= cm/2.54;
Serial.println(cm);
}
long microsecondsToCentimeters(long microseconds) //SPEED OF SOUND IS 29 uSEC PER CM
{
return microseconds/29/2; //THE PULSE TRAVELS FROM THE SENSOR AND AGAIN COMES BACK SO WE DIVIDE IT BY 2 TO TAKE ONLY HALF OF THE TOTAL DISTANCE
}
打开处理程序:
上传 Arduino 代码后,下载并打开处理代码。再次
设置与之前相同的波特率并提及正确的 com 端口。
import processing.serial.*;
int DistanceUltra;
int IncomingDistance;
Serial myPort;
String DataIn;
Pipe p1 = new Pipe();
Pipe p2 = new Pipe();
Pipe p3 = new Pipe();
//bird height and width location
float birdy = 46;
float birdx = 56;
float gravity = 5;
//the speed of the pipes
int speed;
//score and game state
boolean gameOver = false;
int score = 0;
int highscore = 0;
int point = 1;
color birdColor = color(255, 204, 0);
void setup(){
size(400,600);
p1.x = width + 50;
p2.x = width + 220;
p3.x = width + 370;
myPort = new Serial(this, "COM3", 9600);
myPort.bufferUntil(10);
}
void serialEvent (Serial myPort){
DataIn = myPort.readString();
println(DataIn);
IncomingDistance = int(trim(DataIn));
println("incoming distance="+IncomingDistance);
if (IncomingDistance>1 && IncomingDistance<100 ) { DistanceUltra = IncomingDistance; //save the value only if its in the range 1 to 100 } }
}
}
void draw(){
background(0);
p1.pipe();
p2.pipe();
p3.pipe();
fill(birdColor);
ellipse(birdx, birdy, 55,55);
// birdy += gravity;
play();
success(p1);
success(p2);
success(p3);
if (IncomingDistance>10)
{
//birdy -= jumpForce;
birdy -= gravity;
}
else
{
birdy += gravity;
}
}
void play(){
if(gameOver == false)
{
speed = 2;
p1.x -= speed;
p2.x -= speed;
p3.x -= speed;
textSize(24);
fill(255,255,255);
text(score, width/2, 30);
}
if(gameOver == true)
{
speed = 0;
p1.x -= speed;
p2.x -= speed;
p3.x -= speed;
if( highscore < score)
{
highscore = score;
}
textSize(16);
fill(0, 102, 153);
textAlign(CENTER);
text("Click : Play Again", width/2, height/2);
text("Score: " + score, width/2, height/2 - 20);
text("High-Score: " + highscore, width/2, height/2 - 40);
if (mousePressed)
{
delay(900);
score = 0;
gameOver = false;
birdy = 100;
birdx = 56;
p1.x = width + 50;
p2.x = width + 220;
p3.x = width + 370;
p1.top = random(height/2);
p1.bottom = random(height/2);
p2.top = random(height/2);
p2.bottom = random(height/2);
p3.top = random(height/2);
p3.bottom = random(height/2);
}
}
}
void success(Pipe test){
if(birdy < test.top || birdy > height - test.bottom)
{
if(birdx > test.x && birdx < test.x + test.w)
{
gameOver = true;
}
}
}
class Pipe
{
float top = random(height/3 + 200);
float bottom = random(height/3 +200);
float x = width + 150;
float w = 70;
color pipeColor = color(0, 255, 0);
void pipe()
{
fill(pipeColor);
rect(x, 0, w, top);
rect(x, height-bottom, w, bottom);
if(x < -100)
{
score += point;
x = width;
top = random(height/2);
bottom = random(height/2);
}
}
}
现在让我们来试试这个游戏。只需单击处理IDE中的运行按钮,然后
你已准备好出发。小鸟会根据你手之间的距离移动
和传感器。如果您对此项目有任何疑问,请随时发表评论
以下。
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
全部0条评论
快来发表一下你的评论吧 !