當前位置:首頁 » 編程語言 » c語言小游戲編程100例

c語言小游戲編程100例

發布時間: 2024-07-19 06:34:49

『壹』 c語言游戲編程,下落的小鳥 求代碼

下落的小鳥

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<Windows.h>
int Grade = 1, Score = 0, Max_blank = 9, Distance = 18;
struct Birds{int x; int y;}; //定義一種Birds數據類型(含3個成員)
Birds *Bird = (Birds*)malloc(sizeof(Birds)); //定義Birds類型 指針變數Bird並賦初值
struct Bg{int x, y; int l_blank; Bg *pri; Bg *next;}; //定義一種Bg數據類型(含5個成員)
Bg *Bg1 = (Bg*)malloc(sizeof(Bg)); //定義Bg類型 指針變數Bg1並賦初值

void Position(int x, int y) //游標定位函數(用於指定位置輸出)
{COORD pos = { x - 1, y - 1 };
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}

void Csh( ) //初始化界面

{

printf("══════════════════════════════════════ ");

printf(" ■■ ■■ C語言版 Flappy Bird ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ 瞎搞人:yyposs原創 ");

printf(" ■■ ■■ 瞎搞日期:2014.2 ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ 改編:鳴蟬百2021.7 ");

printf(" ■■ ■■ 操作:按向上方向鍵讓小鳥起飛 ");

printf(" ■■ ");

printf(" ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ DEVc++運行通過 ");

printf("══════════════════════════════════════ ");

printf(" 按鍵繼續…");

getch( );

system("cls");

}

void PrFK( ) //輸出方框(游戲范圍區)

{int i;

Position(1, 1); printf("╔"); Position(79, 1); printf("╗");

Position(1, 24); printf("╚"); Position(79, 24); printf("╝");

for (i = 3; i <= 78; i += 2){Position(i, 1); printf("═"); Position(i, 24); printf("═");}

for(i=2;i<=23;i++)

{ Position(1,i); printf("║");if(i<11)printf("0%d",i-1);else printf("%d",i-1);

Position(79,i); printf("║");

}

Position(4, 25); printf("小鳥即將出現,請准備按鍵起飛… ");

getch( );

Position(4, 25); printf(" ");

}

void CreatBg( ) //創建障礙物坐標(便於列印輸出)
{Bg *Bg2 = (Bg*)malloc(sizeof(Bg));
Bg1->x = 90; Bg1->y = 8; //確定障礙物的一對基本坐標(此時值是在游戲框之外)
Bg2->x = Bg1->x + Distance; Bg2->y = 9; //下一障礙物的基本坐標x、y
Bg1->l_blank = Max_blank - Grade; //障礙物上下兩部分之間的空白距離l_blank
Bg2->l_blank = Max_blank - Grade;
Bg1->next = Bg2; Bg1->pri = Bg2;
Bg2->next = Bg1; Bg2->pri = Bg1;
}


void InsertBg(Bg *p) //隨機改變障礙物的y坐標,讓空白通道有上下變化
{int temp;
Bg *Bgs = (Bg*)malloc(sizeof(Bg));
Bgs->x = p->pri->x + Distance;
Bgs->l_blank = Max_blank - Grade;
srand((int)time(0)); //啟動隨機數發生器
temp = rand( ); //產生一個隨機數並賦值給temp
if (temp % 2 == 0)
{if ((temp % 4 + p->pri->y + Max_blank - Grade)<21)
Bgs->y = p->pri->y + temp % 4;
else Bgs->y = p->pri->y;
}
else
{if ((p->pri->y - temp % 4)>2)Bgs->y = p->pri->y - temp % 4;
else Bgs->y = p->pri->y;
}
Bgs->pri = p->pri; Bgs->next = p;
p->pri->next = Bgs; p->pri = Bgs;
}

void CreatBird( ) //建立小鳥的坐標(初始列印輸出小鳥的位置)
{Bird->x = 41; Bird->y = 10;}

int CheckYN(Bg *q) //判斷游戲結束與否(值為0是要結束,為1沒有要結束)
{Bg *p = q; int i = 0;
while (++i <= 5)
{if (Bird->y>23)return 0;
if (Bird->x == p->x&&Bird->y <= p->y)return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y)return 0;
if (Bird->x == p->x&&Bird->y>p->y + p->l_blank)return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y + p->l_blank)
return 0;
p = p->next;
}
return 1;
}


