C++吃金币小游戏

上图:

 

 

游戏规则:按A,D键向左和向右移动小棍子,$表示金币,0表示炸弹,吃到金币+10分,吃到炸弹就GAME OVER。

大体思路和打字游戏相同,都是使用数组,refresh和run函数进行,做了一些微调。

 

#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
char str[]="     $$$00";//随机掉落的字符,空格是没有,$是金币, 0是炸弹 
int score=0;
char a[8][8];
int now_x=0;
int start_time;
char temp;

int randbetween(int bottom,int top){
    return rand()%(top-bottom)+bottom;
}
void refresh(){
    for(int i=7;i>0;i--){
        for(int j=0;j<8;j++){
            a[i][j]=a[i-1][j];
        }
    }
    
    for(int i=0;i<5;i++){
        a[0][i]=str[randbetween(0,9)];
    }
    
    system("cls");
    printf("SCORE..%08d\n",score);
    
    for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){
            printf("%c ",a[i][j]);
        }
        printf("\n");
    }
    for(int i=0;i<now_x;i++)printf("  ");
    printf("|");
    
    if(a[7][now_x]=='$')score+=10;
    else if(a[7][now_x]=='0'){
        printf("GAME OVER\n");exit(0);
    }
    
    a[7][now_x]=' ';
    
}

void run(){
    system("cls");
    printf("SCORE..%08d\n",score);
    
    for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){
            printf("%c ",a[i][j]);
        }
        printf("\n");
    }
    for(int i=0;i<now_x;i++)printf("  ");
    printf("|");
    
    if(a[7][now_x]=='$')score+=10;
    else if(a[7][now_x]=='0'){
        printf("GAME OVER\n");exit(0);
    }
    
    a[7][now_x]=' ';
}

int main(){
    srand(time(0));
    for(;;){
        start_time=clock();
        refresh();
        while(clock()-start_time<=2000){//3秒以内 
            if(_kbhit()){
                temp=getch();
                if(temp=='A')now_x--;
                else if(temp=='D')now_x++;
                run();
            }
        }
    }
}

 

posted @ 2021-08-20 14:33  计算机知识杂谈  阅读(371)  评论(0编辑  收藏  举报