常用C++代码——整理自己的通用代码

一、文件读写

c语言版本(无顺序添加读取方便)

#include <stdio.h>
typedef struct {
    char a[10];
    char b[11];
}stu;
int main()
{
    stu test = {0};
    FILE *fp;
    fp = fopen("D:\\path", "w+");  // +代表可写,没有权限为只读, b表示二进制,a表示追加(打开或新建,文件存在则打开),r表示打开(文件要存在),w表示新建文件(文件存在则覆盖)
    //"r" "打开一个文本文件,文件必须存在,只允许读
    //"rb" 打开一个二进制文件,文件必须存在,只允许读
    //“rb+”打开一个二进制文件,文件必须存在,允许读写
    //"w" = “wt”新建一个文本文件,已存在的文件将内容清空,只允许写
    if (!fp)
    {
        return 0;
    }
    fwrite(&test, sizeof(test), 1, fp);
    
    stu test2;
    fread(&test2, sizeof(test2), 1, fp);
    fclose(fp);
}

c++版本(格式化读写方便)

#include "stdafx.h"
#include <iostream>
#include <fstream> //打开文件供读写
using namespace std;

struct Game
{
	int num;
	int year;
	string location;
	string first;
	string second;
	string third;
};

int _tmain(int argc, _TCHAR* argv[])
{
	Game game = {0};
	char buffer[256];
	//ios::in读; ios::out写 ios::app追加 ios::binary二进制 ios::nocreate文件不存在不创建
	//ios::noreplace文件不存在才创建 ios::trunc清空原有内容 ios::ate位置移动到文件末尾
	//ios::beg文件头 ios::end文件未 ios::cur当前位置 如:file.seekg(10,ios::cur);
	
	ofstream outfile("C:\\Users\\Administrator\\Desktop\\t11.txt", ios::out);    // 写入数据到文件
	outfile << game.location.c_str() << game.num;   // 写入结构体

	ifstream in_file("C:\\Users\\Administrator\\Desktop\\t11.txt", ios::trunc);	// 读取数据
	Game game2 = {0};
	if (!in_file.is_open())
	{
		std::cout << "error" << std::endl;
		return 0;
	}
	in_file >> buffer;   //只能是char数组

	in_file.close();
	outfile.close();

	return 0;
}

https://blog.csdn.net/qq_29406323/article/details/81261926

二、类型转换

string和其他类型
//C11
string to_string (int val);

//long -> string
long ulDefSiz = 100000;
stringstream ss;
ss << ulDefSize;
string str = ss.str();
OutputDebugStringA(str.c_str());

int和char*
//int-》char*
//1.windows的itoa
int aa = 30;
char c[8];
itoa(aa,c,16);

//2.sprintf
int aa = 30;
char c[8]; 
int length = sprintf(c, "%05X", aa); 
//3.stringstream

//char*->int
int i;
sscanf("17","%d",&i);
sscanf("17","%X",&i);

1  string s = "17";
2  int i = boost::lexical_cast<int>(s);
3  cout<<i<<endl; // 17

string s = "17";
stringstream ss;
ss<<s;
int i;
ss>>i;
cout<<i<<endl;

//std::stoi/stol/stoll等等函数


swprintf_s(wszCspName2, L"\"%s\" \"aaaa\"", wszCspName);  wszCspName和wszCspName2不能同一个


1、显式专换是定义让这个值类型转换成要用的值类型,例,定义int
i=5,想把他专换成char类型,就用显式转换(char)i。隐式转换是系统跟据程序需要而自动转换的,不需要定义,但并不是所有值类型都可以互相转
换,所以有了显式转换。例,int i=5; char j='a'; int n=i+j;因char可以隐式专换为int类型,所以结果n=102。
2、c方式类型转换:(target_type)(value),如int i = 5; char c = (char)(i);
c++方式类型转换:方式有4种
static_cast类似c语言的类型转换
dynamic_cast继承体系直接引用和指针直接的转换
const_cast常量转换,常量转换成非常量,非常量转换成常量
reintepret_cast重新解释转换,重新解释数值的含义,如int转换成char*等

https://www.cnblogs.com/hereis00/p/9994312.html  类型

三、字符串操作

