接上两篇:【瑞萨 RA6E2】+机械臂游戏(原创)1-数字舵机
【瑞萨 RA6E2】+机械臂游戏(原创)2-控制机械臂
下面是一个基于瑞萨 RA6E2开发板的老虎、鸡、虫、杠子的创意游戏示例:
部分源代码:
- int main() {
- char player;
- int v;
- int computer;
- char* options[] = {"Tiger", "Chicken", "Worm", "Lion"};
- printf("欢迎来到老虎、鸡、虫、杠子游戏!\n");
- //机械臂做出相应动作
- for ( v = 60; v > 40 ; v-- )
- {
- R_IOPORT_PinWrite (&g_ioport_ctrl, AI5, BSP_IO_LEVEL_HIGH);
- R_BSP_SoftwareDelay(1,BSP_DELAY_UNITS_MILLISECONDS);
- R_IOPORT_PinWrite (&g_ioport_ctrl, AI5, BSP_IO_LEVEL_LOW);
- R_BSP_SoftwareDelay(19,BSP_DELAY_UNITS_MILLISECONDS);
-
- }
-
- // 让计算机随机选择一个选项
- srand(time(NULL));
- computer = rand() % 4;
- //机械臂做出相应动作
-
- for ( v = 60; v > 40 ; v-- )
- {
- R_IOPORT_PinWrite (&g_ioport_ctrl, AI1, BSP_IO_LEVEL_HIGH);
- R_BSP_SoftwareDelay(1,BSP_DELAY_UNITS_MILLISECONDS);
- R_IOPORT_PinWrite (&g_ioport_ctrl, AI1, BSP_IO_LEVEL_LOW);
- R_BSP_SoftwareDelay(19,BSP_DELAY_UNITS_MILLISECONDS);
-
- }
-
- printf("玩家,请选择你的选项(T-老虎、C-鸡、W-虫、L-杠子):\n");
- //机械臂做出相应动作
-
- for ( v = 60; v > 40 ; v-- )
- {
-
-
- R_IOPORT_PinWrite (&g_ioport_ctrl, AI8, BSP_IO_LEVEL_HIGH);
- R_BSP_SoftwareDelay(2,BSP_DELAY_UNITS_MILLISECONDS);
- R_IOPORT_PinWrite (&g_ioport_ctrl, AI8, BSP_IO_LEVEL_LOW);
- R_BSP_SoftwareDelay(8,BSP_DELAY_UNITS_MILLISECONDS);
- }
-
- scanf(" %c", &player);
- printf("计算机选择的选项是:%s\n", options[computer]);
- //机械臂做出相应动作
-
- for ( v = 60; v > 40 ; v-- )
- {
-
-
- R_IOPORT_PinWrite (&g_ioport_ctrl, AI6, BSP_IO_LEVEL_HIGH);
- R_BSP_SoftwareDelay(2,BSP_DELAY_UNITS_MILLISECONDS);
- R_IOPORT_PinWrite (&g_ioport_ctrl, AI6, BSP_IO_LEVEL_LOW);
- R_BSP_SoftwareDelay(8,BSP_DELAY_UNITS_MILLISECONDS);
- }
-
- // 判断获胜或平局
- if (player == options[computer][0]) {
- printf("平局!\n");
- //机械臂做出相应动作
- for ( v = 60; v > 40 ; v-- )
- {
-
-
- R_IOPORT_PinWrite (&g_ioport_ctrl, AI1, BSP_IO_LEVEL_HIGH);
- R_BSP_SoftwareDelay(2,BSP_DELAY_UNITS_MILLISECONDS);
- R_IOPORT_PinWrite (&g_ioport_ctrl, AI1, BSP_IO_LEVEL_LOW);
- R_BSP_SoftwareDelay(8,BSP_DELAY_UNITS_MILLISECONDS);
- }
- } else if ((player == 'T' && options[computer][0] == 'C') ||
- (player == 'C' && options[computer][0] == 'W') ||
- (player == 'W' && options[computer][0] == 'L') ||
- (player == 'L' && options[computer][0] == 'T')) {
- printf("恭喜,你获胜了!\n");
-
- for ( v = 60; v > 40 ; v-- ) //机械臂做出相应动作(只有获胜是两个动作)
- {
- R_IOPORT_PinWrite (&g_ioport_ctrl, AI6, BSP_IO_LEVEL_HIGH);
- R_BSP_SoftwareDelay(1,BSP_DELAY_UNITS_MILLISECONDS);
- R_IOPORT_PinWrite (&g_ioport_ctrl, AI6, BSP_IO_LEVEL_LOW);
- R_BSP_SoftwareDelay(19,BSP_DELAY_UNITS_MILLISECONDS);
- //机械臂做出相应动作 获胜 (只有获胜是两个动作)
-
- for ( v = 60; v > 40 ; v-- )
- {
- R_IOPORT_PinWrite (&g_ioport_ctrl, AI8, BSP_IO_LEVEL_HIGH);
- R_BSP_SoftwareDelay(1,BSP_DELAY_UNITS_MILLISECONDS);
- R_IOPORT_PinWrite (&g_ioport_ctrl, AI8, BSP_IO_LEVEL_LOW);
- R_BSP_SoftwareDelay(19,BSP_DELAY_UNITS_MILLISECONDS);
-
- }
- }
-
- } else {
- printf("很遗憾,你输了!\n");
- for ( v = 60; v > 40 ; v-- ) //机械臂做出相应动作
- {
-
-
- R_IOPORT_PinWrite (&g_ioport_ctrl, AI5, BSP_IO_LEVEL_HIGH);
- R_BSP_SoftwareDelay(2,BSP_DELAY_UNITS_MILLISECONDS);
- R_IOPORT_PinWrite (&g_ioport_ctrl, AI5, BSP_IO_LEVEL_LOW);
- R_BSP_SoftwareDelay(8,BSP_DELAY_UNITS_MILLISECONDS);
- }
- }
- return 0;
- }
在这个创意游戏中,玩家需要选择自己的选项(T-老虎、C-鸡、W-虫、L-杠子),然后计算机会随机选择一个选项。根据规则判断谁是获胜者或平局。规则如下:
- 老虎吃鸡
- 鸡吃虫
- 虫拱杠子
- 杠子打老虎
根据玩家和计算机的选择,根据规则进行判断并输出相应的结果,不同的结果机械臂就会做不同的动作。可根据机械臂不同的动作得知结果,并再次运行代码与计算机进行多次对战,看看谁能获得更多的胜利!你也可以根据需要进行修改和扩展这个游戏,添加更多的选项或增加更复杂的规则。
谢谢。