分享

会动的机器人,C++也能轻松做动画

 凤飞图书 2023-03-22 发布于浙江

No1

学C++可不是只能枯燥刷题呢,也可以调动大脑给C++的学习增添一丝丝乐趣。

比如:做一个跳舞的机器人、设计一把可以拉开的弓 ··· ···

安装了C++学习神器:轻、便、快,学习C++的神器

紧接着,我们捣鼓了一番字母符号:字母符号都被C++玩坏

乘着ChatGPT的热度,我们写个程序:C++写一个蹩脚的ChatGPT,真蹩脚呀

今天需要用C++做个动画,天啦C++要做动画,那不得写很多很多行代码。

是的哦,需要写很多的代码,但是并不复杂都是相同的代码,只需要我们有一些耐心即可。

No2

兴趣是最好的老师,很多家长和小伙伴对C++学习的目的是信奥赛。面对信奥赛刷题是不可避免的,而且需要大量的刷题。这需要小伙伴可以产生刷题通过后的成就感,也需要自驱的学习力。

但是自驱的学习动力和内在的成就感,不是一蹴而就培养出来的。那么在前期的学习中,穿插一些项目制的学习内容,也是不错的选择。

#include<iostream>#include<windows.h>using namespace std;
int main(){ while (true){ cout << " *****" << endl; cout << " *****" << endl; cout << " *****" << endl; cout << " *" << endl; cout << " *" << endl; cout << "****** * ******" << endl; cout << " *" << endl; cout << " *" << endl; cout << " * *" << endl; cout << " * *" << endl; cout << " * *" << endl; cout << " * *" << endl; Sleep(100); system("cls");     cout << "*    *****    *" << endl; cout << " * ***** *" << endl; cout << " * ***** *" << endl; cout << " * * *" << endl; cout << " * * *" << endl; cout << " * * *" << endl; cout << " *" << endl; cout << " *" << endl; cout << " * *" << endl; cout << " * *" << endl; cout << " * *" << endl; cout << " * *" << endl; Sleep(100); system("cls"); } return 0;}

来吧,一起写程序,将代码写入 dev C++,进行编译运行看看效果。



No3

找出没有学习过的语句,一起研究研究。

#include<windows.h>是写window程序需要的重要头文件。因为我们后面的程序中使用到了 Sleep() 函数,所以这里我们需要使用这个头文件。
#include<windows.h>

在这部分,我们先不用深入的理解它。只需要知道被它的大括号包裹的程序会不停的重复执行就可以了。想要什么程序不停的运行,就写到大括号里面。这就是为什么机器人的舞蹈永不停止。
while (true){}

它的作用是让程序暂停一会,要使用它,需要用到头文件:#include<windows.h>。还记得Scratch中的【等待1秒】积木块不。

使用方法:

它有一个参数,参数为 int 类型,表示要暂停的毫秒数。

注:1000毫秒=1秒

特别注意:S要大写哟!


Sleep(100);

执行控制台命令cls,功能是清屏,清除所有屏幕显示信息。这样两个机器人图案就不会打架了。
system("cls");

No4

    cout << "     *****" << endl;    cout << "     *****" << endl;    cout << "     *****" << endl;    cout << "       *" << endl;    cout << "       *" << endl;    cout << "****** * ******" << endl;    cout << "       *" << endl;    cout << "       *" << endl;    cout << "      * *" << endl;    cout << "     *   *" << endl;    cout << "    *     *" << endl;    cout << "   *       *" << endl;

这里就只有一句 cout << "",里面的图案创意完全靠你动脑筋了。如果想不出来,可以尝试搜索,也可以试试聊天时候的符号表情,或者看看被玩坏的字符或许可以激发一些创意灵感。

No5

最后还是需要留个作业,试试看可以把射击编写出来吗?

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多