void Check_Bg(Bg *q) //核查開頭的障礙物坐標是否在游戲區內
{Bg *p = q; int i = 0, temp;
while (++i <= 5)
{if (p->x>-4)p = p->next;
else
{srand((int)time(0)); temp = rand();
if (temp % 2 == 0)
{if ((temp % 4 + p->y + Max_blank - Grade)<21)p->y = p->y + temp % 4;
else p->y = p->y; p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
else
{if ((p->y - temp % 4)>2)p->y = p->y - temp % 4;
else p->y = p->y; p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
}
}
}

void Prt_Bg(Bg *q) //列印輸出障礙物(依據其x、y坐標進行相應輸出)
{Bg *p = q; int i = 0, k, j;
while (++i <= 5)
{if (p->x>0 && p->x <= 78)
{for (k = 2; k<p->y; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
Position(p->x, p->y);
printf("■"); printf("■"); printf("■"); printf(" ");
Position(p->x, p->y + p->l_blank);
printf("■"); printf("■"); printf("■"); printf(" ");
k = k + p->l_blank + 1;
for (k; k <= 23; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
}
p = p->next;
if (p->x == 0)
{for (j = 2; j<p->y; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
Position(p->x + 1, p->y);
printf(" "); printf(" "); printf(" ");
Position(p->x + 1, p->y + Max_blank - Grade);
printf(" "); printf(" "); printf(" ");
j = j + Max_blank - Grade + 1;
for (j; j <= 22; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
}
}
}

void PrtBird( ) //列印輸出小鳥
{Position(Bird->x, Bird->y - 1); printf(" ");
Position(Bird->x, Bird->y); printf("Ю");
Position(38, 2); printf("Score:%d", Score);
}
void Loop_Bg(Bg *q) //障礙物x坐標左移,並記錄成績
{Bg *p = q; int i = 0;
while (++i <= 5)
{p->x = p->x - 1; p = p->next;
if (Bird->x == p->x)
{Score += 1;
if (Score % 4 == 0 && Grade<4)Grade++;
}
}
}

int main( )
{int i = 0; int t;

while (1)

{
Csh( );PrFK( );CreatBg( );
InsertBg(Bg1);InsertBg(Bg1);InsertBg(Bg1);
CreatBird( );
while (1)
{if (!CheckYN(Bg1))break;
Check_Bg(Bg1);Prt_Bg(Bg1);
PrtBird( );Loop_Bg(Bg1);
Bird->y = Bird->y + 1;
if (GetAsyncKeyState(VK_UP))
//按下了向上方向鍵
{Position(Bird->x, Bird->y - 1);printf(" ");
Bird->y = Bird->y - 4;
}
Sleep(200);
//程序延時200毫秒(數值大小決定游戲速度快慢)
i = 0;
}
Position(6, 25);
printf("游戲結束! 請輸入:0.退出 1.重玩");

scanf("%d",&t);

if (t==0)break;

system("cls"); Score = 0;

}
return 0;
}

『貳』 用C語言編寫的小游戲代碼是什麼

/*也不知道你是什麼級帶滾別的,我是一個新手,剛接觸編程語言,以下是我自己變得一個小程序,在所有c語言的編譯器(vc++6.0、turbo????)上都能運行,你還可以進一步改進。這是一個類似貪吃蛇的小游戲。蠢猛余祝你知鍵好運*/x0dx0a/*貪吃蛇*/x0dx0a#includex0dx0a#includex0dx0a#includex0dx0a#includex0dx0aint head=3 ,tail=0;x0dx0aint main()x0dx0a{x0dx0aint i,j,k=0;x0dx0aint zuobiao[2][80];x0dx0along start;x0dx0aint direction=77;x0dx0aint gamespeed;x0dx0aint timeover;x0dx0aint change(char qipan[20][80],int zuobiao[2][80],char direction);x0dx0azuobiao[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;x0dx0a/*處理棋盤*/x0dx0achar qipan[20][80];//定義棋盤x0dx0afor(i=0;i<20;i++)x0dx0a for(j=0;j<80;j++)x0dx0aqipan[i][j]=' '//初始化棋盤x0dx0afor(i=0;i<80;i++)x0dx0aqipan[0][i]='_'x0dx0afor(i=0;i<20;i++)x0dx0aqipan[i][0]='|'x0dx0afor(i=0;i<20;i++)x0dx0aqipan[i][79]='|'x0dx0afor(i=0;i<80;i++)x0dx0aqipan[19][i]='_'x0dx0aqipan[1][1]=qipan[1][2]=qipan[1][3]='*'//初始化蛇的位置x0dx0aqipan[1][4]='#'x0dx0aprintf("This is a game of a SNAKE.\nGOOD LUCK TO YOU !\n");x0dx0aprintf("Input your game speed,please.(e.g.300)\n");x0dx0ascanf("%d",&gamespeed);x0dx0ax0dx0awhile(direction!='q')x0dx0a{x0dx0asystem("cls");x0dx0afor(i=0;i<20;i++)//列印出棋盤x0dx0afor(j=0;j<80;j++)x0dx0aprintf("%c",qipan[i][j]);x0dx0atimeover=1;x0dx0astart=clock();x0dx0awhile(!kbhit()&&(timeover=clock()-start<=gamespeed));x0dx0aif(timeover)x0dx0a{x0dx0agetch();x0dx0adirection=getch();x0dx0a}x0dx0aelsex0dx0adirection=direction;x0dx0aif(!(direction==72||direction==80||direction==75||direction==77))x0dx0a{x0dx0areturn 0;x0dx0asystem("cls");x0dx0aprintf("GAME OVER!\n");x0dx0a}x0dx0aif(!change(qipan,zuobiao,direction))x0dx0a{x0dx0adirection='q'x0dx0asystem("cls");x0dx0aprintf("GAME OVER!\n");x0dx0a}x0dx0a}x0dx0areturn 0;x0dx0a}x0dx0aint change(char qipan[20][80],int zuobiao[2][80],char direction)x0dx0a{x0dx0aint x,y;x0dx0aif(direction==72)x0dx0ax=zuobiao[0][head]-1;y=zuobiao[1][head];x0dx0aif(direction==80)x0dx0ax=zuobiao[0][head]+1;y=zuobiao[1][head];x0dx0aif(direction==75)x0dx0ax=zuobiao[0][head];y=zuobiao[0][head]-1;x0dx0aif(direction==77)x0dx0ax=zuobiao[0][head];y=zuobiao[1][head]+1;x0dx0aif(x==0||x==18||y==78||y==0)x0dx0areturn 0;x0dx0aif(qipan[x][y]!=' ')x0dx0areturn 0;x0dx0aqipan[zuobiao[0][tail]][zuobiao[1][tail]]=' 'x0dx0atail=(tail+1)%80;x0dx0aqipan[zuobiao[0][head]][zuobiao[1][head]]='*'x0dx0ahead=(head+1)%80;x0dx0azuobiao[0][head]=x;x0dx0azuobiao[1][head]=y;x0dx0aqipan[zuobiao[0][head]][zuobiao[1][head]]='#'x0dx0areturn 1;x0dx0a}

『叄』 用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;row<RowOfFrame;row++)
line(frame.left+row*BlockWidth,frame.top,frame.left+row*BlockWidth,frame.bottom);
for(int column=1;column<ColumnOfFrame;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->x&&randy==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(speed<100)
speed=100;
ClearKeyBuf();
break;
case PAGEDOWN:speed=speed+100;
if(speed>500)
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.x&&heady==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:[email protected]");
}
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(x<frame.left||x>=frame.right||y<frame.top||y>=frame.bottom)//hit the wall
return 1;

Node *p=snake.head->next;
for(int i=snake.length-1;i>3;i--,p=p->next)//hit itself
if(x==p->x&&y==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(xx<0||xx>line_num)
{ inputerror();
continue;
}
if(yy<0||yy>line_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>=1&&m<4) {xx0--;m++;}
while(n<9&&xx0<=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>=1&&m<4){yy0--;m++;}
while(n<9&&yy0<=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>=1&&m<4){ xx0--; a++; m++;} m=0;
while(yy0<=line_num&&m<4){ yy0++; b++; m++;}
if(a<=b)
{
xx0=x-a; yy0=y+a;
}
else
{
xx0=x-b; yy0=y+b;
}
while(xx0<=line_num&&yy0>=0&&n<9)
{
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>=0&&yy0>=0&&n<9)
{
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>=1&&m<4) {xx0--;m++;}
while(n<9&&xx0<=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>=1&&m<4){yy0--;m++;}
while(n<9&&yy0<=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>=1&&m<4){ xx0--; a++; m++;} m=0;
while(yy0<=line_num&&m<4){ yy0++; b++; m++;}
if(a<=b)
{
xx0=x-a; yy0=y+a;
}
else
{
xx0=x-b; yy0=y+b;
}
while(xx0<=line_num&&yy0>=0&&n<9)
{
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>=0&&yy0>=0&&n<9)
{
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的小游戲。給你一個基礎的簡單的漢諾塔程序。你看看:
這是個漢諾塔程序,在調試的時候,輸入的數字最好不要大於15,因為每大一個數
所得的結果的步驟都會多一倍。如果你有耐心等待結果的話除外。漢諾塔是在歐洲
流行的一種游戲,有a,b,c三個竿。a竿上有若干個由大到小的圓盤,大的在下面,
小的在上面,b,c都是空桿,請你把a桿上的圓盤都倒到別的桿上,或b或c,在倒盤
的過程中不可以大的壓小的,實常式序如下:

#include <stdio.h>
int i=0;
main()
{
unsigned n;
printf("Please enter the number of discs: ");
scanf("%d",&n);
printf("\tneedle:\ta\t b\t c\n");
movedisc(n,'a','c','b');
printf("\t Total: %d\n",i);
getch();
}
movedisc(n,fromneedle,toneedle,usingneedle)
unsigned n;
char fromneedle,toneedle,usingneedle;
{
if(n>0)
{
movedisc(n-1,fromneedle,usingneedle,toneedle);
i++;
switch(fromneedle)
{
case 'a':switch(toneedle)
{
case 'b':printf("\t[%d]:\t%2d------>%2d\n",i,n,n);
break;
case 'c':printf("\t[%d]:\t%2d------------->%2d\n",i,n,n);
break;
}
break;
case 'b':switch(toneedle)
{
case 'a':printf("\t[%d]:\t%2d<----------%2d\n",i,n,n);
break;
case 'c':printf("\t[%d]:\t\t%2d------>%2d\n",i,n,n);
break;
}
break;
case 'c':switch(toneedle)
{
case 'a':printf("\t[%d]:\t%2d<--------------%2d\n",i,n,n);
break;
case 'b':printf("\t[%d]:\t\t%2d<--------%2d\n",i,n,n);
break;
}
break;
}
movedisc(n-1,usingneedle,toneedle,fromneedle);
}
}

『伍』 誰有一些簡單小游戲的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;i<H;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;i<snk.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==72&&dir!=2)dir=1; //72表示按下了向上方向鍵

if(key==80&&dir!=1)dir=2; //80為向下

if(key==75&&dir!=4)dir=3; //75為向左

if(key==77&&dir!=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("07"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; }//這007是響鈴

else Flag = 0; //沒吃到食物Flag的值為0

if(snk.speed<150) 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);

} //用來設定顏色的函數

熱點內容
校園論壇源碼 發布:2024-11-26 05:42:35 瀏覽:567
民生銀行pin密碼是多少 發布:2024-11-26 05:31:24 瀏覽:774
sql獲取日期部分 發布:2024-11-26 05:25:06 瀏覽:743
怎麼才能把安卓數據轉移到蘋果手機上 發布:2024-11-26 05:14:35 瀏覽:850
手機對比參數配置常看的有哪些 發布:2024-11-26 05:01:23 瀏覽:891
qq默認存儲路徑修改 發布:2024-11-26 04:55:02 瀏覽:709
為什麼吉利配置那麼高 發布:2024-11-26 04:49:20 瀏覽:430
源碼平台排名 發布:2024-11-26 04:44:49 瀏覽:998
電腦網路連接密碼如何更改 發布:2024-11-26 04:39:20 瀏覽:203
安卓手機如何設置airpro自動彈窗 發布:2024-11-26 04:39:17 瀏覽:585