别踩白块儿游戏c语言代码,Arduino 模拟“别踩白块儿”游戏

项目说明:

曾做过电脑版的,后来接触Arduino后做了一个Arduino版。

材料:Arduino、2004液晶屏、电阻(用于调节液晶屏对比度)、面包版、排线等。

代码:

#include

#include

//Keypad.h下载地址:https://github.com/Chris--A/Keypad

int Combo=0;

long randomNumber=0;

int Rand[8];

char key;

char str[32];

char time[32];

const byte ROWS=2;  //矩阵键盘行数

const byte COLS=4;  //矩阵键盘列数

//按键定义

char Keys[ROWS][COLS]={

{'1', '2', '3', '4'},

{'5', '6', '7', '8'}

};

byte rowPins[ROWS]={6, 7};  //行 针脚连接6端口(忽略7端口)

byte colPins[COLS]={5, 4, 3, 2};  //列 针脚分别连接5、4、3、2端口

Keypad keypad=Keypad(makeKeymap(Keys), rowPins, colPins, ROWS, COLS);

LiquidCrystal lcd(8, 9, 10, 11, 12, 13);

//液晶屏最右侧显示提示信息

void start(){

lcd.setCursor(19, 0);

lcd.print('1');

lcd.setCursor(19, 1);

lcd.print('2');

lcd.setCursor(19, 2);

lcd.print('3');

lcd.setCursor(19, 3);

lcd.print('4');

}

//游戏结束显示提示信息

void display(){

lcd.clear();

lcd.setCursor(6, 1);

lcd.print("Game Over");

lcd.setCursor(5, 2);

lcd.print("Combo: ");

lcd.setCursor(12, 2);

itoa(Combo, str, 10);

lcd.print(str);

lcd.setCursor(0, 3);

lcd.print("time:");

for(int i=0; i<10; i++){

delay(1000);

itoa((9-i), time, 10);

lcd.setCursor(5, 3);

lcd.print(time);

}

}

void setup(){

Serial.begin(9600);

randomSeed(analogRead(0));  //0端口随机读取一个数值作为随机数种子

lcd.begin(20, 4);  //设置2004液晶

}

void loop(){

//一共四个键,生成1~4的随机数,用Rand数组储存

for(int i=7; i>=1; i--){

Rand[i]=random(1, 5);

}

while(1){

if(Rand[1]==0){

for(int i=1;i<=6;i++){

int j=i+1;

Rand[i]=Rand[j];

}

Rand[7]=random(1, 5);

}

//讲Rand数组储存的数值以#号代替“白块”显示在液晶屏上

for(int i=7;i>=1;i--){

if(Rand[i]==1){

lcd.setCursor(19-i, 3);

lcd.print('#');

}else if(Rand[i]==2){

lcd.setCursor(19-i, 2);

lcd.print('#');

}else if(Rand[i]==3){

lcd.setCursor(19-i, 1);

lcd.print('#');

}else if(Rand[i]==4){

lcd.setCursor(19-i, 0);

lcd.print('#');

}

if(key){

lcd.clear();

}

}

start();

key=keypad.getKey();

//按键判定

if(key){

if((int)key-48==Rand[1]){

Rand[1]=0;

Combo++;

}else{

display();

Rand[1]=0;

Combo=0;

}

}

}

}

代码比较简单,只能实现按键按下,“块”下落,无法使之自动下落。也由于液晶屏特性,残影比较严重,无法实现其自由下落。

接线:

液晶屏        Arduino

RS ————>  7

E —————> 8

D3 ————> 9

D4 ————> 10

D5 ————> 11

D6 ————> 12

按键接线见程序。

效果如图

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值