本文目录一览:
- 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语言或其他的语言来做几个自己喜欢的小游戏.