基于游戏开发的C语言程序设计-控制台中打游戏-从0到打飞机

简介

大一下学C++的时候老师给我们推荐了一个图形库Easyx,最后的大作业也是要求我们写图形化的东西。之后又在知乎看到童晶老师的文章,甚是敬佩,MOOC上有他的一门课程 - 基于游戏开发的C语言程序设计入门与实践

软件环境

我采用的是Win 10 + mingw64 + VS code
其它C环境也是可以的,VS 201x、2020都可,VC6.0的话emm赶紧换吧,想知道如何配置我上面的环境请查看这篇

输出0

这里x表示行数,y表示空格数
在return前加了getchar是防止窗口一闪而过,写过的都懂

#include <stdio.h>
//  效果需要在资源管理器运行exe来显示,vs code终端捕获有问题

//  在坐标(x,y)显示小球
void showBall(int x, int y)
{
    for(unsigned int i=0; i<x; i++)
        printf("\n");
    for(unsigned int j=0; j<y; j++)
        printf(" ");
    printf("0\n");
}

int main()
{
    //  在坐标(x,y)显示小球
	int x = 10;
    int y = 15;
	getchar();
	return 0;
}

自由落体的0

为了清除上一次显示的0,需要用到一个清除命令-cls

#include <stdio.h>
#include <stdlib.h>
//  效果需要在资源管理器运行exe来显示,vs code终端捕获有问题

//  在坐标(x,y)显示小球
void showBall(int x, int y)
{
    for(unsigned int i=0; i<x; i++)
        printf("\n");
    for(unsigned int j=0; j<y; j++)
        printf(" ");
    printf("0\n");
}

int main()
{
     for(unsigned int i=0; i<x; i++)
    {
        system("cls"); // 需要引入<stdlib.h>
        showBall(i,y);
    }
	getchar();
	return 0;
}

折返跑的0

Sleep函数并非必备的,不用的缺点就是0的移动速度会很快;当然可以用for循环嵌套来替代延时,但是这样会对cpu造成过多占用(见微机原理与接口技术)
Sleep函数的单位是ms

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
//  效果需要在资源管理器运行exe来显示,vs code终端捕获有问题

//  在坐标(x,y)显示小球
void showBall(int x, int y)
{
    for(unsigned int i=0; i<x; i++)
        printf("\n");
    for(unsigned int j=0; j<y; j++)
        printf(" ");
    printf("0\n");
}

int main()
{
	int x = 10;
    int y = 15;
    int height = 20;
    int velocity = 1;
    while(1)
    {
        system("cls"); 
        showBall(x, y);
        Sleep(60);      //延时函数,ms,需要引入windows.h或者cwindows.h
        x = x + velocity;
        if(x == height || x == 1)
            velocity = -velocity;
    }

	return 0;
} 

会转弯的0

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
//  效果需要在资源管理器运行exe来显示,vs code终端捕获有问题

//  在坐标(x,y)显示小球
void showBall(int x, int y)
{
    for(unsigned int i=0; i<x; i++)
        printf("\n");
    for(unsigned int j=0; j<y; j++)
        printf(" ");
    printf("0\n");
}

int main()
{
	int x = 10;
    int y = 15;
    unsigned int height = 20;
    unsigned int width = 40;
    int velocityX = 1;
    int velocityY = 1;
    while(1)
    {
        system("cls"); 
        showBall(x, y);
        Sleep(60);      //延时函数,ms,需要引入windows.h或者cwindows.h
        x = x + velocityX;
        y = y + velocityY;
        if(x == height || x == 0)
            velocityX = -velocityX;
        if(y == width || y == 0)
            velocityY = -velocityY;
    }
	return 0;
}

显示飞机

这里其实对上面的显示0进行稍微更改就好了

#include <stdio.h>

void showPlane(int x, int y)
{
      for(unsigned int i=0; i<x; i++)
           printf("\n");
       for(unsigned int j=0; j<y; j++)
           printf(" ");
       printf("*\n");
       for(unsigned int j=0; j<(y-2); j++)
           printf(" ");
       printf("*****\n");
       for(unsigned int j=0; j<(y-2); j++)
           printf(" ");
       printf(" * *\n");
}

int main()
{
    int x,y;
    x = 15;
    y = 20;
    showPlane(x, y);   
    
    getchar(); 
    return 0;
}

在这里插入图片描述

控制飞机-让飞机飞起来

这里我们开始用到了用户输入
scanf、getchar都是可以获取输入,但是缺点是必须敲回车才会读取输入,可是我们不想敲回车,怎么办呢??可以使用_getch()函数,并引入头文件<conio.h>,linux系统的话请参考 C语言中怎么使用户不用按回车键,输入字符后就直接调用函数

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

void showPlane(int x, int y)
{
    for(int i=0; i<x; i++)
        printf("\n");
    for(int j=0; j<y; j++)
        printf(" ");
    printf("*\n");
    for(int j=0; j<(y-2); j++)
        printf(" ");
    printf("*****\n");
    for(int j=0; j<(y-2); j++)
        printf(" ");
    printf(" * *\n");

}

int main()
{
    int x,y;
    char input;
    x = 15;
    y = 20;

    while(1)
    {
        system("cls"); 
        showPlane(x, y);
        //scanf("%c",&input);     
        //input = getchar();
        input = _getch();   //不需要回车,需要引入conio.h
        if(input == 'w')
            x--;
        else if(input == 's')
            x++;
        else if(input == 'a')
            y--;
        else if(input == 'd')
            y++; 
    }
    getchar();
    return 0;
}

显示开火中的飞机

这个其实难度比较小,只是循环在飞机头上加上炮弹即可

#include <stdio.h>

void showFirePlane(int x, int y)
{
    for(int i=0; i<x; i++)
    {
        for(int j=0; j<y; j++)
            printf(" ");
        printf("|\n");
    }
        
    for(int j=0; j<y; j++)
        printf(" ");
    printf("*\n");
    for(int j=0; j<(y-2); j++)
        printf(" ");
    printf("*****\n");
    for(int j=0; j<(y-2); j++)
        printf(" ");
    printf(" * *\n");
}

int main()
{
    int x,y;
    char input;
    x = 15;
    y = 20;
	showFirePlane(x,y);
   
    getchar();
    return 0;
}

Conclusion

kbhit()判断用户是否输入,是返回1<conio.h>
_getch()获取键盘输入,无需回车<conio.h>
system(“cls”);清除控制台的内容<stdlib.h>
Sleep(time);延时函数,单位ms<windows.h>

如果你觉得对你有帮助,不如点个赞支持鼓励下作者呗

References

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值