C编写的小游戏
【C语言程序设计】—最近超火的小游戏—【数字炸弹】!

【C语⾔程序设计】—最近超⽕的⼩游戏—【数字炸弹】!✍准备⼯作和建议⼀、程序的原理在动⼿编程之前,得先跟⼤家说⼀下这个程序是⼲什么的。
我们可以称呼这个游戏为《数字炸弹》。
游戏的原理是这样:每⼀轮电脑从 1 到 100 中随机抽⼀个整数。
电脑请求你猜这个数字,因此你要输⼊⼀个 1 到 100 之间的整数。
电脑将你输⼊的数和它抽取的数进⾏⽐较,并告知你的数⽐它的数⼤了还是⼩了。
然后它会再次让你输⼊数字,并告诉你⽐较的结果。
⼀直到你猜到这个数为⽌,⼀轮结束。
游戏的⽬的,当然就是⽤最少的次数猜到这个“神秘”数字。
虽然没有绚丽的图形界⾯,但是或多或少,这都是你的第⼀个游戏了,应该值得骄傲。
下⾯演⽰了⼀轮的样式,你要编程来实现它:这个数字是什么?50猜⼩了!这个数字是什么?75猜⼩了!这个数字是什么?85猜⼤了!这个数字是什么?80猜⼤了!这个数字是什么?78猜⼩了!这个数字是什么?79太棒了,你猜到了这个神秘数字!!⼆、随机抽取⼀个数但⼤家要问了:“如何随机地抽取⼀个数呢?不知道怎么办啊,⾂妾做不到啊。
”诚然,我们还没学习如何来产⽣⼀个随机数。
让亲爱的电脑兄来做这个是不简单的:它很会做运算,但是要它随机选择⼀个数,它还不知道怎么做呢。
事实上,为了“尝试”得到⼀个随机数,我们不得不让电脑来做⼀些复杂的运算。
好吧,归根结底还是做运算。
我们有两个解决⽅案:✎请⽤户通过 scanf 函数输⼊这个神秘数字,那么就需要两个玩家咯。
⼀个选数字,⼀个猜数字。
✎孤注⼀掷地让电脑来为我们⾃动产⽣⼀个随机数。
好处是:只需要⼀个玩家,可以⾃娱⾃乐。
缺点是:需要学习该怎么做...我们来学习⽤第⼆种⽅案编写这个游戏,当然你也可以之后⾃⼰编写第⼀种⽅案的代码。
为了⽣成⼀个随机数,我们要⽤到 rand() 函数(rand 是英语 random 的缩写,表⽰“随机的”)。
顾名思义,这个函数能为我们⽣成随机数。
但是我们还想要这个随机数是在 1 到 100 的整数范围内(如果没有限定范围,那会很复杂)。
C编写的小游戏

C编写的小游戏集团档案编码:[YTTR-YTPT28-YTNTL98-UYTYNN08]#i n c l u d e<i o s t r e a m>usingnamespacestd;constcharMOUSE='*';constcharWAY='';constcharWALL='@';constcharPASS='.';constcharIMPASS='X';classGame{public:Game(size_tuWidth,size_tuHeight):m_uWidth(uWidth),m_uHeight(uHe ight),m_pcMaze(newchar[uWidth*uHeight]),m_mouse(0,1){srand(time(NULL));char(*pcMaze)[m_uWidth]=reinterpret_cast<char(*)[m_uWidth]>(m_p cMaze);for(size_ti=0;i<m_uHeight;i++)for(size_tj=0;j<m_uWidth;j++)if((i==1&&j<4)||(i==m_uHeight-2&&j>m_uWidth-5))pcMaze[i][j]=WAY;elseif(i==0||i==m_uHeight-1||j==0||j==m_uWidth-1)pcMaze[i][j]=WALL;elsepcMaze[i][j]=rand()%4?WAY:WALL;}~Game(void){if(m_pcMaze){delete[]m_pcMaze;m_pcMaze=NULL;}}voidRun(void){for(Show();!Quit()&&Step(););}private:classMouse{public:Mouse(size_tx,size_ty):m_x(x),m_y(y){}size_tGetX(void){returnm_x;}size_tGetY(void){returnm_y;}voidStepRight(void){m_x++;m_brain.Remember(EDIR_RIGHT);}voidStepDown(void){m_y++;m_brain.Remember(EDIR_DOWN);}voidStepLeft(void){m_x--;m_brain.Remember(EDIR_LEFT);}voidStepUp(void){m_y--;m_brain.Remember(EDIR_UP);}voidStepBack(void){switch(m_brain.Recollect()){caseEDIR_RIGHT:m_x--;break;caseEDIR_DOWN:m_y--;break;caseEDIR_LEFT:m_x++;break;caseEDIR_UP:m_y++;break;}}private:typedefenumtag_Direction{EDIR_RIGHT,EDIR_DOWN,EDIR_LEFT,EDIR_UP,EDIR_STILL}EDIR;classBrain{public:Brain(void):m_pMemory(NULL){}~Brain(void){for(Step*pStep=m_pMemory,*pLast;pStep;pStep=pLast){pLast=pStep->m_pLast;deletepStep;}}voidRemember(EDIReDir){m_pMemory=newStep(eDir,m_pMemory);}EDIRRecollect(void){EDIReDir=EDIR_STILL;if(m_pMemory){Step*pStep=m_pMemory;m_pMemory=m_pMemory->m_pLast;eDir=pStep->m_eDir;deletepStep;}returneDir;}private:classStep{public:Step(EDIReDir,Step*pLast):m_eDir(eDir),m_pLast(pLast){}EDIRm_eDir;Step*m_pLast;};Step*m_pMemory;};size_tm_x;size_tm_y;Brainm_brain;};voidShow(void){char(*pcMaze)[m_uWidth]=reinterpret_cast<char(*)[m_uWidth]>(m_p cMaze);for(size_ti=0;i<m_uHeight;i++){for(size_tj=0;j<m_uWidth;j++)cout<<pcMaze[i][j];cout<<endl;}}boolQuit(void){cout<<"鎸塓阃€鍑猴紝鍏朵粬阌户缁?"<<flush;intch=getchar();returnch=='Q'||ch=='q';}boolStep(void){char(*pcMaze)[m_uWidth]=reinterpret_cast<char(*)[m_uWidth]>(m_p cMaze);size_tx=m_mouse.GetX();size_ty=m_mouse.GetY();if(x+1<=m_uWidth-1&&pcMaze[y][x+1]==WAY){pcMaze[y][x]=PASS;m_mouse.StepRight();}elseif(y+1<=m_uHeight-1&&pcMaze[y+1][x]==WAY){pcMaze[y][x]=PASS;m_mouse.StepDown();}elseif(x-1>=0&&pcMaze[y][x-1]==WAY){pcMaze[y][x]=PASS;m_mouse.StepLeft();}elseif(y-1>=0&&pcMaze[y-1][x]==WAY){pcMaze[y][x]=PASS;m_mouse.StepUp();}else{pcMaze[y][x]=IMPASS;m_mouse.StepBack();}x=m_mouse.GetX();y=m_mouse.GetY();pcMaze[y][x]=MOUSE;Show();if(x==0&&y==1){cout<<"******小老鼠赢了*****";returnfalse;}if(x==m_uWidth-1&&y==m_uHeight-2){cout<<"*****小老鼠失败了*****"<<endl;returnfalse;}returntrue;}size_tm_uWidth;size_tm_uHeight;char*m_pcMaze;Mousem_mouse; };intmain(void){Gamegame(30,15);game.Run();return0;}。
自己用C语言编写的俄罗斯方块小游戏 hello world级

