❶ 誰能提供幾個比較好的c語言游戲源碼下載網站呢pudn和csdn的就不要說了下寫小游戲代碼研究下!
成都聚人科技有限公司旗下 聚游戲網 西南最大的游戲門戶網 強烈推薦
❷ C語言掃雷游戲源代碼
"掃雷"小游戲C代碼
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
main( )
{char a[102][102],b[102][102],c[102][102],w;
int i,j; /*循環變數*/
int x,y,z[999]; /*雷的位置*/
int t,s; /*標記*/
int m,n,lei; /*計數*/
int u,v; /*輸入*/
int hang,lie,ge,mo; /*自定義變數*/
srand((int)time(NULL)); /*啟動隨機數發生器*/
leb1: /*選擇模式*/
printf("
請選擇模式:
1.標准 2.自定義
");
scanf("%d",&mo);
if(mo==2) /*若選擇自定義模式,要輸入三個參數*/
{do
{t=0; printf("請輸入
行數 列數 雷的個數
");
scanf("%d%d%d",&hang,&lie,&ge);
if(hang<2){printf("行數太少
"); t=1;}
if(hang>100){printf("行數太多
");t=1;}
if(lie<2){printf("列數太少
");t=1;}
if(lie>100){printf("列數太多
");t=1;}
if(ge<1){printf("至少要有一個雷
");t=1;}
if(ge>=(hang*lie)){printf("雷太多了
");t=1;}
}while(t==1);
}
else{hang=10,lie=10,ge=10;} /*否則就是選擇了標准模式(默認參數)*/
for(i=1;i<=ge;i=i+1) /*確定雷的位置*/
{do
{t=0; z[i]=rand( )%(hang*lie);
for(j=1;j<i;j=j+1){if(z[i]==z[j]) t=1;}
}while(t==1);
}
for(i=0;i<=hang+1;i=i+1) /*初始化a,b,c*/
{for(j=0;j<=lie+1;j=j+1) {a[i][j]='1'; b[i][j]='1'; c[i][j]='0';} }
for(i=1;i<=hang;i=i+1)
{for(j=1;j<=lie;j=j+1) {a[i][j]='+';} }
for(i=1;i<=ge;i=i+1) /*把雷放入c*/
{x=z[i]/lie+1; y=z[i]%lie+1; c[x][y]='#';}
for(i=1;i<=hang;i=i+1) /*計算b中數字*/
{for(j=1;j<=lie;j=j+1)
{m=48;
if(c[i-1][j-1]=='#')m=m+1; if(c[i][j-1]=='#')m=m+1;
if(c[i-1][j]=='#')m=m+1; if(c[i+1][j+1]=='#')m=m+1;
if(c[i][j+1]=='#')m=m+1; if(c[i+1][j]=='#')m=m+1;
if(c[i+1][j-1]=='#')m=m+1; if(c[i-1][j+1]=='#')m=m+1;
b[i][j]=m;
}
}
for(i=1;i<=ge;i=i+1) /*把雷放入b中*/
{x=z[i]/lie+1; y=z[i]%lie+1; b[x][y]='#';}
lei=ge; /*以下是游戲設計*/
do
{leb2: /*輸出*/
system("cls");printf("
");
printf(" ");
for(i=1;i<=lie;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c ",w);
}
printf("
|");
for(i=1;i<=lie;i=i+1){printf("---|");}
printf("
");
for(i=1;i<=hang;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c |",w);
for(j=1;j<=lie;j=j+1)
{if(a[i][j]=='0')printf(" |");
else printf(" %c |",a[i][j]);
}
if(i==2)printf(" 剩餘雷個數");
if(i==3)printf(" %d",lei);
printf("
|");
for(j=1;j<=lie;j=j+1){printf("---|");}
printf("
");
}
scanf("%d%c%d",&u,&w,&v); /*輸入*/
u=u+1,v=v+1;
if(w!='#'&&a[u][v]=='@')
goto leb2;
if(w=='#')
{if(a[u][v]=='+'){a[u][v]='@'; lei=lei-1;}
else if(a[u][v]=='@'){a[u][v]='?'; lei=lei+1;}
else if(a[u][v]=='?'){a[u][v]='+';}
goto leb2;
}
a[u][v]=b[u][v];
leb3: /*打開0區*/
t=0;
if(a[u][v]=='0')
{for(i=1;i<=hang;i=i+1)
{for(j=1;j<=lie;j=j+1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1; if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=1;i<=hang;i=i+1)
{for(j=lie;j>=1;j=j-1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1; if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=hang;i>=1;i=i-1)
{for(j=1;j<=lie;j=j+1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1; if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=hang;i>=1;i=i-1)
{for(j=lie;j>=1;j=j-1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1;if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=1;i<=hang;i=i+1) /*檢測0區*/
{for(j=1;j<=lie;j=j+1)
{if(a[i][j]=='0')
{if(a[i-1][j-1]=='+'||a[i-1][j-1]=='@'||a[i-1][j-1]=='?')t=1;
if(a[i-1][j+1]=='+'||a[i-1][j+1]=='@'||a[i-1][j+1]=='?')t=1;
if(a[i+1][j-1]=='+'||a[i+1][j-1]=='@'||a[i+1][j-1]=='?')t=1;
if(a[i+1][j+1]=='+'||a[i+1][j+1]=='@'||a[i+1][j+1]=='?')t=1;
if(a[i+1][j]=='+'||a[i+1][j]=='@'||a[i+1][j]=='?')t=1;
if(a[i][j+1]=='+'||a[i][j+1]=='@'||a[i][j+1]=='?')t=1;
if(a[i][j-1]=='+'||a[i][j-1]=='@'||a[i][j-1]=='?')t=1;
if(a[i-1][j]=='+'||a[i-1][j]=='@'||a[i-1][j]=='?')t=1;
}
}
}
if(t==1)goto leb3;
}
n=0; /*檢查結束*/
for(i=1;i<=hang;i=i+1)
{for(j=1;j<=lie;j=j+1)
{if(a[i][j]!='+'&&a[i][j]!='@'&&a[i][j]!='?')n=n+1;}
}
}
while(a[u][v]!='#'&&n!=(hang*lie-ge));
for(i=1;i<=ge;i=i+1) /*游戲結束*/
{x=z[i]/lie+1; y=z[i]%lie+1; a[x][y]='#'; }
printf(" ");
for(i=1;i<=lie;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c ",w);
}
printf("
|");
for(i=1;i<=lie;i=i+1){printf("---|");}
printf("
");
for(i=1;i<=hang;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c |",w);
for(j=1;j<=lie;j=j+1)
{if(a[i][j]=='0')printf(" |");
else printf(" %c |",a[i][j]);
}
if(i==2)printf(" 剩餘雷個數");
if(i==3)printf(" %d",lei); printf("
|");
for(j=1;j<=lie;j=j+1) {printf("---|");}
printf("
");
}
if(n==(hang*lie-ge)) printf("你成功了!
");
else printf(" 游戲結束!
");
printf(" 重玩請輸入1
");
t=0;
scanf("%d",&t);
if(t==1)goto leb1;
}
/*註:在DEV c++上運行通過。行號和列號都從0開始,比如要確定第0行第9列不是「雷」,就在0和9中間加入一個字母,可以輸入【0a9】三個字元再按回車鍵。3行7列不是雷,則輸入【3a7】回車;第8行第5列是雷,就輸入【8#5】回車,9行0列是雷則輸入【9#0】並回車*/
❸ 求C語言小游戲源程序
新手要方便寫代碼,可以收藏下面幾個自編函數:
gtxy (6, 3) //游標定位於窗口的第6列,第3行處(准備輸出,行與列都是從0算起)
Color (4, 0) //設置為紅字配黑底 如 Color (10, 0)則是淡綠字配黑底
yinc (1,0) //隱藏游標(第二個參數設為0就隱藏,沒有游標閃爍,yinc代表隱藏)
kou(80,25) //設定窗口緩沖區大小為80列,25行
下面幾個是庫函數,不需自己編寫,只要用#include包含就可以使用。
SetConsoleTitle("俄羅斯方塊"); //設置窗口左上角標題欄處出現"俄羅斯方塊"5個字
srand( (unsigned) time(NULL) ); //初始化隨機數發生器
n= rand( ) % 20; //產生隨機數0-19中的一個. 如 rand( )%5 就產生0-4中的一個數
SetConsoleTitle( )函數在<windows.h>里,srand( )函數與rand( )函數要配合用,
就是同時要用,在<stdlib.h>里。如果 rand( )%10+1 就產生1-10之中的一個數。
Sleep(300); //延時300毫秒(就是程序暫停300毫秒後繼續運行)
system("cls"); //清屏(把窗口裡的內容全部清除,游標定於(0,0)位置處)
這兩個函數都在<windows.h>里。開頭4個自編函數 編寫如下:
void gtxy (int x, int y) //控制游標位置的函數
{ COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos );
}
void Color (short ForeColor= 7, short BackGroundColor= 0) //設定顏色的函數
{ HANDLE hl = GetStdHandle ( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute ( hl, ForeColor + BackGroundColor * 0x10 );
}
聲明時原型可寫 void Color (short x, short y);
void yinc (int x,int y) //隱藏游標的函數
{ CONSOLE_CURSOR_INFO gb={ x , y }; //gb代表游標
SetConsoleCursorInfo ( GetStdHandle(STD_OUTPUT_HANDLE), &gb );
}
void kou(int w,int h) //設置窗口大小的函數
{HANDLE hl=GetStdHandle ( STD_OUTPUT_HANDLE ) ;
COORD size={ w , h };
SetConsoleScreenBufferSize( hl , size );
SMALL_RECT rc={ 0, 0, w, h };
SetConsoleWindowInfo( hl, 1, &rc );
}
最後這個函數,參數w是寬h是高。里邊5行中第一行定義了句柄型變數hl,並給它賦值。
第二行定義了坐標型結構體變數size,它的取值決定了緩沖區的大小。第三行就是使用
size的值設置好緩沖區大小。第四行定義了變數rc,它的值決定當前窗口顯示的位置與
大小(不得超過緩沖區的大小)。前兩個0,0是從緩沖區左上角0列0行位置處開始,後兩
個參數可以小於w和h.比如rc={0,0,w-10,h-5}; 最後一行使用rc的值設置好窗口,中間
那個參數要為" 1 "或寫「 true 」才有效。
❹ C語言的貪吃蛇源代碼
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
void gotoxy(int x,int y) {COORD pos={x,y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}//游標定位
class Food {//食物類
private: int m_x; int m_y;
public:
void randfood() {//隨機產生一個食物
srand((int)time(NULL));//利用時間添加隨機數種子,需要ctime頭文件
L1:{m_x=rand()%(85)+2;//2~86
m_y=rand()%(25)+2;//2~26
if(m_x%2) goto L1;//如果食物的x坐標不是偶數則重新確定食物的坐標
gotoxy(m_x,m_y);//在確認好的位置輸出食物
cout << "★";}}
int getFoodm_x() {return m_x;}//返回食物的x坐標
int getFoodm_y() {return m_y;}};//返回食物的y坐標
class Snake {
private:
struct Snakecoor {int x; int y;};//定義一個蛇的坐標機構
vector<Snakecoor> snakecoor;//將坐標存入vector容器中
//判斷並改變前進方向的函數
void degdir(Snakecoor&nexthead) {//定義新的蛇頭變數
static char key='d';//靜態變數防止改變移動方向後重新改回來
if(_kbhit()) {
char temp=_getch();//定義一個臨時變數儲存鍵盤輸入的值
switch(temp) {//如果臨時變數的值為wasd中的一個,則賦值給key
default: break;//default是預設情況,只有任何條件都不匹配的情況下才會執行 必須寫在前面!不然蛇無法轉向
case'w': case'a': case's': case'd':
//如果temp的方向和key的方向不相反則賦值 因為兩次移動方向不能相反 將蛇設置為初始向右走
if(key=='w' && temp!='s' || key=='s' && temp!='w' || key=='a' && temp!='d' || key=='d' && temp!='a') key=temp;}}
switch (key) {//根據key的值來確定蛇的移動方向
case'd': nexthead.x=snakecoor.front().x+2; nexthead.y=snakecoor.front().y; break;
//新的蛇頭的頭部等於容器內第一個數據(舊蛇頭)x坐標+2 因為蛇頭占兩個坐標,移動一次加2
case'a': nexthead.x=snakecoor.front().x-2; nexthead.y=snakecoor.front().y; break;
case'w': nexthead.x=snakecoor.front().x; nexthead.y=snakecoor.front().y-1; break;
//因為控制台的x長度是y的一半,所以用兩個x做蛇頭,需要的坐標是二倍
case's': nexthead.x=snakecoor.front().x; nexthead.y=snakecoor.front().y+1;}}
//游戲結束時設計一個界面輸出「游戲結束」以及分數
void finmatt(const int score) {
system("cls"); gotoxy(40, 14);//清屏然後輸出
cout << "游戲結束"; gotoxy(40, 16);
cout << "得分:" << score; gotoxy(0, 26);
exit(0);}//exit為C++的退出函數 exit(0)表示程序正常退出,非0表示非正常退出
void finishgame(const int score) {//游戲結束
if(snakecoor[0].x>=88 || snakecoor[0].x<0 || snakecoor[0].y>=28 || snakecoor[0].y<0) finmatt(score);//撞牆
for(int i=1;i<snakecoor.size();i++) if(snakecoor[0].x==snakecoor[i].x && snakecoor[0].y==snakecoor[i].y) finmatt(score);}//撞到自身
public://構造初始化蛇的位置
Snake() { Snakecoor temp;//臨時結構變數用於創建蛇
for(int i=5;i>=0;i--) {//反向創建初始蛇身,初始蛇頭朝右
temp.x=16+(i<<1); temp.y=8;//偶數 在蛇頭左移生成蛇身
snakecoor.push_back(temp);}}//在蛇尾尾插入臨時變數
void move(Food&food, int& score) {//蛇運動的函數
Snakecoor nexthead;//新蛇頭變數
degdir(nexthead);//判斷和改變蛇前進的方向
snakecoor.insert(snakecoor.begin(), nexthead);//將蛇頭插入容器的頭部
gotoxy(0, 0); cout << "得分:" << score;//每次移動都在左上角刷新得分
gotoxy(0, 2); cout << "蛇的長度為:" << snakecoor.size();//長度用來測試
finishgame(score);//判斷游戲結束,輸出分數
//吃到食物蛇的變化
if(snakecoor[0].x==food.getFoodm_x() && snakecoor[0].y==food.getFoodm_y()) {//蛇頭與食物重合
gotoxy(snakecoor[0].x, snakecoor[0].y); cout << "●";//吃到食物時這次蛇沒有移動,所以蛇會卡頓一下
gotoxy(snakecoor[1].x, snakecoor[1].y); cout << "■";//重新輸出一下蛇頭和第一節蛇身讓蛇不卡頓
score++; food.randfood(); return;}//吃到食物得分+1,如果蛇頭坐標和食物坐標重合則重新產生一個食物並直接結束本次移動
for(int i=0;i<snakecoor.size();i++) {//遍歷容器,判斷食物與蛇身是否重合並輸出整條蛇
gotoxy(snakecoor[i].x, snakecoor[i].y);
if (!i) cout << "●"; else cout << "■";//頭部輸出圓形否則輸出方塊
if (snakecoor[i].x==food.getFoodm_x() && snakecoor[i].y==food.getFoodm_y())food.randfood();}//如果食物刷新到了蛇身上,則重新產生一個
gotoxy(snakecoor.back().x,snakecoor.back().y);cout << " ";snakecoor.pop_back();}};
//數據與畫面是分開,的在容器尾部的地方輸出空格 清除畫面上的蛇尾,刪除容器中最後一個數據 清除數據上的蛇尾
void HideCursor() {CONSOLE_CURSOR_INFO cursor_info={1,0};SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);}//隱藏游標
int main() {system("mode con cols=88 lines=28"); system("title 貪吃蛇"); HideCursor();//游標隱藏,設置控制台窗口大小、標題
int score=0; Food food; food.randfood(); Snake snake;//得分變數,食物對象,開局隨機產生一個食物,蛇對象
while(true) {snake.move(food, score);Sleep(150);}return 0;}//蛇移動,游戲速度
❺ 求一些C語言小游戲的源代碼,謝謝
學習一下數字版「拼圖」代碼寫法:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<windows.h>
inti,j,r,k; //i、j、r用於循環,k存放隨機數值
intm,n; //m、n是當前空位的下標
inta[4][4]; //存儲4×4共16個數字的數組
voidshow(void); //輸出界面
voidcsh(void); //初始化界面
intyes(void); //判斷排序是否成功(1是0否)
voip(void);//數字向上移動到空位(空位則下移)
voiddown(void); //數字向下移
voidleft(void); //數字向左移
voidrght(void); //數字向右移
voidinkey(void); //按鍵操作
voidgtxy(intx,inty); //控制游標位置的函數
intmain(void)
{while(1)
{csh();
while(1)
{inkey();
show();
if(yes())
{gtxy(6,12);printf("你成功了!再來一局y/n?");break;}
}
if(getch()=='n')break;
}
return0;
}
voidcsh(void) //初始化
{r=0;
CONSOLE_CURSOR_INFOcursor_info={1,0};//以下兩行是隱藏游標的設置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
for(i=0;i<4;i++)//給數組a依序賦值
for(j=0;j<4;j++)
{if(i==3&&j==3)a[i][j]=0;
elsea[i][j]=1+r++;
}
m=3; n=3; //記下空格(值為0)的下標
down( ); rght( ); rght( ); down( ); //預演4步
srand((unsigned)time(0)); //初始化隨機數發生器
for(r=0;r<500;r++)//將數組各值打亂
{k=rand()%(4);
switch(k)
{case0:{up();break;}
case1:{down();break;}
case2:{left();break;}
case3:{rght();break;}
}
}
system("cls");
printf(" 數字拼圖");
printf(" ┌──────┬──────┬──────┬──────┐");
printf(" │││││");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │││││");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │││││");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │││││");
printf(" └──────┴──────┴──────┴──────┘");
show();
}
voidshow(void)//輸出界面
{for(i=0;i<4;i++)
for(j=0;j<4;j++) //gtxy(7*j+9,2*i+4)是游標到指定位置輸出數字
{gtxy(7*j+9,2*i+4); if(a[i][j]==0)printf("│");
elseif(a[i][j]>9)printf("%d│",a[i][j]);
elseprintf("%d│",a[i][j]);
}
}
voidinkey(void) //按鍵操作
{intkey;
key=getch();
switch(key)
{case72:{up();break;}
case80:{down();break;}
case75:{left();break;}
case77:{rght();break;}
}
}
voip(void) //向上移動
{if(m!=3)//空位不得在下邊界
{a[m][n]=a[m+1][n];m++;a[m][n]=0;}
}
voiddown(void) //向下移動
{if(m!=0)//空位不得在上邊界
{a[m][n]=a[m-1][n];m--;a[m][n]=0;}
}
voidleft(void) //向左移動
{if(n!=3)//空位不得在右邊界
{a[m][n]=a[m][n+1];n++;a[m][n]=0;}
}
voidrght(void) //向右移動
{if(n!=0)//空位不得在左邊界
{a[m][n]=a[m][n-1];n--;a[m][n]=0;}
}
intyes(void)//判斷是否成功
{r=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{if(a[i][j]!=1+r++)return(r==16)?1:0;}
}
voidgtxy(intx,inty)//控制游標位置的函數
{COORDcoord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
❻ 跪求c語言游戲源碼(完全版)
大一編的掃雷(完整),一百多行,Turbo C下編譯成功 #include<stdio.h> #include<conio.h> #include<time.h> void adjust(int*,int*); int a[23][23],c[22][22],mm,nn; int roundmine(int,int); void spread(); main() { int i,j,b[22][22],minenum,mine; int x,y,yy,s; char g,h,z; int*x1; int*y1; time_t t; again:srand((unsigned) time(&t)); textcolor(LIGHTGREEN); for(i=2;i<=21;i++) for(j=2;j<=21;j++) { gotoxy(i,j); putch(219); b[i][j]=1; c[i][j]=1; } minenum=0; for(i=1;i<=22;i++) for(j=1;j<=22;j++) { a[i][j]=0; if(i>1&&i<22&j>1&&j<22) {if((s=rand()%10)>0) a[i][j]=0; else if(s==0) a[i][j]=1; } if(a[i][j]) minenum++; } gotoxy(50,2); puts("dirction"); gotoxy(50,3); puts("SPACE:mark"); gotoxy(50,4); puts("ENTER:dig"); gotoxy(50,8); printf("mine num:%4d",minenum); gotoxy(50,5); printf("ESC:exit"); gotoxy(30,15); puts(" "); gotoxy(30,20); puts(" "); mine=minenum; gotoxy(2,2); x=2;y=2; while(mine) { h=getch(); if(h=='H') gotoxy(x,--y); else if(h=='P') gotoxy(x,++y); else if(h=='K') gotoxy(--x,y); else if(h=='M') gotoxy(++x,y); x1=&x;y1=&y; adjust(x1,y1); if(h==' ') { if(c[wherex()][wherey()]) if(b[wherex()][wherey()]) { textcolor(LIGHTRED); putch(16); mine--; gotoxy(59,8); printf("%4d",mine); gotoxy(x,y); b[wherex()][wherey()]=0; } else { textcolor(LIGHTGREEN); putch(219); mine++; gotoxy(59,8); printf("%4d",mine); gotoxy(x,y); b[wherex()][wherey()]=1; } } if(h=='\015') { mm=wherex();nn=wherey(); c[wherex()][wherey()]=0; if(a[wherex()][wherey()]) { for(i=2;i<=21;i++) for(j=2;j<=21;j++) {gotoxy(i,j); if(a[i][j]) {textcolor(LIGHTRED); putch(017); } } break; } else {spread();gotoxy(mm,nn);} } if(h=='\033') exit(0); } yy=1; for(i=2;i<=21;i++) for(j=2;j<=21;j++) if (a[i][j]==1&&b[i][j]==1) {gotoxy(30,15); cprintf("You lose!!"); yy=0; } if(yy) {gotoxy(30,15); cprintf("You win!!"); } gotoxy(30,20); cprintf("Once again(y/n)?"); z=getch(); if(z=='y') goto again; } void adjust(int* x,int* y) { if(*x<2) gotoxy(*x+=20,*y); else if(*x>21) gotoxy(*x-=20,*y); else if(*y<2) gotoxy(*x,*y+=20); else if(*y>21) gotoxy(*x,*y-=20); } int roundmine(int x,int y) { int i,j,r=0; for(i=x-1;i<=x+1;i++) for(j=y-1;j<=y+1;j++) if(a[i][j]) r++; return r; } void spread() { int r,i,j,x,y; x=wherex(); y=wherey(); r=roundmine(x,y); if(r) {printf("%d",r);c[x][y]=0;} else if(r==0&&x>1&&x<22&&y>1&&y<22) { putchar(' ');c[x][y]=0; for(i=x-1;i<=x+1;i++) for(j=y-1;j<=y+1;j++) if(x>1&&x<22&&y>1&&y<22&&c[i][j]) {gotoxy(i,j); spread(); } } }
❼ 誰有能在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語言游戲編程源代碼
不知道樓主學沒學window api等等;如果沒,只懂C語言是編不出來游戲程序,如果基於windows游戲,你可以學widow api,再編吧。如果想要,這里有http://www.easy-share.com/1911030739/經典小游戲的c語言源代碼.rar
❾ 急需一個C語言 猜拳游戲的源代碼!!!!
分類: 電腦/網路 >> 程序設計 >> 其他編程語言
問題描述:
急需一個C語言 猜拳游戲的源代碼!!!!急需一個C語言 猜拳游戲的源代碼!!!!急需一個C語言 猜拳游戲的源代碼!!!!急需一個C語言 猜拳游戲的源代碼!!!!急需一個C語言 猜拳游戲的源代碼!!!!急需一個C語言 猜拳游戲的源代碼!!!!急需一個C語言 猜拳游戲的源代碼!!!!
解析:
enum p_r_s{
paper,rock,scissors,game,help,instructions,quit
};
#include <stdio.h>
main()
{
enum p_r_s player,machine;
enum p_r_s selection_by_player(),selection_by_machine();
int win,lose,tie;
win=lose=tie=0;
instructions_for_the_player();
while((player=selection_by_player())!=quit)
switch(player){
case paper:
case rock:
case scissors:
machine=selection_by_machine();
if(player==machine){
++tie;
printf("\n a tie");
}
else if(you_won(player,machine)){
++win;
printf("\n you won");
}
else{
++lose;
printf("\n i won");
}
break;
case game:
game_status(win,lose,tie);
break;
case instructions:
instructions_for_the_player();
break;
case help:
help_for_the_player();
break;
}
game_status(win,lose,tie);
printf("\n\nBYE\n\n");
}
instructions_for_the_player()
{
printf("\n%s\n\n%s\n\n%s\n%s\n%s\n\n%s\n%s\n%s\n\n%s\n%s\n%s",
"PAPER,ROCK,SCISSORS",
"In this game",
"p is for paper,",
"r is for rock,",
"s is for scissors.",
"Both the player and the machine will choose one",
"of p,r,or s. If the o choices are the same,",
"then the game is a tie. Otherwise:",
"\"paper covers the rock\" (a win for paper),",
"\"rock breaks the scissors\" (a win for rock),",
"\"scissors cut the paper\" (a win for scissors).");
printf("\n\n%s\n\n%s\n%s\n%s\n%s\n\n%s\n\n%s",
"There are other allowable inputs:",
"g for game status (the number of wins so far),",
"h for help,",
"i for instructions (reprin these instructions),",
"q for quit (to quit the game).",
"This game is played repeatedly until q is entered.",
"Good luck!");
}
enum p_r_s selection_by_player()
{
char c;
enum p_r_s player;
printf("\n\ninput p,r,or s:");
while((c=getchar())==''||c=='\n'||c=='t');
;
switch(c){
case 'p':
player=paper;
break;
case 'r':
player=rock;
break;
case 's':
player=scissors;
break;
case 'g':
player=game;
break;
case 'i':
player=instructions;
break;
case 'q':
player=quit;
break;
default:
player=help;
}
return(player);
}
enum p_r_s selection_by_machine()
{
static int i;
i=++i%3;
return((i==0)? paper:((i==1)? rock:scissors));
}
you_won(player,machine)
enum p_r_s player,machine;
{
int victory;
if(player==paper)
victory=machine==rock;
else if(player==rock)
victory=machine==scissors;
else/*player==scissors*/
victory=machine==paper;
return(victory);
}
game_status(win,lose,tie)
{
printf("\nGAME STATUS");
printf("\n\n%7d%s\n%7d%s\n%7d%s\n%7d%s",
win,"games won by you",
lose,"games won by me",
tie,"game tied",
win+lose+tie,"games played:");
}
help_for_the_player()
{
printf("\n%s\n\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
"the following characters can be used for input:",
" p for paper",
" r for rock",
" s for scissors",
" g to find out the game status",
" h to print this list",
" i to reprint the instructions for this game",
" q to quit this game");
}
❿ 急求C語言編譯的小游戲(如掃雷),附帶源代碼和注釋。
小游戲2048:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<windows.h>
int jsk( ); //計算空格數
void rsgm( ); //重置游戲
void inkey( ); //按鍵輸入
void left( ); //向左移動
void right( ); //向右移動
void up( ); //向上移動
void down( ); //向下移動
void show( ); //輸出界面
void adnum( ); //添加隨機數
void yes( ); //游戲是否結束(1是0否)
void gtxy(int x, int y); //控制游標位置的函數
int a[4][4]; //存儲16個格子中的數字
int score = 0; //每局得分
int best = 0; //最高得分
int ifnum; //是否需要添加數字(1是0否)
int over; //游戲結束標志(1是0否)
int i,j,k;
int main( )
{ rsgm( ); //重置游戲
inkey( ); //按鍵輸入
return 0;
}
void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)
{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);
} //用於控制字元顏色的函數
void rsgm( ) //重置游戲
{ score = 0; ifnum = 1; over = 0; srand((unsigned)time(0)); //啟動隨機數發生器
int n = rand( ) % 16; //隨機函數產生0-15的數字
for (i = 0; i < 4; i++)
{for (j = 0; j < 4; j++)
{ if (n == 0) { int k = rand( ) % 3; if (k == 0 || k == 1) { a[i][j] = 2; }
else { a[i][j] = 4; } n--; }
else { a[i][j] = 0; n--; }
}
}
adnum( );
system("cls");
CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下兩行是隱藏游標的設置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
setColor(14, 0); //設置字體淡黃色,背景為黑色
printf(" 2048小游戲"); setColor(7, 0); //恢復白字黑底
printf(" ┌──────┬──────┬──────┬──────┐");
printf(" │ │ │ │ │");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │ │ │ │ │");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │ │ │ │ │");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │ │ │ │ │");
printf(" └──────┴──────┴──────┴──────┘");
show( );
}
void show( ) //輸出界面
{ for(i=0;i<4;i++)
for(j=0;j<4;j++)
{ gtxy(7*j+9,2*i+4); //gtxy(7*j+9, 2*i+4)是游標到指定位置輸出數字
if(a[i][j]==0){printf(" "); setColor(7, 0); printf("│");}
else if(a[i][j]<10){ if (a[i][j] == 2) {setColor(14, 0); }
else if (a[i][j] == 4) {setColor(13, 0); }
else if (a[i][j] == 8) {setColor(12, 0); }
printf(" %d ", a[i][j]); setColor(7, 0); printf("│");
}
else if (a[i][j] < 100){if (a[i][j] == 16) {setColor(12, 0); }
else if (a[i][j] == 32) {setColor(10, 0); }
else if (a[i][j] == 64) {setColor(2, 0); }
printf(" %d ", a[i][j]); setColor(7, 0); printf("│");
}
else if (a[i][j] < 1000) {if (a[i][j] == 128) {setColor(9, 0); }
else if (a[i][j] == 256) {setColor(1, 0); }
else if (a[i][j] == 512) {setColor(13, 0); }
printf(" %d ", a[i][j]); setColor(7, 0); printf("│");
}
else if (a[i][j] < 10000) {if (a[i][j] == 1024) {setColor(5, 0); }
else {setColor(15, 0); }
printf(" %d ", a[i][j]); setColor(7, 0); printf("│");
}
}
if (jsk( ) == 0)
{ yes( ); if (over) { gtxy(9,12); setColor(10, 0);
printf(" 游戲結束!是否繼續? [ Y/N ]:"); }
}
}
void inkey( ) //按鍵輸入
{ int key;
while (1)
{ key = getch( );
if (over) { if (key == 89|| key == 121) {rsgm( ); continue; }
else if (key == 78|| key == 110) { return; }
else continue; }
ifnum = 0;
if(key==224)key=getch( );
switch (key)
{ case 75: left( ); break;
case 77: right( ); break;
case 72: up( ); break;
case 80: down( );break;
}
if (score > best) { best = score; }
if (ifnum) { adnum( ); show( ); }
}
}
int jsk( ) //計算空格數
{ int n = 0;
for (i = 0; i < 4; i++)
{ for (j = 0; j < 4; j++) { if ( a[i][j] == 0) {n++;} } }
return n;
}
void left( ) //向左移動
{ for (i = 0; i < 4; i++)
{for (j = 1, k = 0; j < 4; j++)
{ if (a[i][j] > 0)
{ if ( a[i][k] == a[i][j])
{ a[i][k] *= 2; k++;
score = score + 2 * a[i][j];
a[i][j] = 0; ifnum = 1; }
else if ( a[i][k] == 0) { a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; }
else { a[i][k + 1] = a[i][j]; if ((k + 1) != j) { a[i][j] = 0; ifnum = 1; }
k++; }
}
}
}
}
void right( ) //向右移動
{for (i = 0; i < 4; i++)
{for (j = 2, k = 3; j >= 0; j--)
{if (a[i][j] > 0)
{ if (a[i][k] == a[i][j])
{a[i][k] *= 2; k--; score = score + 2 * a[i][j]; a[i][j] = 0; ifnum = 1; }
else if ( a[i][k] == 0) {a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; }
else { a[i][k - 1] = a[i][j]; if ((k - 1) != j) { a[i][j] = 0; ifnum = 1; } k--; }
}
}
}
}
void up( ) //向上移動
{for (i = 0; i < 4; i++)
{for (j = 1, k = 0; j < 4; j++)
{if (a[j][i] > 0)
{if ( a[k][i] == a[j][i]) { a[k][i] *= 2; k++;score = score + 2 * a[j][i];
a[j][i] = 0; ifnum = 1; }
else if ( a[k][i] == 0) { a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; }
else { a[k + 1][i] = a[j][i]; if ((k + 1) != j) { a[j][i] = 0; ifnum = 1; }
k++; }
}
}
}
}
void down( ) //向下移動
{ for (i = 0; i < 4; i++)
{for (j = 2, k = 3; j >= 0; j--)
{if (a[j][i] > 0)
{if (a[k][i] == a[j][i])
{a[k][i] *= 2; k--;score = score + 2 * a[j][i]; a[j][i] = 0; ifnum = 1; }
else if (a[k][i] == 0) {a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; }
else {a[k - 1][i] = a[j][i];
if ((k - 1) != j) {a[j][i] = 0; ifnum = 1; } k--; }
}
}
}
}
void adnum( ) //添加隨機數
{ srand(time(0)); int n = rand( ) % jsk( );
for (int i = 0; i < 4; i++)
{for (int j = 0; j < 4; j++)
{ if (a[i][j] == 0) {if (n != 0) { n--; }
else {int k = rand() % 3;
if (k == 0 || k == 1) {a[i][j] = 2; return; }
else {a[i][j] = 4; return; } }
}
}
}
}
void yes( ) //游戲是否結束
{ for (int i = 0; i < 4; i++)
{for (int j = 0; j < 3; j++)
{if (a[i][j] == a[i][j + 1] || a[j][i] == a[j + 1][i]) {over = 0; return; }}
}
over = 1;
}
void gtxy(int x, int y) //控制游標位置的函數
{ COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}