本文目錄一覽:
- 1、C語言程序小遊戲—-一條龍遊戲設計
- 2、用C語言設計小遊戲的程序??急!!!
- 3、一種C語言小遊戲程序設計(程序已經附上)
- 4、如何使用C語言編寫簡單小遊戲?
- 5、教你如何使用C語言編寫簡單小遊戲
C語言程序小遊戲—-一條龍遊戲設計
#include iostream
#include vector
#include set
#include algorithm
using namespace std;
typedef __int64 BigInteger;
typedef unsigned int Index;
typedef double Number;
class CombinationIterator
{
vectorIndex mCurrent;
Index mN;
Index mM;
static BigInteger factorial( Index n )
{
BigInteger fact = 1;
for( Index i = 2; i = n; ++i )
fact *= i;
return fact;
}
public:
CombinationIterator(): mN( 0 ), mM( 0 )
{}
CombinationIterator( Index n, Index m )
: mN( n ), mM( m ),
mCurrent( (vectorIndex::size_type) m )
{
if( m n || n 1 || m 1 )
throw 1;
for( Index i = 0; i m; ++i )
mCurrent[ i ] = i;
}
void operator ++ ()
{
if( mM == 0 )
throw 1;
if( mCurrent[ 0 ] == mN – mM )
{
mN = mM = 0;
return;
}
Index i = mM – 1;
while( mCurrent[ i ] == mN – mM + i )
–i;
mCurrent[ i ] = mCurrent[ i ] + 1;
for( Index j = i + 1; j mM; ++j )
mCurrent[ j ] = mCurrent[ i ] + j – i;
}
const vectorIndex operator* () const
{
return mCurrent;
}
bool operator == ( const CombinationIterator that ) const
{
if( mM == that.mM mM == 0 )
return true;// both end
return mM == that.mM mN == that.mN mCurrent == that.mCurrent;
}
bool operator != ( const CombinationIterator that ) const
{
return !( *this == that );
}
};
templatetypename T
class VectorCombinationIterator
{
vectorT mVector;
Index mCurrentM;
CombinationIterator mIter;
public:
VectorCombinationIterator()
{}
VectorCombinationIterator(const vectorT v)
: mVector( v ), mCurrentM( 1 ), mIter( (Index) v.size(), mCurrentM )
{}
void operator ++ ()
{
++mIter;
if( mIter == CombinationIterator() mCurrentM mVector.size() )
{
++mCurrentM;
mIter = CombinationIterator( (Index) mVector.size(), mCurrentM );
}
}
pair vectorT, vectorT operator* () const
{
const vectorIndex current = *mIter;
pair vectorT, vectorT result;
for( vectorT::size_type i = 0; i mVector.size(); ++i )
if( find( current.begin(), current.end(), i ) != current.end() )
result.first.push_back( mVector[i] );
else
result.second.push_back( mVector[i] );
return result;
}
bool operator == ( const VectorCombinationIterator that ) const
{
if( mIter == that.mIter mIter == CombinationIterator() )
return true;// both end
return mCurrentM == that.mCurrentM mVector == that.mVector mIter == that.mIter;
}
bool operator != ( const VectorCombinationIterator that ) const
{
return !( *this == that );
}
};
static char gOperatorChars[]={‘+’,’-‘,’*’,’/’};
struct ExpressionItem
{
Number mValue;
char mOperator;
ExpressionItem* mLeftChild;
ExpressionItem* mRightChild;
ExpressionItem(){}
ExpressionItem( Number v, char oper, ExpressionItem* left, ExpressionItem* right )
: mValue( v ), mOperator( oper ), mLeftChild( left ), mRightChild( right )
{}
static vectorExpressionItem mPool;
static ExpressionItem* alloc( Number v, char oper, ExpressionItem* left, ExpressionItem* right )
{
mPool.push_back( ExpressionItem( v, oper, left, right ) );
return *mPool.rbegin();
}
};
vectorExpressionItem ExpressionItem::mPool( 1024*1024 );
vectorExpressionItem* getPossibleResult( const vectorExpressionItem* left, const vectorExpressionItem* right )
{
vectorExpressionItem* result;
for( vectorExpressionItem*::const_iterator liter = left.begin(); liter != left.end() ; ++liter )
for( vectorExpressionItem*::const_iterator riter = right.begin(); riter != right.end() ; ++riter )
{
result.push_back( ExpressionItem::alloc( (*liter)-mValue + (*riter)-mValue, ‘+’, *liter, *riter ) );
result.push_back( ExpressionItem::alloc( (*liter)-mValue – (*riter)-mValue, ‘-‘, *liter, *riter ) );
result.push_back( ExpressionItem::alloc( (*liter)-mValue * (*riter)-mValue, ‘*’, *liter, *riter ) );
if( (*riter)-mValue != 0.0 )
result.push_back( ExpressionItem::alloc( (*liter)-mValue / (*riter)-mValue, ‘/’, *liter, *riter ) );
}
return result;
}
vectorExpressionItem* getPossibleResult( const vectorExpressionItem* expItems )
{
if( expItems.size() == 1 )
return expItems;
vectorExpressionItem* result;
VectorCombinationIteratorExpressionItem* iter( expItems );
while( iter != VectorCombinationIteratorExpressionItem*() )
{
pair vectorExpressionItem*, vectorExpressionItem* p = *iter;
if( p.first.size() != 0 p.second.size() != 0 )
{
vectorExpressionItem* items = getPossibleResult( getPossibleResult( p.first ),
getPossibleResult( p.second ) );
result.insert( result.end(), items.begin(), items.end() );
}
++iter;
}
return result;
}
vectorExpressionItem* getPossibleResult( const vectorNumber numbers )
{
vectorExpressionItem* expItems;
for( vectorNumber::const_iterator iter = numbers.begin(); iter != numbers.end(); ++iter )
expItems.push_back( ExpressionItem::alloc( *iter, 0, NULL, NULL ) );
return getPossibleResult( expItems );
}
void printExpression( ExpressionItem* item, char oper, bool isLeftChild )
{
if( item-mLeftChild == NULL )
{
coutitem-mValue;
}
else if( ( item-mOperator == ‘+’ || item-mOperator == ‘-‘ ) ( oper == ‘*’ || oper == ‘/’ ) )
{
cout”( “;
printExpression( item-mLeftChild, item-mOperator, true );
cout” “;
coutitem-mOperator;
cout” “;
printExpression( item-mRightChild, item-mOperator, false );
cout” )”;
}
else if( !isLeftChild
( ( ( item-mOperator == ‘+’ || item-mOperator == ‘-‘ ) oper == ‘-‘ ) ||
( ( item-mOperator == ‘*’ || item-mOperator == ‘/’ ) oper == ‘/’ ) ) )
{
cout”( “;
printExpression( item-mLeftChild, item-mOperator, true );
cout” “;
coutitem-mOperator;
cout” “;
printExpression( item-mRightChild, item-mOperator, false );
cout” )”;
}
else
{
printExpression( item-mLeftChild, item-mOperator, true );
cout” “;
coutitem-mOperator;
cout” “;
printExpression( item-mRightChild, item-mOperator, false );
}
}
void printExpression( ExpressionItem* item )
{
if( item-mLeftChild != NULL )
{
printExpression( item-mLeftChild, item-mOperator, true );
cout” “;
coutitem-mOperator;
cout” “;
printExpression( item-mRightChild, item-mOperator, false );
}
else
coutitem-mValue;
}
int main()
{
for( int i = 1; i = 10; ++i )
for( int j = 1; j = 10; ++j )
for( int k = 1; k = 10; ++k )
for( int l = 1; l = 10; ++l )
{
if( i j || j k || k l )
continue;
ExpressionItem::mPool.clear();
ExpressionItem::mPool.reserve( 1024*1024 );
vectorNumber vn;
vn.push_back( i );
vn.push_back( j );
vn.push_back( k );
vn.push_back( l );
vectorExpressionItem* exps = getPossibleResult( vn );
for( int m = 0; m exps.size(); ++m )
if( exps[ m ]-mValue = 23.999 exps[ m ]-mValue = 24.001 )
{
couti’,’j’,’k’,’l” \t—\t”;
printExpression( exps[ m ] );
cout” = “exps[ m ]-mValueendl;
break;
}
// if( m == exps.size() )
// couti’,’j’,’k’,’l” have no possible result”endl;
}
return 0;
}
用C語言設計小遊戲的程序??急!!!
用c++實現的”貪吃蛇”遊戲源碼
// greedsnake.cpp
#include bios.h
#include conio.h
#include dos.h
#include graphics.h
#include stdlib.h
#include time.h
#include “conf.h”
typedef struct node
{
int x,y;
struct node *next;
}Node;
typedef struct
{
Node *head,*tail;
int length;
}Snake;
typedef struct
{
int left,top,right,bottom;
}Frame;
typedef enum //四個方向
{
up,down,left,right
}Direction;
typedef enum
{
false,true
}bool;//*/
void InitGraphMode(); //初始化圖形驅動
void CloseGraphMode();
void Foot(int,int);
void Head(int,int);
void CreateFrame(); //完成整個遊戲框架的繪製
void CreateSnake(); //創建一條兩個節點的蛇,蛇的每一節是隊列中的一個節點
bool PlayGame(); //遊戲的主體函數,
int Hit(int,int); //判斷是否越界,或者撞到自身,兩個參數分別是新的頭接點的x,y坐標
bool GameOver(); //繪製遊戲結束時彈出的對話框
void Enqueue(Node); //入隊函數
Node Dequeue(); //出隊函數
void ClearKeyBuf(); //清除鍵盤緩衝,此函數可以消除不停的按無效鍵的影響
Snake snake;
Frame frame;
void main()
{
InitGraphMode();
do
{
CreateFrame();
}while(PlayGame());
CloseGraphMode();
}
void InitGraphMode()
{
int gdriver=DETECT,gmode;
initgraph(gdriver,gmode,”../bgi/”);
cleardevice();
}
void CloseGraphMode()
{
cleardevice();
closegraph();
}
void CreateFrame()
{
setbkcolor(CYAN);
//下面的四行代碼用於計算主框架的左上角和右下角的坐標
frame.left=(getmaxx()+1-BlockWidth*RowOfFrame)/2;
frame.top=(getmaxy()+1-BlockHeight*ColumnOfFrame)/2;
frame.right=frame.left+BlockWidth*RowOfFrame;
frame.bottom=frame.top+BlockHeight*ColumnOfFrame;
Head(frame.left+100,frame.top-20);
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(frame.left,frame.top,frame.right,frame.bottom);
setlinestyle(SOLID_LINE,1,1);
setcolor(DARKGRAY);
line(frame.left,frame.top,frame.right,frame.top);
line(frame.left,frame.top,frame.left,frame.bottom);
setlinestyle(SOLID_LINE,1,1);
setcolor(WHITE);
line(frame.left,frame.bottom,frame.right,frame.bottom);
line(frame.right,frame.top,frame.right,frame.bottom);
setlinestyle(DOTTED_LINE,1,1);
setcolor(BLUE);
for(int row=1;rowRowOfFrame;row++)
line(frame.left+row*BlockWidth,frame.top,frame.left+row*BlockWidth,frame.bottom);
for(int column=1;columnColumnOfFrame;column++)
line(frame.left,frame.top+column*BlockHeight,frame.right,frame.top+column*BlockHeight);
Foot(frame.left,frame.bottom+20);
}
void CreateSnake()
{
Node *node1=new Node;
Node *node2=new Node;
node1-x=frame.left+BlockWidth;
node1-y=frame.top;
node1-next=NULL;
snake.tail=node1;
node2-x=frame.left;
node2-y=frame.top;
node2-next=snake.tail;
snake.head=node2;
snake.length=2;
setfillstyle(SOLID_FILL,BLUE);
bar(snake.head-x+1,snake.head-y+1,snake.head-x+BlockWidth-1,snake.head-y+BlockHeight-1);
bar(snake.tail-x+1,snake.tail-y+1,snake.tail-x+BlockWidth-1,snake.tail-y+BlockHeight-1);
}
bool PlayGame()
{
int speed=300,key;
Direction CurrentDirection=right;
Node randomNode;
Node newNode,outNode;
bool neednode=true;
bool overlap=false;
int randx,randy;
CreateSnake();
while(true)
{
if(neednode==true)
{
randomize();
do
{
randx=frame.left+rand()%RowOfFrame*BlockWidth;
randy=frame.top+rand()%ColumnOfFrame*BlockHeight;
for(Node *p=snake.head;p!=NULL;p=p-next)//hit itself
if(randx==p-xrandy==p-y)
{overlap=true;break;}
}
while(overlap==true);
randomNode.x=randx;
randomNode.y=randy;
randomNode.next=NULL;
setfillstyle(SOLID_FILL,RED);
bar(randomNode.x+1,randomNode.y+1,randomNode.x+BlockWidth-1,randomNode.y+BlockHeight-1);
neednode=false;
}
if((key=bioskey(1))!=0)
{
switch(key)
{
case ESC: return false;
case UP:
if(CurrentDirection!=down)
CurrentDirection=up;
ClearKeyBuf();
break;
case DOWN:
if(CurrentDirection!=up)
CurrentDirection=down;
ClearKeyBuf();
break;
case LEFT:
if(CurrentDirection!=right)
CurrentDirection=left;
ClearKeyBuf();
break;
case RIGHT:
if(CurrentDirection!=left)
CurrentDirection=right;
ClearKeyBuf();
break;
case PAGEUP:speed=speed-100;
if(speed100)
speed=100;
ClearKeyBuf();
break;
case PAGEDOWN:speed=speed+100;
if(speed500)
speed=500;
ClearKeyBuf();
break;
default :break;
}
}
int headx=snake.tail-x;
int heady=snake.tail-y;
switch(CurrentDirection)
{
case up: heady-=BlockHeight;break;
case down: heady+=BlockHeight;break;
case left: headx-=BlockWidth;break;
case right: headx+=BlockWidth;break;
}
if(Hit(headx,heady)) //whether the snake hit the wall or itself
return GameOver();
else
{ //eat
if(headx==randomNode.xheady==randomNode.y)
{
Enqueue(randomNode);
setfillstyle(SOLID_FILL,BLUE);
bar(randomNode.x+1,randomNode.y+1,randomNode.x-1+BlockWidth,randomNode.y-1+BlockHeight);
neednode=true;
}
else //no eat
{
newNode.x=headx;
newNode.y=heady;
newNode.next=NULL;
Enqueue(newNode);
outNode=Dequeue();
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(outNode.x+1,outNode.y+1,outNode.x+BlockWidth-1,outNode.y+BlockHeight-1);
setfillstyle(SOLID_FILL,BLUE);
bar(newNode.x+1,newNode.y+1,newNode.x-1+BlockWidth,newNode.y-1+BlockHeight);
}
}
delay(speed);
}
}
void ClearKeyBuf()
{
do
bioskey(0);
while(bioskey(1));
}
void Foot(int x,int y)
{
setcolor(BLUE);
outtextxy(x,y,”writer:[T]RealXL E-MAIL:realgeneral@hotmail.com”);
}
void Head(int x,int y)
{
setcolor(RED);
outtextxy(x,y,”GREEDY SNAKE”);
}
void Enqueue(Node inNode)
{
Node *p=new Node;
p-x=inNode.x;
p-y=inNode.y;
p-next=inNode.next;
snake.tail-next=p;
snake.tail=p;
snake.length++;
}
Node Dequeue()
{
Node *p=snake.head;
Node outNode=*p;
snake.head=p-next;
snake.length–;
delete p;
return outNode;
}
int Hit(int x,int y)
{
if(xframe.left||x=frame.right||yframe.top||y=frame.bottom)//hit the wall
return 1;
Node *p=snake.head-next;
for(int i=snake.length-1;i3;i–,p=p-next)//hit itself
if(x==p-xy==p-y)
return 1;
return 0;
}
bool GameOver()
{
int x=getmaxx()/2-50;
int y=getmaxy()/2-20;
setfillstyle(SOLID_FILL,DARKGRAY);
bar(x+3,y+3,x+103,y+43);
setfillstyle(SOLID_FILL,MAGENTA);
bar(x,y,x+100,y+40);
setlinestyle(0,3,1);
setcolor(RED);
rectangle(x,y,x+100,y+40);
outtextxy(x+20,y+10,”GAGE OVER!”);
char c;
while(true) //按q或Q表示退出程序,按r或R表示重新開始遊戲
{
c=getch();
if(c==’q’||c==’Q’)
return false;
else if(c==’r’||c==’R’)
return true;
}
}
C++五子棋源程序:
#include
#include
#include
#define backcolor CYAN
#define defaultcolor BLACK
#define linecolor MAGENTA
#define player1_color RED
#define player2_color WHITE
#define error_color RED
#define winner_color RED
const int left=40;
const int top=390;
const int d=30;
const int line_num=9;
const int turn=0;
const int r=d/3;
const int j=10;
int x,y,k=1,step=(line_num+1)*(line_num+1);
union REGS regs1,regs2;
class player1;
class player2;
class qipan{
public:
qipan();
~qipan(){};
void init_qipan();
friend void fall(player1 num1,player2 num2,qipan num);
friend void input(player1 num1,player2 num2,qipan num);
private:
int point[line_num+1][line_num+1];
};
class player1{
public:
player1();
~player1(){};
friend void fall(player1 num1,player2 num2,qipan num);
friend void input(player1 num1,player2 num2);
friend int judge_winner(player1 num1,player2 num2);
private:
int point1[line_num+1][line_num+1];
};
class player2{
public:
player2();
~player2(){};
friend void fall(player1 num1,player2 num2,qipan num);
friend void input(player1 num1,player2 num2,qipan num);
friend int judge_winner(player1 num1,player2 num2);
private:
int point2[line_num+1][line_num+1];
};
void input(player1 num1,player2 num2);
void fall(player1 num1,player2 num2,qipan num);
int judge_winner(qipan num,player1 num1,player2 num2);
void inputerror();
void display_winner(int);
void main()
{
int driver=DETECT,mode;
initgraph(driver,mode,”e:\tc30\bgi”);
qipan num;
player1 num1;
player2 num2;
while(step–)
{
input(num1,num2,num);
fall(num1,num2,num);
if(judge_winner(num1,num2))
{
display_winner(k);
}
}
// getchar();
}
qipan::qipan(void)
{ int j,i;
char ch[2]=”0″;
setbkcolor(backcolor);
setcolor(linecolor);
for(i=0;i=line_num;i++)
{
line(left,top-i*d,left+line_num*d,top-i*d);
}
for(i=0;i=line_num;i++)
{
line(left+i*d,top,left+i*d,top-line_num*d);
}
for(i=0;i=line_num;i++)
{ if(*ch==’9’+1) *ch=’a’;
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(left+i*d-2,top+r+3,ch);
(*ch)=(*ch)+1;
}
*ch=’0′;
for(i=0;i=line_num;i++)
{if(*ch==’9’+1) *ch=’a’;
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(left-r-10,top-d*i-3,ch);
(*ch)=(*ch)+1;
}
setcolor(defaultcolor);
for(i=0;i=line_num;i++)
{
for(j=0;j=line_num;j++)
point[i][j]=0;
}
}
void fall(player1 num1,player2 num2,qipan num)
{
int flag=k%2;
if(flag)
{ setcolor(player2_color);
num2.point2[x][y]=1;
num.point[x][y]=2;
circle(left+d*x,top-d*y,r);
setfillstyle(1,player2_color);
floodfill(left+d*x,top-d*y,player2_color);
}
else
{ num1.point1[x][y]=1;
num.point[x][y]=1;
setcolor(player1_color);
circle(left+d*x,top-d*y,r);
setfillstyle(1,player1_color);
floodfill(left+d*x,top-d*y,player1_color);
}
setcolor(defaultcolor);
}
void input(player1 num1,player2 num2,qipan num)
{ char xx,yy;
k++;
while(1)
{
regs1.h.ah=0;
xx=int86(22,®s1,®s1)-‘0’;
if(xx==(‘q’-‘0’)||xx==(‘Q’-‘0’))
{ step=0;
return;
}
regs1.h.ah=0;
yy=int86(22,®s1,®s1)-‘0’;
if(yy==(‘q’-‘0’)||yy==(‘Q’-‘0’))
{
step=0;
return ;
}
if(xx0||xxline_num)
{ inputerror();
continue;
}
if(yy0||yyline_num)
{inputerror();
continue;
}
if(num.point[xx][yy]==0)
{
break;
}
else
{
inputerror();
continue;
}
}
x=(int)xx;
y=(int)yy;
setcolor(backcolor);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(left+d*line_num/3,top+d*2,”Input error”);
setcolor(defaultcolor);
}
player1::player1()
{
int i,j;
for(i=0;i=line_num;i++)
{
for(j=0;j=line_num;j++)
point1[i][j]=0;
}
}
player2::player2()
{ int i,j;
for(i=0;i=line_num;i++)
{
for(j=0;j=line_num;j++)
point2[i][j]=0;
}
}
void inputerror(void)
{ setcolor(error_color);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(left+d*line_num/3,top+d*2,”Input error”);
setcolor(defaultcolor);
}
int judge_winner(player1 num1,player2 num2)
{
int count=0,m=0,n=0,a=0,b=0,xx0,yy0;
int flag=k%2;
xx0=x; yy0=y;
if(!flag)
{ //left ——- right
while(xx0=1m4) {xx0–;m++;}
while(n9xx0=line_num)
{
if(num1.point1[xx0][y]==1)
{
count++;
if(count==5) return 1;
}
else
{
count=0;
}
n++;
xx0++;
}
//up —— down
count=0; xx0=x; m=0; n=0;
while(yy0=1m4){yy0–;m++;}
while(n9yy0=line_num)
{
if(num1.point1[x][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
{
count=0;
}
n++;
yy0++;
}
//left up —– right down
xx0=x;
yy0=y;
m=0;
n=0;
count=0;
while(xx0=1m4){ xx0–; a++; m++;} m=0;
while(yy0=line_numm4){ yy0++; b++; m++;}
if(a=b)
{
xx0=x-a; yy0=y+a;
}
else
{
xx0=x-b; yy0=y+b;
}
while(xx0=line_numyy0=0n9)
{
if(num1.point1[xx0][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
{
count=0;
}
xx0++;
yy0–;
n++;
}
//right up —– left down
count=0;
a=0;
b=0;
n=0;
m=0;
xx0=x;
yy0=y;
while(xx0while(yy0if(a=b)
{
xx0=x+a;
yy0=y+a;
}
else
{
xx0=x+b;
yy0=y+b;
}
while(xx0=0yy0=0n9)
{
if(num1.point1[xx0][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
count=0;
xx0–;
yy0–;
n++;
}
//no winer
return 0;
}
else
{
//left ——- right
while(xx0=1m4) {xx0–;m++;}
while(n9xx0=line_num)
{
if(num1.point1[xx0][y]==1)
{
count++;
if(count==5) return 1;
}
else
{
count=0;
}
n++;
xx0++;
}
//up —— down
count=0; xx0=x; m=0; n=0;
while(yy0=1m4){yy0–;m++;}
while(n9yy0=line_num)
{
if(num2.point2[x][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
{
count=0;
}
n++;
yy0++;
}
//left up —– right down
xx0=x;
yy0=y;
m=0;
n=0;
count=0;
while(xx0=1m4){ xx0–; a++; m++;} m=0;
while(yy0=line_numm4){ yy0++; b++; m++;}
if(a=b)
{
xx0=x-a; yy0=y+a;
}
else
{
xx0=x-b; yy0=y+b;
}
while(xx0=line_numyy0=0n9)
{
if(num2.point2[xx0][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
{
count=0;
}
xx0++;
yy0–;
n++;
}
//right up —– left down
count=0;
a=0;
b=0;
n=0;
m=0;
xx0=x;
yy0=y;
while(xx0while(yy0if(a=b)
{
xx0=x+a;
yy0=y+a;
}
else
{
xx0=x+b;
yy0=y+b;
}
while(xx0=0yy0=0n9)
{
if(num2.point2[xx0][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
count=0;
xx0–;
yy0–;
n++;
}
//no winer
return 0;
}
}
void display_winner(int k)
{
int flag=k%2;
if(!flag)
{ setcolor(winner_color);
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(left+d*2,top+40,”Red is winner”);
setcolor(defaultcolor);
step=0;
getchar();
}
else
{ setcolor(winner_color);
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(left+2*d,top+40,”White is winner”);
setcolor(defaultcolor);
step=0;
}
}
一種C語言小遊戲程序設計(程序已經附上)
“掃雷”小遊戲C代碼
#includestdio.h
#includemath.h
#includetime.h
#includestdlib.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(“\n 請選擇模式:\n 1.標準 2.自定義\n”);
scanf(“%d”,mo);
if(mo==2) /*若選擇自定義模式,要輸入三個參數*/
{do
{t=0; printf(“請輸入\n行數 列數 雷的個數\n”);
scanf(“%d%d%d”,hang,lie,ge);
if(hang2){printf(“行數太少\n”); t=1;}
if(hang100){printf(“行數太多\n”);t=1;}
if(lie2){printf(“列數太少\n”);t=1;}
if(lie100){printf(“列數太多\n”);t=1;}
if(ge1){printf(“至少要有一個雷\n”);t=1;}
if(ge=(hang*lie)){printf(“雷太多了\n”);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;ji;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(“\n\n\n\n”);
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(“\n |”);
for(i=1;i=lie;i=i+1){printf(“—|”);}
printf(“\n”);
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(“\n |”);
for(j=1;j=lie;j=j+1){printf(“—|”);}
printf(“\n”);
}
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(“\n |”);
for(i=1;i=lie;i=i+1){printf(“—|”);}
printf(“\n”);
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(“\n |”);
for(j=1;j=lie;j=j+1) {printf(“—|”);}
printf(“\n”);
}
if(n==(hang*lie-ge)) printf(“你成功了!\n”);
else printf(” 遊戲結束!\n”);
printf(” 重玩請輸入1\n”);
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語言編寫簡單小遊戲?
C語言是計算機專業都要學習的一門基礎學科。一般來說,是比較枯燥的.那麼,我們能不能通過編一些小遊戲來提高它的趣味性呢?這樣學習程序設計,就不會是一件艱苦 ,枯燥的事,它變得象電腦遊戲一樣充滿好奇,富有樂趣。
例如2048這款遊戲:
方法/步驟:
#includestdio.h
#includestdlib.h
#includetime.h
#includeconio.h
#includewindows.h
#define SIZE 4
static int score=0;
void putn(int n[][SIZE]);
void getn(int n[][SIZE]);
int isempty(int n[][SIZE]);
int isfull(int n[][SIZE]);
void math(int n[][SIZE],char c);
void tow(int n[][SIZE]);
void toa(int n[][SIZE]);
void tos(int n[][SIZE]);
void tod(int n[][SIZE]);
//主函數
int main()
{
int i,j;
int n[SIZE][SIZE];
char c=’ ‘;
for(i=0;iSIZE;i++)
{
for(j=0;jSIZE;j++)
{
n[i][j]=0;
}
}
printf( “***********************\n”
” 2048(%dX%d) \n”
” control:W/A/S/D \n”
“press any key to begin \n”
“***********************\n”,SIZE,SIZE);
getch();
system(“cls”);
//n[0][1]=2048;
//n[0][3]=2048;
while(1)
{
if(isempty(n))
getn(n);
putn(n);
if(!isempty(n)isfull(n))
break;
sleep(200);
c=getch();
while(c!=’w’c!=’a’c!=’s’c!=’d’)
c=getch();
math(n,c);
system(“cls”);
}
printf(” Game Over!\n”,score);
return 0;
}
//函數
void putn(int n[][SIZE])
{
int i,j;
for(i=0;iSIZE;i++)
{
for(j=0;jSIZE;j++)
printf(“| “);
printf(“|\n”);
for(j=0;jSIZE;j++)
{
if(n[i][j]==0)
printf(“| “);
else
printf(“|%4d “,n[i][j]);
}
printf(“|\n”);
for(j=0;jSIZE;j++)
printf(“|_____”);
printf(“|\n”);
}
printf(“score: %d”,score);
}
void getn(int n[][SIZE])
{
int a,b;
a=rand()%SIZE;
b=rand()%SIZE;
while(n[a][b]!=0)
{
a=rand()%SIZE;
b=rand()%SIZE;
}
n[a][b]=2;
}
int isempty(int n[][SIZE])
{
int i,j,count=0;
for(i=0;iSIZE;i++)
for(j=0;jSIZE;j++)
if(n[i][j]==0)
count++;
return count;
}
int isfull(int n[][SIZE])
{
int i,j,count=0;
for(i=0;iSIZE;i++)
{
for(j=1;jSIZE-1;j++)
{
if(n[i][j]==n[i][j+1]||n[i][j]==n[i][j-1])
count++;
}
}
for(j=0;jSIZE;j++)
{
for(i=1;iSIZE-1;i++)
{
if(n[i][j]==n[i+1][j]||n[i][j]==n[i-1][j])
count++;
}
}
return count0?0:1;
}
void math(int n[][SIZE],char c)
{
switch(c)
{
case ‘w’:tow(n);break;
case ‘a’:toa(n);break;
case ‘s’:tos(n);break;
case ‘d’:tod(n);break;
default :;
}
}
void tow(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;aSIZE;a++)
m[a]=0;
for(j=0;jSIZE;j++)
{
for(a=0;aSIZE;a++)
{
for(i=0;iSIZE-1;i++)
{
if(n[i][j]==0)
{
n[i][j]=n[i+1][j];
n[i+1][j]=0;
}
}
}
}
for(j=0;jSIZE;j++)
{
for(a=0,i=0;iSIZE;i++)
{
if(n[i][j]!=n[i+1][j]n[i][j]!=0||n[i][j]==2048)
{
m[a++]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i+1][j])
{
m[a++]=n[i][j]+n[i+1][j];
score+=m[a-1];
n[i][j]=0,n[i+1][j]=0;
}
}
for(i=0;iSIZE;i++)
{
n[i][j]=m[i];
m[i]=0;
}
}
}
void toa(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;aSIZE;a++)
m[a]=0;
for(i=0;iSIZE;i++)
{
for(a=0;aSIZE;a++)
{
for(j=0;jSIZE-1;j++)
{
if(n[i][j]==0)
{
n[i][j]=n[i][j+1];
n[i][j+1]=0;
}
}
}
}
for(i=0;iSIZE;i++)
{
for(a=0,j=0;jSIZE;j++)
{
if(n[i][j]!=n[i][j+1]n[i][j]!=0||n[i][j]==2048)
{
m[a++]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i][j+1])
{
m[a++]=n[i][j]+n[i][j+1];
score+=m[a-1];
n[i][j]=0,n[i][j+1]=0;
}
}
for(j=0;jSIZE;j++)
{
n[i][j]=m[j];
m[j]=0;
}
}
}
void tos(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;aSIZE;a++)
m[a]=0;
for(j=SIZE-1;j=0;j–)
{
for(a=SIZE-1;a=0;a–)
{
for(i=SIZE-1;i0;i–)
{
if(n[i][j]==0)
{
n[i][j]=n[i-1][j];
n[i-1][j]=0;
}
}
}
}
for(j=SIZE-1;j=0;j–)
{
for(a=SIZE-1,i=SIZE-1;i=0;i–)
{
if(n[i][j]!=n[i-1][j]n[i][j]!=0||n[i][j]==2048)
{
m[a–]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i-1][j])
{
m[a–]=n[i][j]+n[i-1][j];
score+=m[a+1];
n[i][j]=0,n[i-1][j]=0;
}
}
for(i=SIZE-1;i=0;i–)
{
n[i][j]=m[i];
m[i]=0;
}
}
}
void tod(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;aSIZE;a++)
m[a]=0;
for(i=SIZE-1;i=0;i–)
{
for(a=SIZE-1;a=0;a–)
{
for(j=SIZE-1;j0;j–)
{
if(n[i][j]==0)
{
n[i][j]=n[i][j-1];
n[i][j-1]=0;
}
}
}
}
for(i=SIZE-1;i=0;i–)
{
for(a=SIZE-1,j=SIZE-1;j=0;j–)
{
if(n[i][j]!=n[i][j-1]n[i][j]!=0||n[i][j]==2048)
{
m[a–]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i][j-1])
{
m[a–]=n[i][j]+n[i][j-1];
score+=m[a+1];
n[i][j]=0,n[i][j-1]=0;
}
}
for(j=SIZE-1;j=0;j–)
{
n[i][j]=m[j];
m[j]=0;
}
}
}
教你如何使用C語言編寫簡單小遊戲
愛玩是人的天性,而C語言是我們計算機專業都要學習的一門基礎 學科.一般來說,是比較枯燥的.那麼,我們能不能通過編一些小遊戲來提高它的趣味性呢?這樣學習程序設計,就不會是一件艱苦 ,枯燥的事,它變得象電腦遊戲一樣充滿好奇,富有樂趣. 1, 總是從Hello,world開始 學習編程的第一個程序,一般就是列印一個親切的詞語——”Hell o,world!”.讓我們來看看這個最簡單的C程序:#incolude /*把輸入輸出函數的頭文件包含進來*/int main(){printf(“Hello, world!”);/*在屏幕上輸出字元串”Hello,world!”*/return 0;/*退出main函數,並返回0*/} 下面我們發現幾個值得改進的地方,1,程序的運行結果一閃而過 .2,每執行這個程序一次都能看見上次運行留下的字元.3,我們 還希望屏幕輸出一個笑臉來歡迎我們. 讓我們來改進一下這個程序吧!1,在return語句的前面加一句:getch ();,表示按任意鍵結束.2,在printf語句前用clrscr函數清屏,要使用這個函數和getch函數,需要在程序開頭再包含頭文件conio.h.3,ASCII碼也有 許多非常好玩的字元,比如ASCII碼值為2的就是一個笑臉,我們可 以用printf(“%c”, 2)來輸出一個笑臉. 現在我們把Hello,world程序改成一個更好看的Hello,world了.下面讓我們開始做遊戲吧! 2, 心動的開始,一個運動中的笑臉 大家小時侯喜歡看動畫片嗎?哈哈,我猜你們都喜歡吧!下面就讓我們來做一個小動畫吧.在屏幕上顯示一個運動的小笑臉,而且當它到達屏幕的邊緣時會自動彈回來.先在程序定義一個在屏幕中運動的點的結構:struct move_point{ int x, y;/*該點的位置,包括x坐標和y坐標*/ int xv, yv;/*該點在x軸,y軸的速度*/};運動的原理是,先擦去物體先前的軌跡,讓物體按其速度移動一段距離,再畫出該物體.讓我們看到以下代碼:gotoxy(man.x, man.y);/*把游標移到指定的坐標*/printf(” “);/*輸出一個空格,把先前的字元擦去*/然後我們讓物體按其速度運動:man.x += man.xv;/*水平方向按x軸的速度運動*/man.y += man.yv;/*垂直方向按y軸的速度運動*/運動後還要判斷物體是否出界,如果出了界,就令物體反彈,即讓 它下一刻的速度等於現在的速度的相反數.最後列印出這個笑臉:gotoxy(man.x, man.y);printf(“%c\b”, 2); /*輸出ASCII碼值為2的”笑臉”字元*/怎麼樣?是不是很有趣呢?不過這個笑臉一直是自己運動,能不能 讓我們來控制它運動呢?不過這個程序沒有什麼目的,也沒有什麼判斷勝負的條件.下面我們就利用這個能控制它移動的笑臉來做一個更有趣的遊戲吧! 4, 在迷宮中探索 小時侯,我常在一些小人書和雜誌上看見一些迷宮的遊戲,非常喜歡玩,還常到一些書上找迷宮玩呢.好的,現在我們用C語言來編個迷宮的遊戲,重溫一下童年的樂趣. 首先,我們定義一個二維數組map,用它來保存迷宮的地圖,其中map[x][y] == ‘#’表示在(x,y)坐標上的點是牆壁.DrawMap函數在屏幕上輸出迷宮的地圖和一些歡迎信息.在main函數里,我們定義了”小人”man的坐標和”目的地”des的 坐標.在遊戲循環中,我們增加了一些用來判斷勝負的語句:if (man.x == des.x man.y == des.y) /*如果人的坐標等於目的地的坐標*/{ gotoxy(35, 3); printf(“Ok! You win!”); /*輸出勝利信息*/….}在判斷按鍵時,如果玩家按的是方向鍵,我們還要先判斷前面是不是有”牆壁”,如果有的話,就不能往前移動了.好的,我們在判斷按鍵的switch語句的各個分支加上了判斷語句,如下:if (map[…][…] == ‘#’) break;/*如果前面是牆壁,就不執行下去*/哇噻!真棒,我們做出了一個完整的遊戲了.當然你還可以通過修改二維數組map來修改迷宮的地圖,讓它更有挑戰性.不過,我們要設計一個更好玩的遊戲—— 5, 聰明的搬運工 大家一定玩過”搬運工”的遊戲吧!這是在電腦和電子字典上較流行的益智遊戲,讓我們動手做一個屬於自己的”搬運工”吧!程序依然用數組map來保存地圖,數組元素如果為空格則表示什麼也沒有,’b’表示箱子,’#’表示牆壁,’*’表示目的地,’i’表示箱子在目的地.我們以後每推一下箱子,不但要改變屏幕的顯示,也要改變map相應元素的值.遊戲的主循環依然是接受按鍵.當接收一個方向鍵,需要判斷小人前面一格的狀態,如果是空地或目的地,則人物可以直接移動;如果是牆壁,則不可移動;如果是箱子或目的地上的箱子,則需要繼續判斷箱子前面一格的狀態:如果前一格是空地或目的地,則人推箱子前進,否則不可移動.好的,我們在switch中增加了這些判斷語句.程序還有一個重要的功能就是判斷勝利.數組Des用來記錄全部目的地的坐標,我們每執行一步操作後,程序就要通過Des數組判斷這些目的地上是否都有箱子了.真棒啊!我們可以做遊戲了.而且是一個老少皆宜,趣味十足的遊戲呢!當然,我們可以通過修改map數組來製作不同的遊戲地圖,我們還可以相互分享好的遊戲地圖呢. 尾聲: 在C++等高級語言還沒出來的時候,很多應用程序也是C語言開發的.C語言在與硬體聯繫緊密的編程中,也佔有重要地位.其實我覺得學習編程,可以通過一些小遊戲,實用的例子來學習.象學習音樂的人,不是要等到把全部樂理學完後才演奏一個完整的曲子.而是剛開始學時就有一些簡單的曲子讓你演奏,讓你立刻就有成就感,讓你很快就能賣弄出來在別人面前表現自己了.通過編遊戲來學習編程,把學習變成遊戲,不失為學習計算機的一種好方法. 好了,編遊戲就這麼簡單,希望大家也嘗試用C語言或其他的語言來做幾個自己喜歡的小遊戲.
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/192554.html