default : return ;
}
}
void clear_small_screen()
{
int i,j;
int x,y;
for(i=0;i<SMALL_CUBSIZE;i++){
for(j=0;j<SMALL_CUBSIZE;j++){
for (x = X_SMALL_START+j*SMALL_UNIT; x <X_SMALL_START+SMALL_UNIT+j*SMALL_UNIT; x++)
break;
case 17:
for(i=0;i<3;i++)
small_cub[0][i]=1;
small_cub[1][1]=1;
break;
case 18:
for(i=0;i<3;i++)
small_cub[i][1]=1;
small_cub[1][0]=1;
break;
case 7:
for(i=0;i<3;i++)
small_cub[i][0]=1;
small_cub[0][1]=1;
break;
case 8:
for(i=0;i<3;i++)
small_cub[0][i]=1;
small_cub[1][2]=1;
{
int i,j;
for(i=0;i<x;i+Байду номын сангаас){
for(j=0;j<y;j++){
if(chosen==1)
c语言课程设计 综合型小游戏

#include<stdio.h>#include<stdlib.h>#include<time.h>int money1=10000,money2=10000,money=10000;int main(){void game1(int put);void game2(int put);int put,game,i;printf("单人模式请输入1,双人模式请输入2.\n");scanf("%d",&put);if(put==1)printf("你的本钱有一万元,你的任务是翻一倍,达到两万元则游戏胜利\n");if(put==2)printf("最后金钱多者为胜者\n");system("pause");system("cls");for(i=0;i<=1000;i++){printf("请选择游戏:1.思维风暴2.猜数字3.退出\n");scanf("%d",&game);if(game==1){game1(put);}if(game==2){game2(put);}if(game==3){break;}}if(put==1){if(money>=20000)printf("恭喜你通关了\n");if(money>=10000&&money<20000)printf("很遗憾未能通关,不过至少没亏本了\n");}if(put==2){if(money1>money2)printf("恭喜玩家一,你实在太强势了\n");if(money1<money2)printf("恭喜玩家二,简直是虐菜啊\n");if(money1==money2)printf("二位简直势均力敌啊,真是好基友\n");}system("pause");}void game1(int put){int JudgeA(int a[4],int b[4]),JudgeB(int a[4],int b[4]);int a[4],b[4];int c,i,j,m,n,k,l,under,under1,under2;printf("游戏规则:系统将随机产生一个四位不重复数字,你输入猜想的数字后\n");printf("系统将判断你猜对的数字个数和正确位置数,系统将以-A-B的形式提示,其中A 前面的数字表示位置正确的数的个数");printf("而B前的数字表示数字正确而位置不对的数的个数,如正确答案为5234,而猜的人猜5346,则是1A2B.\n **记住你只有八次机会**\n");system("pause");system("cls");if(put==1){for(l=0;l<100;l++){printf("请压底,最高为五千\n");for(m=0;m<=20;m++){scanf("%d",&under);if(under>5000||under<=0){printf("超过上限,请重新输入\n");continue;}elsebreak;}printf("请输入四位数\n");srand(time(NULL));do{a[0]=rand()%10;//产生首位随机数,对10取模得0~9的数字}while(a[0]==0);//若首位为零则重新选择for(i = 1;i < 4; i++){do{a[i]=rand()%10;//产生其它几位随机数for(j = 0; j < i; j++){if(a[i]==a[j])//若与前几位相同则跳出,重置a[i]{k=0;break;}elsek=1;//若不同,则该位有效,置标记k为1}}while(k!=1);}k=a[0];for(i=1;i<4;i++){k=k*10+a[i];}for(n=0;n<=8;n++){if(n==8){printf("you are lost,the number is %d\n",k);money=money-under*2;break;}scanf("%d",&b[0]);b[3]=b[0]%10;b[2]=(b[0]%100-b[3])/10;b[1]=b[0]%1000/100;b[0]=b[0]/1000;printf("%dA%dB\n",JudgeA(a,b),JudgeB(a,b));if(JudgeA(a,b)==4){printf("you win\n");printf("the number is %d\n",k);money=money+under*2;break;}elsecontinue;}printf("your money:%d\n重玩请输入1,返回请输入2\n",money); scanf("%d",&c);if(c==1)continue;if(c==2)break;}}if(put==2){printf("请play1压底,最高为五千\n");scanf("%d",&under1);printf("请play2压底\n");scanf("%d",&under2);for(m=0;m<=10;m++){if(under1>5000||under2>5000){printf("超过上限,请重新输入\n");continue;}elsebreak;}printf("play1's turn\n");printf("请输入四位数\n");srand(time(NULL));do{a[0]=rand()%10;}while(a[0]==0);for(i = 1;i < 4; i++){do{a[i]=rand()%10;for(j = 0; j < i; j++){if(a[i]==a[j]){k=0;break;}elsek=1;}}while(k!=1);}k=a[0];for(i=1;i<4;i++){k=k*10+a[i];}for(n=0;n<=8;n++){if(n==8){printf("you are lost,the number is %d\n",k);money1=money1-under1*2;break;}scanf("%d",&b[0]);b[3]=b[0]%10;b[2]=(b[0]%100-b[3])/10;b[1]=b[0]%1000/100;b[0]=b[0]/1000;printf("%dA%dB\n",JudgeA(a,b),JudgeB(a,b));if(JudgeA(a,b)==4){printf("you win\n");printf("the number is %d\n",k);money1=money1+under1*2;break;}elsecontinue;}printf("play1's money:%d\n",money1);system("pause");printf("play2's turn\n");printf("请输入四位数\n");srand(time(NULL));do{a[0]=rand()%10;}while(a[0]==0);for(i = 1;i < 4; i++){do{a[i]=rand()%10;for(j = 0; j < i; j++){if(a[i]==a[j]){k=0;break;}elsek=1;}}while(k!=1);}k=a[0];for(i=1;i<4;i++){k=k*10+a[i];}for(n=0;n<=8;n++){if(n==8){printf("you are lost,the number is %d\n",k);money2=money2-under2*2;break;}scanf("%d",&b[0]);b[3]=b[0]%10;b[2]=(b[0]%100-b[3])/10;b[1]=b[0]%1000/100;b[0]=b[0]/1000;printf("%dA%dB\n",JudgeA(a,b),JudgeB(a,b));if(JudgeA(a,b)==4){printf("you win\n");printf("the number is %d\n",k);money2=money2+under2*2;break;}elsecontinue;}printf("play2's money:%d\n",money2);}}int JudgeA(int a[4],int b[4]){int i,result1=0;for(i=0;i<4;i++){if(b[i]==a[i]) result1++;}return result1;}int JudgeB(int a[4],int b[4]){int i,j,result=0;for(i=0;i<4;i++){for(j=0;j<4;j++){if(a[i]==b[j]&&i!=j)result++;}}return result;}void game2(int put){int i,j,k,l,a,num,down,down1,down2,random;int nu[6];int *p;p=nu;system("pause");system("cls");printf("游戏规则:单人模式为猜数,双人模式为比大小\n");if(put==1){for(i=0;i<=100;i++){for(k=0;k<=100;k++){printf("请下注,最高为500\n");scanf("%d",&down);if(down>0&&down<=500)break;else{printf("超过上线,请重新下注\n");continue;}}printf("请输入所猜数\n");for(j=0;j<100;j++){scanf("%d",&num);if(num>0&&num<=6)break;else{printf("错误,请重新输入\n");continue;}}for(l=0;l<100;l++){srand((unsigned)(time(NULL)));random = rand()%6+1;if(random>0&&random<=6)break;}printf("正确数为%d,继续玩请输入1,返回菜单输入2\n",random);if(num==random){printf("***********************YOUWIN************************\n");money=money+down*2;}else{printf("***********************YOULOST***********************\n");money=money-down*2;}printf("你的金钱为%d\n",money);scanf("%d",&a);if(a==1)continue;if(a==2)break;}}if(put==2){for(i=0;i<100;i++){printf("游戏规则:玩家分别得到三次随机数字,总和大者胜利\n");printf("请下注,最高为一千\n");scanf("%d",&down);system("pause");for(j=1;j<=6;j++){if(j%2==1){srand((unsigned)(time(NULL)));p[j-1] = rand()%6+1;printf("玩家一第%d次得数为%d\n",j/2+1,p[j-1]);}else{srand((unsigned)(time(NULL)));p[j-1] = rand()%6+1;printf("玩家二第%d次得数为%d\n",j/2,p[j-1]);}system("pause");}printf("玩家一总得数为%d\n玩家二总得数为%d\n",p[0]+p[2]+p[4],p[1]+p[3]+p[5]);if(p[0]+p[2]+p[4]>p[1]+p[3]+p[5]){printf("玩家一获胜\n");money1=money1+down*2;money2=money2-down*2;}if(p[0]+p[2]+p[4]<p[1]+p[3]+p[5]){printf("玩家二获胜\n");money1=money1-down*2;money2=money2+down*2;}if(p[0]+p[2]+p[4]==p[1]+p[3]+p[5]){printf("恭喜\n");money1=money1+down*2;money2=money2+down*2;}printf("玩家一金钱为%d\n玩家二金钱为%d\n重玩输入1,返回输入2\n",money1,money2);scanf("%d",&a);if(a==1)continue;if(a==2)break;}}}。
c语言编写的小游戏代码

用vc6.0新建一个Win32 Application工程,把附件代码拷贝进去即可。
上下左右键控制蛇的方向,空格键用于启动或者停止游戏。
上下左右空格键#include <windows.h>#include <stdio.h>#include<time.h>#define C_W 516#define C_H 548//#define C_W 1024//#define C_H 1024#define GO_RIGHT 0x01#define GO_DOWN 0x02#define GO_LEFT 0x03#define GO_UP 0x04#define SNAKE_NUMBER 30typedef struct node_struct{unsigned char direction;unsigned char cnt;}s_node,*s_node_handle;s_node s_count[SNAKE_NUMBER ];typedef struct SNAKE{unsigned char Head_X;unsigned char Head_Y;unsigned char Tail_X;unsigned char Tail_Y;unsigned char h_index;unsigned char t_index;unsigned char food_state;unsigned char score;unsigned char snake_state;} Snake_Data,*Snake_Data_handle;Snake_Data snk_1;#define MAP_X 64#define MAP_Y 64unsigned char game_map[MAP_Y][MAP_X];LRESULT CALLBACK Win_tetris_Proc(HWND hwnd, // handle to windowUINT uMsg, // message identifierWPARAM wParam, // first message parameter LPARAM lParam // second message parameter);int WINAPI WinMain(HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command lineint nCmdShow // show state){snk_1.Head_X = 0x01;//head xsnk_1.Head_Y = 0x00;//head ysnk_1.Tail_X = 0x00;//tail xsnk_1.Tail_Y = 0x00;//tail ysnk_1.h_index=0;snk_1.t_index=0;snk_1.food_state=0;snk_1.score=0;snk_1.snake_state = 1;s_count[snk_1.h_index].cnt=2;s_count[snk_1.h_index].direction=GO_RIGHT;s_count[snk_1.t_index].cnt=2;s_count[snk_1.t_index].direction=GO_RIGHT;WNDCLASS wndcls;wndcls.cbClsExtra=0;wndcls.cbWndExtra=0;wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION);wndcls.hInstance=hInstance;wndcls.lpfnWndProc=Win_tetris_Proc;wndcls.lpszClassName="Game_tetris";wndcls.lpszMenuName=NULL;wndcls.style=CS_HREDRAW | CS_VREDRAW;RegisterClass(&wndcls);//Game_TetrisHWND hwnd;hwnd=CreateWindow("Game_tetris","Game_Snake(/zook0k/)",WS _OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX & ~WS_THICKFRAME,0,0,C_W,C_H,NULL,NULL,hInstance,NULL);ShowWindow(hwnd,SW_SHOWNORMAL);//initial snakeHDC hdc;hdc=GetDC(hwnd);HBRUSH hbr;RECT rect;rect.left = 0;rect.top = 0;rect.right = 16;rect.bottom = 8;hbr= CreateSolidBrush(RGB(255,0,0));FillRect(hdc,&rect,hbr);ReleaseDC(hwnd,hdc);game_map[0][0]=1;game_map[0][1]=1;//initial randSetTimer(hwnd,1,100,NULL) ;srand((int)time(0));UpdateWindow(hwnd);MSG msg;while(GetMessage(&msg,NULL,0,0)){// TranslateMessage(&msg);DispatchMessage(&msg);}return 0;}LRESULT CALLBACK Win_tetris_Proc(HWND hwnd, // handle to windowUINT uMsg, // message identifierWPARAM wParam, // first message parameterLPARAM lParam // second message parameter){char szChar[20] = "score:";unsigned char xx,yy;switch(uMsg){case WM_KEYDOWN:{if(32 == wParam){if(1 == snk_1.snake_state){snk_1.snake_state = 0;}else{snk_1.snake_state = 1;}}if(1 == snk_1.snake_state){if((wParam > 36)&&(wParam < 41)){if(38 == wParam){if((s_count[snk_1.h_index].direction == GO_RIGHT)||(s_count[snk_1.h_index].direction == GO_LEFT)){snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;s_count[snk_1.h_index].direction = GO_UP;s_count[snk_1.h_index].cnt = 1;}}else if(40 == wParam){if((s_count[snk_1.h_index].direction == GO_RIGHT)||(s_count[snk_1.h_index].direction == GO_LEFT)){snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;s_count[snk_1.h_index].direction = GO_DOWN;s_count[snk_1.h_index].cnt = 1;}}else if(39 == wParam){if((s_count[snk_1.h_index].direction == GO_DOWN)||(s_count[snk_1.h_index].direction == GO_UP)){snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;s_count[snk_1.h_index].direction = GO_RIGHT;s_count[snk_1.h_index].cnt = 1;}}else if(37 == wParam){if((s_count[snk_1.h_index].direction == GO_DOWN)||(s_count[snk_1.h_index].direction == GO_UP)){snk_1.h_index = (snk_1.h_index+1)%SNAKE_NUMBER ;s_count[snk_1.h_index].direction = GO_LEFT;s_count[snk_1.h_index].cnt = 1;}}}}break;}case WM_TIMER://case WM_PAINT:time_t t;HDC hdc;hdc=GetDC(hwnd);HBRUSH hbr;RECT rect;if(1 == snk_1.snake_state){//headCHECK:switch(s_count[snk_1.h_index].direction){case GO_RIGHT:{if(snk_1.Head_X < 63)snk_1.Head_X++;else snk_1.Head_X = 0;break;}case GO_LEFT:{if(snk_1.Head_X >0 )snk_1.Head_X--;else snk_1.Head_X = 63;break;}case GO_DOWN :{if(snk_1.Head_Y < 63)snk_1.Head_Y++;else snk_1.Head_Y = 0;break;}case GO_UP:{if(snk_1.Head_Y > 0)snk_1.Head_Y--;else snk_1.Head_Y = 63;break;}default:{break;}}s_count[snk_1.h_index].cnt++;if(0 == game_map[snk_1.Head_Y][snk_1.Head_X])//no point{game_map[snk_1.Head_Y][snk_1.Head_X] = 1;}else if(1 == game_map[snk_1.Head_Y][snk_1.Head_X])//game over{KillTimer(hwnd,1);sprintf(szChar,"score:%d",snk_1.score);MessageBox(hwnd,szChar,"GAME OVER!",0);}else if(2 == game_map[snk_1.Head_Y][snk_1.Head_X])//eat food{game_map[snk_1.Head_Y][snk_1.Head_X] = 1;snk_1.food_state = 0;snk_1.score++;goto CHECK;}rect.left = snk_1.Head_X*8;rect.top = snk_1.Head_Y*8;rect.right = rect.left + 8;rect.bottom = rect.top + 8;hbr= CreateSolidBrush(RGB(255,0,0));FillRect(hdc,&rect,hbr);//show head point//tailgame_map[snk_1.Tail_Y][snk_1.Tail_X] = 0;rect.left = snk_1.Tail_X*8;rect.top = snk_1.Tail_Y*8;rect.right = rect.left + 8;rect.bottom = rect.top + 8;//hbr= CreateSolidBrush(RGB(0,100,0));hbr= CreateSolidBrush(RGB(0,0,0));//clear tail pointFillRect(hdc,&rect,hbr);switch(s_count[snk_1.t_index].direction){case GO_RIGHT:{if(snk_1.Tail_X < 63)snk_1.Tail_X++;else snk_1.Tail_X = 0;break;}case GO_LEFT:{if(snk_1.Tail_X >0 )snk_1.Tail_X--;else snk_1.Tail_X = 63;break;}case GO_DOWN :{if(snk_1.Tail_Y < 63)snk_1.Tail_Y++;else snk_1.Tail_Y = 0;break;}case GO_UP:{if(snk_1.Tail_Y > 0)snk_1.Tail_Y--;else snk_1.Tail_Y = 63;break;}default:{break;}}if(s_count[snk_1.t_index].cnt == 2){snk_1.t_index = (snk_1.t_index + 1)%SNAKE_NUMBER ;}else{s_count[snk_1.t_index].cnt--;}//output foodif(0 == snk_1.food_state){snk_1.food_state = 1;do{xx = rand()%3970%63;yy = rand()%3970/63;}while(1 == game_map[yy][xx]);game_map[yy][xx]=2;rect.left = xx*8;rect.top = yy*8;rect.right = rect.left + 8;rect.bottom = rect.top + 8;hbr= CreateSolidBrush(RGB(155,110,10));FillRect(hdc,&rect,hbr);//show foodsrand((unsigned) time(&t));}ReleaseDC(hwnd,hdc);}break;case WM_CLOSE:DestroyWindow(hwnd);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hwnd,uMsg,wParam,lParam);}return 0;}。
c语言简单小游戏(模拟魔塔)

int map[35][66];//定义地图数组 int batt=1;//关卡数 int cs=1;//层数(第三关开始有) int is=0;//判断是否初始化地图 int money=0;//将打怪获得的金钱放在一个整形变量中 int ak=0; int ac1=0; int flag=0; struct holysword {
2.操作说明**********************************************************************
C++编写的小游戏

#include <iostream>using namespace std;const char MOUSE = '*';const char WAY = ' ';const char WALL = '@';const char PASS='.';const char IMPASS='X';class Game {public:Game (size_t uWidth,size_t uHeight) : m_uWidth(uWidth),m_uHeight(uHeight),m_pcMaze(new char[uWidth*uHeight]),m_mouse(0,1){ srand(time(NULL));char (*pcMaze)[m_uWidth] = reinterpret_cast<char (*)[m_uWidth]>(m_pcMaze);for(size_t i=0;i<m_uHeight;i++)for(size_t j=0;j<m_uWidth;j++)if((i==1&&j<4)||(i==m_uHeight-2&&j>m_uWidth-5))pcMaze[i][j] = WAY;else if(i==0||i==m_uHeight-1||j==0||j==m_uWidth-1)pcMaze[i][j] = WALL;elsepcMaze[i][j] = rand()%4 ? W AY:WALL;}~Game (void) {if(m_pcMaze){delete[] m_pcMaze;m_pcMaze = NULL;}}void Run(void){for(Show();!Quit()&&Step(););}private:class Mouse{public:Mouse(size_t x,size_t y) : m_x(x),m_y(y){}size_t GetX(void){return m_x;}size_t GetY(void){return m_y;}void StepRight(void){m_x++;m_brain.Remember (EDIR_RIGHT);}void StepDown(void){m_y++;m_brain.Remember (EDIR_DOWN);}void StepLeft(void){m_x--;m_brain.Remember (EDIR_LEFT);}void StepUp(void){m_y--;m_brain.Remember (EDIR_UP);}void StepBack(void){switch (m_brain.Recollect()){case EDIR_RIGHT:m_x--;break;case EDIR_DOWN:m_y--;break;case EDIR_LEFT:m_x++;break;case EDIR_UP:m_y++;break;}}private:typedef enum tag_Direction {EDIR_RIGHT,EDIR_DOWN,EDIR_LEFT,EDIR_UP,EDIR_STILL} EDIR;class Brain {public:Brain(void) : m_pMemory(NULL){}~Brain(void){for(Step* pStep = m_pMemory,*pLast;pStep;pStep = pLast){pLast = pStep->m_pLast;delete pStep;}}void Remember (EDIR eDir) {m_pMemory = new Step (eDir,m_pMemory);}EDIR Recollect(void){EDIR eDir = EDIR_STILL;if(m_pMemory){Step* pStep = m_pMemory;m_pMemory = m_pMemory->m_pLast;eDir = pStep->m_eDir;delete pStep;}return eDir;}private:class Step {public:Step (EDIR eDir,Step* pLast) : m_eDir(eDir),m_pLast(pLast){}EDIR m_eDir;Step* m_pLast;};Step* m_pMemory;};size_t m_x;size_t m_y;Brain m_brain;};void Show(void){char (*pcMaze)[m_uWidth] = reinterpret_cast<char (*)[m_uWidth]>(m_pcMaze);for(size_t i=0;i<m_uHeight;i++){for(size_t j=0;j<m_uWidth;j++)cout<<pcMaze[i][j];cout<<endl;}}bool Quit(void){cout<<"鎸塓閫€鍑猴紝鍏朵粬閿户缁?"<<flush;int ch = getchar ();return ch=='Q'||ch=='q';}bool Step(void){char (*pcMaze)[m_uWidth] = reinterpret_cast<char (*)[m_uWidth]>(m_pcMaze);size_t x = m_mouse.GetX();size_t y = m_mouse.GetY();if(x+1<=m_uWidth-1&&pcMaze[y][x+1]==W AY){pcMaze[y][x] = PASS;m_mouse.StepRight();}else if(y+1<=m_uHeight-1&&pcMaze[y+1][x]==W AY){ pcMaze[y][x] = PASS;m_mouse.StepDown();}else if(x-1>=0&&pcMaze[y][x-1]==WAY){pcMaze[y][x] = PASS;m_mouse.StepLeft();}else if(y-1>=0&&pcMaze[y-1][x]==W AY){pcMaze[y][x] = PASS;m_mouse.StepUp();}else{pcMaze[y][x]=IMPASS;m_mouse.StepBack();}x = m_mouse.GetX();y = m_mouse.GetY();pcMaze[y][x]=MOUSE;Show();if(x==0&&y==1){cout<<"******小老鼠赢了*****";return false;}if(x==m_uWidth-1&&y==m_uHeight-2){cout<<"*****小老鼠失败了*****"<<endl;return false;}return true;}size_t m_uWidth;size_t m_uHeight;char* m_pcMaze;Mouse m_mouse;};int main(void){Game game(30,15);game.Run();return 0;}。
C语言小游戏源代码《打砖块》

C 语言小游戏源代码《打砖块》#include "graphics.h"#include "stdio.h"#include "conio.h" /* 所需的头文件*/int on; /* 声明具有开关作用的全局变量*/ static int score;/* 声明静态的记分器变量*//* 定义开始界面函数*/int open(){setviewport(100,100,500,380,1); /* 设置图形窗口区域*/setcolor(4); /* 设置作图色*/rectangle(0,0,399,279); /* 以矩形填充所设的图形窗口区域*/ setfillstyle(SOLID_FILL,7); /* 设置填充方式*/floodfill(50,50,4); /* 设置填充范围*/setcolor(8);settextstyle(0,0,9); /* 文本字体设置*/outtextxy(90,80,"BALL"); /* 输出文本内容*/settextstyle(0,0,1);outtextxy(110,180,"version 1.0");outtextxy(110,190,"made by ddt");setcolor(128);settextstyle(0,0,1);outtextxy(120,240,"Press any key to continue "); }/* 定义退出界面函数*/int quitwindow(){char s[100]; /* 声明用于存放字符串的数组*/setviewport(100,150,540,420,1);setcolor(YELLOW);rectangle(0,0,439,279);setfillstyle(SOLID_FILL,7);floodfill(50,50,14);setcolor(12);settextstyle(0,0,8);outtextxy(120,80,"End");settextstyle(0,0,2);outtextxy(120,200,"quit? Y/N");sprintf(s,"Your score is:%d",score);/* 格式化输出记分器的值*/outtextxy(120,180,s);on=1; /* 初始化开关变量*/}/* 主函数*/main(){int gdriver,gmode;gdriver=DETECT; /* 设置图形适配器*/gmode=VGA; /* 设置图形模式*/registerbgidriver(EGAVGA_driver); /* 建立独立图形运行程序*/ initgraph(&gdriver,&gmode,""); /* 图形系统初试化*/ setbkcolor(14);open(); /* 调用开始界面函数*/getch(); /* 暂停*/while(1) /* 此大循环体控制游戏的反复重新进行*/{int driver,mode,l=320,t=400,r,a,b,dl=5,n,x=200,y=400,r1=10,dx=- 2,dy=-2;/* 初始化小球相关参数*/intleft[100],top[100],right[100],bottom[100],i,j,k,off=1,m,num[100][10 0];/*方砖阵列相关参数*/static int pp;static int phrase; /* 一系列起开关作用的变量*/ int oop=15;pp=1;score=0;driver=DETECT;mode=VGA;registerbgidriver(EGAVGA_driver);initgraph(&driver,&mode,"");setbkcolor(10);cleardevice(); /**/ clearviewport(); /* 清除现行图形窗口内容*/b=t+6;r=l+60;setcolor(1);rectangle(0,0,639,479);setcolor(4);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,1);floodfill(l+2,t+2,4);for(i=0,k=0;i<=6;i++) /* 此循环绘制方砖阵列*/ {top[i]=k;bottom[i]=top[i]+20;k=k+21;oop--;for(j=0,m=0;j<=7;j++){left[j]=m;right[j]=left[j]+80;m=m+81;setcolor(4);rectangle(left[j],top[i],right[j],bottom[i]);setfillstyle(SOLID_FILL,j+oop); floodfill(left[j]+1,top[i]+1,4);num[i][j]=pp++;}}while(1) /* 此循环控制整个动画*/ {while(!kbhit()){x=x+dx; /* 小球运动的圆心变量控制*/ y=y+dy;if(x+r1>r||x+r1<r)phrase=0;} {if((x-r1<=r||x+r1<=r)&&x+r1>=l){if(y<t)phrase=1;if(y+r1>=t&&phrase==1){dy=-dy;y=t-1-r1;}}if(off==0)continue;for(i=0;i<=6;i++) /*for(j=0;j<=7;j++) 此循环用于判断、控制方砖阵列的撞击、擦除* /if((x+r1<=right[j]&&x+r1>=left[j])||(x-r1<=right[j]&&x- r1>=left[j])){if(( y-r1>top[i]&&y-r1<=bottom[i])||(y+r1>=top[i]&&y+r1<=bottom[i] )) {if(num[i][j]==0){continue; }setcolor(10);rectangle(left[j],top[i],right[j],bottom[i]);setfillstyle(SOLID_FILL,10);floodfill(left[j]+1,top[i]+1,10); dy=-dy;num[i][j]=0;score=score+10;printf("%d\b\b\b",score);}}if((y+r1>=top[i]&&y+r1<=bottom[i])||(y-r1>=top[i]&&y-r1<=bottom[i])){if((x+r1>=left[j]&&x+r1<right[j])||(x-r1<=right[j]&&x-r1>left[j])){if(num[i][j]==0){ continue;}setcolor(10);rectangle(left[j],top[i],right[j],bottom[i]);setfillstyle(SOLID_FILL,10);floodfill(left[j]+1,top[i]+1,10);dx=-dx;num[i][j]=0;score=score+10;printf("%d\b\b\b",score);}}}if(x+r1>639) /* 控制小球的弹射范围*/{dx=-dx;x=638-r1;}if(x<=r1){dx=-dx;x=r1+1;}if(y+r1>=479){off=0;quitwindow();break;}if(y<=r1){dy=-dy;y=r1+1;}if(score==560){off=0;quitwindow();break;} setcolor(6); circle(x,y,r1);setfillstyle(SOLID_FILL,14);floodfill(x,y,6);delay(1000);setcolor(10);circle(x,y,r1);setfillstyle(SOLID_FILL,10);floodfill(x,y,10); }a=getch();setcolor(10);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,10);floodfill(l+2,t+2,10);if(a==77&&l<=565) /* 键盘控制设定*/ {dl=20;l=l+dl;}if(a==75&&l>=15){dl=-20;l=l+dl;}if(a=='y'&&on==1)break;if(a=='n'&&on==1)break;if(a==27){quitwindow();off=0;}r=l+60;setcolor(4);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,1);floodfill(l+5,t+5,4);delay(100);}if(a=='y'&&on==1) /* 是否退出游戏*/ {break;}if(a=='n'&&on==1){ continue;}}closegraph();}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C编写的小游戏
集团企业公司编码:(LL3698-KKI1269-TM2483-LUI12689-ITT289-
#i n c l u d e<i o s t r e a m> usingnamespacestd;
constcharMOUSE='*';
constcharWAY='';
constcharWALL='@';
constcharPASS='.';
constcharIMPASS='X';
classGame{
public:
Game(size_tuWidth,size_tuHeight):m_uWidth(uWidth),m_uHeigh t(uHeight),m_pcMaze(newchar[uWidth*uHeight]),m_mouse(0,1){
srand(time(NULL));
char(*pcMaze)[m_uWidth]=reinterpret_cast<char(*)[m_uWidth] >(m_pcMaze);
for(size_ti=0;i<m_uHeight;i++)
for(size_tj=0;j<m_uWidth;j++)
if((i==1&&j<4)||(i==m_uHeight-
2&&j>m_uWidth-5))
pcMaze[i][j]=WAY;
elseif(i==0||i==m_uHeight-
1||j==0||j==m_uWidth-1)
pcMaze[i][j]=WALL;
else
pcMaze[i][j]=rand()%4WAY:WALL;
}
~Game(void){
if(m_pcMaze){
delete[]m_pcMaze;
m_pcMaze=NULL;
}
}
voidRun(void){
for(Show();!Quit()&&Step(););
}
private:
classMouse{
public:
Mouse(size_tx,size_ty):m_x(x),m_y(y){}
size_tGetX(void){returnm_x;}
size_tGetY(void){returnm_y;}
voidStepRight(void){
m_x++;
m_brain.Remember(EDIR_RIGHT); }
voidStepDown(void){
m_y++;
m_brain.Remember(EDIR_DOWN); }
voidStepLeft(void){
m_x--;
m_brain.Remember(EDIR_LEFT); }
voidStepUp(void){
m_y--;
m_brain.Remember(EDIR_UP);
}
voidStepBack(void){
switch(m_brain.Recollect()){
caseEDIR_RIGHT:
m_x--;
break;
caseEDIR_DOWN:
m_y--;
break;
caseEDIR_LEFT:
m_x++;
break;
caseEDIR_UP:
m_y++;
break;
}
}
private:
typedefenumtag_Direction{
EDIR_RIGHT,
EDIR_DOWN,
EDIR_LEFT,
EDIR_UP,
EDIR_STILL
}EDIR;
classBrain{
public:
Brain(void):m_pMemory(NULL){}
~Brain(void){
for(Step*pStep=m_pMemory,*pLast;pStep;pStep=pLast){
pLast=pStep->m_pLast;
deletepStep;
}
}
voidRemember(EDIReDir){
m_pMemory=newStep(eDir,m_pMemory);
}
EDIRRecollect(void){
EDIReDir=EDIR_STILL;
if(m_pMemory){
Step*pStep=m_pMemory;
m_pMemory=m_pMemory->m_pLast;
eDir=pStep->m_eDir;
deletepStep;
}
returneDir;
}
private:
classStep{
public:
Step(EDIReDir,Step*pLast):m_eDir(eDir),m_pLast(pLast){}
EDIRm_eDir;
Step*m_pLast;
};
Step*m_pMemory;
};
size_tm_x;
size_tm_y;
Brainm_brain;
};
voidShow(void){
char(*pcMaze)[m_uWidth]=reinterpret_cast<char(*)[m_uWidth] >(m_pcMaze);
for(size_ti=0;i<m_uHeight;i++){
for(size_tj=0;j<m_uWidth;j++)
cout<<pcMaze[i][j];
cout<<endl;
}
}
boolQuit(void){
cout<<"鎸塓阃€鍑猴紝鍏朵粬阌户缁"<<flush;
intch=getchar();
returnch=='Q'||ch=='q';
}
boolStep(void){
char(*pcMaze)[m_uWidth]=reinterpret_cast<char(*)[m_uWidth] >(m_pcMaze);
size_tx=m_mouse.GetX();
size_ty=m_mouse.GetY();
if(x+1<=m_uWidth-1&&pcMaze[y][x+1]==WAY){
pcMaze[y][x]=PASS;
m_mouse.StepRight();
}
elseif(y+1<=m_uHeight-1&&pcMaze[y+1][x]==WAY){
pcMaze[y][x]=PASS;
m_mouse.StepDown();
}
elseif(x-1>=0&&pcMaze[y][x-1]==WAY){
pcMaze[y][x]=PASS;
m_mouse.StepLeft();
}
elseif(y-1>=0&&pcMaze[y-1][x]==WAY){
pcMaze[y][x]=PASS;
m_mouse.StepUp();
}
else{
pcMaze[y][x]=IMPASS;
m_mouse.StepBack();
}
x=m_mouse.GetX();
y=m_mouse.GetY();
pcMaze[y][x]=MOUSE;
Show();
if(x==0&&y==1){
cout<<"******小老鼠赢了*****";
returnfalse;
}
if(x==m_uWidth-1&&y==m_uHeight-2){
cout<<"*****小老鼠失败了*****"<<endl;
returnfalse;
}
returntrue;
}
size_tm_uWidth;
size_tm_uHeight;
char*m_pcMaze;
Mousem_mouse; };
intmain(void){
Gamegame(30,15);
game.Run();
return0;
}。