// char数组赋值char数组
char tmp[10] = {0};
char tmp2[10] = "aa\0aaaa";
char *tmp3 = new char[10];
//方法一(tmp主动加\0)
strcpy(tmp, tmp2);	//执行到tmp2的\0结束(末尾加\0)
strcpy(tmp3, tmp2);
printf("strcpy tmp =%s\n", tmp);
printf("strcpy tmp3 =%s\n", tmp);
//方法二(tmp主动加\0)
sprintf_s(tmp, 4, tmp2); //(末尾加\0)//执行到tmp2的\0结束(sprintf第二个参数要在\0之后)
printf("sprintf_s tmp =%s\n", tmp);
//方法三(tmp不主动加\0)
memcpy(tmp, tmp2, 1);
printf("memcpy,tmp = %s\n", tmp);

//清空1
strcpy(tmp, tmp2);
ZeroMemory(tmp, strlen(tmp2));					//全部清空(无使用\0)
printf("ZeroMemory,tmp = %s\n", tmp);
//清空2
strcpy(tmp, tmp2);
memset(tmp, '\0', strlen(tmp)*(sizeof(char)));  //全部之\0,直到遇到tmp的\0
printf("memset,tmp = %s\n", tmp);


//赋值+串接字符串
//拼接:使用string 字符串和字符串
//拼接:使用char *strcat(char *dest, const char *src)   字符串和字符串
//拼接:使用sprinf_s(tmp, 10, "%s%s",tmp2,tmp3) 字符串和其他类型
//拼接:stringstream 字符串和其他类型
//比较 int memcmp(const void *str1, const void *str2, size_t n) == int strncmp(const char *str1, const char *str2, size_t n)
//比较 int strcmp(const char *str1, const char *str2)
//stringstream拼接     aaaa:10
#include <sstream>
char *str = "aaaa";
int port = 10;
stringstream ss;
ss << str;
ss << string(":");
ss << port;

四、效率计算(clock)

#include <stdio.h>
#include <time.h>

// clock_t 是clock()函数的返回值类型
clock_t start, stop;
// 记录被测代码的运行时间,以秒为单位
double duration;

int main()
{
    // 记录开始时间
    start = clock();
    //......代码
    // 记录结束时间
    stop = clock();
    // 计算代码执行花费的时间
    duration = ((double)(stop-start)) / CLK_TCK;   (不除CLK_TCK代表毫秒)

    return 0;
}
时间获取
std::string getDateTime() {
	//system_clock::time_point tp = system_clock::now();
	//time_t raw_time = system_clock::to_time_t(tp);
	//struct tm  *timeinfo = std::localtime(&raw_time);

	//std::stringstream ss;
	//ss << std::put_time(timeinfo, "%Y-%m-%d");
	ss << std::put_time(timeinfo, "%Y-%m-%d-%H-%M-%S");
	//return ss.str();
	time_t timep;
	time (&timep);
	char tmp[64];
	strftime(tmp, sizeof(tmp), "%Y-%m-%d",localtime(&timep) );
	return tmp;
}


#include “stdio.h” 
#include “stdlib.h” 
#include “time.h” 
int main( void )
 {
    long i = 10000000L; 
    clock_t start, finish; 
    double duration;
    printf( "Time to do %ld empty loops is ", i );
    start = clock(); while( i-- ) ; 
    finish = clock();
    duration = (double)(finish - start) / CLOCKS_PER_SEC;  ///clock是毫秒,除 CLOCKS_PER_SEC代表秒;
    printf( "%f seconds/n", duration ); 
    char tmp[MAX_PATH] = {0};
    sprintf_s(tmp, MAX_PATH, "debug===================%f", duration);
    OutputDebugStringA(tmp);
    system("pause"); 
}

五、错误

	try
	{	...
	}
	catch(boost::exception &e/*std::exception& e*/)
	{
		//std::cout << e.what() << std::endl;
		 std::cerr << boost::diagnostic_information(e);
	}

六、定时器

		//boost::asio::io_service io;
		//boost::asio::deadline_timer timer(io, boost::posix_time::seconds(3));
		//异步
		//timer.async_wait(&print);
		//io.run();
