1. 求助大神
C++语言写的打砖块游戏源码
不用graphics.h的那种
因为学校电脑没有运行它的图
选择好的电脑学校要从以下几个方面考虑:
1、办学时间长短,像办学时间长的院校,应该靠谱一点。。
2、办学规模,一定要有独立的校园环境,
3、教学水平,这是决定你能不能学到技术的根本,教学水平包括专业设置、师资力量、教学配套设施、项目实训等。
4、就业保障,学校是不是安排工作,是不是有就业指导中心,有多少家用人单位与他们合作过等等。
2. 用C++编写的小游戏源代码
使用语言:C++使用工具:vs2019
3. 求大神解析一下C++打砖块游戏程序,比如说是如何增加关卡,声音,窗口颜色等 ,帮忙帮忙 期末老师提问 急
声音播放你装播放控件,移动时发消息,给声音播放。串口颜色可以画背景,也可以加载BMP图片,关卡加判断,打一个累加1,当达到多少时进入下一关。
4. VC++编程打砖块的游戏(急)
你可以把MFC中的代码提炼出来 写到一般的创建的窗口中就是了啊
5. 求基于C++的贪吃蛇游戏完整代码,能运行出来的~~~
一个参考实例,在VC++6.0中可以运行成功(可能还有问题)
#include
#include
#include
#include
#include //使用当前时间做种子;
enum dir{up,down,left,right}; //枚举类型enum dir;
//围墙;
class Fence{
public:
void InitFence();
void OutputF();
public:
char game[20][20];
}f; //定义对象;
//画框框;
void Fence::InitFence(){
for(int i=0; i<20; i++)
for(int j=0; j<20; j++){
if(i==0||i==19||j==0||j==19)
game[i][j]= '*';
else game[i][j]= ' ';
}
}
//显示框框;
void Fence::OutputF(){
for(int i=0; i<20; i++){
for(int j=0; j<20; j++)
cout<<game[i][j]<<' ';
cout<<endl;
}
}
//蛇结点;
class SnakeNode{
private:
int x,y;
SnakeNode *prior,*next;
public:
void add_head(int x,int y);
int get_x();
int get_y();
void delete_tail();
}*head=NULL, *tail =NULL;
//插入头结点;
void SnakeNode::add_head(int x,int y){
SnakeNode *q=new SnakeNode;
q->x =x; q->y =y;
q->next =head;
q->prior =NULL;
if(head) head->prior =q;
head =q;
if(!tail) tail =head;
f.game[x][y]= '*'; //f对象可以在定义Fence类时定义; 且Fence类在SnakeNode类前定义;
}
int SnakeNode::get_x(){
return x;
}
int SnakeNode::get_y(){
return y;
}
//删除尾结点;
void SnakeNode::delete_tail(){
SnakeNode *p =tail;
f.game[tail->get_x()][tail->get_y()]= ' ';//把尾结点的坐标表示的'*'置为空格;
if(tail==head)
tail= head= NULL;
else{
tail= tail->prior;
tail->next= NULL;
}
delete p;
}
//move移动;
class move{
public:
dir point; //枚举变量point: 控制方向;
int food_x;
int food_y;
public:
void moving();
void change_point(char); //改变方向;
void get_food();
};
void move::moving(){
int a,b;
a= head->get_x(); //取得头结点横坐标
b= head->get_y(); //头结点纵坐标
switch(point){
case up: --a; break;
case down: ++a; break;
case left: --b; break;
case right: ++b; break;
}
if(a==19||b==19||a==0||b==0){ //判断是否撞墙;
cout<<"GAME OVER!!!"<<endl;
system("pause");
exit(0);
}
if(a==food_x && b==food_y){ //吃food;
head->add_head(a,b);
get_food();
}
else{
head->add_head(a,b); //插入头结点;
head->delete_tail(); //删除尾结点;
}
}
void move::change_point(char keydown){
switch(keydown){
case 'w': point= up; break;
case 's': point= down; break;
case 'a': point= left; break;
case 'd': point= right; break;
}
}
void move::get_food(){
srand((unsigned int) time(NULL)); //做种子(程序运行时间);
food_x= rand()%18+1;
food_y= rand()%18+1;
f.game[food_x][food_y]= '*';
}
//main();
int main()
{
cout<<"^_^ 请使用\" w,s,a,d \"控制方向 ^_^\n\n\n";
//画框框和小蛇;
move m;
f.InitFence();
head->add_head(4,3);
head->add_head(4,4);
head->add_head(4,5);
m.get_food();
f.OutputF();
while (true){
char keydown= getch(); //getch()返回键盘上读取的字符;包含头文件
m.change_point(keydown);
while(!kbhit()){ //判断有没有按键落下;
system("cls"); //清屏函数;
m.moving();
f.OutputF();
Sleep(200);
}
}
return 0;
}
6. 求c++游戏代码,要能打怪能做任务升级的那种
#include
using namespace std;
double shengmingli=200;//定义主角初始生命力
int gongjili=10;//定义主角初始攻击力
int fangyuli=20;//定义主角初始防御力
int money=200;//定义主角初始金钱数量
bool guoguan;//定义是否通关判定
void wuqidian();//定义武器店函数
void yaodian();//定义药店函数
void guaiwu1();//定义小怪物函数
void guaiwu2();//定义大怪物函数
int main()
{
cout<<"欢迎你开始游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
int xiaozhen;//定义选择项目
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
cin>>xiaozhen;
while(xiaozhen!=5)//输入5时退出游戏
{
if(shengmingli<=0)//主角生命力小于等于0时游戏结束
{
cout<<"你死啦!"<<endl;
break;
}
if(guoguan)
{
cout<<"恭喜通关!"<<endl;
break;
}
if(xiaozhen==6)//输入6可检测自己的状态
{
cout<<"你的生命力:"<<shengmingli<<endl;
cout<<"你的攻击力:"<<gongjili<<endl;
cout<<"你的防御力:"<<fangyuli<<endl;
cout<<"你拥有的钱:"<<money<<endl;
}
else
switch(xiaozhen)
{
case 1 : wuqidian();break;
case 2 : yaodian();break;
case 3 : guaiwu1();break;
case 4 : guaiwu2();break;
default : cout<<"请不要乱选!"<<endl;break;
}
cin>>xiaozhen;
}
if(xiaozhen==5)
{
cout<<"正在退出游戏……"<<endl;
}
cin.get();
cin.get();
return 0;
}
void wuqidian()
{
cout<<"欢迎来到武器店!"<<endl;
cout<<"1、买小刀(加攻击力)"<<endl;
cout<<"2、买短剑(加攻击力)"<<endl;
cout<<"3、买大砍刀(加攻击力)"<<endl;
cout<<"4、买电锯(加攻击力)"<<endl;
cout<<"5、买盾牌(加防御力)"<<endl;
cout<<"7、离开武器店"<<endl;
int wuqidian;
cin>>wuqidian;
while(wuqidian!=7)//输入7时结束函数
{
switch(wuqidian)
{
case 1 : if(money<10)
cout<<"你的钱不够"<<endl;//钱不够时返回Flase
else
cout<<"购买成功!"<<endl;//钱足够时返回True
gongjili+=20;
money-=10;
break;
case 2 : if(money<20)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
gongjili+=40;
money-=20;
break;
case 3 : if(money<30)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
gongjili+=60;
money-=30;
break;
case 4 : if(money<40)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
gongjili+=100;
money-=40;
break;
case 5 : if(money<10)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
fangyuli+=30;
money-=10;
break;
fangyuli+=60;
money-=20;
break;
default : cout<<"无"<<endl;
break;
}
cin>>wuqidian;
}
if(wuqidian==7)
{ //返回main()主函数
cout<<"欢迎下次再来!"<<endl;
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
}
/*
yaodian()的设置与wuqidian()相同,可参照阅读.
*/
void yaodian()
{
cout<<"欢迎来到药品店!"<<endl;
cout<<"1、买1号补血药(加生命)"<<endl;
cout<<"2、买2号补血药(加生命力)"<<endl;
cout<<"3、买3号补血药(加生命力)"<<endl;
cout<<"4、离开武器店"<<endl;
int yaodian;
cin>>yaodian;
while(yaodian!=1)
{
switch(yaodian)
{
case 1 : if(money<10)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
shengmingli+=30;
money-=10;
break;
case 2 : if(money<20)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
shengmingli+=50;
money-=1;
break;
case 3 : if(money<40)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
shengmingli+=100;
money-=40;
break;
default : cout<<"无"<<endl;
break;
}
cin>>yaodian;
}
if(yaodian==4)
{
cout<<"欢迎下次再来!"<<endl;
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
}
/*这里是两个战斗函数,使用指针来处理.避免造成内存崩溃.*/
void guaiwu1()
{
cout<<"开始与小怪物战斗!!!"<<endl;
double* g_shengmingli=new double;//定义怪物生命
int* g_gongjili=new int;//定义怪物攻击力
int* g_fangyuli=new int;//定义怪物防御力
int* g_money=new int;//定义怪物金钱
*g_shengmingli=20;
*g_gongjili=1;
*g_fangyuli=1;
*g_money=50;
double* tongji1=new double;//用来计算主角对怪物的杀伤
double* tongji2=new double;//用来计算怪物对主角的杀伤
*tongji1=0;
*tongji2=0;
int* huihe=new int;//定义回合数
*huihe=1;
cout<<"你开始对小怪物进行攻击!"<<endl;
int* xuanze=new int;
/*
攻击计算公式
杀伤=攻击力*2-防御力
玩家每回合可以选择攻击与逃跑
*/
while((*g_shengmingli)>0 && shengmingli>0 && (*xuanze)!=2)
{
cout<<"现在是"<<"第"<<*huihe<<"回合!"<<endl;
cout<<"请选择你的动作:\n";
cout<<"1、攻击\n2、逃跑\n";
cin>>*xuanze;
switch((*xuanze))
{
case 1 : cout<<"你对小怪物发动了攻击!"<<endl;
*g_shengmingli-=gongjili*2-(*g_fangyuli);
*tongji1=gongjili*2-(*g_fangyuli);
cout<<"你打掉了小怪物"<<*tongji1<<"的生命!"<<endl;
cout<<"小怪物还剩"<<(*g_shengmingli)-(*tongji1)<<"点生命"<<endl;
shengmingli-=(*g_gongjili)*2-fangyuli;
*tongji2=(*g_gongjili)*2-fangyuli;
cout<<"小怪物对你发动了攻击!"<<endl;
cout<<"小怪物打掉了你"<<*tongji2<<"的生命!"<<endl;
cout<<"你还剩"<<shengmingli-(*tongji2)<<"点生命"<<endl;break;
case 2 : cout<<"你决定逃跑!"<<endl;
cout<<"逃跑成功!"<<endl;continue;
default : cout<<"请不要乱选!"<<endl;
}
(*huihe)++;
}
if((*g_shengmingli)<=0)
{//杀死怪物后的返回
cout<<"小怪物被你杀死了!你真厉害!!!"<<endl;
money+=(*g_money);
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
else
if(shengmingli<=0)
{//被怪物杀死后的返回
cout<<"你被小怪物杀死了!游戏结束!!!"<<endl;
}
else
if((*xuanze)==2)
{//逃跑的返回
cout<<"你逃回了小镇!"<<endl;
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
delete g_shengmingli;
delete g_gongjili;
delete g_fangyuli;
delete g_money;
delete tongji1;
delete tongji2;
}
/*
设置均与void函数guaiwu1()相同,可参照上例阅读.
*/
void guaiwu2()
{
cout<<"开始与大怪物战斗!!!"<<endl;
double* g_shengmingli=new double;
int* g_gongjili=new int;
int* g_fangyuli=new int;
*g_shengmingli=50;
*g_gongjili=15;
*g_fangyuli=10;
double* tongji1=new double;
double* tongji2=new double;
*tongji1=0;
*tongji2=0;
int* huihe=new int;
*huihe=1;
cout<<"你开始对大怪物进行攻击!"<<endl;
int* xuanze=new int;
while((*g_shengmingli)>0 && shengmingli>0 && (*xuanze)!=2)
{
cout<<"现在是"<<"第"<<*huihe<<"回合!"<<endl;
cout<<"请选择你的动作:\n";
cout<<"1、攻击\n2、逃跑\n";
cin>>*xuanze;
switch((*xuanze))
{
case 1 : cout<<"你对大怪物发动了攻击!"<<endl;
*g_shengmingli-=gongjili*2-(*g_fangyuli);
*tongji1=gongjili*2-(*g_fangyuli);
cout<<"你打掉了大怪物"<<*tongji1<<"的生命!"<<endl;
cout<<"大怪物还剩"<<(*g_shengmingli)-(*tongji1)<<"点生命"<<endl;
shengmingli-=(*g_gongjili)*2-fangyuli;
*tongji2=(*g_gongjili)-fangyuli;
cout<<"大怪物对你发动了攻击!"<<endl;
cout<<"大怪物打掉了你"<<*tongji2<<"的生命!"<<endl;
cout<<"你还剩"<<shengmingli-(*tongji2)<<"点生命"<<endl;break;
case 2 : cout<<"你决定逃跑!"<<endl;
cout<<"逃跑成功!"<<endl;continue;
default : cout<<"请不要乱选!"<<endl;
}
(*huihe)++;
}
if((*g_shengmingli)<=0)
{
cout<<"大怪物被你杀死了!你真厉害!!!"<<endl;
guoguan=true;
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
else
if(shengmingli<=0)
{
cout<<"你被大怪物杀死了!游戏结束!!!"<<endl;
}
else
if((*xuanze)==2)
{
cout<<"你逃回了小镇!"<<endl;
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
delete g_shengmingli;
delete g_gongjili;
delete g_fangyuli;
delete tongji1;
delete tongji2;
}
7. 做一个打砖块的游戏 用C语言
请编译前在主目录下建立一个文本文件(xia.txt)内容为:0 0 0 0 0
哈哈
#include
#include
#include
#include
union REGS regs;
unsigned int zhuan[5];
int Msinit();
void Setmouse(int left,int right,int top,int buttom);
int Msread(int *xp,int *yp,int *bup);
void Draw(int x,int y,int sizex,int sizey);
main()
{int mode=VGAHI,driver=VGA;
unsigned int l;
int i,gi,gj,j,flag=1;/*i,j是循环变量,flag是标记变量,-1:向x负半轴移动,+1:向x正半轴移动*/
double qx,qy,k,b=0.0,speech=0.4,x,y;
double r=2.0,bx=60.0,byy=270.0;
double pianx=100.0,piany=100.0,tx=20.0,ty=10.0,jx=2.0,jy=2.0;
int mx=0,my=0,mb,sum;/*sum纪录砖块的数目*/
FILE * p;
if((p = fopen("xia.txt", "r")) == NULL)
{printf("The file cannot open!\n");
exit(1);}
initgraph(&driver,&mode,"\\tc\\BGI");
Msinit();
Setmouse((int)(pianx+1+bx/2),(int)((tx+jx)*8+pianx-1-bx/2),(int)byy+piany,(int)byy+piany);
star:cleardevice();/*程序重载的介入点*/
sum=0;
qx=100.0+pianx-10;qy=180.0+pianx-10;k=0.33;
setcolor(7);
rectangle((int)(pianx-2),(int)(piany-2),(int)((tx+jx)*8+2+pianx),302+piany);
setcolor(1);
rectangle((int)(pianx-1),(int)(piany-1),(int)((tx+jx)*8+1+pianx),301+piany);
/*读取盘面情况*/
for(i=0;i<5;i++)
fscanf(p,"%x ",&zhuan[i]);
/*画砖块*/
for(i=0;i<5;i++)
{l=1;
for(j=0;j<16;j++)
{if((zhuan[i]&l)==0)
{Draw((int)((jx+tx)*((16*i+j)%8)+pianx+jx),(int)((jy+ty)*((int)((16*i+j)/8))+piany+jy),
(int)tx,(int)ty);
sum+=1;
}
l=l*2;
}
}
for(;;)
{ setfillstyle(1, 0);
bar(mx-bx/2,my,mx+bx/2,my+5);
Msread(&mx, &my, &mb);
Draw(mx-bx/2,my,bx,5);
setcolor(0);
circle(qx,qy,r);
/*判断求是否反弹*/
if(qx-r=(tx+jx)*8+pianx-1) {flag=-flag;k=-k;}
if(qy-r<=piany+1) k=-k;
for(gi=0;gi<5;gi++)
{l=1;
for(gj=0;gj<16;gj++)
{
if((zhuan[gi]&l)==0)
{j=(16*gi+gj)/8;
i=(16*gi+gj)%8;
x=(jx+tx)*i+jx+tx/2+pianx;
y=(jy+ty)*j+jy+ty/2+piany;
/*边判断1*/
if(qy>=y-ty/2 && qy<=y+ty/2 &&(pow(qx+r-x+tx/2,2)<1 || pow(qx-r-x-tx/2,2)<1))
{flag=-flag;k=-k;
zhuan[gi]=zhuan[gi]|l;
sum-=1;
if(sum==0) {printf("Win!\n");getch();goto star;}
setfillstyle(1, 0);
bar((jx+tx)*i+pianx+jx,(jy+ty)*j+piany+jy,(jx+tx)*i+pianx+jx+tx,(jy+ty)
*j+piany+jy+ty);
}
else
/*边判断2*/
if(qx>
;=x-tx/2 && qx<=x+tx/2 &&(pow(qy+r-y+ty/2,2)<1 || pow(qy-r-y-ty/2,2)<1))
{k=-k;
zhuan[gi]=zhuan[gi]|l;
sum-=1;
if(sum==0) {printf("Win!\n");getch();goto star;}
setfillstyle(1, 0);
bar((jx+tx)*i+pianx+jx,(jy+ty)*j+piany+jy,(jx+tx)*i+pianx+jx+tx,(jy+ty)
*j+piany+jy+ty);
}
else
/*角判断*/
if(pow(qx-x+tx/2,2)+pow(qy-y+ty/2,2)<=r*r || pow(qx-x-tx/2,2)+pow(qy-y+ty/2,2)<=r*r ||
pow(qx-x+tx/2,2)+pow(qy-y-ty/2,2)<=r*r || pow(qx-x-tx/2,2)+pow(qy-y-ty/2,2)<=r*r)
{flag=-flag;
zhuan[gi]=zhuan[gi]|l;
sum-=1;
if(sum==0) {printf("Win!\n");getch();goto star;}
setfillstyle(1, 0);
bar((jx+tx)*i+pianx+jx,(jy+ty)*j+piany+jy,(jx+tx)*i+pianx+jx+tx,(jy+ty)
*j+piany+jy+ty);
}
}l=l*2;}}
/*棍棒的反弹*/
if(qx=mx-bx/2 && pow(qy+r-my,2)<1) {k=-(k/pow(k*k,0.5))*(0.3*bx/(pow(pow
(qx-mx,2),0.5)+0.000001));}
if((int)(qy+r)>my+0.5) {printf("DEAD!");Setmouse(0,0,640,480);getch();exit(1);}
b=qy-qx*k;
if(flag==1) qx=qx+speech/pow(1.0+k*k,0.5);
if(flag==-1) qx=qx-speech/pow(1.0+k*k,0.5);
qy=qx*k+b;/*计算球心坐标*/
setcolor(14);
circle((int)qx,(int)qy,r);
delay(1);
if(mb==1) {Setmouse(0,0,640,480);exit(1);}
}
}
/*这个函数用于实现鼠标的初始化*/
int Msinit()
{int recored;
regs.x.ax=0;
int86 (0x33, & regs, & regs);
recored=regs.x.ax;
if(recored==0)
{printf("Mouse not foundd or Mouse driver not installed.\n");
return 0;
}
else
return recored;
}
/*下面的函数用于实现设置鼠标的移动范围*/
void Setmouse(int left,int right,int top,int buttom)
{
regs.x.ax=7;
regs.x.cx=left;
regs.x.dx=right;
int86(0x33,& regs,& regs);
regs.x.ax=8;
regs.x.cx=top;
regs.x.dx=buttom;
int86(0x33,& regs,& regs);
}
/*下面这个函数用于实现鼠标的读取*/
int Msread(int *xp,int *yp,int *bup)
{int xnew,ynew,ch;
if(kbhit()) return getch();
regs.x.ax=3;
int86(0x33,& regs,& regs);
xnew=regs.x.cx;
ynew=regs.x.dx;
*bup=regs.x.bx;
*xp=xnew;
*yp=ynew;
return -1;
}
/*下面这个子程序是完成描绘一个按钮*/
void Draw(int x,int y,int sizex,int sizey)/* x, y为左上角坐标sizex,sizey为长和宽*/
{int sizx=sizex-1;
int sizy=sizey-1;
setcolor(15);/*这里最好用白色*/
line(x,y,x+sizx-1,y);
line(x,y+1,x+sizx-2,y+1);
line(x,y,x,y+sizy-1);
line(x+1,y,x+1,y+sizy-2);
setcolor(8);/*这里最好用深灰色*/
line(x+1,y+sizy,x+sizx,y+sizy);
line(x+2,y+sizy-1,x+sizx,y+sizy-1);
line(x+sizx-1,y+1,x+sizx-1,y+sizy);
line(x+sizx,y+2,x+sizx,y+sizy);
setcolor(7);/*这里最好用灰色*/
putpixel(x,y+sizy,3);
putpixel(x+1,y+sizy-1,3);
putpixel(x+sizx,y,3);
putpixel(x+sizx-1,y+1,3);
setfillstyle(1, 7);/*这里最好用灰色,设置填充模式*/
bar(x+2,y+2,x+sizx-2,y+sizy-2);
}
8. c++编程小游戏代码
以下是贪吃蛇源代码: #include#include#include#include#include#define N 21void gotoxy(int x,int y)//位置函数{COORD pos;pos.X=2*x;pos.Y=y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}void color(int a)//颜色函数{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);}void init(int apple[2])//初始化函数(初始化围墙、显示信息、苹果){int i,j;//初始化围墙int wall[N+2][N+2]={{0}};for(i=1;i0;i--){snake[i][0]=snake[i-1][0];snake[i][1]=snake[i-1][1];gotoxy(snake[i][0],snake[i][1]);color(14);cout<<"★"<<endl;}if(kbhit()){gotoxy(0,N+2);ch=getche();}switch(ch){case 'w':snake[0][1]--;break;case 's':snake[0][1]++;break;case 'a':snake[0][0]--;break;case 'd':snake[0][0]++;break;default: break;}gotoxy(snake[0][0],snake[0][1]);color(14);cout<<"★"<<endl;Sleep(abs(200-0.5*score));if(snake[0][0]==apple[0]&&snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1{score++;len++;snake=(int**)realloc(snake,sizeof(int*)*len);snake[len-1]=(int*)malloc(sizeof(int)*2);apple[0]=rand()%N+1;apple[1]=rand()%N+1;gotoxy(apple[0],apple[1]);color(12);cout<<"●"<<endl;gotoxy(N+5,3);color(20);cout<<score<<endl;}if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到围墙后失败{gotoxy(N/2,N/2);color(30);cout<<"失败!!!"<<endl;for(i=0;i<len;i++)free(snake[i]);Sleep(INFINITE);exit(0);}}return 0;}