本文目錄一覽:
- 1、求幾C語言個小遊戲代碼,簡單的,要注釋、、謝謝了、
- 2、用C++編寫的小遊戲源代碼
- 3、用C語言編寫的小遊戲代碼是什麼?
- 4、怎樣用C語言編寫一個小遊戲?
- 5、c語言小遊戲代碼
- 6、c語言猜數字遊戲源代碼
求幾C語言個小遊戲代碼,簡單的,要注釋、、謝謝了、
// Calcu24.cpp : Defines the entry point for the console application.
//
/*
6-6
24點遊戲
*/
#include “conio.h”
#include “stdlib.h”
#include “time.h”
#include “math.h”
#include “string.h”/*
從一副撲克牌中,任取4張。
2-10 按其點數計算(為了表示方便10用T表示),J,Q,K,A 統一按 1 計算
要求通過加減乘除四則運算得到數字 24。
本程序可以隨機抽取紙牌,並用試探法求解。
*/void GivePuzzle(char* buf)
{
char card[] = {‘A’,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’T’,’J’,’Q’,’K’}; for(int i=0; i4; i++){
buf[i] = card[rand() % 13];
}
}
void shuffle(char * buf)
{
for(int i=0; i5; i++){
int k = rand() % 4;
char t = buf[k];
buf[k] = buf[0];
buf[0] = t;
}
}
int GetCardValue(int c)
{
if(c==’T’) return 10;
if(c=’0′ c=’9′) return c – ‘0’;
return 1;
}
char GetOper(int n)
{
switch(n)
{
case 0:
return ‘+’;
case 1:
return ‘-‘;
case 2:
return ‘*’;
case 3:
return ‘/’;
} return ‘ ‘;
}double MyCalcu(double op1, double op2, int oper)
{
switch(oper)
{
case 0:
return op1 + op2;
case 1:
return op1 – op2;
case 2:
return op1 * op2;
case 3:
if(fabs(op2)0.0001)
return op1 / op2;
else
return 100000;
} return 0;
}
void MakeAnswer(char* answer, int type, char* question, int* oper)
{
char p[4][3];
for(int i=0; i4; i++)
{
if( question[i] == ‘T’ )
strcpy(p[i], “10”);
else
sprintf(p[i], “%c”, question[i]);
}
switch(type)
{
case 0:
sprintf(answer, “%s %c (%s %c (%s %c %s))”,
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 1:
sprintf(answer, “%s %c ((%s %c %s) %c %s)”,
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 2:
sprintf(answer, “(%s %c %s) %c (%s %c %s)”,
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 3:
sprintf(answer, “((%s %c %s) %c %s) %c %s”,
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 4:
sprintf(answer, “(%s %c (%s %c %s)) %c %s”,
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
}
}
bool TestResolve(char* question, int* oper, char* answer)
{
// 等待考生完成
int type[5]={0,1,2,3,4};//計算類型
double p[4];
double sum=0;
//
for(int i=0; i4; i++) //循環取得點數
{
p[i]=GetCardValue(int(question[i]));
} for(i=0;i5;i++)
{
MakeAnswer(answer,type[i],question,oper); //獲取可能的答案
switch(type[i])
{
case 0:
sum=MyCalcu(p[0],MyCalcu( p[1],MyCalcu(p[2], p[3], oper[2]),oper[1]),oper[0]); //A*(B*(c*D))
break;
case 1:
sum=MyCalcu(p[0],MyCalcu(MyCalcu(p[1], p[2], oper[1]),p[3],oper[2]),oper[0]); //A*((B*C)*D)
break;
case 2:
sum=MyCalcu(MyCalcu(p[0], p[1], oper[0]),MyCalcu(p[2], p[3], oper[2]),oper[1]); // (A*B)*(C*D)
break;
case 3:
sum=MyCalcu(MyCalcu(MyCalcu(p[0], p[1], oper[0]),p[2],oper[1]),p[3],oper[2]); //((A*B)*C)*D
break;
case 4:
sum=MyCalcu(MyCalcu(p[0],MyCalcu(p[1], p[2], oper[1]),oper[0]),p[3],oper[2]); //(A*(B*C))*D
break;
}
if(sum==24) return true;
}
return false;
}
/*
採用隨機試探法:就是通過隨機數字產生 加減乘除的 組合,通過大量的測試來命中的解法
提示:
1. 需要考慮用括號控制計算次序的問題 比如:( 10 – 4 ) * ( 3 + A ), 實際上計算次序的數目是有限的:
A*(B*(c*D))
A*((B*C)*D)
(A*B)*(C*D)
((A*B)*C)*D
(A*(B*C))*D
2. 需要考慮計算結果為分數的情況:( 3 + (3 / 7) ) * 7
3. 題目中牌的位置可以任意交換
*/
bool TryResolve(char* question, char* answer)
{
int oper[3]; // 存儲運算符,0:加法 1:減法 2:乘法 3:除法
for(int i=0; i1000 * 1000; i++)
{
// 打亂紙牌順序
shuffle(question);
// 隨機產生運算符
for(int j=0; j3; j++)
oper[j] = rand() % 4; if( TestResolve(question, oper, answer) ) return true;
} return false;
}
int main(int argc, char* argv[])
{
// 初始化隨機種子
srand( (unsigned)time( NULL ) ); char buf1[4]; // 題目
char buf2[30]; // 解答
printf(“***************************\n”);
printf(“計算24\n”);
printf(“A J Q K 均按1計算,其它按牌點計算\n”);
printf(“目標是:通過四則運算組合出結果:24\n”);
printf(“***************************\n\n”);
for(;;)
{
GivePuzzle(buf1); // 出題
printf(“題目:”);
for(int j=0; j4; j++){
if( buf1[j] == ‘T’ )
printf(“10 “);
else
printf(“%c “, buf1[j]);
} printf(“\n按任意鍵參考答案…\n”);
getch(); if( TryResolve(buf1, buf2) ) // 解題
printf(“參考:%s\n”, buf2);
else
printf(“可能是無解…\n”); printf(“按任意鍵出下一題目,x 鍵退出…\n”);
if( getch() == ‘x’ ) break;
} return 0;
}
用C++編寫的小遊戲源代碼
五子棋的代碼:
#includeiostream
#includestdio.h
#includestdlib.h
#include time.h
using namespace std;
const int N=15; //15*15的棋盤
const char ChessBoardflag = ‘ ‘; //棋盤標誌
const char flag1=’o’; //玩家1或電腦的棋子標誌
const char flag2=’X’; //玩家2的棋子標誌
typedef struct Coordinate //坐標類
{
int x; //代表行
int y; //代表列
}Coordinate;
class GoBang //五子棋類
{
public:
GoBang() //初始化
{
InitChessBoard();
}
void Play() //下棋
{
Coordinate Pos1; // 玩家1或電腦
Coordinate Pos2; //玩家2
int n = 0;
while (1)
{
int mode = ChoiceMode();
while (1)
{
if (mode == 1) //電腦vs玩家
{
ComputerChess(Pos1,flag1); // 電腦下棋
if (GetVictory(Pos1, 0, flag1) == 1) //0表示電腦,真表示獲勝
break;
PlayChess(Pos2, 2, flag2); //玩家2下棋
if (GetVictory(Pos2, 2, flag2)) //2表示玩家2
break;
}
else //玩家1vs玩家2
{
PlayChess(Pos1, 1, flag1); // 玩家1下棋
if (GetVictory(Pos1, 1, flag1)) //1表示玩家1
break;
PlayChess(Pos2, 2, flag2); //玩家2下棋
if (GetVictory(Pos2, 2, flag2)) //2表示玩家2
break;
}
}
cout “***再來一局***” endl;
cout “y or n :”;
char c = ‘y’;
cin c;
if (c == ‘n’)
break;
}
}
protected:
int ChoiceMode() //選擇模式
{
int i = 0;
system(“cls”); //系統調用,清屏
InitChessBoard(); //重新初始化棋盤
cout “***0、退出 1、電腦vs玩家 2、玩家vs玩家***” endl;
while (1)
{
cout “請選擇:”;
cin i;
if (i == 0) //選擇0退出
exit(1);
if (i == 1 || i == 2)
return i;
cout “輸入不合法” endl;
}
}
void InitChessBoard() //初始化棋盤
{
for (int i = 0; i N + 1; ++i)
{
for (int j = 0; j N + 1; ++j)
{
_ChessBoard[i][j] = ChessBoardflag;
}
}
}
void PrintChessBoard() //打印棋盤,這個函數可以自己調整
{
system(“cls”); //系統調用,清空屏幕
for (int i = 0; i N+1; ++i)
{
for (int j = 0; j N+1; ++j)
{
if (i == 0) //打印列數字
{
if (j!=0)
printf(“%d “, j);
else
printf(” “);
}
else if (j == 0) //打印行數字
printf(“%2d “, i);
else
{
if (i N+1)
{
printf(“%c |”,_ChessBoard[i][j]);
}
}
}
cout endl;
cout ” “;
for (int m = 0; m N; m++)
{
printf(“–|”);
}
cout endl;
}
}
void PlayChess(Coordinate pos, int player, int flag) //玩家下棋
{
PrintChessBoard(); //打印棋盤
while (1)
{
printf(“玩家%d輸入坐標:”, player);
cin pos.x pos.y;
if (JudgeValue(pos) == 1) //坐標合法
break;
cout “坐標不合法,重新輸入” endl;
}
_ChessBoard[pos.x][pos.y] = flag;
}
void ComputerChess(Coordinate pos, char flag) //電腦下棋
{
PrintChessBoard(); //打印棋盤
int x = 0;
int y = 0;
while (1)
{
x = (rand() % N) + 1; //產生1~N的隨機數
srand((unsigned int) time(NULL));
y = (rand() % N) + 1; //產生1~N的隨機數
srand((unsigned int) time(NULL));
if (_ChessBoard[x][y] == ChessBoardflag) //如果這個位置是空的,也就是沒有棋子
break;
}
pos.x = x;
pos.y = y;
_ChessBoard[pos.x][pos.y] = flag;
}
int JudgeValue(const Coordinate pos) //判斷輸入坐標是不是合法
{
if (pos.x 0 pos.x = Npos.y 0 pos.y = N)
{
if (_ChessBoard[pos.x][pos.y] == ChessBoardflag)
{
return 1; //合法
}
}
return 0; //非法
}
int JudgeVictory(Coordinate pos, char flag) //判斷有沒有人勝負(底層判斷)
{
int begin = 0;
int end = 0;
int begin1 = 0;
int end1 = 0;
//判斷行是否滿足條件
(pos.y – 4) 0 ? begin = (pos.y – 4) : begin = 1;
(pos.y + 4) N ? end = N : end = (pos.y + 4);
for (int i = pos.x, j = begin; j + 4 = end; j++)
{
if (_ChessBoard[i][j] == flag_ChessBoard[i][j + 1] == flag
_ChessBoard[i][j + 2] == flag_ChessBoard[i][j + 3] == flag
_ChessBoard[i][j + 4] == flag)
return 1;
}
//判斷列是否滿足條件
(pos.x – 4) 0 ? begin = (pos.x – 4) : begin = 1;
(pos.x + 4) N ? end = N : end = (pos.x + 4);
for (int j = pos.y, i = begin; i + 4 = end; i++)
{
if (_ChessBoard[i][j] == flag_ChessBoard[i + 1][j] == flag
_ChessBoard[i + 2][j] == flag_ChessBoard[i + 3][j] == flag
_ChessBoard[i + 4][j] == flag)
return 1;
}
int len = 0;
//判斷主對角線是否滿足條件
pos.x pos.y ? len = pos.y – 1 : len = pos.x – 1;
if (len 4)
len = 4;
begin = pos.x – len; //橫坐標的起始位置
begin1 = pos.y – len; //縱坐標的起始位置
pos.x pos.y ? len = (N – pos.x) : len = (N – pos.y);
if (len4)
len = 4;
end = pos.x + len; //橫坐標的結束位置
end1 = pos.y + len; //縱坐標的結束位置
for (int i = begin, j = begin1; (i + 4 = end) (j + 4 = end1); ++i, ++j)
{
if (_ChessBoard[i][j] == flag_ChessBoard[i + 1][j + 1] == flag
_ChessBoard[i + 2][j + 2] == flag_ChessBoard[i + 3][j + 3] == flag
_ChessBoard[i + 4][j + 4] == flag)
return 1;
}
//判斷副對角線是否滿足條件
(pos.x – 1) (N – pos.y) ? len = (N – pos.y) : len = pos.x – 1;
if (len 4)
len = 4;
begin = pos.x – len; //橫坐標的起始位置
begin1 = pos.y + len; //縱坐標的起始位置
(N – pos.x) (pos.y – 1) ? len = (pos.y – 1) : len = (N – pos.x);
if (len4)
len = 4;
end = pos.x + len; //橫坐標的結束位置
end1 = pos.y – len; //縱坐標的結束位置
for (int i = begin, j = begin1; (i + 4 = end) (j – 4 = end1); ++i, –j)
{
if (_ChessBoard[i][j] == flag_ChessBoard[i + 1][j – 1] == flag
_ChessBoard[i + 2][j – 2] == flag_ChessBoard[i + 3][j – 3] == flag
_ChessBoard[i + 4][j – 4] == flag)
return 1;
}
for (int i = 1; i N + 1; ++i) //棋盤有沒有下滿
{
for (int j =1; j N + 1; ++j)
{
if (_ChessBoard[i][j] == ChessBoardflag)
return 0; //0表示棋盤沒滿
}
}
return -1; //和棋
}
bool GetVictory(Coordinate pos, int player, int flag) //對JudgeVictory的一層封裝,得到具體那個玩家獲勝
{
int n = JudgeVictory(pos, flag); //判斷有沒有人獲勝
if (n != 0) //有人獲勝,0表示沒有人獲勝
{
PrintChessBoard();
if (n == 1) //有玩家贏棋
{
if (player == 0) //0表示電腦獲勝,1表示玩家1,2表示玩家2
printf(“***電腦獲勝***\n”);
else
printf(“***恭喜玩家%d獲勝***\n”, player);
}
else
printf(“***雙方和棋***\n”);
return true; //已經有人獲勝
}
return false; //沒有人獲勝
}
private:
char _ChessBoard[N+1][N+1];
};
擴展資料:
設計思路
1、進行問題分析與設計,計劃實現的功能為,開局選擇人機或雙人對戰,確定之後比賽開始。
2、比賽結束後初始化棋盤,詢問是否繼續比賽或退出,後續可加入復盤、悔棋等功能。
3、整個過程中,涉及到了棋子和棋盤兩種對象,同時要加上人機對弈時的AI對象,即涉及到三個對象。
用C語言編寫的小遊戲代碼是什麼?
/*也不知道你是什麼級別的,我是一個新手,剛接觸編程語言,以下是我自己變得一個小程序,在所有c語言的編譯器(vc++6.0、turbo????)上都能運行,你還可以進一步改進。這是一個類似貪吃蛇的小遊戲。祝你好運*/\x0d\x0a/*貪吃蛇*/\x0d\x0a#include\x0d\x0a#include\x0d\x0a#include\x0d\x0a#include\x0d\x0aint head=3 ,tail=0;\x0d\x0aint main()\x0d\x0a{\x0d\x0aint i,j,k=0;\x0d\x0aint zuobiao[2][80];\x0d\x0along start;\x0d\x0aint direction=77;\x0d\x0aint gamespeed;\x0d\x0aint timeover;\x0d\x0aint change(char qipan[20][80],int zuobiao[2][80],char direction);\x0d\x0azuobiao[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;\x0d\x0a/*處理棋盤*/\x0d\x0achar qipan[20][80];//定義棋盤\x0d\x0afor(i=0;i for(j=0;jqipan[i][j]=’ ‘;//初始化棋盤\x0d\x0afor(i=0;iqipan[0][i]=’_’;\x0d\x0afor(i=0;iqipan[i][0]=’|’;\x0d\x0afor(i=0;iqipan[i][79]=’|’;\x0d\x0afor(i=0;iqipan[19][i]=’_’;\x0d\x0aqipan[1][1]=qipan[1][2]=qipan[1][3]=’*’;//初始化蛇的位置\x0d\x0aqipan[1][4]=’#’;\x0d\x0aprintf(“This is a game of a SNAKE.\nGOOD LUCK TO YOU !\n”);\x0d\x0aprintf(“Input your game speed,please.(e.g.300)\n”);\x0d\x0ascanf(“%d”,gamespeed);\x0d\x0a\x0d\x0awhile(direction!=’q’)\x0d\x0a{\x0d\x0asystem(“cls”);\x0d\x0afor(i=0;ifor(j=0;jprintf(“%c”,qipan[i][j]);\x0d\x0atimeover=1;\x0d\x0astart=clock();\x0d\x0awhile(!kbhit()(timeover=clock()-startif(timeover)\x0d\x0a{\x0d\x0agetch();\x0d\x0adirection=getch();\x0d\x0a}\x0d\x0aelse\x0d\x0adirection=direction;\x0d\x0aif(!(direction==72||direction==80||direction==75||direction==77))\x0d\x0a{\x0d\x0areturn 0;\x0d\x0asystem(“cls”);\x0d\x0aprintf(“GAME OVER!\n”);\x0d\x0a}\x0d\x0aif(!change(qipan,zuobiao,direction))\x0d\x0a{\x0d\x0adirection=’q’;\x0d\x0asystem(“cls”);\x0d\x0aprintf(“GAME OVER!\n”);\x0d\x0a}\x0d\x0a}\x0d\x0areturn 0;\x0d\x0a}\x0d\x0aint change(char qipan[20][80],int zuobiao[2][80],char direction)\x0d\x0a{\x0d\x0aint x,y;\x0d\x0aif(direction==72)\x0d\x0ax=zuobiao[0][head]-1;y=zuobiao[1][head];\x0d\x0aif(direction==80)\x0d\x0ax=zuobiao[0][head]+1;y=zuobiao[1][head];\x0d\x0aif(direction==75)\x0d\x0ax=zuobiao[0][head];y=zuobiao[0][head]-1;\x0d\x0aif(direction==77)\x0d\x0ax=zuobiao[0][head];y=zuobiao[1][head]+1;\x0d\x0aif(x==0||x==18||y==78||y==0)\x0d\x0areturn 0;\x0d\x0aif(qipan[x][y]!=’ ‘)\x0d\x0areturn 0;\x0d\x0aqipan[zuobiao[0][tail]][zuobiao[1][tail]]=’ ‘;\x0d\x0atail=(tail+1)%80;\x0d\x0aqipan[zuobiao[0][head]][zuobiao[1][head]]=’*’;\x0d\x0ahead=(head+1)%80;\x0d\x0azuobiao[0][head]=x;\x0d\x0azuobiao[1][head]=y;\x0d\x0aqipan[zuobiao[0][head]][zuobiao[1][head]]=’#’;\x0d\x0areturn 1;\x0d\x0a}
怎樣用C語言編寫一個小遊戲?
「貪吃蛇」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; //方向變量,初值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;iH;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;isnk.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==72dir!=2)dir=1; //72表示按下了向上方向鍵
if(key==80dir!=1)dir=2; //80為向下
if(key==75dir!=4)dir=3; //75為向左
if(key==77dir!=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(“\007”); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //007是響鈴
else Flag = 0; //沒吃到食物Flag的值為0
if(snk.speed150) 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語言小遊戲代碼
最基礎的貪吃蛇的代碼
#includestdio.h
#includewindows.h//基本型態定義。支援型態定義函數。使用者界面函數 圖形裝置界面函數。
#includeconio.h //用戶通過按鍵盤產生的對應操作 (控制台)
#includestdlib.h
#includetime.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]!=1Snake[Sna_Hea_X][Sna_Hea_Y]!=0Snake[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語言猜數字遊戲源代碼
小遊戲2048:
#includestdio.h
#includestdlib.h
#includeconio.h
#includetime.h
#includewindows.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 Color(int a) //設定字符顏色的函數(a應為1-15)
{ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a); }
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 gb={1,0}; //以下兩行是隱藏光標的設置,gb代指光標
SetConsoleCursorInfo( GetStdHandle(STD_OUTPUT_HANDLE), gb );
Color(14); //設置字體淡黃色
printf(“\n\n\t\t 2048小遊戲”); Color(7); //恢復白字黑底
printf(“\n\t┌──────┬──────┬──────┬──────┐”);
printf(“\n\t│ │ │ │ │”);
printf(“\n\t├──────┼──────┼──────┼──────┤”);
printf(“\n\t│ │ │ │ │”);
printf(“\n\t├──────┼──────┼──────┼──────┤”);
printf(“\n\t│ │ │ │ │”);
printf(“\n\t├──────┼──────┼──────┼──────┤”);
printf(“\n\t│ │ │ │ │”);
printf(“\n\t└──────┴──────┴──────┴──────┘”);
show( );
}
void show( ) //輸出界面
{ for(i=0;i4;i++)
for(j=0;j4;j++)
{ gtxy(7*j+9,2*i+4); //gtxy(7*j+9, 2*i+4)是光標到指定位置輸出數字
if(a[i][j]==0){printf(” “); Color(7); printf(“│”);}
else if(a[i][j]10){ if (a[i][j] == 2) { Color(14); }
else if (a[i][j] == 4) { Color(13); }
else if (a[i][j] == 8) { Color(12); }
printf(” %d “, a[i][j]); Color(7 ); printf(“│”);
}
else if (a[i][j] 100){if (a[i][j] == 16) { Color(12); }
else if (a[i][j] == 32) { Color(10); }
else if (a[i][j] == 64) { Color(2 ); }
printf(” %d “, a[i][j]); Color(7); printf(“│”);
}
else if (a[i][j] 1000) {if (a[i][j] == 128) { Color(9); }
else if (a[i][j] == 256) { Color(1); }
else if (a[i][j] == 512) { Color(13); }
printf(” %d “, a[i][j]); Color(7); printf(“│”);
}
else if (a[i][j] 10000) {if (a[i][j] == 1024) { Color(5); }
else { Color(15); }
printf(” %d “, a[i][j]); Color(7); printf(“│”);
}
}
if (jsk( ) == 0)
{ yes( ); if (over) { gtxy(9,12); Color(10);
printf(“\n\n 遊戲結束!是否繼續? [ 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 zb; //zb代指坐標
zb.X = x;
zb.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), zb);
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/300544.html