#include #include #include #include #include #include #define G 9.8 /*重力加速度*/ #define PI 3.141593 /*圆周率*/ #define L1 60 /*小屋运动的范围*/ #define T1 100 #define R1 200 #define B1 450 #define AMD1 5 /*修订数*/ #define AMD2 1.78 /*修订数*/ /*鼠标信息宏定义*/ #define WAITING 0xff00 #define LEFTPRESS 0xff01 #define LEFTCLICK 0xff10 #define LEFTDRAG 0xff19 #define RIGHTPRESS 0xff02 #define RIGHTCLICK 0xff20 #define RIGHTDRAG 0xff2a #define MIDDLEPRESS 0xff04 #define MIDDLECLICK 0xff40 #define MIDDLEDRAG 0xff4c #define MOUSEMOVE 0xff08 int Keystate; int MouseExist; int MouseButton; int MouseX; int MouseY; int up[16][16],down[16][16],mouse_draw[16][16],pixel_save[16][16]; void MouseMath()/*计算鼠标的样子*/ { int i,j,jj,k; long UpNum[16]={ 0x3fff,0x1fff,0x0fff,0x07ff, 0x03ff,0x01ff,0x00ff,0x007f, 0x003f,0x00ff,0x01ff,0x10ff, 0x30ff,0xf87f,0xf87f,0xfc3f }; long DownNum[16]={ 0x0000,0x7c00,0x6000,0x7000, 0x7800,0x7c00,0x7e00,0x7f00, 0x7f80,0x7e00,0x7c00,0x4600, 0x0600,0x0300,0x0300,0x0180 }; for(i=0;i=0;k--) up[i][k]=0; for(k=jj;k>=0;k--) down[i][k]=0; for(k=0;k<16;k++)/*四种组合方式*/ { if(up[i][k]==0&&down;[i][k]==0) mouse_draw[i][k]=1; else if(up[i][k]==0&&down;[i][k]==1) mouse_draw[i][k]=2; else if(up[i][k]==1&&down;[i][k]==0) mouse_draw[i][k]=3; else mouse_draw[i][k]=4; } } mouse_draw[1][2]=4;/*特殊点*/ } /*鼠标光标显示*/ void MouseOn() { int x=MouseX,y=MouseY; int i,j; int color; for(i=0;i<16;i++)/*画鼠标*/ { for(j=0;j<16;j++) { pixel_save[i][j]=getpixel(x+j,y+i);/*保存原来的颜色*/ if(mouse_draw[i][j]==1) putpixel(x+j,y+i,0); else if(mouse_draw[i][j]==2) putpixel(x+j,y+i,15); } } } /*隐藏鼠标*/ void MouseOff() { int i,j,x,y,color; x=MouseX; y=MouseY; for(i=0;i<16;i++)/*原位置异或消去*/ for(j=0;j<16;j++) { if(mouse_draw[i][j]==3||mouse_draw[i][j]==4) continue; color=getpixel(x+j,y+i); putpixel(x+j,y+i,color^color); putpixel(x+j,y+i,pixel_save[i][j]); } } /*鼠标状态值初始化*/ void MouseReset() { _AX=0x00; geninterrupt(0x33); } /*设置鼠标左右边界 lx:左边界 rx:右边界 */ void MouseSetX(int lx,int rx) { _CX=lx; _DX=rx; _AX=0x07; geninterrupt(0x33); } /*设置鼠标上下边界 uy:上边界 dy:下边界 */ void MouseSetY(int uy,int dy) { _CX=uy; _DX=dy; _AX=0x08; geninterrupt(0x33); } /*设置鼠标当前位置 x:横向坐标 y:纵向坐标 */ void MouseSetXY(int x,int y) { _CX=x; _DX=y; _AX=0x04; geninterrupt(0x33); } /*获取鼠标按下键的信息*/ /*是否按下左键 返回值: 1=按下 0=释放*/ int LeftPress() { _AX=0x03; geninterrupt(0x33); return(_BX&1); } /*是否按下中键 返回值同上 */ int MiddlePress() { _AX=0x03; geninterrupt(0x33); return(_BX&4); } /*是否按下右键 返回值同上 */ int RightPress() { _AX=0x03; geninterrupt(0x33); return(_BX&2); } /*获取鼠标当前位置*/ void MouseGetXY() { _AX=0x03; geninterrupt(0x33); MouseX=_CX; MouseY=_DX; } /*鼠标按键情况,返回0表示只移动,返回1表示左右键同时按下,2表示只按了左键,3表示只按了右键*/ int MouseStatus() { int x,y; int status; int press=0; int i,j,color; status=0;/*默认鼠标没有移动*/ x=MouseX; y=MouseY; while(x==MouseX&&y==MouseY&&status;==0&&press;==0) { if(LeftPress()&&RightPress;()) press=1; else if(LeftPress()) press=2; else if(RightPress()) press=3; MouseGetXY(); if(MouseX!=x||MouseY!=y) status=1; } if(status)/*移动情况才重新显示鼠标*/ { for(i=0;i<16;i++)/*原位置异或消去*/ for(j=0;j<16;j++) { if(mouse_draw[i][j]==3||mouse_draw[i][j]==4) continue; color=getpixel(x+j,y+i); putpixel(x+j,y+i,color^color); putpixel(x+j,y+i,pixel_save[i][j]); } MouseOn();/*新位置显示*/ } if(press!=0)/*有按键的情况*/ return press; return 0;/*只移动的情况*/ } /*定义玩家的结构体*/ struct Ren{ int x,y; int life; int color; int lr;/*1表示左,2表示右。*/ }; /*绘制游戏界面*/ void Desktop() { setcolor(14); line(320,0,320,480); rectangle(L1-20,T1-40,R1+20,B1+10); rectangle(640-(R1+20),(T1-40),640-(L1-20),B1+10); outtextxy(25,20,"P1"); outtextxy(345,20,"P2"); } /*把一个数字n转换成字符串,并存储在a中,带符号+-*/ void numtostr(int n,char a[5]) { int w,e; e=n; n=abs(n); a[3]=(n)+'0'; w=n/10; a[2]=(w)+'0'; w=w/10; a[1]=(w)+'0'; a[4]='\0'; if(e<0) a[0]='-'; else a[0]='+'; } /*把速度和角度装换成字符串输出*/ void AngleSpeed(double s,double angle) { int ss,aa; char zzs[5],zza[5]; int left,top,right,bottom; left=275; top=50; right=left+90; bottom=top+10; ss=(int)(s); aa=(int)((angle)*180/PI); numtostr(ss,zzs); numtostr(aa,zza); setfillstyle(1,15); setcolor(10); bar(left,top,right,bottom); outtextxy(left+5,top+3,zzs); outtextxy((left+right)/2+5,top+3,zza); circle(right-6,top+3,2); } /*实现人机对抗的函数*/ void Fire (int a[4],double *v,double *angle,int n)/*a数组存放对射的两点,v和angle存放机器射击的角度和速度,n表式机器射击的准确度*/ { int t; double vx,vy; double sx,sy; int m; m=12*4/n; randomize(); m=random(m)-m/2; t=20; sx=(double)(a[2]-a[0]); sy=(double)(a[3]-a[1]); vx=sx/(double)(t); vy=(sy-0.5*PI*(double)(t*t))/(double)(t); *angle=atan((-vy)/vx); *v=sqrt(vx*vx+vy*vy); *v=(*v)*(AMD2+0.01*(double)(m)); AngleSpeed(*v,*angle); } /*绘制生命线的函数*/ void LifePicture(int life,int color,int location) { char lm[5]; int l,t,r,b; l=50; t=20; r=l+200; b=t+10; numtostr(life,lm); setfillstyle(1,color); setcolor(15); if(location==1||location==3) { bar(l,t,r,b); setfillstyle(1,4); bar(l,t+(b-t)/4,l+life,t+3*(b-t)/4); setfillstyle(1,color); bar(r+10,t,r+50,b); outtextxy(r+10+5,t+2,lm); } else { l=320+50; r=l+200; bar(l,t,r,b); setfillstyle(1,4); bar(l,t+(b-t)/4,l+life,t+3*(b-t)/4); setfillstyle(1,color); bar(r+10,t,r+50,b); outtextxy(r+10+5,t+2,lm); } } /*绘制小屋的函数*/ void RenPicture(int x,int y,int color) { setcolor(color); setwritemode(1); line(x,y-40,x-10,y-30);/*画头*/ line(x,y-40,x+10,y-30); line(x-10,y-30,x+10,y-30); line(x-5,y-30,x-5,y-10);/*画脖子*/ line(x+5,y-30,x+5,y-10); line(x-20,y-10,x+20,y-10);/*画身子*/ line(x-20,y+10,x+20,y+10); line(x-20,y-10,x-20,y+10); line(x+20,y-10,x+20,y+10); } /*绘制箭的函数*/ void PictureBullets (int wx,int wy,int tx,int ty) { setcolor(RED); line(wx,wy,tx,ty); line(wx-1,wy-1,tx,ty); line(wx+1,wy+1,tx,ty); } /*绘制小屋上箭的函数*/ void InitialArrow (int x,int y,int a[4]) { int addx,addy; addx=(a[2]-a[0])/6; addy=(a[3]-a[1])/6; PictureBullets(x+addx,y+addy,x,y); } /*判断点qx,qy在直线的什么位置*/ int PointPlace(int qx,int qy,int x1,int y1,int x2,int y2)/*返回0表示在直线上,当斜率存在时:1表示在直线的上面,2表示在直线的下面,当斜率不存在时:3表示在左面,4表示在右面*/ { int s; if(x1==x2) { if(qxx1) return 4; else return 0; } else { s=(int)(((double)(y1-y2))/((double)(x1-x2))*((double)(qx-x1))+(double)(y1)); if(qys) return 2; else return 0; } } /*根据两点坐标计算出两点距离和斜率。*/ void DistanceAngle (int twoxy1[4],double *distance,double *angle) { double a,b; if(twoxy1[0]!=twoxy1[2]) { a=(double)((double)((double)twoxy1[3]-(double)twoxy1[1])/(double)((double)twoxy1[0]-(double)twoxy1[2])); *angle=atan(a); if(twoxy1[0]<twoxy1[2]) *angle=PI+(*angle); } else if(twoxy1[1]<twoxy1[3]) *angle=PI/2; else *angle=-PI/2; b=(double)((double)(twoxy1[3]-twoxy1[1])*(double)(twoxy1[3]-twoxy1[1])+(double)(twoxy1[2]-twoxy1[0])*(double)(twoxy1[2]-twoxy1[0])); *distance=sqrt(b); } /*由速度角度算sx,sy随时间的变化*/ void RelativePosition(int *sx,int *sy,double v,double angle,double t) { *sx=(int)((v*cos(angle))*t); *sy=(int)((v*sin(angle))*t-0.5*G*t*t); } /*用鼠标画一条直线,把直线的两点坐标放在twoxy数组内。*/ void TwoPoints(int twoxy[4],int dx,int dy) { int i,q=1; int ddx=dx,ddy=dy; double speed=0.0,angle=0.0; twoxy[0]=0; twoxy[1]=0; setcolor(13); line(dx,dy+30,dx,dy-30); line(dx-30,dy,dx+30,dy); setcolor(4); MouseOn();/*显示鼠标*/ setwritemode(1); i=0; while(q) { if(i==1) { MouseOff(); DistanceAngle(twoxy,&speed;,&angle;); AngleSpeed(speed/AMD1,angle); InitialArrow (ddx,ddy,twoxy); setcolor(4); line(twoxy[0],twoxy[1],twoxy[2],twoxy[3]); MouseOn(); if((twoxy[2]!=MouseX)||(twoxy[3]!=MouseY)) { twoxy[2]=MouseX; twoxy[3]=MouseY; MouseOff(); DistanceAngle(twoxy,&speed;,&angle;); AngleSpeed(speed/AMD1,angle); InitialArrow (ddx,ddy,twoxy); setcolor(4); line(twoxy[0],twoxy[1],twoxy[2],twoxy[3]); MouseOn(); } } if(MouseStatus()) { sound(1000);/*响声函数*/ delay(10000); nosound(); delay(1000000); delay(1000000); delay(1000000); delay(1000000); if(i==0) { twoxy[0]=MouseX; twoxy[1]=MouseY; twoxy[2]=MouseX; twoxy[3]=MouseY; i=1; } else { MouseOff(); DistanceAngle(twoxy,&speed;,&angle;); AngleSpeed(speed/AMD1,angle); InitialArrow (ddx,ddy,twoxy); setcolor(4); line(twoxy[0],twoxy[1],twoxy[2],twoxy[3]); setcolor(13); line(dx,dy+30,dx,dy-30); line(dx-30,dy,dx+30,dy); q=0; i=0; } } } } /*发射箭,speed1和speed2控制速度,返回中弹位置*/ int Launch(int lx,int ly,int tx,int ty,int hm,int grade) { double speed1=0.01; int speed2=1000; int a[4]; int xx[2],xy[2]; double s=0.0,angle=0.0,t=0.0; lx=lx;ly=ly-50; if(hm==3) { a[0]=lx; a[1]=ly; a[2]=tx; a[3]=ty; Fire (a,&s,&angle;,grade); } else { TwoPoints(a,lx,ly); DistanceAngle(a,&s,&angle;); s=s/AMD1; } RelativePosition(&xx;[0],&xy;[0],s,angle,t-1); RelativePosition(&xx;[1],&xy;[1],s,angle,t); for(t=0.0;ly-xy[1]<480;t=t+speed1) { RelativePosition(&xx;[0],&xy;[0],s,angle,t-1); RelativePosition(&xx;[1],&xy;[1],s,angle,t); if(PointPlace(lx+xx[1],ly-xy[1],tx,ty-40,tx+10,ty-30)==2&&PointPlace;(lx+xx[1],ly-xy[1],tx,ty-40,tx-10,ty-30)==2&&PointPlace;(lx+xx[1],ly-xy[1],tx-10,ty-30,tx+10,ty-30)==1) { sound(4000);/*响声函数*/ delay(10000); nosound(); return 1; } if(PointPlace(lx+xx[1],ly-xy[1],tx-5,ty-30,tx+5,ty-30)==2&&PointPlace;(lx+xx[1],ly-xy[1],tx-5,ty-10,tx+5,ty-10)==1&&PointPlace;(lx+xx[1],ly-xy[1],tx-5,ty-30,tx-5,ty-10)==4&&PointPlace;(lx+xx[1],ly-xy[1],tx+5,ty-30,tx+5,ty-10)==3) { sound(3000);/*响声函数*/ delay(10000); nosound(); return 2; } if(PointPlace(lx+xx[1],ly-xy[1],tx-20,ty-10,tx-20,ty+10)==4&&PointPlace;(lx+xx[1],ly-xy[1],tx+20,ty-10,tx+20,ty+10)==3&&PointPlace;(lx+xx[1],ly-xy[1],tx-20,ty-10,tx+20,ty-10)==2&&PointPlace;(lx+xx[1],ly-xy[1],tx-20,ty+10,tx+20,ty+10)==1) { sound(2000);/*响声函数*/ delay(10000); nosound(); return 3; } if(ly-xy[1]<1) { delay(speed2); continue; } if(lx+xx[1]640-1) { return 0; } PictureBullets (lx+xx[0],ly-xy[0],lx+xx[1],ly-xy[1]); delay(speed2); PictureBullets (lx+xx[0],ly-xy[0],lx+xx[1],ly-xy[1]); } return 0; } /*小屋移动的函数*/ int MoveRen(struct Ren *p) { int a,k=19200,b=0,d; int q=1; randomize(); for(;q;) { if(b==1) { p->lr=3; } RenPicture(p->x,p->y,p->color); if(p->lr==3) { b=1; delay(10000); delay(10000); delay(10000); delay(10000); delay(10000); delay(10000); //sleep(1); d=random(10); if(d==0) k=19200; if(d==1) k=19712; if(d==2) k=18432; if(d==3) k=20480; if(d==4) k=7181; p->lr=1; } else { k=bioskey(0); } RenPicture(p->x,p->y,p->color); switch(k){ case 19200: /*按向左键*/ a=(p->x)-5; if(p->lr==1) { if(a>L1&&ax=a; break; } } else { if(a>640-R1&&ax=a; break; } } break; case 19712: /*按向右键*/ a=(p->x)+5; if(p->lr==1) { if(a>L1&&ax=a; break; } } else { if(a>640-R1&&ax=a; break; } } break; case 18432: /*按向上键*/ a=(p->y)-5; if(p->lr==1) { if(a>T1&&ay=a; break; } } else { if(a>T1&&ay=a; break; } } break; case 20480: /*按向下键*/ a=(p->y)+5; if(a>T1&&ay=a; } break; case 7181: /*enter键的扫描码*/ if(b==1) p->lr=3; q=0; break; case 283: return 0; } } RenPicture(p->x,p->y,p->color); return 1; } /*游戏开始前画面*/ int GameStar()/*返回1表示单人游戏初级,2表示单人游戏中级,3表示单人游戏高级,4表示两人对战,5表示退出游戏*/ { int q,k,h=0; for(;1;) { q=1; cleardevice();/*清屏函数*/ setcolor(15); settextstyle(0,0,5); outtextxy(100,100,"Start Game!"); settextstyle(0,0,1); outtextxy(20,300,"keys used:"); outtextxy(20,300," Arrow keys"); outtextxy(20,310," The left mouse button"); outtextxy(20,320," Enter"); outtextxy(20,330," Esc to Quit!"); setcolor(5); outtextxy(250,400,"One player!"); outtextxy(250,420,"Two players!"); outtextxy(250,440,"Quit!"); setwritemode(1); setcolor(6); rectangle(245,395+h*20,345,415+h*20); for(;q;) { setcolor(6); k=bioskey(0); sound(1000);/*响声函数*/ delay(10000); nosound(); if(k==20480) { rectangle(245,395+h*20,345,415+h*20); h=(h+1)%3; rectangle(245,395+h*20,345,415+h*20); }else if(k==7181) { if(h==0)/*单人游戏,选择等级*/ { cleardevice();/*清屏函数*/ setcolor(2); outtextxy(20,30," Esc to back!"); outtextxy(250,240,"Lower"); outtextxy(250,260,"Middle"); outtextxy(250,280,"Higher"); setcolor(4); rectangle(245,235+h*20,300,255+h*20); for(;q;) { k=bioskey(0); sound(1000);/*响声函数*/ delay(10000); nosound(); if(k==20480) { rectangle(245,235+h*20,300,255+h*20); h=(h+1)%3; rectangle(245,235+h*20,300,255+h*20); }else if(k==7181) { return h+1; }else if(k==283) { h=0; k=1; q=0; break; }else{} } } if(h==1)/*两人对抗*/ return 4; if(h==2)/*退出游戏*/ return 5; }else if(k==283) { return 5; }else {} } } } /*退出游戏画面*/ void GameOver() { cleardevice();/*清屏函数*/ setcolor(14); settextstyle(0,0,6); outtextxy(100,200,"Game Over!"); settextstyle(1,0,1); outtextxy(400,400,"Producer:ChenChen"); outtextxy(400,410," QQ:804620957"); outtextxy(400,420," Time:2010.5.28"); } /*主函数*/ void main() { int gd=DETECT,gm; int q=0,schoose=1; int out=1; int pmc=1; int cla2s=1; struct Ren ren1,ren2; initgraph(&gd;,&gm;,""); /* registerbgidriver(EGAVGA_driver);*/ cleardevice();/*清屏函数*/ MouseMath();/*计算鼠标形状,一开始必须使用,后面就不用了*/ MouseSetY(0,479); MouseSetX(0,649); MouseSetXY(100,100); for(;out;) { pmc=GameStar(); cleardevice();/*清屏函数*/ settextstyle(1,0,1);/*初始化*/ schoose=1; ren2.x=540;ren2.y=320;ren2.life=200;ren2.color=3;ren2.lr=2; if(pmc0&&ren2;.life>0;schoose++) { if(schoose%2) { RenPicture(ren1.x,ren1.y,ren1.color); if(MoveRen(&ren1;)==0) break; q=Launch(ren1.x,ren1.y,ren2.x,ren2.y,ren1.lr,cla2s); if(q==1) ren2.life=ren2.life-40; if(q==2) ren2.life=ren2.life-20; if(q==3) ren2.life=ren2.life-10; if(ren2.life<0) ren2.life=0; LifePicture(ren2.life,ren2.color,ren2.lr); } else { RenPicture(ren2.x,ren2.y,ren2.color); if(MoveRen(&ren2;)==0) break; q=Launch(ren2.x,ren2.y,ren1.x,ren1.y,ren2.lr,cla2s); if(q==1) ren1.life=ren1.life-40; if(q==2) ren1.life=ren1.life-20; if(q==3) ren1.life=ren1.life-10; if(ren1.life<0) ren1.life=0; LifePicture(ren1.life,ren1.color,ren1.lr); } } if(ren1.liferen2.life) { settextstyle(0,0,6); setcolor(ren1.color); outtextxy(150,280,"P1 win!"); settextstyle(1,0,1); } else { settextstyle(0,0,6); setcolor(15); outtextxy(150,280,"Drew!"); settextstyle(1,0,1); } getch(); } GameOver(); getch(); closegraph(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值