游戏源码分享资源分享网站?游戏源码是什么东西

大家好,今天小编来为大家解答游戏源码分享资源分享网站这个问题,游戏源码是什么东西很多人还不知道,现在让我们一起来看看吧!

每天一个C语言小项目,提升你的编程能力!

【第一版】

花了一天时间,用easyx做了一个小游戏,程序中所有的类函数都是内联函数,大约300行。

【第二版】

主要做了代码优化,加强可读性。

同时改了操作方式,玩家和敌人都可以在x、y方向上移动,敌人每隔一段时间会随机换向。

游戏运行截图如下:

操作方式

玩家通过方向键移动,z键射击,左Shift进入低速移动模式提高操作精度。

代码说明

关于无阻塞延时,首先,先要ctime创建一个clock_t变量a,初始化为clock(),貌似是自从1970年到现在的毫秒数。

我们要每隔0.5秒执行函数func()一次。

那么创建主循环while(1),调用前用clock()-a;如果clock()-a>500,那么执行func(),并把a重新赋值为clock()。

如果使用Sleep(500)的话,这个循环就只能执行func函数了,在此期间什么也做不了。

代码展示:

(直接上源码,大家可以看注释)

/*作者:STF(QQ:2292683261)*/\ninclude<cstdlib>\ndefineFRAMERATE20\t\t\t\t\t//画面刷新的周期(ms)\ndefineE_FIRERATE350\t\t\t\t\t//敌人射击间隔\ndefineBACKGROUND80\t\t\t\t\t//绘制背景线条的周期\n\ndefineMAX_PLAYER_BULLETS40\t\t\t//最多同屏自机子弹数目\n34;微软雅黑&34;方向键移动,Z攻击,左Shift切换低速模式&39;Z&34;黑体&34;你打败了boss!你赢了!!&34;黑体&34;你被boss打败了!&34;黑体&34;BOSS的生命值:&34;玩家的生命值:”);\n\tsetfillcolor(RGB(0,255,1));\n\tsetlinecolor(WHITE);\n\trectangle(160,515,560,540);\t\t\t\t\t\t\t\t//血条外框\n\tsetfillcolor(RGB(0,255,1));\n\tsetlinecolor(RGB(255,255,255));\n\tif(player_health>0)\n\t\tfillrectangle(160,515,160+player_health*4,540);\t//玩家血条\n\tsetlinecolor(WHITE);\n\trectangle(160,485,560,510);\t\t\t\t\t\t\t\t//敌人血条外框\n\tsetfillcolor(RGB(230,0,1));\n\tsetlinecolor(RGB(255,255,255));\n\tif(enemy_health>0)\n\t\tfillrectangle(160,485,160+enemy_health*4,510);\t//敌人血条\n\n}\n\nvoidshow_player()\n{\n\tif(isBleeding_p)\n\t\tsetfillcolor(RGB(255,0,0));\n\telse\n\t\tsetfillcolor(RGB(150,180,210));\n\tfillrectangle(player_pos[0]-25,player_pos[1]-25,player_pos[0]+25,player_pos[1]+25);\n\tsetfillcolor(RGB(100,200,180));\n\tfillrectangle(player_pos[0],player_pos[1]+5,player_pos[0]+40,player_pos[1]-5);\n}\n\nvoidshow_enemy()\n{\n\tif(isBleeding_e)\n\t\tsetfillcolor(RGB(255,0,0));\n\telse\n\t\tsetfillcolor(RGB(0,130,125));\n\tfillrectangle(enemy_pos[0]-20,enemy_pos[1]-40,enemy_pos[0]+20,enemy_pos[1]+40);\n\tsetfillcolor(RGB(100,200,180));\n\tfillrectangle(enemy_pos[0],enemy_pos[1]+5,enemy_pos[0]-40,enemy_pos[1]-5);\n}\n\nvoidmove_enemy()\n{\n\tstaticboolangle_v;\t\t//控制敌机的竖直移动方向,true为向上,到边缘就换向\n\tstaticboolangle_h;\t\t//控制敌机的水平移动方向,true为向左,到边缘就换向\n\tstaticclock_tinterval;\t//定时随机换向\n\n\tif(clock()-interval>=2000)\n\t{\n\t\tinterval=clock();\n\t\tif(rand()%2)\t\t\t//一般的概率换向\n\t\t\tangle_v=!angle_v;\n\t\tif(rand()%2)\n\t\t\tangle_h=!angle_h;\n\t}\n\n\tif(angle_v==true)\t\t//到了地图边缘就调头\n\t\tenemy_pos[1]-=3;\n\telse\n\t\tenemy_pos[1]+=3;\n\tif(angle_h==true)\n\t\tenemy_pos[0]-=3;\n\telse\n\t\tenemy_pos[0]+=3;\n\n\n\tif(enemy_pos[1]>=440)\n\t\tangle_v=true;\n\telseif(enemy_pos[1]<=40)\n\t\tangle_v=false;\n\n\n\tif(enemy_pos[0]>=580)\n\t\tangle_h=true;\n\telseif(enemy_pos[0]<=380)\n\t\tangle_h=false;\n\n}\n\nvoiddraw_background()\n{\n\tsetlinecolor(WHITE);\n\tintn_b_l=number_lines;\t\t//待处理线条数目\n\tfor(inti=0;i<MAX_LINES&&(n_b_l>0);++i)\n\t{\n\t\tif(line_slots[i]==true)\n\t\t{\n\t\t\tif(background_line[i][0]+background_line[i][2]<=0)\t\t//说明线条出了屏幕\n\t\t\t{\n\t\t\t\t–number_lines;\n\t\t\t\tline_slots[i]=false;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tbackground_line[i][0]-=10;\t\t\t\t//线条移动\n\t\t\t\tline(background_line[i][0],background_line[i][1],background_line[i][0]+background_line[i][2],background_line[i][1]);\n\t\t\t}\n\t\t\t–n_b_l;\n\t\t}\n\n\t}\n}\n\nintgenerate_line()\n{\n\tif(number_lines>=MAX_LINES)\n\t\treturn-1;\n\t++number_lines;\n\tfor(inti=0;i<MAX_LINES;++i)\n\t{\n\t\tif(line_slots[i]==false)\n\t\t{\n\t\t\tline_slots[i]=true;\n\t\t\tbackground_line[i][0]=640;\t\t\t\t//线条出现于屏幕最右边\n\t\t\tbackground_line[i][1]=rand()%480;\t\t//线条高度随机\n\t\t\tbackground_line[i][2]=10+rand()%50;\t//线条长度随机在10-50像素之间\n\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn0;\n}\n\nintcreate_p_b()\n{\n\tif(number_p_b>MAX_PLAYER_BULLETS)\t\t\t//空间不够\n\t\treturn-1;\n\tfor(inti=0;i<MAX_PLAYER_BULLETS;++i)\t//搜索slots,寻找空位\n\t{\n\t\tif(p_b_slots[i]==false)\n\t\t{\n\t\t\tp_b_slots[i]=true;\n\t\t\tplayer_bullet[i][0]=player_pos[0]+45;\n\t\t\tplayer_bullet[i][1]=player_pos[1];\t//创建子弹\n\t\t\t++number_p_b;\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn0;\n}\n\nintcreate_e_b()\n{\n\tif(number_e_b>MAX_ENEMY_BULLETS)\t\t\t\t//空间不够\n\t\treturn-1;\n\tfor(inti=0;i<MAX_ENEMY_BULLETS;++i)\t\t//搜索slots,寻找空位\n\t{\n\t\tif(e_b_slots[i]==false)\n\t\t{\n\t\t\te_b_slots[i]=true;\n\t\t\tenemy_bullet[i][0]=enemy_pos[0]-45;\n\t\t\tenemy_bullet[i][1]=enemy_pos[1];\t\t//创建子弹\n\t\t\t++number_e_b;\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn0;\n}\n\nintdestroy_p_b(intindex)\n{\n\tif(index>MAX_PLAYER_BULLETS-1)\n\t\treturn-2;\n\tif(p_b_slots[index]==false)\n\t\treturn-1;\n\tp_b_slots[index]=false;\n\t–number_p_b;\n\treturn0;\n}\n\nintdestroy_e_b(intindex)\n{\n\tif(index>MAX_ENEMY_BULLETS-1)\n\t\treturn-2;\n\tif(e_b_slots[index]==false)\n\t\treturn-1;\n\te_b_slots[index]=false;\n\t–number_e_b;\n\treturn0;\n}

大家赶紧去动手试试吧!

此外,我也给大家分享我收集的其他资源,从最零基础开始的教程到C语言C++项目案例,帮助大家在学习C语言的道路上披荆斩棘!

编程学习书籍分享:

编程学习视频分享:

整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)最重要的是你可以在群里面交流提问编程问题哦!

对于C/C++感兴趣可以关注小编在后台私信我:【编程交流】一起来学习哦!可以领取一些C/C++的项目学习视频资料哦!已经设置好了关键词自动回复,自动领取就好了!

关于游戏源码分享资源分享网站到此分享完毕,希望能帮助到您。

Published by

风君子

独自遨游何稽首 揭天掀地慰生平