c语言各种小游戏的代码,能发给我吗

DEV C++写的

#include<windows.h>
#include<io.h>
#include<direct.h>
#include<string.h>

struct_finddata_tFile[100];
charcStr[100][260];
intfilenum=0,picnum=0;
HDChDC=NULL,mDC=NULL;
HBITMAPhBitmap=NULL;
intFirst_Run=0;
intxpos,ypos;
voidCALLBACKmovewindow(HWNDhWnd,UINTnMsg,UINTnTimerid,DWORDdwTime)
{
POINTpoi;
LPRECTinfo;
GetCursorPos(&poi);
MoveWindow(hWnd,(poi.x-xpos),(poi.y-ypos),371,342,FALSE);
return;
}


voidfindbmp()
{
longhandle;
handle=_findfirst("E:\DancingMiku\Picture\*.bmp",&File[0]);
if(handle==-1L)
{
MessageBox(NULL,"Thereisnofilewithbmpinthe“E:/DancingMiku”!!","Warn!",MB_OKCANCEL);
exit(0);
}
else
{
picnum++;
while(_findnext(handle,&File[picnum])==0)
picnum++;
}
inti;
picnum--;
for(i=0;i<=picnum;i++)
{
sprintf(cStr[i],"E:\DancingMiku\Picture\%s",File[i].name);
}
}


voidCALLBACKTimer(HWNDhWnd,UINTnMsg,UINTnTimerid,DWORDdwTime)
{
hBitmap=LoadImage(NULL,cStr[filenum%picnum],IMAGE_BITMAP,204,200,LR_LOADFROMFILE);
SelectObject(mDC,hBitmap);
BitBlt(hDC,0,0,204,200,mDC,0,0,SRCCOPY);
DeleteObject(hBitmap);
//DeleteDC(mDC);
//DeleteDC(hDC);
filenum++;
return;
}
voidCALLBACKTimer2(HWNDhWnd,UINTnMsg,UINTnTimerid,DWORDdwTime)
{
KillTimer(hWnd,1);
SetTimer(hWnd,1,30,Timer);
}
LRESULTCALLBACKWinProc(HWNDhWnd,UINTMsg,WPARAMwParam,LPARAMlParam)
{
switch(Msg)
{
caseWM_RBUTTONUP:
{
if(First_Run==0)
{
First_Run=1;
MessageBox(NULL,"这是一个测试的软件,你的点击意味着某些事情即将发生,至于是什么事情,哼哼...","提醒",MB_OKCANCEL);
}
break;

}

caseWM_LBUTTONDOWN:
{
POINTtempos;
GetCursorPos(&tempos);
ScreenToClient(hWnd,&tempos);
xpos=tempos.x;
ypos=tempos.y;
SetTimer(hWnd,3,0,movewindow);
break;
}
caseWM_LBUTTONUP:
{
KillTimer(hWnd,3);
break;
}

}
returnDefWindowProc(hWnd,Msg,wParam,lParam);
}

intWINAPIWinMain(HINSTANCEhInstance,
HINSTANCEhPreInstance,
LPSTRIpCmdLine,
intnShowCmd)
{
char*cName="DancingMiku";
WNDCLASSEXmywindow;
HWNDhWnd;
MSGMsg;
HMENUhMenu;


mywindow.cbClsExtra=0;
mywindow.cbWndExtra=0;
mywindow.cbSize=sizeof(WNDCLASSEX);
mywindow.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
mywindow.hCursor=LoadImage(NULL,"E:\DancingMiku\Pointer\Arrow.ani",IMAGE_CURSOR,0,0,LR_LOADFROMFILE);
mywindow.hIcon=LoadImage(NULL,"E:\DancingMiku\Icon\01.ico",IMAGE_ICON,0,0,LR_LOADFROMFILE);
mywindow.hIconSm=LoadImage(NULL,"E:\DancingMiku\Icon\01.ico",IMAGE_ICON,0,0,LR_LOADFROMFILE);
mywindow.hInstance=hInstance;
mywindow.lpfnWndProc=WinProc;
mywindow.lpszClassName=(LPSTR)cName;
mywindow.lpszMenuName=NULL;
mywindow.style=CS_HREDRAW;

RegisterClassEx(&mywindow);

// hMenu=CreatePopupMenu();
// AppendMenu(hMenu,MF_ENABLED,MF_STRING,"New");
intheight,width;
height=GetSystemMetrics(SM_CYSCREEN);
width=GetSystemMetrics(SM_CXSCREEN);
hWnd=CreateWindowEx((WS_EX_TOPMOST|WS_EX_TOOLWINDOW|WS_EX_LAYERED),cName,"DancingMiku",(WS_SIZEBOX|WS_THICKFRAME),(width-50),100,371,324,NULL,NULL,hInstance,NULL);
if(hWnd==NULL)
{
MessageBox(hWnd,"Thereisaerror!!","warn",MB_OKCANCEL);
return0;
}


//SetLayeredWindowAttributes(hWnd,RGB(165,201,247),210,LWA_COLORKEY);
SetLayeredWindowAttributes(hWnd,RGB(255,255,255),200,(LWA_ALPHA|LWA_COLORKEY));
SetWindowLong(hWnd,GWL_STYLE,GetWindowLong(hWnd,GWL_STYLE)&(~(WS_CAPTION|WS_SIZEBOX)));
ShowWindow(hWnd,nShowCmd);
//下面是搜索文件的
if(_access("E:\DancingMiku",0)==-1)
{
intresult;
result=MessageBox(hWnd,"Thereisaerror!ShouldIcreatefilesformyself?","warn",MB_YESNO);
switch(result)
{
caseIDYES:
{
_mkdir("E:\DancingMiku\");
_mkdir("E:\DancingMiku\Picture\");
_mkdir("E:\DancingMiku\Pointer\");
_mkdir("E:\DancingMiku\Icon\");
break;
}
caseIDNO:
{
MessageBox(hWnd,"Nofilemeansnoruning!","Warn",MB_OK);
return0;
}
}
}
elsefindbmp();
if(hDC==NULL)
hDC=GetDC(hWnd);
if(mDC==NULL)
mDC=CreateCompatibleDC(hDC);
SetTimer(hWnd,2,1000,Timer2);
UpdateWindow(hWnd);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
returnMsg.message;
}

Ⅱ 谁有能在vc++6.0上运行的c语言小游戏代码

最基础的贪吃蛇的代码

#include<stdio.h>
#include<windows.h>//基本型态定义。支援型态定义函数。使用者界面函数 图形装置界面函数。
#include<conio.h> //用户通过按键盘产生的对应操作 (控制台)
#include<stdlib.h>
#include<time.h> //日期和时间头文件
#define LEN 30
#define WID 25
int Snake[LEN][WID] = {0}; //数组的元素代表蛇的各个部位
char Sna_Hea_Dir = 'a';//记录蛇头的移动方向
int Sna_Hea_X, Sna_Hea_Y;//记录蛇头的位置
int Snake_Len = 3;//记录蛇的长度
clock_t Now_Time;//记录当前时间,以便自动移动
int Wait_Time ;//记录自动移动的时间间隔
int Eat_Apple = 1;//吃到苹果表示为1
int Level ;
int All_Score = -1;
int Apple_Num = -1;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //获取标准输出的句柄 <windows.h>
//句柄 :标志应用程序中的不同对象和同类对象中的不同的实例 方便操控,
void gotoxy(int x, int y)//设置光标位置
{
COORD pos = {x,y}; //定义一个字符在控制台屏幕上的坐标POS

SetConsoleCursorPosition(hConsole, pos); //定位光标位置的函数<windows.h>

}

void Hide_Cursor()//隐藏光标 固定函数
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(hConsole, &cursor_info);
}

void SetColor(int color)//设置颜色
{
SetConsoleTextAttribute(hConsole, color);
//是API设置字体颜色和背景色的函数 格式:SetConsoleTextAttribute(句柄,颜色);
}

void Print_Snake()//打印蛇头和蛇的脖子和蛇尾
{
int iy, ix, color;
for(iy = 0; iy < WID; ++iy)
for(ix = 0; ix < LEN; ++ix)
{

if(Snake[ix][iy] == 1)//蛇头
{
SetColor(0xf); //oxf代表分配的内存地址 setcolor:34行自定义设置颜色的函数
gotoxy(ix*2, iy);
printf("※");
}
if(Snake[ix][iy] == 2)//蛇的脖子
{
color = rand()%15 + 1; //rand()函数是产生随机数的一个随机函数。C语言里还有 srand()函数等。
//头文件:stdlib.h
if(color == 14)
color -= rand() % 13 + 1; //变色
SetColor(color);
gotoxy(ix*2, iy);
printf("■");
}
if(Snake[ix][iy] == Snake_Len)
{
gotoxy(ix*2, iy);
SetColor(0xe);
printf("≈");
}
}
}

void Clear_Snake()//擦除贪吃蛇
{
int iy, ix;
for(iy = 0; iy < WID; ++iy)
for(ix = 0; ix < LEN; ++ix)
{
gotoxy(ix*2, iy);
if(Snake[ix][iy] == Snake_Len)
printf(" ");
}
}

void Rand_Apple()//随机产生苹果
{
int ix, iy;

do
{
ix = rand() % LEN;
iy = rand() % WID;
}while(Snake[ix][iy]);

Snake[ix][iy] = -1;
gotoxy(ix*2, iy);
printf("⊙");
Eat_Apple = 0;
}

void Game_Over()//蛇死掉了
{
gotoxy(30, 10);
printf("Game Over");
Sleep(3000);
system("pause > nul");
exit(0);
}

void Move_Snake()//让蛇动起来
{
int ix, iy;

for(ix = 0; ix < LEN; ++ix)//先标记蛇头
for(iy = 0; iy < WID; ++iy)
if(Snake[ix][iy] == 1)
{
switch(Sna_Hea_Dir)//根据新的蛇头方向标志蛇头
{
case 'w':
if(iy == 0)
Game_Over();
else
Sna_Hea_Y = iy - 1;
Sna_Hea_X = ix;

break;
case 's':
if(iy == (WID -1))
Game_Over();
else
Sna_Hea_Y = iy + 1;
Sna_Hea_X = ix;

break;
case 'a':
if(ix == 0)
Game_Over();
else
Sna_Hea_X = ix - 1;
Sna_Hea_Y = iy;

break;
case 'd':
if(ix == (LEN - 1))
Game_Over();
else
Sna_Hea_X = ix + 1;
Sna_Hea_Y = iy;

break;
default:
break;
}
}

if(Snake[Sna_Hea_X][Sna_Hea_Y]!=1&&Snake[Sna_Hea_X][Sna_Hea_Y]!=0&&Snake[Sna_Hea_X][Sna_Hea_Y]!=-1)
Game_Over();

if(Snake[Sna_Hea_X][Sna_Hea_Y] < 0)//吃到苹果
{
++Snake_Len;
Eat_Apple = 1;
}
for(ix = 0; ix < LEN; ++ix)//处理蛇尾
for(iy = 0; iy < WID; ++iy)
{
if(Snake[ix][iy] > 0)
{
if(Snake[ix][iy] != Snake_Len)
Snake[ix][iy] += 1;
else
Snake[ix][iy] = 0;
}
}

Snake[Sna_Hea_X][Sna_Hea_Y] = 1;//处理蛇头
}

void Get_Input()//控制蛇的移动方向
{
if(kbhit())
{
switch(getch())
{
case 87:

Sna_Hea_Dir = 'w';
break;
case 83:

Sna_Hea_Dir = 's';
break;
case 65:

Sna_Hea_Dir = 'a';
break;
case 68:

Sna_Hea_Dir = 'd';
break;
default:
break;
}
}

if(clock() - Now_Time >= Wait_Time)//蛇到时间自动行走
{
Clear_Snake();
Move_Snake();
Print_Snake();
Now_Time = clock();
}
}

void Init()//初始化
{
system("title 贪吃毛毛蛇");
system("mode con: cols=80 lines=25");
Hide_Cursor();

gotoxy(61, 4);
printf("You Score:");
gotoxy(61, 6);
printf("You Level:");
gotoxy(61, 8);
printf("The Lenght:");
gotoxy(61, 10);
printf("The Speed:");
gotoxy(61, 12);
printf("Apple Num:");

int i;
for(i = 0; i < Snake_Len; ++i)//生成蛇
Snake[10+i][15] = i+1;

int iy, ix;//打印蛇
for(iy = 0; iy < WID; ++iy)
for(ix = 0; ix < LEN; ++ix)
{
if(Snake[ix][iy])
{
SetColor(Snake[ix][iy]);
gotoxy(ix*2, iy);
printf("■");
}
}
}

void Pri_News()//打印信息
{
SetColor(0xe);
gotoxy(73,4);
All_Score += Level;
printf("%3d", All_Score);
gotoxy(73, 6);
printf("%3d", Level);
gotoxy(73, 8);
printf("%3d",Snake_Len);
gotoxy(73, 10);
printf("0.%3ds", Wait_Time/10);
gotoxy(73, 12);
printf("%d", Apple_Num);
}

void Lev_Sys()//等级系统
{
if(((Apple_Num-1) / 10) == Level)
{
++Level;
if(Wait_Time > 50)
Wait_Time -= 50;
else
if(Wait_Time > 10)
Wait_Time -= 10;
else
Wait_Time -= 1;
}
}

int main(void)
{
Init();
srand((unsigned)time(NULL));//设置随机数的种子
Now_Time = clock();
int speed1=1000,speed2,a;
printf("\n");
printf("请输入你想要的速度\n");
scanf("%d",&speed2);
Level=1;
Wait_Time=speed1-speed2;
printf("请输入你想要的苹果数\n");
scanf("%d",&a);

while(a--)
Rand_Apple();
while(1)
{
if(Eat_Apple)
{
++Apple_Num;
Rand_Apple();
Lev_Sys();
Pri_News();
}
Get_Input();
Sleep(10);
}
return 0;
}

Ⅲ 求用C语言编写一个简单游戏的代码

太长了,发不上去,LZ给个邮箱什么的呗,。

Ⅳ c语言小游戏代码

“贪吃蛇”C代码,在dev C++试验通过(用4个方向键控制)

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <time.h>

#include <Windows.h>

#define W 78 //游戏框的宽,x轴

#define H 26 //游戏框的高,y轴

int dir=3; //方向变量,初值3表示向“左”

int Flag=0; //吃了食物的标志(1是0否)

int score=0; //玩家得分

struct food{ int x; //食物的x坐标

int y; //食物的y坐标

}fod; //结构体fod有2个成员

struct snake{ int len; //蛇身长

int speed; //移动速度

int x[100]; //蛇身某节x坐标

int y[100]; //蛇身某节y坐标

}snk; //结构体snk有4个成员

void gtxy( int x,int y) //控制光标移动的函数

{ COORD coord;

coord.X=x;

coord.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

void gtxy( int x,int y); //以下声明要用到的几个自编函数

void csh( ); //初始化界面

void keymove( ); //按键操作移动蛇

void putFod( ); //投放食物

int Over( ); //游戏结束(1是0否)

void Color(int a); //设定显示颜色的函数

int main( ) //主函数

{ csh( );

while(1)

{ Sleep(snk.speed);

keymove( );

putFod( );

if(Over( ))

{ system(“cls”);

gtxy(W/2+1,H/2); printf(“游戏结束!T__T”);

gtxy(W/2+1,H/2+2); printf(“玩家总分:%d分”,score);

getch( );

break;

}

}

return 0;

}

void csh( ) //初始化界面

{ int i;

gtxy(0,0);

CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下两行是隐藏光标的设置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

for(i=0;i<=W;i=i+2) //横坐标要为偶数,因为这个要打印的字符占2个位置

{Color(2); //设定打印颜色为绿色

gtxy(i,0); printf("■"); //打印上边框

gtxy(i,H); printf("■"); //打印下边框

}

for(i=1;i<H;i++)

{ gtxy(0,i); printf("■"); //打印左边框

gtxy(W,i); printf("■"); //打印右边框

}

while(1)

{ srand((unsigned)time(NULL)); //初始化随机数发生器srand( )

fod.x=rand()%(W-4)+2; //随机函数rand( )产生一个从0到比”(W-4)”小1的数再加2

fod.y=rand()%(H-2)+1; //随机函数rand( )产生一个从0到比”(H-2)”小1的数再加1

if (fod.x%2==0) break; //fod.x是食物的横坐标,要是2的倍数(为偶数)

}

Color(12); //设定打印颜色为淡红

gtxy(fod.x,fod.y); printf("●"); //到食物坐标处打印初试食物

snk.len=3; //蛇身长初值为3节

snk.speed=350; //刷新蛇的时间,即移动速度初值为350毫秒

snk.x[0]=W/2+1; //蛇头横坐标要为偶数(因为W/2=39)

snk.y[0]=H/2; //蛇头纵坐标

Color(9); //设定打印颜色为淡蓝

gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头

for(i=1;i<snk.len;i++)

{ snk.x[i]=snk.x[i-1]+2; snk.y[i]=snk.y[i-1];

gtxy(snk.x[i],snk.y[i]); printf("■"); //打印蛇身

}

Color(7, 0); //恢复默认的白字黑底

return;

}

void keymove( ) //按键操作移动蛇

{ int key;

if( kbhit( ) ) //如有按键输入才执行下面操作

{ key=getch( );

if (key==224) //值为224表示按下了方向键,下面要再次获取键值

{ key=getch( );

if(key==72&&dir!=2)dir=1; //72表示按下了向上方向键

if(key==80&&dir!=1)dir=2; //80为向下

if(key==75&&dir!=4)dir=3; //75为向左

if(key==77&&dir!=3)dir=4; //77为向右

}

if (key==32)

{ while(1) if((key=getch( ))==32) break; } //32为空格键,这儿用来暂停

}

if (Flag==0) //如没吃食物,才执行下面操作擦掉蛇尾

{ gtxy(snk.x[snk.len-1],snk.y[snk.len-1]); printf(" "); }

int i;

for (i = snk.len - 1; i > 0; i--) //从蛇尾起每节存储前一节坐标值(蛇头除外)

{ snk.x[i]=snk.x[i-1]; snk.y[i]=snk.y[i-1]; }

switch (dir) //判断蛇头该往哪个方向移动,并获取最新坐标值

{ case 1: snk.y[0]--; break; //dir=1要向上移动

case 2: snk.y[0]++; break; //dir=2要向下移动

case 3: snk.x[0]-=2; break; //dir=3要向左移动

case 4: snk.x[0]+=2; break; //dir=4要向右移动

}

Color(9);

gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头

if (snk.x[0] == fod.x && snk.y[0] == fod.y) //如吃到食物则执行以下操作

{ printf("7"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //7是响铃

else Flag = 0; //没吃到食物Flag的值为0

if(snk.speed<150) snk.speed= snk.speed+5; //作弊码,不让速度无限加快

}

void putFod( ) //投放食物

{ if (Flag == 1) //如吃到食物才执行以下操作,生成另一个食物

{ while (1)

{ int i,n= 1;

srand((unsigned)time(NULL)); //初始化随机数发生器srand( )

fod.x = rand( ) % (W - 4) + 2; //产生在游戏框范围内的一个x坐标值

fod.y = rand( ) % (H - 2) + 1; //产生在游戏框范围内的一个y坐标值

for (i = 0; i < snk.len; i++) //随机生成的食物不能在蛇的身体上

{ if (fod.x == snk.x[i] &&fod.y == snk.y[i]) { n= 0; break;} }

if (n && fod.x % 2 == 0) break; //n不为0且横坐标为偶数,则食物坐标取值成功

}

Color(12); //设定字符为红色

gtxy(fod.x, fod.y); printf("●"); //光标到取得的坐标处打印食物

}

return;

}

int Over( ) //判断游戏是否结束的函数

{ int i;

Color(7);

gtxy(2,H+1); printf(“暂停键:space.”); //以下打印一些其它信息

gtxy(2,H+2); printf(“游戏得分:%d”,score);

if (snk.x[0] == 0 || snk.x[0] == W) return 1; //蛇头触碰左右边界

if (snk.y[0] == 0 || snk.y[0] == H) return 1; //蛇头触碰上下边界

for (i = 1; i < snk.len; i++)

{ if (snk.x[0] == snk.x[i] && snk.y[0] == snk.y[i]) return 1; } //蛇头触碰自身

return 0; //没碰到边界及自身时就返回0

}

void Color(int a) //设定颜色的函数

{ SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ),a ); }

Ⅳ C语言 游戏 代码

“坑人的无限”(一):

#include<iostream>
#include<windows.h>
#include<ctime>
#include<cstdlib>
#include<conio.h>
using namespace std;
int a;
class Screen
{
private:
int n;
public:
Screen()
{
n=5;
}
void move1()//注意只是循环输出各个数字,不能对循环输出再进行循环(如果对循环输出0123456789再进行循环,则move1就变成一个无限循环的函数,则下面的screen循环就进行不下去了)
{
for(int i=0;i<10;++i)
{
cout<<char(1)<<" ";
}
}
void move2()
{
char i;
for(i='a';i<='z';++i)
{
cout<<char(1)<<" ";
}
}
void screen()
{
int t;
while(!kbhit())
{
t=time(0)%(2*n);//如果是放在循环外面的话,time(0)的值就一直不变,放在循环里面,一秒钟进行一次判断,一秒钟进行一次循环
if(t<n)
move1();
else
move2();
}
}
};
int main(){
cout<<"欢迎来到“无限 ”游戏"<<char(1)<<endl;
cout<<"下面会输出无限个笑脸"<<char(1)<<endl;
cout<<"按'enter'取消"<<endl;
Sleep(4000);
Screen s;
s.screen();
cout<<endl<<"哈哈!!控制不住了吧!"<<char(1)<<endl;
cout<<"接下来会更让你丧心病狂的!"<<char(1)<<endl;
cout<<"但是坚持过后必有彩蛋!!!!!!加油!!";
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
Sleep(10000);
for(int as=0;as<=50;as++){
for(int i=0;i<=100;i++){
for(int j=0;j<=10;j++){
cout<<char(2)<<" ";
}
cout<<endl;
}
for(int i=0;i<=100;i++){
for(int j=0;j<=10;j++){
cout<<char(1)<<" ";
}
cout<<endl;
}
}
cout<<"你居然坚持下来了!"<<char(1)<<" "<<char(2)<<endl<<"不可思议呀!"<<endl;
for(int i=0;i<=10;i++){
cout<<"-----------------------------------------------"<<endl;
}
cout<<"敬请期待!等待无限游戏(二)!";
return 0;
}

Ⅵ 求C语言小游戏源程序

简易“推箱子”游戏C代码:

#include <stdio.h>

#include <conio.h>

#include<stdlib.h>

#include<windows.h>

int m =0; //m代表第几关

struct maps{short a[9][11]; };

struct maps map[5]={ 0,0,0,0,0,0,0,0,0,0,0, //共5关,每关9行

0,1,1,1,1,1,1,1,0,0,0,

0,1,0,0,0,0,0,1,1,1,0,

1,1,4,1,1,1,0,0,0,1,0, //0空地,1墙

1,5,0,0,4,0,0,4,0,1,0, //4是箱子,5是人

1,0,3,3,1,0,4,0,1,1,0, //3是目的地

1,1,3,3,1,0,0,0,1,0,0, //7是箱子在目的地(4+3)

0,1,1,1,1,1,1,1,1,0,0, //8是人在目的地(5+3)

0,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,0,

0,0,1,1,1,1,0,0,0,0,0,

0,0,1,5,0,1,1,1,0,0,0,

0,0,1,0,4,0,0,1,0,0,0,

0,1,1,1,0,1,0,1,1,0,0,

0,1,3,1,0,1,0,0,1,0,0,

0,1,3,4,0,0,1,0,1,0,0,

0,1,3,0,0,0,4,0,1,0,0,

0,1,1,1,1,1,1,1,1,0,0,

0,0,0,0,0,0,0,0,0,0,0,

0,0,0,1,1,1,1,1,1,1,0,

0,0,1,1,0,0,1,0,5,1,0,

0,0,1,0,0,0,1,0,0,1,0,

0,0,1,4,0,4,0,4,0,1,0,

0,0,1,0,4,1,1,0,0,1,0,

1,1,1,0,4,0,1,0,1,1,0,

1,3,3,3,3,3,0,0,1,0,0,

1,1,1,1,1,1,1,1,1,0,0,

0,1,1,1,1,1,1,1,1,1,0,

0,1,0,0,1,1,0,0,0,1,0,

0,1,0,0,0,4,0,0,0,1,0,

0,1,4,0,1,1,1,0,4,1,0,

0,1,0,1,3,3,3,1,0,1,0,

1,1,0,1,3,3,3,1,0,1,1,

1,0,4,0,0,4,0,0,4,0,1,

1,0,0,0,0,0,1,0,5,0,1,

1,1,1,1,1,1,1,1,1,1,1,

0,0,0,0,0,0,0,0,0,0,0,

0,0,0,1,1,1,1,1,1,0,0,

0,1,1,1,0,0,0,0,1,0,0,

1,1,3,0,4,1,1,0,1,1,0,

1,3,3,4,0,4,0,0,5,1,0,

1,3,3,0,4,0,4,0,1,1,0,

1,1,1,1,1,1,0,0,1,0,0,

0,0,0,0,0,1,1,1,1,0,0,

0,0,0,0,0,0,0,0,0,0,0 };

void DrMap( ) //绘制地图

{ CONSOLE_CURSOR_INFO cursor_info={1,0}; //隐藏光标的设置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

printf(" 推箱子");

printf(" ");

for (int i = 0; i < 9; i++)

{for (int j = 0; j < 11; j++)

{switch (map[m].a[i][j])

{case 0: printf(" "); break;

case 1: printf("■"); break;

case 3: printf("◎");break;

case 4: printf("□"); break;

case 5: printf("♀"); break; //5是人

case 7: printf("□"); break; //4 + 3箱子在目的地中

case 8: printf("♀");break; // 5 + 3人在目的地中

}

}

printf(" ");

}

}

void gtxy(int x, int y) //控制光标位置的函数

{ COORD coord;

coord.X = x;

coord.Y = y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

void start( ) //开始游戏

{ int r, c; //存储人的下标

for (int i = 0; i < 9; i++)

{ for (int j = 0; j < 11; j++)

{if (map[m].a[i][j] == 5||map[m].a[i][j]==8) { r = i; c = j; } } //i j 人的下标

}

char key;

key = getch( );

switch (key)

{case 'W':

case 'w':

case 72:

if (map[m]. a[r - 1][c] == 0|| map[m]. a [r - 1][c] == 3)

{ gtxy(2*c+8,r-1+3); printf("♀"); // gtxy(2*c+8,r-1+3)是到指定位置输出字符

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r - 1][c] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r - 1][c] == 4 || map[m]. a [r - 1][c] == 7)

{ if (map[m]. a [r - 2][c] == 0 || map[m]. a [r - 2][c] == 3)

{ gtxy(2*c+8,r-2+3); printf("□"); gtxy(2*c+8,r-1+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r - 2][c] += 4; map[m]. a [r - 1][c] += 1;

map[m]. a [r][c] -= 5; }

} break;

case 'S':

case 's':

case 80:

if (map[m]. a [r + 1][c] == 0 || map[m]. a [r + 1][c] == 3)

{ gtxy(2*c+8,r+1+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r + 1][c] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r + 1][c] == 4 || map[m]. a [r+ 1][c] == 7)

{ if (map[m]. a [r + 2][c] == 0 || map[m]. a [r + 2][c] == 3)

{ gtxy(2*c+8,r+2+3); printf("□"); gtxy(2*c+8,r+1+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r + 2][c] += 4; map[m]. a [r + 1][c] += 1;

map[m]. a [r][c] -= 5; }

}break;

case 'A':

case 'a':

case 75:

if (map[m]. a [r ][c - 1] == 0 || map[m]. a [r ][c - 1] == 3)

{ gtxy(2*(c-1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r ][c - 1] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r][c - 1] == 4 || map[m]. a [r][c - 1] == 7)

{if (map[m]. a [r ][c - 2] == 0 || map[m]. a [r ][c - 2] == 3)

{ gtxy(2*(c-2)+8,r+3); printf("□"); gtxy(2*(c-1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r ][c - 2] += 4; map[m]. a [r ][c - 1] += 1;

map[m]. a [r][c] -= 5; }

}break;

case 'D':

case 'd':

case 77:

if (map[m]. a [r][c + 1] == 0 || map[m]. a [r][c + 1] == 3)

{ gtxy(2*(c+1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8) {gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r][c + 1] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r][c + 1] == 4 || map[m]. a [r][c + 1] == 7)

{ if (map[m]. a [r][c + 2] == 0 || map[m]. a [r][c + 2] == 3)

{ gtxy(2*(c+2)+8,r+3); printf("□"); gtxy(2*(c+1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r][c + 2] += 4; map[m]. a [r][c + 1] += 1;

map[m]. a [r][c] -= 5; }

}break;

}

}

int ifwan( ) //是否完成(1是0否)

{ if(m==0){if(map[m].a[5][2]==7&& map[m].a[5][3]==7&&

map[m].a[6][2]==7&& map[m].a[6][3]==7) return 1;}

if(m==1){if(map[m].a[5][2]==7&& map[m].a[6][2]==7&&

map[m].a[7][2]==7) return 1;}

if(m==2){if(map[m].a[7][1]==7&& map[m].a[7][2]==7&& map[m].a[7][3]==7&&

map[m].a[7][4]==7&& map[m].a[7][5]==7) return 1;}

if(m==3){if(map[m].a[4][4]==7&& map[m].a[4][5]==7&& map[m].a[4][6]==7&&

map[m].a[5][4]==7&& map[m].a[5][5]==7&& map[m].a[5][6]==7) return 1;}

if(m==4){if(map[m].a[3][2]==7&& map[m].a[4][1]==7&& map[m].a[4][2]==7&&

map[m].a[5][1]==7&& map[m].a[5][2]==7) return 1;}

return 0;

}

int main( ) //主函数

{ while (1)

{ system("cls");

DrMap( );

while (1)

{ start( );

if(ifwan()){printf("07");break;} //完成后响铃

}

m+=1;

}

return 0;

}

Ⅶ 谁有一些简单小游戏的C语言程序

可以学写“贪吃蛇”代码:

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <time.h>

#include <Windows.h>

#define W 78 //游戏框的宽,x轴

#define H 26 //游戏框的高,y轴

int dir=3; //方向变量,初值表示向“左”

int Flag=0; //吃了食物的标志(1是0否)

int score=0; //玩家得分

struct food{ int x; //食物的x坐标

int y; //食物的y坐标

}fod; //结构体fod有2个成员

struct snake{ int len; //蛇身长度

int speed; //速度

int x[100];

int y[100];

}snk; //结构体snk有4个成员

void gtxy( int x,int y) //控制光标移动的函数

{ COORD coord;

coord.X=x;

coord.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

void gtxy( int x,int y); //以下声明要用到的几个自编函数

void csh( ); //初始化界面

void keymove( ); //按键操作移动蛇

void putFod( ); //投放食物

int Over( ); //游戏结束(1是0否)

void setColor(unsigned short p, unsigned short q); //设定显示颜色

int main( ) //主函数

{ csh( );

while(1)

{ Sleep(snk.speed);

keymove( );

putFod( );

if(Over( )) {system(“cls”);

gtxy(W/2+1,H/2); printf(“游戏结束!T__T”);

gtxy(W/2+1,H/2+2); printf(“玩家总分:%d分”,score);

getch( ); break;

}

}

return 0;

}

void csh( ) //初始化界面

{ int i; gtxy(0,0);

CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下两行是隐藏光标的设置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

for(i=0;i<=W;i=i+2) //横坐标要为偶数,因为这个要打印的字符占2个位置

{ setColor(2, 0); //设定打印颜色为绿字黑底

gtxy(i,0); printf("■"); //打印上边框

gtxy(i,H); printf("■"); //打印下边框

}

for(i=1;i<H;i++)

{ gtxy(0,i); printf("■"); //打印左边框

gtxy(W,i); printf("■"); //打印右边框

}

while(1)

{ srand((unsigned)time(NULL)); //启动随机数发生器srand( )

fod.x=rand()%(W-4)+2; //随机函数rand( )产生一个从0到比”(W-4)”小1的数再加2

fod.y=rand()%(H-2)+1; //随机函数rand( )产生一个从0到比”(H-2)”小1的数再加1

if (fod.x%2==0) break; //fod.x是食物的横坐标,要是2的倍数(为偶数)

}

setColor(12, 0); //设定打印颜色为淡红字黑底

gtxy(fod.x,fod.y); printf("●"); //到食物坐标处打印初试食物

snk.len=3; //蛇身长

snk.speed=350; //刷新蛇的时间,即是移动速度

snk.x[0]=W/2+1; //蛇头横坐标要为偶数(因为W/2=39)

snk.y[0]=H/2; //蛇头纵坐标

setColor(9, 0); //设定打印颜色为淡蓝字黑底

gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头

for(i=1;i<snk.len;i++)

{ snk.x[i]=snk.x[i-1]+2; snk.y[i]=snk.y[i-1];

gtxy(snk.x[i],snk.y[i]); printf("■"); //打印蛇身

}

setColor(7, 0); //恢复默认的白字黑底

return;

}

void keymove( ) //按键操作移动蛇

{ int key;

if( kbhit( ) ) //如有按键输入才执行下面操作

{ key=getch( );

if (key==224) //值为224表示按下了方向键,下面要再次获取键值

{ key=getch( );

if(key==72&&dir!=2)dir=1; //72表示按下了向上方向键

if(key==80&&dir!=1)dir=2; //80为向下

if(key==75&&dir!=4)dir=3; //75为向左

if(key==77&&dir!=3)dir=4; //77为向右

}

if (key==32)

{ while(1) if((key=getch( ))==32) break; } //32为空格键,这儿用来暂停

}

if (Flag==0) //如没吃食物,才执行下面操作擦掉蛇尾

{gtxy(snk.x[snk.len-1],snk.y[snk.len-1]); printf(" "); }

int i;

for (i = snk.len - 1; i > 0; i--) //从蛇尾起每节存储前一节坐标值(蛇头除外)

{ snk.x[i]=snk.x[i-1]; snk.y[i]=snk.y[i-1]; }

switch (dir) //判断蛇头该往哪个方向移动,并获取最新坐标值

{ case 1: snk.y[0]--; break; //dir=1要向上移动

case 2: snk.y[0]++; break; //dir=2要向下移动

case 3: snk.x[0]-=2; break; //dir=3要向左移动

case 4: snk.x[0]+=2; break; //dir=4要向右移动

}

setColor(9, 0);

gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头

if (snk.x[0] == fod.x && snk.y[0] == fod.y) //如吃到食物则执行以下操作

{ printf("07"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; }//这007是响铃

else Flag = 0; //没吃到食物Flag的值为0

if(snk.speed<150) snk.speed= snk.speed+5; //作弊码,不让速度无限加快

}

void putFod( ) //投放食物

{ if (Flag == 1) //如吃到食物才执行以下操作,生成另一个食物

{ while (1)

{ int i,n= 1;

srand((unsigned)time(NULL)); //启动随机数发生器srand( ),接下产生食物坐标值

fod.x = rand( ) % (W - 4) + 2; //产生在游戏框范围内的一个x坐标值

fod.y = rand( ) % (H - 2) + 1; //产生在游戏框范围内的一个y坐标值

for (i = 0; i < snk.len; i++) //随机生成的食物不能在蛇的身体上

{ if (fod.x == snk.x[i] &&fod.y == snk.y[i]) { n= 0; break;} }

if (n && fod.x % 2 == 0) break; //n不为0且横坐标为偶数,则食物坐标取值成功

}

setColor(12, 0);

gtxy(fod.x, fod.y); printf("●"); //光标到取得的坐标处打印食物

}

return;

}

int Over( ) //判断游戏是否结束的函数

{ int i;

setColor(7, 0);

gtxy(2,H+1); printf(“暂停键:space.”); //以下打印一些其它信息

gtxy(2,H+2); printf(“游戏得分:%d”,score);

if (snk.x[0] == 0 || snk.x[0] == W) return 1; //蛇头触碰左右边界

if (snk.y[0] == 0 || snk.y[0] == H) return 1; //蛇头触碰上下边界

for (i = 1; i < snk.len; i++)

{ if (snk.x[0] == snk.x[i] && snk.y[0] == snk.y[i]) return 1; } //蛇头触碰自身

return 0; //没碰到边界及自身时就返回0

}

void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)

{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);

} //用来设定颜色的函数

Ⅷ 求c语言的“纸牌游戏”代码

贪心算法,首先计算平均值,然后从左往右扫描,不够平均值的从右边拿,次数回+1;超过平均值的往右边答放,次数+1。#include
int
main(){
int
n,i,move=0;
long
a[101],num=0;
scanf("%d",&n);
for
(i=0;i
num){
a[i+1]+=(a[i]-num);
move++;
}
if
(a[i]

评论
0

0

加载更多

Ⅸ 求一个用C语言编写的小游戏代码

/*也不知道你是什么级别的,我是一个
新手
,刚接触
编程语言
,以下是我自己变得一个小
程序
,在所有c语言的编译器(vc++6.0、turbo…………)上都能运行,你还可以进一步改进。这是一个类似
贪吃蛇

小游戏

祝你好运
*/
/*贪吃蛇*/
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
int
head=3
,tail=0;
int
main()
{
int
i,j,k=0;
int
zuobiao[2][80];
long
start;
int
direction=77;
int
gamespeed;
int
timeover;
int
change(char
qipan[20][80],int
zuobiao[2][80],char
direction);
zuobiao[0][tail]=1;zuobiao[1][tail]=1;zuobiao[0][1]=1;zuobiao[1][1]=2;zuobiao[0][2]=1;zuobiao[1][2]=3;zuobiao[0][head]=1;zuobiao[1][head]=4;
/*处理棋盘*/
char
qipan[20][80];//
定义
棋盘
for(i=0;i<20;i++)
for(j=0;j<80;j++)
qipan[i][j]='
';//
初始化
棋盘
for(i=0;i<80;i++)
qipan[0][i]='_';
for(i=0;i<20;i++)
qipan[i][0]='|';
for(i=0;i<20;i++)
qipan[i][79]='|';
for(i=0;i<80;i++)
qipan[19][i]='_';
qipan[1][1]=qipan[1][2]=qipan[1][3]='*';//初始化蛇的位置
qipan[1][4]='#';
printf("This
is
a
game
of
a
SNAKE.
GOOD
LUCK
TO
YOU
!
");
printf("Input
your
game
speed,please.(e.g.300)
");
scanf("%d",&gamespeed);
while(direction!='q')
{
system("cls");
for(i=0;i<20;i++)//打印出棋盘
for(j=0;j<80;j++)
printf("%c",qipan[i][j]);
timeover=1;
start=clock();
while(!kbhit()&&(timeover=clock()-start<=gamespeed));
if(timeover)
{
getch();
direction=getch();
}
else
direction=direction;
if(!(direction==72||direction==80||direction==75||direction==77))
{
return
0;
system("cls");
printf("GAME
OVER!
");
}
if(!change(qipan,zuobiao,direction))
{
direction='q';
system("cls");
printf("GAME
OVER!
");
}
}
return
0;
}
int
change(char
qipan[20][80],int
zuobiao[2][80],char
direction)
{
int
x,y;
if(direction==72)
x=zuobiao[0][head]-1;y=zuobiao[1][head];
if(direction==80)
x=zuobiao[0][head]+1;y=zuobiao[1][head];
if(direction==75)
x=zuobiao[0][head];y=zuobiao[0][head]-1;
if(direction==77)
x=zuobiao[0][head];y=zuobiao[1][head]+1;
if(x==0||x==18||y==78||y==0)
return
0;
if(qipan[x][y]!='
')
return
0;
qipan[zuobiao[0][tail]][zuobiao[1][tail]]='
';
tail=(tail+1)%80;
qipan[zuobiao[0][head]][zuobiao[1][head]]='*';
head=(head+1)%80;
zuobiao[0][head]=x;
zuobiao[1][head]=y;
qipan[zuobiao[0][head]][zuobiao[1][head]]='#';
return
1;
}