_getch一段高复用代码

本文介绍了一个使用C语言实现的控制台程序,该程序能够接收用户输入的数字并判断其是否能被2整除。此外,程序还支持通过按下特定按键来退出程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

按照自己的需求进一步进行修改.

#include "stdafx.h"
#include <stdlib.h>
#include <conio.h>

int _tmain(int argc, _TCHAR* argv[])
{

    system("title 能否被2整除呢.按“T”键退出");

    while(1)
    {
        int  x;
        system("CLS");
        printf ("输入要判断的数:");
        char tmp[2];
        int iPanduan=0;

        while(1)
        {

            iPanduan=!(iPanduan);//一共有两个位置,通过这个语句使循环存储字符
            tmp[iPanduan]=_getch();
            putchar(tmp[iPanduan]);

            switch(tmp[iPanduan])
            {
            case '\r':
                { 
                    iPanduan=!iPanduan;//判断末位数字在数组中哪个位置
                    x=tmp[iPanduan];

                    break;
                }


                //按T退出
            case 't':

            case 'T':
                {
                    return 0;
                    system("exit");
                }

            case '.':
                {
                //分割数据哟
                }
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                {

                }


            }
        }
    }



    return 0;
}

#include <iostream> #include <conio.h> #include <windows.h> #include <ctime> #include <cstdlib> using namespace std; const int width = 20; const int height = 20; int x, y, fruitX, fruitY, score; int tailX[100], tailY[100]; int nTail; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; eDirection dir; bool gameOver; void Setup() { gameOver = false; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; nTail = 0; } void Draw() { system("cls"); // 清屏 // 绘制上边界 for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { // 绘制左边界 if (j == 0) cout << "#"; // 绘制蛇头 if (i == y && j == x) cout << "O"; // 绘制食物 else if (i == fruitY && j == fruitX) cout << "F"; else { bool printTail = false; // 绘制蛇身 for (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { cout << "o"; printTail = true; } } if (!printTail) cout << " "; } // 绘制右边界 if (j == width - 1) cout << "#"; } cout << endl; } // 绘制下边界 for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; cout << "Score: " << score << endl; } void Input() { if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameOver = true; break; } } } void Logic() { // 保存尾部位置 int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; // 更新蛇身位置 for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } // 更新蛇头位置 switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; } // 穿墙处理 if (x >= width) x = 0; else if (x < 0) x = width - 1; if (y >= height) y = 0; else if (y < 0) y = height - 1; // 检测碰撞 for (int i = 0; i < nTail; i++) { if (tailX[i] == x && tailY[i] == y) gameOver = true; } // 吃食物 if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % width; fruitY = rand() % height; nTail++; } } int main() { Setup(); while (!gameOver) { Draw(); Input(); Logic(); Sleep(100); // 控制游戏速度 } return 0; }这是一份贪吃蛇小游戏代码,请再次基础上添加宝箱,使蛇碰到宝箱时,增加随即长度,要求能够在Dev-C++ 5.11版本的软件上运行
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值