将做游戏学编程里的打飞机游戏实现了一遍然后加了几个功能

本文介绍了一个基于控制台的游戏开发示例,通过C语言实现了一个简单的射击游戏。游戏包括玩家控制的飞机、敌人以及子弹等元素,并实现了基本的移动、射击及碰撞检测等功能。

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

#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>

#define true 1
#define false 0

int BackGroundHeight, BackGroundWidth;
int PlaneX, PlaneY;
int EnemyX, EnemyY;
int IncreasedEnemyY;
int SpeedTimes;//飞机的速度是敌机的多少倍
int Pause;
int BulletX, BulletY;
int Fired;
int Killed;

int atoiMy( char ch );
int SpeedEnemyY( );
int returnEnemyX();
void startup();
void show();
void updateWithInput();
void updateWithoutInput();
void judgeOutput( int x, int y );
void gotoxy( int x, int y );

int main( void ) {

    startup();

    while( 1 ) {
        show();

        updateWithInput();

        updateWithoutInput();
    }

    return 0;
}

void startup() {
    BackGroundHeight = 10;
    BackGroundWidth = 20;
    PlaneX = 9;
    PlaneY = 9;
    EnemyX = returnEnemyX();
    EnemyY = 1;
    Pause = false;
    SpeedTimes = 1;
    BulletX = PlaneX;
    BulletY = PlaneY;
    Fired = false;
    Killed = false;
}
void show() {
    int i = 0, j = 0;

    for( i = 0; i <= BackGroundHeight; i++ ) {
        for( j = 0; j <= BackGroundWidth; j++ ) {
            judgeOutput( j, i );
        }
        printf("\n");
    }
    gotoxy( 0, 0 );
}
void updateWithInput() {
    char ch = 0;

    if( kbhit() ) {
        ch = getch();
        switch ( ch ) {
            case 'a' : {
                if( PlaneX > 1 ) {
                    PlaneX--;
                }
                break;
            }
            case 'd' : {
                if( PlaneX < BackGroundWidth-1 ) {
                    PlaneX++;
                }
                break;
            }
            case 'w' : {
                if( PlaneY > 1 ) {
                    PlaneY--;
                }
                break;
            }
            case 's' : {
                if( PlaneY < BackGroundHeight-1 ) {
                    PlaneY++;
                }
                break;
            }
            case 'p' : {
                Pause = !Pause;
                break;
            }
            case ' ' : {
                Fired = true;
                BulletX = PlaneX;
                BulletY = PlaneY;
            }
            default : {
                break;
            }

        }

        if( ch <= '9' && ch >= '1' ) {
            SpeedTimes = atoiMy( ch );
        }
    }
}
void updateWithoutInput() {

    if( EnemyY < BackGroundHeight-1 ) {
        EnemyY += SpeedEnemyY();
    }
    else {
        EnemyX = returnEnemyX();
        EnemyY = 1;
    }

    BulletY--;

    if( BulletY == EnemyY && BulletX == EnemyX ) {

        EnemyX = returnEnemyX();
        EnemyY = 1;
    }
}
void judgeOutput( int x, int y ) {
    if( x == PlaneX && y == PlaneY ) {
        printf("*");
    }
    else if( x == 0 || x == BackGroundWidth ) {
        printf("|");
    }
    else if( y == 0 || y == BackGroundHeight ) {
        printf("-");
    }
    else if( (x == EnemyX && y == EnemyY) && !Killed ) {
        printf("0");
    }
    else if( (x == BulletX && y == BulletY) && Fired ) {
        printf("|");
    }
    else {
        printf(" ");
    }
}

void gotoxy(int x, int y) {

    COORD coord = {x, y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

int returnEnemyX() {

    int tmp = 0, enemyx = 0;
    srand(time(NULL));
    tmp = rand()%BackGroundWidth;

    enemyx = tmp > 0 ? tmp : tmp+1;//There is little chance for 0;

    return enemyx;
}

int SpeedEnemyY() {

    if( Pause ) {
        return 0;
    }

    static int i = 1;

    if( i % SpeedTimes == 0 ) {
        i = 1;
        return 1;
    }
    else {
        i++;
        return 0;
    }

}

int atoiMy( char ch ) {

    return ch - '0';
}



断更的几篇,会陆续补上。(差两篇)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值