c++ 游戲代碼(1)


迷宮代碼如下:

#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
void print(char a[10][10]){
    system("cls");
    for(int i=0;i<5;i++)
    cout<<a[i]<<endl;
}
int main(){
    int x=1,y=1;
    int ch;
    char a[10][10] = {
        "######",
        "#0   #",
        "#    #",
        "#  #  ",
        "######"
    };
    print(a);
    while(x != 3 || y != 5){
        if (_kbhit()){
            ch = _getch();
            if (ch == 27){ break; }
            if (ch == 97 && a[x][y-1] == ' '){
                a[x][y] = ' ';
                y--;
                a[x][y] = '0';
                print(a);
            }
            if (ch == 100 && a[x][y+1] == ' '){
                a[x][y] = ' ';
                y++;
                a[x][y] = '0';
                print(a);
            }
            if (ch == 115 && a[x+1][y] == ' '){
                a[x][y] = ' ';
                x++;
                a[x][y] = '0';
                print(a);
            }
            if (ch == 119 && a[x-1][y] == ' '){
                a[x][y] = ' ';
                x--;
                a[x][y] = '0';
                print(a);
            }
        }    
    }
    system("cls");
    cout<<"you win";
    return 0;
}

  運行如下:

 

 

 用a,s,w,d按鍵控制小球:

 移動代碼如下:

#include <iostream>
#include <conio.h>
using namespace std;
void print(){
    cout<<"12345"<<endl<<"|||||"<<endl;
}
int main(){
    int ch,x=0;
    print();
    cout<<"*";
     while(true){
        if (_kbhit()){
            ch = _getch();
            if(ch==100 && x < 4){
                x++;
                system("cls");
                print();
                for(int i = 0;i < x;i++){
                    cout<<" ";
                }
                cout<<"*";
            }
            if(ch==97 && x > 0){
                x--;
                system("cls");
                print();
                for(int i = 0;i < x;i++){
                    cout<<" ";
                }
                cout<<"*";
            }
        }
    }
    return 0;
}

運行如下:

 

 用a,d按鍵控制小球

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM