c游戲代碼大全
「猜數字小游戲」,每個數字後按空格,最後按回車確認
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int a[4],b[4];
int count=0; //計算猜測次數
void csh( ); //初始化
void start( ); //開始游戲
int main( )
{ csh( );
start( );
}
void csh( ) //初始化
{ printf(" 猜 數 字 小 游 戲 ");
printf(「 猜四個數字,如數字與順序都正確記為A,數字正確位置不對記為B. 」);
}
void start( ) //開始游戲
{int m,n; //m是完全猜對的個數,n是順序不對的個數
while(1)
{srand((unsigned)time(NULL)); //初始化隨機數發生器srand( )
while(1) { for(int i=0;i<4;i++) a[i]=rand( )%10; //rand( )函數每次隨機產生一個0-9的數
if( (a[3]!=a[2]&&a[3]!=a[1]&&a[3]!=a[0])&&
(a[2]!=a[1]&&a[2]!=a[0])&&a[1]!=a[0] ) break; } //4個隨機數各自不相等
printf(" 請依次輸入4個一位整數: ");
while(1)
{for(int i=0;i<4;i++) scanf(「%d」,&b[i]);
printf(" 你輸入的是:%d %d %d %d ",b[0],b[1],b[2],b[3]);
m=0;n=0;
for(int i=0;i<4;i++)
{for(int j=0;j<4;j++)
{ if(b[i]==a[j]&&i==j)m=m+1; if(b[i]==a[j]&&i!=j)n=n+1; }
}
count=count+1;
printf(" %dA %dB 你試了%d次 ",m,n,count);
if(m==4)break;
if(count==8){ count=0; break; }
}
printf(" ");
if(m==4)printf(" 你猜對了(^-^)! 就是:%d %d %d %d ",a[0],a[1],a[2],a[3]);
else printf(" 你輸了(T-T)!哈哈!應該是:%d %d %d %d ",a[0],a[1],a[2],a[3]);
int z;
printf(" (要繼續嗎?1或0) ");
scanf(「%d」,&z);
if(z==0) break;
}
}
Ⅱ 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語言各種小游戲的代碼,能發給我嗎
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;
}
Ⅳ 求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("