1. 教你如何使用c語言編寫簡單小游戲
編寫程序,實現如下表所示的5-魔方陣。
17
24
1
8
15
23
5
7
14
16
4
6
13
20
22
10
12
19
21
3
11
18
25
2
9
5-魔方陣
問題分析
所謂「n-魔方陣」,指的是使用1〜n2共n2個自然數排列成一個n×n的方陣,其中n為奇數;該方陣的每行、每列及對角線元素之和都相等,並為一個只與n有關的常數,該常數為n×(n2+1)/2。
例如5-魔方陣,其第一行、第一列及主對角線上各元素之和如下:
第一行元素之和:17+24+1+8+15=65
第一列元素之和:17+23+4+10+11=65
主對角線上元素之和:17+5+13+21+9=65
而
n×(n2+1)/2=5×(52+1)/2=65
可以驗證,5-魔方陣中其餘各行、各列及副對角線上的元素之和也都為65。
假定陣列的行列下標都從0開始,則魔方陣的生成方法為:在第0行中間置1,對從2開始的其餘n2-1個數依次按下列規則存放:
(1)
假定當前數的下標為(i,j),則下一個數的放置位置為當前位置的右上方,即下標為(i-1,j+1)的位置。
(2)
如果當前數在第0行,即i-1小於0,則將下一個數放在最後一行的下一列上,即下標為(n-1,j+1)的位置。
(3)
如果當前數在最後一列上,即j+1大於n-1,則將下一個數放在上一行的第一列上,即下標為(i-1,0)的位置。
(4)
如果當前數是n的倍數,則將下一個數直接放在當前位置的正下方,即下標為(i+1,j)的位置。
演算法設計
在設計演算法時釆用了下面一些方法:
定義array()函數,array()函數的根據輸入的n值,生成並顯示一個魔方陣,當發現n不是奇數時,就加1使之成為奇數。
使用動態內存分配與釋放函數malloc()與free(),在程序執行過程中動態分配與釋放內存,這樣做的好處是使代碼具有通用性,同時提高內存的使用率。
在分配內存時還要注意,由於一個整型數要佔用兩個內存,因此,如果魔方陣中要存放的數有max個,則分配內存時要分配2*max個單元,從而有malloc(max+max)。在malloc()函數中使用max+max而不是2*max是考慮了程序運行的性能。
顯然應該使用二維數組來表示魔方陣,但雖然數組是二維形式的,而由於內存是一維線性的,因此在存取數組元素時,要將雙下標轉換為單個索引編號。在程序中直接定義了指針變數來指向數組空間,即使用malloc()函數分配的內存。
2. C語言小程序推箱子問題
你好,這是推箱子的全部代碼
/*圖形模式下的推箱子*/
#define MAX 2 /*游戲中總關數*/
#define STARTX 180
#define STARTY 80
#define BKCOLOR BLACK
#define MANCOLOR RED
#define OBJECTCOLOR YELLOW
#define TIMEINT 2
#define STARNUM 300
#define STEPMAX 5
#include<string.h>
#include<bios.h>
#include<stdio.h>
#include<dos.h>
#include<graphics.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#define Key_R 0x1372
#define Key_Up 0x4800
#define Key_Enter 0x1c0d
#define Key_Down 0x5000
#define Key_P 0x1970
#define Key_Esc 0x11b
#define Key_Right 0x4d00
#define Key_Left 0x4b00
typedef struct star
{
int x;
int y;
int c;
}Star;
Star s[STARNUM];
typedef struct record
{
char name[20];
int second;
struct time t;
struct date d;
}Record;
Record r[MAX];
typedef struct c
{
int x;
int y;
}Add;
typedef struct a
{
int x;
int y;
}Player;
Player p;
char name[20]; /*進入游戲時記錄玩家的姓名*/
time_t t1,t2; /*游戲結束時的時間*/
int Ide; /*開始進入游戲時,選擇菜單時返的功能號(1,2,3)*/
int MissionNum; /*玩家正在玩的關數*/
int BoxNum; /*目的地的個數*/
int Key; /*玩家按鍵*/
int map[10][10]; /*地圖.(空=0),(人=1),(箱子=2),(牆=3),(目的地=4),(人+目的地=5),(箱子+目的地=6)*/
int StepNum;
int DirectionKey; /*用來表示方向鍵最後一次按鍵*/
int BoxMove[STEPMAX];
int Step[STEPMAX];
/*函數定義*/
void InputName();
void Init();
void MainMenu();
void JudgeRecord();
void WriteRecord();
void JudgeIde();
void DrawMenu(int );
void Game();
void InitMission(int );
void NextMission();
void InitPic(int ,int ,int );
int Move(Add );
void DrawWall(int ,int );
void DrawBack(int ,int );
void DrawBox(int ,int );
void DrawObject(int ,int );
void DrawMan(int ,int );
void DrawStar();
int JudgeWin();
void InitMission1();
void InitMission2();
int TimeCome();
void ChangeStar();
void InputName();
void ViewRecords();
void DeleteRecords();
void RegisterStep();
void ReverselyMove();
void MoveBack(Add );
void main()
{
InputName();
Init(); /*驅動顯卡*/
srand(time(&t1));
MainMenu(); /*畫開始菜單(1.start game 2.view records 3.delete records 4.exit game)*/
}
void InputName()
{
char b;
char c;
do
{
printf("\n\nPlease input your password:");
scanf("%s",&b);
}while(b!='l');
clrscr();
do
{
printf("\n\nPlease input your name:");
scanf("%s",name);
printf("Are you sure the name right(Y/N):");
do
{
c=getch();
}while(c!='Y'&&c!='y'&&c!='N'&&c!='n');
}while(c!='Y'&&c!='y');
}
void Init()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc");
}
void MainMenu()
{
setbkcolor(BKCOLOR);
cleardevice();
DrawStar(); /*畫開始菜單的背景畫面*/
DrawStar();
Ide=0,Key=0;
DrawMenu(Ide);
do
{
if(bioskey(1)) /*有鍵按下則處理按鍵*/
{
Key=bioskey(0);
switch(Key)
{
case Key_Down: {Ide++;Ide=Ide%4;DrawMenu(Ide);break;}
case Key_Up: {Ide--;Ide=(Ide+4)%4;DrawMenu(Ide);break;}
}
}
else {if(TimeCome()) ChangeStar();} /*改變背景*/
}while(Key!=Key_Enter);
JudgeIde(); /*根據Ide運行不同的程序*/
}
void JudgeIde()
{
switch(Ide)
{
case 0:Game();break;
case 1:{ViewRecords();bioskey(0);MainMenu();break;}
case 2:{DeleteRecords();ViewRecords();bioskey(0);MainMenu();break;}
case 3:exit(0);
}
}
void Game()
{
int i,j,flag;
Add ad;
MissionNum=0;
NextMission();
do
{
flag=0;
Key=bioskey(0);
switch(Key)
{
case Key_Enter:{NextMission();time(&t1);break;}
case Key_Up:{ad.x=-1;ad.y=0;flag=1;DirectionKey=Key;break;}
case Key_Down:{ad.x=1;ad.y=0;flag=1;DirectionKey=Key;break;}
case Key_Left:{ad.x=0;ad.y=-1;flag=1;DirectionKey=Key;break;}
case Key_Right:{ad.x=0;ad.y=1;flag=1;DirectionKey=Key;break;}
case Key_R:{MissionNum--;NextMission();break;}
case Key_Esc:{MainMenu();break;}
case Key_P:{ReverselyMove();break;}
}
if(flag==1)
{if(Move(ad)) {RegisterStep(); if(JudgeWin()) {JudgeRecord();NextMission();}}}
}while(1);
}
void InitMission(int n)
{
int i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
map[i][j]=0;
switch(n)
{
case 1:InitMission1();break; /*第一關*/
case 2:InitMission2();break; /*第二關*/
}
}
void InitPic(int n,int i,int j)
{
switch(n)
{
case 0:DrawBack(i,j);break;
case 1:DrawMan(i,j);break;
case 2:DrawBox(i,j);break;
case 3:DrawWall(i,j);break;
case 4:DrawObject(i,j);break;
case 5:DrawMan(i,j);break;
case 6:DrawBox(i,j);break;
}
}
void NextMission()
{
int i,j;
if(MissionNum+1>MAX) MissionNum=1;
else MissionNum++;
InitMission(MissionNum);
setbkcolor(BKCOLOR);
cleardevice();
for(i=0;i<10;i++)
for(j=0;j<10;j++)
InitPic(map[i][j],i,j);
switch(MissionNum)
{
case 1:outtextxy(200,230,"Mission 1");break;
case 2:outtextxy(200,230,"Mission 2");break;
}
time(&t1);
for(i=0;i<STEPMAX;i++)
{Step[i]=BoxMove[i]=0;}
StepNum=0;
}
int Move(Add a)
{
int flag;
int i=StepNum%STEPMAX;
switch(map[p.x+a.x][p.y+a.y]) /*看下一位置為什麼*/
{
case 0:{map[p.x][p.y]-=1;InitPic(map[p.x][p.y],p.x,p.y);
p.x=p.x+a.x;p.y=p.y+a.y;
map[p.x][p.y]+=1;InitPic(map[p.x][p.y],p.x,p.y);flag=1;break;}
case 2:{if(map[p.x+2*a.x][p.y+2*a.y]==0||map[p.x+2*a.x][p.y+2*a.y]==4)
{map[p.x][p.y]-=1;map[p.x+a.x][p.y+a.y]=1;map[p.x+2*a.x][p.y+2*a.y]+=2;
InitPic(map[p.x][p.y],p.x,p.y);
InitPic(map[p.x+a.x][p.y+a.y],p.x+a.x,p.y+a.y);
InitPic(map[p.x+2*a.x][p.y+2*a.y],p.x+2*a.x,p.y+2*a.y);
p.x=p.x+a.x;p.y=p.y+a.y;flag=1;BoxMove[i]=1;}
else flag=0;
break;}
case 3:flag=0;break;
case 4:{map[p.x][p.y]-=1;InitPic(map[p.x][p.y],p.x,p.y);
p.x=p.x+a.x;p.y=p.y+a.y;
map[p.x][p.y]+=1;InitPic(map[p.x][p.y],p.x,p.y);flag=1;break;}
case 6:{if(map[p.x+2*a.x][p.y+2*a.y]==0||map[p.x+2*a.x][p.y+2*a.y]==4)
{map[p.x][p.y]-=1;map[p.x+a.x][p.y+a.y]=5;map[p.x+2*a.x][p.y+2*a.y]+=2;
InitPic(map[p.x][p.y],p.x,p.y);
InitPic(map[p.x+a.x][p.y+a.y],p.x+a.x,p.y+a.y);
InitPic(map[p.x+2*a.x][p.y+2*a.y],p.x+2*a.x,p.y+2*a.y);
p.x=p.x+a.x;p.y=p.y+a.y;flag=1;BoxMove[i]=1;}
else flag=0;
break;}
}
return flag;
}
void DrawWall(int i,int j)
{
DrawBack(i,j);
setfillstyle(9,1);
bar(STARTX+20*j-9,STARTY+20*i-9,STARTX+20*j+9,STARTY+20*i+9);
}
void DrawMan(int i,int j)
{
DrawBack(i,j);
setcolor(MANCOLOR);
circle(STARTX+20*j,STARTY+20*i,9);
arc(STARTX+20*j-3,STARTY+20*i-2,20,160,3);
arc(STARTX+20*j+4,STARTY+20*i-2,20,160,3);
arc(STARTX+20*j,STARTY+20*i-2,220,320,7);
}
void DrawBack(int i,int j)
{
setfillstyle(1,BKCOLOR);
bar(STARTX+20*j-9,STARTY+20*i-9,STARTX+20*j+9,STARTY+20*i+9);
}
void DrawObject(int i,int j)
{
DrawBack(i,j);
setcolor(OBJECTCOLOR);
line(STARTX+20*j-9,STARTY+20*i,STARTX+20*j+9,STARTY+20*i);
line(STARTX+20*j-9,STARTY+20*i-9,STARTX+20*j+9,STARTY+20*i+9);
line(STARTX+20*j-9,STARTY+20*i+9,STARTX+20*j+9,STARTY+20*i-9);
}
void DrawBox(int i,int j)
{
DrawBack(i,j);
setfillstyle(9,3);
bar(STARTX+20*j-9,STARTY+20*i-9,STARTX+20*j+9,STARTY+20*i+9);
}
void DrawMenu(int j)
{
int n;
char *s[4]={"1.Start Game","2.View Records","3.Delete Records","4.Exit Game"};
settextstyle(0,0,1);
setcolor(GREEN);
for(n=0;n<4;n++)
outtextxy(250,170+n*20,s[n]);
setcolor(RED);
outtextxy(250,170+j*20,s[j]);
}
void DrawStar()
{
int w,h,i,dotx,doty,color,maxcolor;
w=getmaxx();
h=getmaxy();
maxcolor=getmaxcolor();
for(i=0;i<STARNUM;i++)
{
s[i].x=1+random(w-1);
s[i].y=1+random(h-1);
s[i].c=random(maxcolor);
putpixel(s[i].x,s[i].y,s[i].c);
}
}
void ChangeStar()
{
int i,maxcolor;
maxcolor=getmaxcolor();
for(i=0;i<STARNUM;i++)
{
s[i].c=random(maxcolor);
putpixel(s[i].x,s[i].y,s[i].c);
}
}
int TimeCome()
{
static long tm, old;
tm=biostime(0,tm);
if(tm-old<TIMEINT) return 0;
else
{
old=tm; return 1;
}
}
int JudgeWin()
{
int n=0,i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(map[i][j]==6) n++;
if(n==BoxNum) return 1;
else return 0;
}
void InitMission1() /*第九關*/
{
int i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
map[i][j]=0;
for(i=0;i<=5;i++)
map[0][i]=3;
for(i=5;i<=7;i++)
{map[2][i]=map[i-1][1]=3;}
for(i=1;i<=4;i++)
{map[6][i]=map[5][i+3]=map[i][0]=3;}
map[3][7]=map[4][7]=map[1][5]=3;
for(i=2;i<=4;i++)
map[2][i]=2;
map[3][4]=map[4][5]=2;
for(i=2;i<=3;i++)
{map[3][i]=map[4][i]=4;}
map[4][4]=4;
p.x=3;p.y=5;
map[3][5]=1;
BoxNum=5;
}
void InitMission2()
{
int i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
map[i][j]=0;
for(i=1;i<=5;i++)
{map[0][i]=map[6][i]=3;}
for(i=2;i<=4;i++)
{map[1][i+3]=map[i][7]=map[i+2][5]=map[i][0]=3;}
map[1][1]=map[2][1]=map[5][0]=map[5][5]=map[4][6]=map[6][0]=3;
map[2][4]=map[3][3]=map[4][2]=map[4][3]=2;
map[2][3]=map[3][2]=map[3][4]=map[4][4]=4;
p.x=1;p.y=3;
map[1][3]=1;
BoxNum=4;
}
void ViewRecords()
{
FILE *fp;
int i;
setbkcolor(BKCOLOR);
cleardevice();
if((fp=fopen("record","r"))==NULL)
{
printf("\nerror on open file!");
getch();
exit(1);
}
gotoxy(1,1);
printf("\n\t\t\tRecord Information\n");
printf("Record-holder Achievement(s)\t Time(h:m:s)\t\tDate(y/m/d)");
for(i=0;i<MAX;i++)
{fseek(fp,i*sizeof(Record),0);
fread(&r[i],sizeof(Record),1,fp);
printf("\n%-10s\t%d\t\t %02d:%02d:%02d\t\t%02d/%02d/%02d",r[i].name,r[i].second,r[i].t.ti_hour,r[i].t.ti_min,r[i].t.ti_sec,r[i].d.da_year,r[i].d.da_mon,r[i].d.da_day);}
fclose(fp);
gotoxy(10,25);
printf("Press any key to return mainmenu...");
}
void DeleteRecords()
{
int i;
FILE *fp;
fp=fopen("record","w");
for(i=0;i<MAX;i++)
{
strcpy(r[i].name,"nameless");
r[i].second=0;
gettime(&r[i].t);
getdate(&r[i].d);
}
for(i=0;i<MAX;i++)
fwrite(&r[i],sizeof(Record),1,fp);
fclose(fp);
}
void JudgeRecord()
{
int i=MissionNum-1;
time(&t2);
if(r[i].second==0||difftime(t2,t1)<r[i].second)
{
gotoxy(10,3);printf("\t\tYou have broken the record");
r[i].second=difftime(t2,t1);
strcpy(r[i].name,name);
gettime(&r[i].t);
getdate(&r[i].d);
WriteRecord();
}
else
{gotoxy(10,3);printf("\t\tYou have pass this mission");}
gotoxy(10,4);
printf("\t\tpress any key continue...");
getch();
getch();
}
void WriteRecord()
{
FILE *fp;
int i=MissionNum-1;
fp=fopen("record","rt+");
fseek(fp,i*sizeof(Record),0);
fwrite(&r[MissionNum-1],sizeof(Record),1,fp);
fclose(fp);
}
void RegisterStep()
{
int i;
StepNum++;
i=(StepNum-1)%STEPMAX;
Step[i]=DirectionKey;
}
void ReverselyMove()
{
int i;
Add ad;
i=(StepNum-1)%STEPMAX;
if(Step[i]==0) return;
else
{
switch(Step[i])
{
case Key_Up:{ad.x=1;ad.y=0;MoveBack(ad);break;}
case Key_Down:{ad.x=-1;ad.y=0;MoveBack(ad);break;}
case Key_Left:{ad.x=0;ad.y=1;MoveBack(ad);break;}
case Key_Right:{ad.x=0;ad.y=-1;MoveBack(ad);break;}
}
StepNum--;Step[i]=0;BoxMove[i]=0;
}
}
void MoveBack(Add a) /*一定可以移動*/
{
int i=(StepNum-1)%STEPMAX;
if(BoxMove[i]==0)
{
map[p.x][p.y]-=1;InitPic(map[p.x][p.y],p.x,p.y);
p.x=p.x+a.x;p.y=p.y+a.y;
map[p.x][p.y]+=1;InitPic(map[p.x][p.y],p.x,p.y);
}
else if(BoxMove[i]==1)
{
map[p.x-a.x][p.y-a.y]-=2;InitPic(map[p.x-a.x][p.y-a.y],p.x-a.x,p.y-a.y);
map[p.x][p.y]+=1;InitPic(map[p.x][p.y],p.x,p.y);
p.x=p.x+a.x;p.y=p.y+a.y;
map[p.x][p.y]+=1;InitPic(map[p.x][p.y],p.x,p.y);
}
}
3. 求助啊,誰有有趣的c語言小程序,並且要有源代碼!!
一個貪吃蛇C源代碼,本人稍加優化,練手應當不錯。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <Windows.h>
#define WIDTH 78 //地圖的寬,x軸
#define HEIGHT 26 //地圖的高,y軸
int dire=3; //方向變數,初值為向「左」
int Flag=0; //判斷是否吃了食物的標志
int score=0; //玩家得分
struct foods{ int x;
int y;
}food; //結構體food有2個成員
struct snakes{int len;
int speed;
int x[100];
int y[100];
}snake; //結構體snake有4個成員
void gotoxy( int x,int y) //獲得句柄,才能控制游標移動
{ COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void gotoxy( int x,int y); //以下聲明要用到的幾個自編函數
void csh( );
void keyDown( );
void Move( );
void putFood( );
int pdOver( );
int main( ) //主函數
{ csh( );
while(1)
{ keyDown( );
Move( );
putFood( );
if(pdOver( ))
{system(「cls」);
gotoxy(WIDTH/2+1,HEIGHT/2);
printf(「游戲結束!T__T」);
gotoxy(WIDTH/2+1,HEIGHT/2+1);
printf(「玩家總分:%d分」,score);
getch( );
break; }
Sleep(snake.speed);
}
return 0;
}
void csh( ) //初始化界面
{ int i;
gotoxy(0,0);
CONSOLE_CURSOR_INFO cursor_info={1,0}; //游標值設為0就隱藏了
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
for(i=0;i<=WIDTH;i=i+2) //橫坐標要為偶數,因為這里要列印的字元佔2個位置
{ gotoxy(i,0); //列印上邊框
printf("■");
gotoxy(i,HEIGHT); //列印下邊框
printf("■");
}
for(i=1;i<HEIGHT;i++)
{ gotoxy(0,i); //列印左邊框
printf("■");
gotoxy(WIDTH,i); //列印右邊框
printf("■");
}
while(1)
{ srand((unsigned int)time(NULL)); //設定種子為當前時間
food.x=rand()%(WIDTH-4)+2;
food.y=rand()%(HEIGHT-2)+1;
if(food.x%2==0)break;
}
gotoxy(food.x,food.y); //到食物坐標處列印初試食物
printf("●");
snake.len=3; //蛇身長
snake.speed=350; //刷新蛇的時間,即是移動速度
snake.x[0]=WIDTH/2+1; //蛇頭橫坐標為偶數
snake.y[0]=HEIGHT/2; //蛇頭縱坐標
gotoxy(snake.x[0], snake.y[0]); //列印蛇頭
printf("■");
for(i=1;i<snake.len;i++)
{ snake.x[i]=snake.x[i-1]+2;
snake.y[i]=snake.y[i-1];
gotoxy(snake.x[i],snake.y[i]); //列印蛇身
printf("■");
}
return;
}
void keyDown( ) //按鍵操作
{ int key;
if(kbhit( )) //如有按鍵輸入才執行下面操作
{ key=getch( );
if(key==224) //值為224表示按下了方向鍵,下面要再次獲取鍵值
{ key=getch( );
if(key==72&&dire!=2)dire=1; //72為向上
if(key==80&&dire!=1)dire=2; //80為向下
if(key==75&&dire!=4)dire=3; //75為向左
if(key==77&&dire!=3)dire=4; //77為向右
}
if(key==13)
{ while(1) if((key=getch( ))==13) break; } //13為回車鍵,這兒用來暫停
}
}
void Move( ) //蛇移動一節
{ if(Flag==0) //如沒吃食物,才執行下面操作擦掉蛇尾
{ gotoxy(snake.x[snake.len-1],snake.y[snake.len-1]);
printf(" ");
}
int i;
for (i = snake.len - 1; i > 0; i--) //從蛇尾起每節存儲前一節坐標值(蛇頭除外)
{ snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
switch (dire) //以下判斷蛇頭該往哪個方向移動,並獲取最新坐標值
{ case 1: snake.y[0]--; break;
case 2: snake.y[0]++; break;
case 3: snake.x[0]-=2; break;
case 4: snake.x[0]+=2; break;
}
gotoxy(snake.x[0], snake.y[0]); //列印蛇頭
printf("■");
if (snake.x[0] == food.x && snake.y[0] == food.y) //如吃到食物執行以下操作
{ snake.len++; score += 50; snake.speed -= 5; Flag = 1;}
else Flag = 0;
if(snake.speed<160) snake.speed= snake.speed+5; //作弊碼,不讓速度無限加快
}
void putFood( ) //投放食物
{ if(Flag == 1) //如吃到食物才執行以下操作,生成另一個食物
{ while (1)
{ int i,n= 1;
srand((unsigned int)time(NULL)); //設定當前時間,接下產生食物坐標值
food.x = rand( ) % (WIDTH - 4) + 2;
food.y = rand( ) % (HEIGHT - 2) + 1;
for (i = 0; i < snake.len; i++) //隨機生成的食物不能在蛇的身體上
{ if (food.x == snake.x[i] &&food.y == snake.y[i])
{ n= 0; break;}
}
if (n && food.x % 2 == 0) break; //n不為0且橫坐標為偶數,食物坐標取值成功
}
gotoxy(food.x, food.y); //游標到取得的坐標處列印食物
printf("●");
}
return;
}
int pdOver( ) //判斷游戲是否結束
{ int i;
gotoxy(2,HEIGHT+1); //以下列印一些其它信息
printf(「暫停鍵:Enter.」);
gotoxy(2,HEIGHT+2);
printf(「游戲得分:%d」,score);
if (snake.x[0] == 0 || snake.x[0] == WIDTH) return 1; //蛇頭觸碰左右邊界
if (snake.y[0] == 0 || snake.y[0] == HEIGHT) return 1; //蛇頭觸碰上下邊界
for (i = 1; i < snake.len; i++)
{ if (snake.x[0] == snake.x[i] && snake.y[0] == snake.y[i]) return 1; } //蛇頭觸碰自身
return 0;
}
4. 誰有能在vc++6.0上運行的c語言小游戲代碼
最基礎的貪吃蛇的代碼
#include<stdio.h>
#include<windows.h>//基本型態定義。支援型態定義函數。使用者界面函數 圖形裝置界面函數。
#include<conio.h> //用戶通過按鍵盤產生的對應操作 (控制台)
#include<stdlib.h>
#include<time.h> //日期和時間頭文件
#define LEN 30
#define WID 25
int Snake[LEN][WID] = {0}; //數組的元素代表蛇的各個部位
char Sna_Hea_Dir = 'a';//記錄蛇頭的移動方向
int Sna_Hea_X, Sna_Hea_Y;//記錄蛇頭的位置
int Snake_Len = 3;//記錄蛇的長度
clock_t Now_Time;//記錄當前時間,以便自動移動
int Wait_Time ;//記錄自動移動的時間間隔
int Eat_Apple = 1;//吃到蘋果表示為1
int Level ;
int All_Score = -1;
int Apple_Num = -1;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //獲取標准輸出的句柄 <windows.h>
//句柄 :標志應用程序中的不同對象和同類對象中的不同的實例 方便操控,
void gotoxy(int x, int y)//設置游標位置
{
COORD pos = {x,y}; //定義一個字元在控制台屏幕上的坐標POS
SetConsoleCursorPosition(hConsole, pos); //定位游標位置的函數<windows.h>
}
void Hide_Cursor()//隱藏游標 固定函數
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(hConsole, &cursor_info);
}
void SetColor(int color)//設置顏色
{
SetConsoleTextAttribute(hConsole, color);
//是API設置字體顏色和背景色的函數 格式:SetConsoleTextAttribute(句柄,顏色);
}
void Print_Snake()//列印蛇頭和蛇的脖子和蛇尾
{
int iy, ix, color;
for(iy = 0; iy < WID; ++iy)
for(ix = 0; ix < LEN; ++ix)
{
if(Snake[ix][iy] == 1)//蛇頭
{
SetColor(0xf); //oxf代表分配的內存地址 setcolor:34行自定義設置顏色的函數
gotoxy(ix*2, iy);
printf("※");
}
if(Snake[ix][iy] == 2)//蛇的脖子
{
color = rand()%15 + 1; //rand()函數是產生隨機數的一個隨機函數。C語言里還有 srand()函數等。
//頭文件:stdlib.h
if(color == 14)
color -= rand() % 13 + 1; //變色
SetColor(color);
gotoxy(ix*2, iy);
printf("■");
}
if(Snake[ix][iy] == Snake_Len)
{
gotoxy(ix*2, iy);
SetColor(0xe);
printf("≈");
}
}
}
void Clear_Snake()//擦除貪吃蛇
{
int iy, ix;
for(iy = 0; iy < WID; ++iy)
for(ix = 0; ix < LEN; ++ix)
{
gotoxy(ix*2, iy);
if(Snake[ix][iy] == Snake_Len)
printf(" ");
}
}
void Rand_Apple()//隨機產生蘋果
{
int ix, iy;
do
{
ix = rand() % LEN;
iy = rand() % WID;
}while(Snake[ix][iy]);
Snake[ix][iy] = -1;
gotoxy(ix*2, iy);
printf("⊙");
Eat_Apple = 0;
}
void Game_Over()//蛇死掉了
{
gotoxy(30, 10);
printf("Game Over");
Sleep(3000);
system("pause > nul");
exit(0);
}
void Move_Snake()//讓蛇動起來
{
int ix, iy;
for(ix = 0; ix < LEN; ++ix)//先標記蛇頭
for(iy = 0; iy < WID; ++iy)
if(Snake[ix][iy] == 1)
{
switch(Sna_Hea_Dir)//根據新的蛇頭方向標志蛇頭
{
case 'w':
if(iy == 0)
Game_Over();
else
Sna_Hea_Y = iy - 1;
Sna_Hea_X = ix;
break;
case 's':
if(iy == (WID -1))
Game_Over();
else
Sna_Hea_Y = iy + 1;
Sna_Hea_X = ix;
break;
case 'a':
if(ix == 0)
Game_Over();
else
Sna_Hea_X = ix - 1;
Sna_Hea_Y = iy;
break;
case 'd':
if(ix == (LEN - 1))
Game_Over();
else
Sna_Hea_X = ix + 1;
Sna_Hea_Y = iy;
break;
default:
break;
}
}
if(Snake[Sna_Hea_X][Sna_Hea_Y]!=1&&Snake[Sna_Hea_X][Sna_Hea_Y]!=0&&Snake[Sna_Hea_X][Sna_Hea_Y]!=-1)
Game_Over();
if(Snake[Sna_Hea_X][Sna_Hea_Y] < 0)//吃到蘋果
{
++Snake_Len;
Eat_Apple = 1;
}
for(ix = 0; ix < LEN; ++ix)//處理蛇尾
for(iy = 0; iy < WID; ++iy)
{
if(Snake[ix][iy] > 0)
{
if(Snake[ix][iy] != Snake_Len)
Snake[ix][iy] += 1;
else
Snake[ix][iy] = 0;
}
}
Snake[Sna_Hea_X][Sna_Hea_Y] = 1;//處理蛇頭
}
void Get_Input()//控制蛇的移動方向
{
if(kbhit())
{
switch(getch())
{
case 87:
Sna_Hea_Dir = 'w';
break;
case 83:
Sna_Hea_Dir = 's';
break;
case 65:
Sna_Hea_Dir = 'a';
break;
case 68:
Sna_Hea_Dir = 'd';
break;
default:
break;
}
}
if(clock() - Now_Time >= Wait_Time)//蛇到時間自動行走
{
Clear_Snake();
Move_Snake();
Print_Snake();
Now_Time = clock();
}
}
void Init()//初始化
{
system("title 貪吃毛毛蛇");
system("mode con: cols=80 lines=25");
Hide_Cursor();
gotoxy(61, 4);
printf("You Score:");
gotoxy(61, 6);
printf("You Level:");
gotoxy(61, 8);
printf("The Lenght:");
gotoxy(61, 10);
printf("The Speed:");
gotoxy(61, 12);
printf("Apple Num:");
int i;
for(i = 0; i < Snake_Len; ++i)//生成蛇
Snake[10+i][15] = i+1;
int iy, ix;//列印蛇
for(iy = 0; iy < WID; ++iy)
for(ix = 0; ix < LEN; ++ix)
{
if(Snake[ix][iy])
{
SetColor(Snake[ix][iy]);
gotoxy(ix*2, iy);
printf("■");
}
}
}
void Pri_News()//列印信息
{
SetColor(0xe);
gotoxy(73,4);
All_Score += Level;
printf("%3d", All_Score);
gotoxy(73, 6);
printf("%3d", Level);
gotoxy(73, 8);
printf("%3d",Snake_Len);
gotoxy(73, 10);
printf("0.%3ds", Wait_Time/10);
gotoxy(73, 12);
printf("%d", Apple_Num);
}
void Lev_Sys()//等級系統
{
if(((Apple_Num-1) / 10) == Level)
{
++Level;
if(Wait_Time > 50)
Wait_Time -= 50;
else
if(Wait_Time > 10)
Wait_Time -= 10;
else
Wait_Time -= 1;
}
}
int main(void)
{
Init();
srand((unsigned)time(NULL));//設置隨機數的種子
Now_Time = clock();
int speed1=1000,speed2,a;
printf("\n");
printf("請輸入你想要的速度\n");
scanf("%d",&speed2);
Level=1;
Wait_Time=speed1-speed2;
printf("請輸入你想要的蘋果數\n");
scanf("%d",&a);
while(a--)
Rand_Apple();
while(1)
{
if(Eat_Apple)
{
++Apple_Num;
Rand_Apple();
Lev_Sys();
Pri_News();
}
Get_Input();
Sleep(10);
}
return 0;
}
5. c語言推箱子代碼
Here
you
are!
編譯通過。
/*
推箱子游戲
*/
#include
<dos.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<ctype.h>
#include
<conio.h>
#include
<bios.h>
#include
<alloc.h>
/*
定義二維數組ghouse來記錄屏幕上各點的狀態,
其中:0表示什麼都沒有,'b'表示箱子,'w'表示牆壁,'m'表示目的地,'i'表示箱子在目的地。
*/
char
ghouse[20][20];
/*
以下函數為直接寫屏函數,很酷的函數哦!是我朋友告訴我的。
*/
char
far
*screen=(char
far*
)0xb8000000;
void
putchxy(int
y,int
x,char
ch,char
fc,char
bc)
{
screen[(x*160)+(y<<1)+0]=ch;
screen[(x*160)+(y<<1)+1]=(bc*16)+fc;
}
/*
定義判斷是否勝利的數據結構
*/
typedef
struct
winer
{
int
x,y;
struct
winer
*p;
}winer;
/*
箱子位置的數據結構
*/
typedef
struct
boxs
{
int
x,y;
struct
boxs
*next;
}boxs;
/*
在特定的坐標上畫牆壁並用數組記錄狀態的函數
*/
void
printwall(int
x,int
y)
{
putchxy(y-1,x-1,219,MAGENTA,BLACK);
ghouse[x][y]='w';
}
/*
在特定的坐標上畫箱子並用數組記錄狀態的函數
*/
void
printbox(int
x,int
y)
{
putchxy(y-1,x-1,10,WHITE,BLACK);
ghouse[x][y]='b';
}
/*
在特定的坐標上畫目的地並用數組記錄狀態的函數
*/
void
printwhither1(int
x,int
y,winer
**win,winer
**pw)
{
winer
*qw;
putchxy(y-1,x-1,'*',YELLOW,BLACK);
ghouse[x][y]='m';
if(*win==NULL)
{
*win=*pw=qw=(winer*
)malloc(sizeof(winer));
(*pw)->x=x;(*pw)->y=y;(*pw)->p=NULL;
}
else
{
qw=(winer*
)malloc(sizeof(winer));
qw->x=x;qw->y=y;(*pw)->p=qw;(*pw)=qw;qw->p=NULL;
}
}
/*
在特定的坐標上畫目的地並用數組記錄狀態的函數
*/
void
printwhither(int
x,int
y)
{
putchxy(y-1,x-1,'*',YELLOW,BLACK);
ghouse[x][y]='m';
}
/*
在特定的坐標上畫人的函數
*/
void
printman(int
x,int
y)
{
gotoxy(y,x);
_AL=02;_CX=01;_AH=0xa;
geninterrupt(0x10);
}
/*
在特定的坐標上畫箱子在目的地上並用數組記錄狀態的函數
*/
void
printboxin(int
x,int
y)
{
putchxy(y-1,x-1,10,YELLOW,BLACK);
ghouse[x][y]='i';
}
/*
初始化函數,初始化數組和屏幕
*/
void
init()
{
int
i,j;
system("cls");
for(i=0;i<20;i++)
for(j=0;j<20;j++)
ghouse[i][j]=0;
_AL=3;
_AH=0;
geninterrupt(0x10);
gotoxy(40,4);
printf("Welcome
to
push
box
world!");
gotoxy(40,6);
printf("Press
up,down,left,right
to
play.");
gotoxy(40,8);
printf("Press
Esc
to
quit
it.");
gotoxy(40,10);
printf("Press
space
to
reset
the
game.");
gotoxy(40,12);
printf("April
30th
2004.");
}
/*
第一關的圖象初始化
*/
winer
*inithouse1()
{
int
x,y;
winer
*win=NULL,*pw;
gotoxy(8,2);
printf("Level
No.1");
for(x=1,y=5;y<=9;y++)
printwall(x+4,y+10);
for(y=5,x=2;x<=5;x++)
printwall(x+4,y+10);
for(y=9,x=2;x<=5;x++)
printwall(x+4,y+10);
for(y=1,x=3;x<=8;x++)
printwall(x+4,y+10);
for(x=3,y=3;x<=5;x++)
printwall(x+4,y+10);
for(x=5,y=8;x<=9;x++)
printwall(x+4,y+10);
for(x=7,y=4;x<=9;x++)
printwall(x+4,y+10);
for(x=9,y=5;y<=7;y++)
printwall(x+4,y+10);
for(x=8,y=2;y<=3;y++)
printwall(x+4,y+10);
printwall(5+4,4+10);
printwall(5+4,7+10);
printwall(3+4,2+10);
printbox(3+4,6+10);
printbox(3+4,7+10);
printbox(4+4,7+10);
printwhither1(4+4,2+10,&win,&pw);
printwhither1(5+4,2+10,&win,&pw);
printwhither1(6+4,2+10,&win,&pw);
printman(2+4,8+10);
return
win;
}
/*
第三關的圖象初始化
*/
winer
*inithouse3()
{int
x,y;
winer
*win=NULL,*pw;
gotoxy(8,3);
printf("Level
No.3");
for(x=1,y=2;y<=8;y++)
printwall(x+4,y+10);
for(x=2,y=2;x<=4;x++)
printwall(x+4,y+10);
for(x=4,y=1;y<=3;y++)
printwall(x+4,y+10);
for(x=5,y=1;x<=8;x++)
printwall(x+4,y+10);
for(x=8,y=2;y<=5;y++)
printwall(x+4,y+10);
for(x=5,y=5;x<=7;x++)
printwall(x+4,y+10);
for(x=7,y=6;y<=9;y++)
printwall(x+4,y+10);
for(x=3,y=9;x<=6;x++)
printwall(x+4,y+10);
for(x=3,y=6;y<=8;y++)
printwall(x+4,y+10);
printwall(2+4,8+10);
printwall(5+4,7+10);
printbox(6+4,3+10);
printbox(4+4,4+10);
printbox(5+4,6+10);
printwhither1(2+4,5+10,&win,&pw);
printwhither1(2+4,6+10,&win,&pw);
printwhither1(2+4,7+10,&win,&pw);
printman(2+4,4+10);
return
win;
}
/*
第二關的圖象初始化
*/
winer
*inithouse2()
{int
x,y;
winer
*win=NULL,*pw;
gotoxy(8,2);
printf("Level
No.2");
for(x=1,y=4;y<=7;y++)
printwall(x+4,y+10);
for(x=2,y=2;y<=4;y++)
printwall(x+4,y+10);
for(x=2,y=7;x<=4;x++)
printwall(x+4,y+10);
for(x=4,y=1;x<=8;x++)
printwall(x+4,y+10);
for(x=8,y=2;y<=8;y++)
printwall(x+4,y+10);
for(x=4,y=8;x<=8;x++)
printwall(x+4,y+10);
for(x=4,y=6;x<=5;x++)
printwall(x+4,y+10);
for(x=3,y=2;x<=4;x++)
printwall(x+4,y+10);
for(x=4,y=4;x<=5;x++)
printwall(x+4,y+10);
printwall(6+4,3+10);
printbox(3+4,5+10);
printbox(6+4,6+10);
printbox(7+4,3+10);
printwhither1(5+4,7+10,&win,&pw);
printwhither1(6+4,7+10,&win,&pw);
printwhither1(7+4,7+10,&win,&pw);
printman(2+4,6+10);
return
win;
}
/*
第四關的圖象初始化
*/
winer
*inithouse4()
{int
x,y;
winer
*win=NULL,*pw;
gotoxy(8,2);
printf("Level
No.4");
for(x=1,y=1;y<=6;y++)
printwall(x+4,y+10);
for(x=2,y=7;y<=8;y++)
printwall(x+4,y+10);
for(x=2,y=1;x<=7;x++)
printwall(x+4,y+10);
for(x=7,y=2;y<=4;y++)
printwall(x+4,y+10);
for(x=6,y=4;y<=9;y++)
printwall(x+4,y+10);
for(x=3,y=9;x<=5;x++)
printwall(x+4,y+10);
for(x=3,y=3;y<=4;y++)
printwall(x+4,y+10);
printwall(3+4,8+10);
printbox(3+4,5+10);
printbox(4+4,4+10);
printbox(4+4,6+10);
printbox(5+4,5+10);
printbox(5+4,3+10);
printwhither1(3+4,7+10,&win,&pw);
printwhither1(4+4,7+10,&win,&pw);
printwhither1(5+4,7+10,&win,&pw);
printwhither1(4+4,8+10,&win,&pw);
printwhither1(5+4,8+10,&win,&pw);
printman(2+4,2+10);
return
win;
}
/*
移動在空地上的箱子到空地上
*/
movebox(int
x,int
y,char
a)
{switch(a)
{
case
'u':ghouse[x-1][y]=0;printf("
");
printbox(x-2,y);printman(x-1,y);
ghouse[x-2][y]='b';break;
case
'd':ghouse[x+1][y]=0;printf("
");
printbox(x+2,y);printman(x+1,y);
ghouse[x+2][y]='b';break;
case
'l':ghouse[x][y-1]=0;printf("
");
printbox(x,y-2);printman(x,y-1);
ghouse[x][y-2]='b';break;
case
'r':ghouse[x][y+1]=0;printf("
");
printbox(x,y+2);printman(x,y+1);
ghouse[x][y+2]='b';break;
default:
break;
}
}
/*
移動在目的地上的箱子到空地上
*/
moveinbox(int
x,int
y,char
a)
{switch(a)
{
case
'u':ghouse[x-1][y]='m';printf("
");
printbox(x-2,y);printman(x-1,y);
ghouse[x-2][y]='b';break;
case
'd':ghouse[x+1][y]='m';printf("
");
printbox(x+2,y);printman(x+1,y);
ghouse[x+2][y]='b';break;
case
'l':ghouse[x][y-1]='m';printf("
");
printbox(x,y-2);printman(x,y-1);
ghouse[x][y-2]='b';break;
case
'r':ghouse[x][y+1]='m';printf("
");
printbox(x,y+2);printman(x,y+1);
ghouse[x][y+2]='b';break;
default:
break;
}
}
/*
移動在空地上的箱子到目的地上
*/
moveboxin(int
x,int
y,char
a)
{switch(a)
{
case
'u':ghouse[x-1][y]=0;printf("
");
printboxin(x-2,y);printman(x-1,y);
ghouse[x-2][y]='i';break;
case
'd':ghouse[x+1][y]=0;printf("
");
printboxin(x+2,y);printman(x+1,y);
ghouse[x+2][y]='i';break;
case
'l':ghouse[x][y-1]=0;printf("
");
printboxin(x,y-2);printman(x,y-1);
ghouse[x][y-2]='i';break;
case
'r':ghouse[x][y+1]=0;printf("
");
printboxin(x,y+2);printman(x,y+1);
ghouse[x][y+2]='i';break;
default:
break;
}
}
/*
移動在目的地上的箱子到目的地
*/
moveinboxin(int
x,int
y,char
a)
{switch(a)
{
case
'u':ghouse[x-1][y]='m';printf("
");
printboxin(x-2,y);printman(x-1,y);
ghouse[x-2][y]='i';break;
case
'd':ghouse[x+1][y]='m';printf("
");
printboxin(x+2,y);printman(x+1,y);
ghouse[x+2][y]='i';break;
case
'l':ghouse[x][y-1]='m';printf("
");
printboxin(x,y-2);printman(x,y-1);
ghouse[x][y-2]='i';break;
case
'r':ghouse[x][y+1]='m';printf("
");
printboxin(x,y+2);printman(x,y+1);
ghouse[x][y+2]='i';break;
default:
break;
}
}
/*
判斷特定的坐標上的狀態
*/
int
judge(int
x,int
y)
{int
i;
switch(ghouse[x][y])
{
case
0:
i=1;break;
case
'w':
i=0;break;
case
'b':
i=2;break;
case
'i':
i=4;break;
case
'm':
i=3;break;
default:
break;
}
return
i;
}
/*
處理按下鍵盤後,人物移動的主函數
*/
move(int
x,int
y,char
a)
{switch(a)
{
case
'u':if(!judge(x-1,y))
{gotoxy(y,x);break;}
else
if(judge(x-1,y)==1||judge(x-1,y)==3)
{if(judge(x,y)==3)
{
printwhither(x,y);printman(x-1,y);break;}
else
{printf("
");printman(x-1,y);break;}
}
else
if(judge(x-1,y)==2)
{
if(judge(x-2,y)==1)
{movebox(x,y,'u');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y,x-1);
}
else
if(judge(x-2,y)==3)
{
moveboxin(x,y,'u');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y,x-1);
}
else
gotoxy(y,x);
break;
}
else
if(judge(x-1,y)==4)
{
if(judge(x-2,y)==1)
{moveinbox(x,y,'u');
if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x-1);
}
else
if(judge(x-2,y)==3)
{
moveinboxin(x,y,'u');
if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x-1);
}
else
gotoxy(y,x);
break;
}
case
'd':if(!judge(x+1,y))
{gotoxy(y,x);break;}
else
if(judge(x+1,y)==1||judge(x+1,y)==3)
{if(judge(x,y)==3)
{
printwhither(x,y);printman(x+1,y);break;}
else
{printf("
");printman(x+1,y);break;}
}
else
if(judge(x+1,y)==2)
{
if(judge(x+2,y)==1)
{movebox(x,y,'d');
if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x+1);
}
else
if(judge(x+2,y)==3)
{moveboxin(x,y,'d');
if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x+1);
}
else
gotoxy(y,x);
break;
}
else
if(judge(x+1,y)==4)
{
if(judge(x+2,y)==1)
{moveinbox(x,y,'d');
if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x+1);
}
else
if(judge(x+2,y)==3)
{moveinboxin(x,y,'d');
if(judge(x,y)==3)
printwhither(x,y);gotoxy(y,x+1);
}
else
gotoxy(y,x);
break;
}
case
'l':if(!judge(x,y-1))
{gotoxy(y,x);break;}
else
if(judge(x,y-1)==1||judge(x,y-1)==3)
{if(judge(x,y)==3)
{
printwhither(x,y);printman(x,y-1);break;}
else
{printf("
");printman(x,y-1);break;}
}
else
if(judge(x,y-1)==2)
{
if(judge(x,y-2)==1)
{movebox(x,y,'l');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y-1,x);
}
else
if(judge(x,y-2)==3)
{moveboxin(x,y,'l');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y-1,x);
}
else
gotoxy(y,x);
break;
}
else
if(judge(x,y-1)==4)
{
if(judge(x,y-2)==1)
{moveinbox(x,y,'l');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y-1,x);
}
else
if(judge(x,y-2)==3)
{moveinboxin(x,y,'l');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y-1,x);
}
else
gotoxy(y,x);
break;
}
case
'r':if(!judge(x,y+1))
{gotoxy(y,x);break;}
else
if(judge(x,y+1)==1||judge(x,y+1)==3)
{if(judge(x,y)==3)
{printwhither(x,y);printman(x,y+1);break;}
else
{printf("
");printman(x,y+1);break;}
}
else
if(judge(x,y+1)==2)
{
if(judge(x,y+2)==1)
{movebox(x,y,'r');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y+1,x);
}
else
if(judge(x,y+2)==3)
{moveboxin(x,y,'r');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y+1,x);
}
else
gotoxy(y,x);
break;
}
else
if(judge(x,y+1)==4)
{
if(judge(x,y+2)==1)
{moveinbox(x,y,'r');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y+1,x);
}
else
if(judge(x,y+2)==3)
{moveinboxin(x,y,'r');
if(judge(x,y)==3)
printwhither(x,y);
gotoxy(y+1,x);
}
else
gotoxy(y,x);
break;
}
default:
break;
}
}
/*
按下空格鍵後,回到本關開頭的函數
*/
void
reset(int
i)
{switch(i)
{
case
0:
init();
inithouse1();break;
case
1:
init();
inithouse2();break;
case
2:
init();
inithouse3();break;
case
3:
init();
inithouse4();break;
default:break;
}
}
/*
主函數main
*/
main()
{int
key,x,y,s,i=0;
winer
*win,*pw;
_AL=3;_AH=0;
geninterrupt(0x10);
init();
win=inithouse1();
do{
_AH=3;
geninterrupt(0x10);
x=_DH+1;y=_DL+1;
while(bioskey(1)==0);
key=bioskey(0);
switch(key)
{
case
0x4800:move(x,y,'u');break;
/*
按下向上鍵後
*/
case
0x5000:move(x,y,'d');break;
/*
按下向下鍵後
*/
case
0x4b00:move(x,y,'l');break;
/*
按下向左鍵後
*/
case
0x4d00:move(x,y,'r');break;
/*
按下向右鍵後
*/
case
0x3920:reset(i);break;
/*
按下空格鍵後
*/
default:break;
}
s=0;
pw=win;
while(pw)
{
if(ghouse[pw->x][pw->y]=='m')
s++;
pw=pw->p;
}
if(s==0)
{
free(win);
gotoxy(25,2);
printf("Congratulate!
You
have
passed
Level
%d!",i+1);
getch();
i++;
switch(i)
{
case
1:
init();
win=inithouse2();break;
case
2:
init();
win=inithouse3();break;
case
3:
init();
win=inithouse4();break;
case
4:
gotoxy(8,14);
printf("Great!
You
have
passed
all
the
levels!
Press
any
key
to
quit!");
key=0x011b;getch();break;
default:
break;
}
}
}while(key!=0x011b);
_AL=3;
_AH=0;
geninterrupt(0x10);
}
6. 幫我搞個完整的C語言推箱子程序好嗎
#include"stdio.h"
#include"bios.h"
#define LEFT 75
#define RIGHT 77
#define UPPER 72
#define DOWN 80
#define ESC 27
struct Boxss /*定義箱子結構體,其中包含宴握坐標屬性*/
{
int x,y;
};
union keyboard /*定義讀取鍵盤碼的共用體類型*/
{
unsigned int iKeyInfo;
char chKeyBit[2];
};
int fnGetKey(void) /*定義讀取鍵盤碼的函數*/
{
union keyboard uniKey1; /*定義讀取鍵盤碼的共用體變數*/
while(bioskey(1)==0); /*檢測用戶是否按鍵*/
uniKey1.iKeyInfo=bioskey(0); /*讀取按鍵信息*/
return(uniKey1.chKeyBit[0]==0?uniKey1.chKeyBit[1]:uniKey1.chKeyBit[0]); /*返回ASCII碼或擴培老充碼*/
}
void main()
{
int iKey,x=11,y=6,tx=11,ty=6; /*x,y為人物移動後坐標,tx,ty為人物移動前坐標*/
struct Boxss Box[4]; /*定義箱子數量*/
int chMap[10][10]={ /*用二維數組定義地圖*/
{0,0,0,0,0,0,0,0,0,0}, /*0表示牆1表示路2表示目標*/
{0,1,0,0,0,0,1,1,1,0},
{0,1,0,2,0,0,1,0,1,0},
{0,1,0,1,0,0,1,0,1,0},
{0,1,1,1,0,0,1,0,1,0},
{0,1,0,0,0,0,1,0,1,0},
{0,1,1,1,1,1,1,0,1,0},
{0,1,0,1,0,0,0,0,2,0},
{0,2,0,1,1,1,1,2,0,0},
{0,0,0,0,0,0,0,0,0,0},
};
int i,j;
Box[0].x=13; /*定義箱子的坐標屬性*/
Box[1].x=11;
Box[2].x=14;
Box[3].x=18;
Box[0].y=8;
Box[1].y=7;
Box[2].y=13;
Box[3].y=7;
while(1) /*反復進行求移動的坐標運算*/
{
for(i=0;i<10;i++) /*輸出新地圖(刷新地圖)*/
{
gotoxy(10,5+i);
for(j=0;j<10;j++)
{
if(chMap[i][j]==0)
printf("#");
if(chMap[i][j]==1)
printf(" ");
if(chMap[i][j]==2)
printf("X");
}
}
j=0; /*判斷是否所有箱子都在目標坐標上*/
for(i=0;i<4;i++)
if(chMap[Box[i].y-5][Box[i].x-10]==2)
j++;
if(j==4) /*如果所有箱子都就位輸出"晌中慶YOU WIN!"退出*/
{
clrscr();
printf("You Win!");
break;
}
for(i=0;i<4;i++) /*在起始(或移動後)的坐標輸出箱子*/
{
gotoxy(Box[i].x,Box[i].y);
printf("0");
}
gotoxy(x,y); /*在起始(或移動後)的坐標輸出人*/
printf("*\b");
tx=x; /*記錄本次移動前的坐標*/
ty=y;
iKey=fnGetKey();
if(iKey==LEFT&&chMap[y-5][x-1-10]!=0) /*按讀取的按鍵信息改變坐標如果改變的坐標和牆(0)重合則不改變*/
x--;
if(iKey==RIGHT&&chMap[y-5][x+1-10]!=0)
x++;
if(iKey==UPPER&&chMap[y-1-5][x-10]!=0)
y--;
if(iKey==DOWN&&chMap[y+1-5][x-10]!=0)
y++; /*輸入ESC退出並輸出"YOU LOST"*/
if(iKey==ESC)
{
clrscr();
printf("You Lost");
break;
}
for(i=0;i<4;i++) /*如果移動後的人的坐標與箱子坐標重合,則改變箱子坐標向前一格*/
if(Box[i].x==x&&Box[i].y==y)
{
Box[i].x+=(x-tx);
Box[i].y+=(y-ty);
if(chMap[Box[i].y-5][Box[i].x-10]==0) /*如果移動後的箱子坐標會出現在牆上,則使箱子坐標和人坐標都返回移動前的值*/
{
Box[i].x-=(x-tx);
Box[i].y-=(y-ty);
x=tx;
y=ty;
}
break;
}
clrscr();
}
getch();
}
7. 簡單的搬運工C語言代碼
程序在tc3.0下編譯通過. 回車鍵選關,我只做了兩關, r鍵重新開始 p鍵悔步,只能悔五步啊.不過你可以改#define STEPMAX的值 #define MAX 2 /*游戲中總關數*/#define STARTX 180#define STARTY 80#define ...
程序在tc3.0下編譯通過. 回車鍵
選關,我只做了兩關, r鍵重新開始
p鍵悔步,只能悔五步啊.不過你可以
改#define STEPMAX的值
#define MAX 2 /*游戲中總關數*/
#define STARTX 180
#define STARTY 80
#define BKCOLOR BLACK
#define MANCOLOR RED
#define OBJECTCOLOR YELLOW
#define TIMEINT 2
#define STARNUM 300
#define STEPMAX 5
#include<sing.h>
#include<bios.h>
#include<sio.h>
#include<dos.h>
#include<graphics.h>
#include<conio.h>
#include<time.h>
#include<slib.h>
#include<sio.h>
#define Key_R 0x1372
#define Key_Up 0x4800
#define Key_Enter 0x1c0d
#define Key_Down 0x5000
#define Key_P 0x1970
#define Key_Esc 0x11b
#define Key_Right 0x4d00
#define Key_Left 0x4b00
typedef suct star
{
int x;
int y;
int c;
}Star;
Star s;
typedef suct record
{
char name;
int second;
suct time t;
suct date d;
}Record;
Record r;
typedef suct c
{
int x;
int y;
}Add;
typedef suct a
{
int x;
int y;
}Player;
Player p;
char name; /*進入游戲時記錄玩家的姓名*/
time_t t1,t2; /*游戲結束時的時間*/
int Ide; /*開始進入游戲時,選擇菜單時返的功能號(1,2,3)*/
int MissionNum; /*玩家正在玩的關數*/
int BoxNum; /*目的地的個數*/
int Key; /*玩家按鍵*/
int map; /*地圖.(空=0),(人=1),(箱子=2),(牆=3),(目的地=4),(人 目的地=5),(箱子 目的地=6)*/
int StepNum;
int DirectionKey; /*用來表示方向鍵最後一次按鍵*/
int BoxMove;
int Step;
/*函數定義*/
void InputName();
void Init();
void MainMenu();
void JudgeRecord();
void WriteRecord();
void JudgeIde();
void DrawMenu(int );
void Game();
void InitMission(int );
void NextMission();
void InitPic(int ,int ,int );
int Move(Add );
void DrawWall(int ,int );
void DrawBack(int ,int );
void DrawBox(int ,int );
void DrawObject(int ,int );
void DrawMan(int ,int );
void DrawStar();
int JudgeWin();
void InitMission1();
void InitMission2();
int TimeCome();
void ChangeStar();
void InputName();
void ViewRecords();
void DeleteRecords();
void RegisterStep();
void ReverselyMove();
void MoveBack(Add );
void main()
{
InputName();
Init(); /*驅動顯卡*/
srand(time(&t1));
MainMenu(); /*畫開始菜單(1.start game 2.view records 3.delete records 4.exit game)*/
}
void InputName()
{
char c;
clrscr();
do
{
printf("\n\nPlease input your name:");
scanf("%s",name);
printf("Are you sure the name right(Y/N):");
do
{
c=getch();
}while(c!='Y'&&c!='y'&&c!='N'&&c!='n');
}while(c!='Y'&&c!='y');
}
void Init()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
}
void MainMenu()
{
setbkcolor(BKCOLOR);
cleardevice();
/*DrawStar(); /*畫開始菜單的背景畫面*/
DrawStar();
Ide=0,Key=0;
DrawMenu(Ide);
do
{
if(bioskey(1)) /*有鍵按下則處理按鍵*/
{
Key=bioskey(0);
switch(Key)
{
case Key_Down: {Ide ;Ide=Ide%4;DrawMenu(Ide);break;}
case Key_Up: {Ide--;Ide=(Ide 4)%4;DrawMenu(Ide);break;}
}
}
else {if(TimeCome()) ChangeStar();} /*改變背景*/
}while(Key!=Key_Enter);
JudgeIde(); /*根據Ide運行不同的程序*/
}
void JudgeIde()
{
switch(Ide)
{
case 0:Game();break;
case 1:{ViewRecords();bioskey(0);MainMenu();break;}
case 2:{DeleteRecords();ViewRecords();bioskey(0);MainMenu();break;}
case 3:exit(0);
}
}
void Game()
{
int i,j,flag;
Add ad;
MissionNum=0;
NextMission();
do
{
flag=0;
Key=bioskey(0);
switch(Key)
{
case Key_Enter:{NextMission();time(&t1);break;}
case Key_Up:{ad.x=-1;ad.y=0;flag=1;DirectionKey=Key;break;}
case Key_Down:{ad.x=1;ad.y=0;flag=1;DirectionKey=Key;break;}
case Key_Left:{ad.x=0;ad.y=-1;flag=1;DirectionKey=Key;break;}
case Key_Right:{ad.x=0;ad.y=1;flag=1;DirectionKey=Key;break;}
case Key_R:{MissionNum--;NextMission();break;}
case Key_Esc:{MainMenu();break;}
case Key_P:{ReverselyMove();break;}
}
if(flag==1)
{if(Move(ad)) {RegisterStep(); if(JudgeWin()) {JudgeRecord();NextMission();}}}
}while(1);
}
void InitMission(int n)
{
int i,j;
for(i=0;i<10;i )
for(j=0;j<10;j )
map=0;
switch(n)
{
case 1:InitMission1();break; /*第一關*/
case 2:InitMission2();break; /*第二關*/
}
}
void InitPic(int n,int i,int j)
{
switch(n)
{
case 0:DrawBack(i,j);break;
case 1:DrawMan(i,j);break;
case 2:DrawBo
x(i,j);break;
case 3:DrawWall(i,j);break;
case 4:DrawObject(i,j);break;
case 5:DrawMan(i,j);break;
case 6:DrawBox(i,j);break;
}
}
void NextMission()
{
int i,j;
if(MissionNum 1>MAX) MissionNum=1;
else MissionNum ;
InitMission(MissionNum);
setbkcolor(BKCOLOR);
cleardevice();
for(i=0;i<10;i )
for(j=0;j<10;j )
InitPic(map,i,j);
switch(MissionNum)
{
case 1:outtextxy(200,230,"Mission 1");break;
case 2:outtextxy(200,230,"Mission 2");break;
}
time(&t1);
for(i=0;i<STEPMAX;i )
{Step=BoxMove=0;}
StepNum=0;
}
int Move(Add a)
{
int flag;
int i=StepNum%STEPMAX;
switch(map) /*看下一位置為什麼*/
{
case 0:{map-=1;InitPic(map,p.x,p.y);
p.x=p.x a.x;p.y=p.y a.y;
map =1;InitPic(map,p.x,p.y);flag=1;break;}
case 2:{if(map==0||map==4)
{map-=1;map=1;map =2;
InitPic(map,p.x,p.y);
InitPic(map,p.x a.x,p.y a.y);
InitPic(map,p.x 2*a.x,p.y 2*a.y);
p.x=p.x a.x;p.y=p.y a.y;flag=1;BoxMove=1;}
else flag=0;
break;}
case 3:flag=0;break;
case 4:{map-=1;InitPic(map,p.x,p.y);
p.x=p.x a.x;p.y=p.y a.y;
map =1;InitPic(map,p.x,p.y);flag=1;break;}
case 6:{if(map==0||map==4)
{map-=1;map=5;map =2;
InitPic(map,p.x,p.y);
InitPic(map,p.x a.x,p.y a.y);
InitPic(map,p.x 2*a.x,p.y 2*a.y);
p.x=p.x a.x;p.y=p.y a.y;flag=1;BoxMove=1;}
else flag=0;
break;}
}
return flag;
}
void DrawWall(int i,int j)
{
DrawBack(i,j);
setfillstyle(9,1);
bar(STARTX 20*j-9,STARTY 20*i-9,STARTX 20*j 9,STARTY 20*i 9);
}
void DrawMan(int i,int j)
{
DrawBack(i,j);
setcolor(MANCOLOR);
circle(STARTX 20*j,STARTY 20*i,9);
arc(STARTX 20*j-3,STARTY 20*i-2,20,160,3);
arc(STARTX 20*j 4,STARTY 20*i-2,20,160,3);
arc(STARTX 20*j,STARTY 20*i-2,220,320,7);
}
void DrawBack(int i,int j)
{
setfillstyle(1,BKCOLOR);
bar(STARTX 20*j-9,STARTY 20*i-9,STARTX 20*j 9,STARTY 20*i 9);
}
void DrawObject(int i,int j)
{
DrawBack(i,j);
setcolor(OBJECTCOLOR);
line(STARTX 20*j-9,STARTY 20*i,STARTX 20*j 9,STARTY 20*i);
line(STARTX 20*j-9,STARTY 20*i-9,STARTX 20*j 9,STARTY 20*i 9);
line(STARTX 20*j-9,STARTY 20*i 9,STARTX 20*j 9,STARTY 20*i-9);
}
void DrawBox(int i,int j)
{
DrawBack(i,j);
setfillstyle(9,3);
bar(STARTX 20*j-9,STARTY 20*i-9,STARTX 20*j 9,STARTY 20*i 9);
}
void DrawMenu(int j)
{
int n;
char *s={"1.Start Game","2.View Records","3.Delete Records","4.Exit Game"};
settextstyle(0,0,1);
setcolor(GREEN);
for(n=0;n<4;n )
outtextxy(250,170 n*20,s);
setcolor(RED);
outtextxy(250,170 j*20,s);
}
void DrawStar()
{
int w,h,i,dotx,doty,color,maxcolor;
w=getmaxx();
h=getmaxy();
maxcolor=getmaxcolor();
for(i=0;i<STARNUM;i )
{
s.x=1 random(w-1);
s.y=1 random(h-1);
s.c=random(maxcolor);
putpixel(s.x,s.y,s.c);
}
}
void ChangeStar()
{
int i,maxcolor;
maxcolor=getmaxcolor();
for(i=0;i<STARNUM;i )
{
s.c=random(maxcolor);
putpixel(s.x,s.y,s.c);
}
}
int TimeCome()
{
static long tm, old;
tm=biostime(0,tm);
if(tm-old<TIMEINT) return 0;
else
{
old=tm; return 1;
}
}
int JudgeWin()
{
int n=0,i,j;
for(i=0;i<10;i )
for(j=0;j<10;j )
if(map==6) n ;
if(n==BoxNum) return 1;
else return 0;
}
void InitMission1() /*第九關*/
{
int i,j;
for(i=0;i<10;i )
for(j=0;j<10;j )
map=0;
for(i=0;i<=5;i )
map=3;
for(i=5;i<=7;i )
{map=map=3;}
for(i=1;i<=4;i )
{map=map=map=3;}
map=map=map=3;
for(i=2;i<=4;i )
map=2;
map=map=2;
for(i=2;i<=3;i )
{map=map=4;}
map=4;
p.x=3;p.y=5;
map=1;
BoxNum=5;
}
void InitMission2()
{
int i,j;
for(i=0;i<10;i )
for(j=0;j<10;j )
map=0;
for(i=1;i<=5;i )
{map=map=3;}
for(i=2;i<=4;i )
{map=map=map=map=3;}
map=map=map=map=map=map=3;
map=map=map=map=2;
map=map=map=map=4;
p.x=1;p.y=3;
map=1;
BoxNum=4;
}
void ViewRecords()
{
FILE *fp;
int i;
setbkcolor(BKCOLOR);
cleardevice();
if((fp=fopen("record","r"))==NULL)
{
printf("\nerror on open file!");
getch();
exit(1);
}
gotoxy(1,1);
printf("\n\t\t\tRecord Information\n");
printf("Record-holder Achievement(s)\t Time(h:m:s)\t\tDate(y/m/d)");
for(i=0;i<MAX;i )
{fseek(fp,i*sizeof(Record),0);
fread(&r,sizeof(Record),1,fp);
printf("\n%-10s\t%d\t\t d:d:d\t\td/d/d",r.name,r.second,r.t.ti_hour,r.t.ti_min,r.t.ti_sec,r.d.da_year,r.d.da_mon,r.d.da_day);}
fclose(fp);
gotoxy(10,25);
printf("Press any key to return mainmenu...");
}
void DeleteRecords()
{
int i;
FILE *fp;
fp=fopen("record","w");
for(i=0;i<MAX;i )
{
scpy(r.name,"nameless");
r.second=0;
gettime(&r.t);
geate(&r.d);
}
for(i=0;i<MAX;i )
fwrite(&r,sizeof(Record),1,fp);
fclose(fp);
}
void JudgeRecord()
{
int i=MissionNum-1;
time(&t2);
if(r.second==0||difftime(t2,t1)<r.second)
{
gotoxy(10,3);printf("\t\tYou have broken the record");
r.second=difftime(t2,t1);
scpy(r.name,name);
gettime(&r.t);
geate(&r.d);
WriteRecord();
}
else
{gotoxy(10,3);printf("\t\tYou have pass this mission");}
gotoxy(10,4);
printf("\t\tpress any key continue...");
getch();
getch();
}
void WriteRecord()
{
FILE *fp;
int i=MissionNum-1;
fp=fopen("record","rt ");
fseek(fp,i*sizeof(Record),0);
fwrite(&r,sizeof(Record),1,fp);
fclose(fp);
}
void RegisterStep()
{
int i;
StepNum ;
i=(StepNum-1)%STEPMAX;
Step=DirectionKey;
}
void ReverselyMove()
{
int i;
Add ad;
i=(StepNum-1)%STEPMAX;
if(Step==0) return;
else
{
switch(Step)
{
case Key_Up:{ad.x=1;ad.y=0;MoveBack(ad);break;}
case Key_Down:{ad.x=-1;ad.y=0;MoveBack(ad);break;}
case Key_Left:{ad.x=0;ad.y=1;MoveBack(ad);break;}
case Key_Right:{ad.x=0;ad.y=-1;MoveBack(ad);break;}
}
StepNum--;Step=0;BoxMove=0;
}
}
void MoveBack(Add a) /*一定可以移動*/
{
int i=(StepNum-1)%STEPMAX;
if(BoxMove==0)
{
map-=1;InitPic(map,p.x,p.y);
p.x=p.x a.x;p.y=p.y a.y;
map =1;InitPic(map,p.x,p.y);
}
else if(BoxMove==1)
{
map-=2;InitPic(map,p.x-a.x,p.y-a.y);
map =1;InitPic(map,p.x,p.y);
p.x=p.x a.x;p.y=p.y a.y;
map =1;InitPic(map,p.x,p.y);
}
}
點這里下載
p=map=map=4;
p.x=1;p.y=3;
map=1;
BoxNum=4;
}
void ViewRecords()
{
FILE *fp;
int i;
setbkcolor(BKCOLOR);
cleardevice();
if((fp=fopen("record","r"))==NULL)
{
printf("\nerror on open file!");
getch();
exit(1);
}
gotoxy(1,1);
printf("\n\t\t\tRecord Information\n");
printf("Record-holder Achievement(s)\t Time(h:m:s)\t\tDate(y/m/d)");
for(i=0;i<MAX;i )
{fseek(fp,i*sizeof(Record),0);
fread(&r,sizeof(Record),1,fp);
printf("\n%-10s\t%d\t\t d:d:d\t\td/d/d",r.name,r.second,r.t.ti_hour,r.t.ti_min,r.t.ti_sec,r.d.da_year,r.d.da_mon,r.d.da_day);}
fclose(fp);
gotoxy(10,25);
printf("Press any key to return mainmenu...");
}
void DeleteRecords()
{
int i;
FILE *fp;
fp=fopen("record","w");
for(i=0;i<MAX;i )
{
scpy(r.name,"nameless");
r.second=0;
gettime(&r.t);
geate(&r.d);
}
for(i=0;i<MAX;i )
fwrite(&r,sizeof(Record),1,fp);
fclose(fp);
}
void JudgeRecord()
{
int i=MissionNum-1;
time(&t2);
if(r.second==0||difftime(t2,t1)<r.second)
{
gotoxy(10,3);printf("\t\tYou have broken the record");
r.second=difftime(t2,t1);
scpy(r.name,name);
gettime(&r.t);
geate(&r.d);
WriteRecord();
}
else
{gotoxy(10,3);printf("\t\tYou have pass this mission");}
gotoxy(10,4);
printf("\t\tpress any key continue...");
getch();
getch();
}
void WriteRecord()
{
FILE *fp;
int i=MissionNum-1;
fp=fopen("record","rt ");
fseek(fp,i*sizeof(Record),0);
fwrite(&r,sizeof(Record),1,fp);
fclose(fp);
}
void RegisterStep()
{
int i;
StepNum ;
i=(StepNum-1)%STEPMAX;
Step=DirectionKey;
}
void ReverselyMove()
{
int i;
Add ad;
i=(StepNum-1)%STEPMAX;
if(Step==0) return;
else
{
switch(Step)
{
case Key_Up:{ad.x=1;ad.y=0;MoveBack(ad);break;}
case Key_Down:{ad.x=-1;ad.y=0;MoveBack(ad);break;}
case Key_Left:{ad.x=0;ad.y=1;MoveBack(ad);break;}
case Key_Right:{ad.x=0;ad.y=-1;MoveBack(ad);break;}
}
StepNum--;Step=0;BoxMove=0;
}
}
void MoveBack(Add a) /*一定可以移動*/
{
int i=(StepNum-1)%STEPMAX;
if(BoxMove==0)
{
map-=1;InitPic(map,p.x,p.y);
p.x=p.x a.x;p.y=p.y a.y;
map =1;InitPic(map,p.x,p.y);
}
else if(BoxMove==1)
{
map-=2;InitPic(map,p.x-a.x,p.y-a.y);
map =1;InitPic(map,p.x,p.y);
p.x=p.x a.x;p.y=p.y a.y;
map =1;InitPic(map,p.x,p.y);
}
}
8. 求C語言小游戲源程序
新手要方便寫代碼,可以收藏下面幾個自編函數:
gtxy (6, 3) //游標定位於窗口的第6列,第3行處(准備輸出,行與列都是從0算起)
Color (4, 0) //設置為紅字配黑底 如 Color (10, 0)則是淡綠字配黑底
yinc (1,0) //隱藏游標(第二個參數設為0就隱藏,沒有游標閃爍,yinc代表隱藏)
kou(80,25) //設定窗口緩沖區大小為80列,25行
下面幾個是庫函數,不需自己編寫,只要用#include包含就可以使用。
SetConsoleTitle("俄羅斯方塊"); //設置窗口左上角標題欄處出現"俄羅斯方塊"5個字
srand( (unsigned) time(NULL) ); //初始化隨機數發生器
n= rand( ) % 20; //產生隨機數0-19中的一個. 如 rand( )%5 就產生0-4中的一個數
SetConsoleTitle( )函數在<windows.h>里,srand( )函數與rand( )函數要配合用,
就是同時要用,在<stdlib.h>里。如果 rand( )%10+1 就產生1-10之中的一個數。
Sleep(300); //延時300毫秒(就是程序暫停300毫秒後繼續運行)
system("cls"); //清屏(把窗口裡的內容全部清除,游標定於(0,0)位置處)
這兩個函數都在<windows.h>里。開頭4個自編函數 編寫如下:
void gtxy (int x, int y) //控制游標位置的函數
{ COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos );
}
void Color (short ForeColor= 7, short BackGroundColor= 0) //設定顏色的函數
{ HANDLE hl = GetStdHandle ( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute ( hl, ForeColor + BackGroundColor * 0x10 );
}
聲明時原型可寫 void Color (short x, short y);
void yinc (int x,int y) //隱藏游標的函數
{ CONSOLE_CURSOR_INFO gb={ x , y }; //gb代表游標
SetConsoleCursorInfo ( GetStdHandle(STD_OUTPUT_HANDLE), &gb );
}
void kou(int w,int h) //設置窗口大小的函數
{HANDLE hl=GetStdHandle ( STD_OUTPUT_HANDLE ) ;
COORD size={ w , h };
SetConsoleScreenBufferSize( hl , size );
SMALL_RECT rc={ 0, 0, w, h };
SetConsoleWindowInfo( hl, 1, &rc );
}
最後這個函數,參數w是寬h是高。里邊5行中第一行定義了句柄型變數hl,並給它賦值。
第二行定義了坐標型結構體變數size,它的取值決定了緩沖區的大小。第三行就是使用
size的值設置好緩沖區大小。第四行定義了變數rc,它的值決定當前窗口顯示的位置與
大小(不得超過緩沖區的大小)。前兩個0,0是從緩沖區左上角0列0行位置處開始,後兩
個參數可以小於w和h.比如rc={0,0,w-10,h-5}; 最後一行使用rc的值設置好窗口,中間
那個參數要為" 1 "或寫「 true 」才有效。
9. 如何用C語言編寫一如圖模型的推箱子游戲的程序!
/*TC環境下編的*/
#include <dos.h>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <bios.h>
#include <alloc.h>
typedef struct winer
{
int x,y;
struct winer *p;
}winer;
char status [20][20];
char far *printScreen=(char far* )0xB8000000;
void putoutChar(int y,int x,char ch,char fc,char bc);
void printWall(int x, int y);
void printBox(int x, int y);
void printBoxDes(int x, int y);
void printDestination(int x, int y);
void printDestination1(int x,int y,winer **win,winer **pw);
void printMan(int x, int y);
void init();
winer *initStep1();
winer *initStep2();
winer *initStep3();
winer *initStep4();
void moveBoxSpacetoSpace(int x ,int y, char a);
void moveBoxDestoSpace(int x ,int y, char a) ;
void moveBoxSpacetoDes(int x, int y, char a);
void moveBoxDestoDes(int x, int y, char a);
int judge(int x, int y);
void move(int x, int y, char a);
void reset(int i);
void putoutChar(int y,int x,char ch,char fc,char bc)
{
printScreen[(x*160)+(y<<1)+0]=ch;
printScreen[(x*160)+(y<<1)+1]=(bc*16)+fc;
}
void printWall(int x,int y)
{
putoutChar(y-1,x-1,219,GREEN,BLACK);
status[x][y]='w';
}
void printBox(int x,int y)
{
putoutChar(y-1,x-1,10,WHITE,BLACK);
status[x][y]='b';
}
void printDestination1(int x,int y,winer **win,winer **pw)
{
winer *qw;
putoutChar(y-1,x-1,003,YELLOW,BLACK);
status[x][y]='m';
if(*win==NULL)
{
*win=*pw=qw=(winer* )malloc(sizeof(winer));
(*pw)->x=x;
(*pw)->y=y;
(*pw)->p=NULL;
}
else
{
qw=(winer* )malloc(sizeof(winer));
qw->x=x;
qw->y=y;
(*pw)->p=qw;
(*pw)=qw;qw->p=NULL;
}
}
void printDestination(int x,int y)
{
putoutChar(y-1,x-1,003,YELLOW,BLACK);
status[x][y]='m';
}
void printMan(int x,int y)
{
gotoxy(y,x);
_AL=02;
_CX=01;
_AH=0xa;
geninterrupt(0x10);
}
void printBoxDes(int x,int y)
{
putoutChar(y-1,x-1,10,YELLOW,BLACK);
status[x][y]='i';
}
void init()
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<20;j++)
status[i][j]=0;
_AL=3;
_AH=0;
geninterrupt(0x10);
gotoxy(40,4);
printf("Welcome to the box world!");
gotoxy(40,6);
printf("You can use up, down, left,");
gotoxy(40,8);
printf("right key to control it, or");
gotoxy(40,10);
printf("you can press Esc to quit it.");
gotoxy(40,12);
printf("Press space to reset the game.");
gotoxy(40,14);
printf("Wish you have a good time !");
gotoxy(40,16);
printf("April , 2007");
}
winer *initStep1()
{
int x;
int y;
winer *win=NULL;
winer *pw;
for(x=1,y=5;y<=9;y++)
printWall(x+4,y+10);
for(y=5,x=2;x<=5;x++)
printWall(x+4,y+10);
for(y=9,x=2;x<=5;x++)
printWall(x+4,y+10);
for(y=1,x=3;x<=8;x++)
printWall(x+4,y+10);
for(x=3,y=3;x<=5;x++)
printWall(x+4,y+10);
for(x=5,y=8;x<=9;x++)
printWall(x+4,y+10);
for(x=7,y=4;x<=9;x++)
printWall(x+4,y+10);
for(x=9,y=5;y<=7;y++)
printWall(x+4,y+10);
for(x=8,y=2;y<=3;y++)
printWall(x+4,y+10);
printWall(5+4,4+10);
printWall(5+4,7+10);
printWall(3+4,2+10);
printBox(3+4,6+10);
printBox(3+4,7+10);
printBox(4+4,7+10);
printDestination1(4+4,2+10,&win,&pw);
printDestination1(5+4,2+10,&win,&pw);
printDestination1(6+4,2+10,&win,&pw);
printMan(2+4,8+10);
return win;
}
winer *initStep2()
{
int x;
int y;
winer *win=NULL;
winer *pw;
for(x=1,y=4;y<=7;y++)
printWall(x+4,y+10);
for(x=2,y=2;y<=4;y++)
printWall(x+4,y+10);
for(x=2,y=7;x<=4;x++)
printWall(x+4,y+10);
for(x=4,y=1;x<=8;x++)
printWall(x+4,y+10);
for(x=8,y=2;y<=8;y++)
printWall(x+4,y+10);
for(x=4,y=8;x<=8;x++)
printWall(x+4,y+10);
for(x=4,y=6;x<=5;x++)
printWall(x+4,y+10);
for(x=3,y=2;x<=4;x++)
printWall(x+4,y+10);
for(x=4,y=4;x<=5;x++)
printWall(x+4,y+10);
printWall(6+4,3+10);
printBox(3+4,5+10);
printBox(6+4,6+10);
printBox(7+4,3+10);
printDestination1(5+4,7+10,&win,&pw);
printDestination1(6+4,7+10,&win,&pw);
printDestination1(7+4,7+10,&win,&pw);
printMan(2+4,6+10);
return win;
}
winer *initStep3()
{
int x;
int y;
winer *win=NULL;
winer *pw;
for(x=1,y=2;y<=8;y++)
printWall(x+4,y+10);
for(x=2,y=2;x<=4;x++)
printWall(x+4,y+10);
for(x=4,y=1;y<=3;y++)
printWall(x+4,y+10);
for(x=5,y=1;x<=8;x++)
printWall(x+4,y+10);
for(x=8,y=2;y<=5;y++)
printWall(x+4,y+10);
for(x=5,y=5;x<=7;x++)
printWall(x+4,y+10);
for(x=7,y=6;y<=9;y++)
printWall(x+4,y+10);
for(x=3,y=9;x<=6;x++)
printWall(x+4,y+10);
for(x=3,y=6;y<=8;y++)
printWall(x+4,y+10);
printWall(2+4,8+10);
printWall(5+4,7+10);
printBox(6+4,3+10);
printBox(4+4,4+10);
printBox(5+4,6+10);
printDestination1(2+4,5+10,&win,&pw);
printDestination1(2+4,6+10,&win,&pw);
printDestination1(2+4,7+10,&win,&pw);
printMan(2+4,4+10);
return win;
}
winer *initStep4()
{
int x;
int y;
winer *win=NULL;
winer *pw;
for(x=1,y=1;y<=6;y++)
printWall(x+4,y+10);
for(x=2,y=7;y<=8;y++)
printWall(x+4,y+10);
for(x=2,y=1;x<=7;x++)
printWall(x+4,y+10);
for(x=7,y=2;y<=4;y++)
printWall(x+4,y+10);
for(x=6,y=4;y<=9;y++)
printWall(x+4,y+10);
for(x=3,y=9;x<=5;x++)
printWall(x+4,y+10);
for(x=3,y=3;y<=4;y++)
printWall(x+4,y+10);
printWall(3+4,8+10);
printBox(3+4,5+10);
printBox(4+4,4+10);
printBox(4+4,6+10);
printBox(5+4,5+10);
printBox(5+4,3+10);
printDestination1(3+4,7+10,&win,&pw);
printDestination1(4+4,7+10,&win,&pw);
printDestination1(5+4,7+10,&win,&pw);
printDestination1(4+4,8+10,&win,&pw);
printDestination1(5+4,8+10,&win,&pw);
printMan(2+4,2+10);
return win;
}
void moveBoxSpacetoSpace(int x,int y,char a)
{
switch(a)
{
case 'u':
status[x-1][y]=0;
printf(" ");
printBox(x-2,y);
printMan(x-1,y);
status[x-2][y]='b';
break;
case 'd':
status[x+1][y]=0;
printf(" ");
printBox(x+2,y);
printMan(x+1,y);
status[x+2][y]='b';
break;
case 'l':
status[x][y-1]=0;
printf(" ");
printBox(x,y-2);
printMan(x,y-1);
status[x][y-2]='b';
break;
case 'r':
status[x][y+1]=0;
printf(" ");
printBox(x,y+2);
printMan(x,y+1);
status[x][y+2]='b';
break;
default:
break;
}
}
void moveBoxDestoSpace(int x,int y,char a)
{
switch(a)
{
case 'u':
status[x-1][y]='m';
printf(" ");
printBox(x-2,y);
printMan(x-1,y);
status[x-2][y]='b';
break;
case 'd':
status[x+1][y]='m';
printf(" ");
printBox(x+2,y);
printMan(x+1,y);
status[x+2][y]='b';
break;
case 'l':
status[x][y-1]='m';
printf(" ");
printBox(x,y-2);
printMan(x,y-1);
status[x][y-2]='b';
break;
case 'r':
status[x][y+1]='m';
printf(" ");
printBox(x,y+2);
printMan(x,y+1);
status[x][y+2]='b';
break;
default:
break;
}
}
void moveBoxSpacetoDes(int x,int y,char a)
{
switch(a)
{
case 'u':
status[x-1][y]=0;
printf(" ");
printBoxDes(x-2,y);
printMan(x-1,y);
status[x-2][y]='i';
break;
case 'd':
status[x+1][y]=0;
printf(" ");
printBoxDes(x+2,y);
printMan(x+1,y);
status[x+2][y]='i';
break;
case 'l':
status[x][y-1]=0;
printf(" ");
printBoxDes(x,y-2);
printMan(x,y-1);
status[x][y-2]='i';
break;
case 'r':
status[x][y+1]=0;
printf(" ");
printBoxDes(x,y+2);
printMan(x,y+1);
status[x][y+2]='i';
break;
default:
break;
}
}
void moveBoxDestoDes(int x,int y,char a)
{
switch(a)
{
case 'u':
status[x-1][y]='m';
printf(" ");
printBoxDes(x-2,y);
printMan(x-1,y);
status[x-2][y]='i';
break;
case 'd':
status[x+1][y]='m';
printf(" ");
printBoxDes(x+2,y);
printMan(x+1,y);
status[x+2][y]='i';
break;
case 'l':
status[x][y-1]='m';
printf(" ");
printBoxDes(x,y-2);
printMan(x,y-1);
status[x][y-2]='i';
break;
case 'r':
status[x][y+1]='m';
printf(" ");
printBoxDes(x,y+2);
printMan(x,y+1);
status[x][y+2]='i';
break;
default:
break;
}
}
int judge(int x,int y)
{
int i;
switch(status[x][y])
{
case 0:
i=1;
break;
case 'w':
i=0;
break;
case 'b':
i=2;
break;
case 'i':
i=4;
break;
case 'm':
i=3;
break;
default:
break;
}
return i;
}
void move(int x,int y,char a)
{
switch(a)
{
case 'u':
if(!judge(x-1,y))
{
gotoxy(y,x);
break;
}
else if(judge(x-1,y)==1||judge(x-1,y)==3)
{
if(judge(x,y)==3)
{
printDestination(x,y);
printMan(x-1,y);
break;
}
else
{
printf(" ");
printMan(x-1,y);
break;
}
}
else if(judge(x-1,y)==2)
{
if(judge(x-2,y)==1)
{
moveBoxSpacetoSpace(x,y,'u');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x-1);
}
else if(judge(x-2,y)==3)
{
moveBoxSpacetoDes(x,y,'u');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x-1);
}
else
gotoxy(y,x);
break;
}
else if(judge(x-1,y)==4)
{
if(judge(x-2,y)==1)
{
moveBoxDestoSpace(x,y,'u');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x-1);
}
else if(judge(x-2,y)==3)
{
moveBoxDestoDes(x,y,'u');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x-1);
}
else
gotoxy(y,x);
break;
}
case 'd':
if(!judge(x+1,y))
{
gotoxy(y,x);
break;
}
else if(judge(x+1,y)==1||judge(x+1,y)==3)
{
if(judge(x,y)==3)
{
printDestination(x,y);
printMan(x+1,y);
break;
}
else
{
printf(" ");
printMan(x+1,y);
break;
}
}
else if(judge(x+1,y)==2)
{
if(judge(x+2,y)==1)
{
moveBoxSpacetoSpace(x,y,'d');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x+1);
}
else if(judge(x+2,y)==3)
{
moveBoxSpacetoDes(x,y,'d');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x+1);
}
else
gotoxy(y,x);
break;
}
else if(judge(x+1,y)==4)
{
if(judge(x+2,y)==1)
{
moveBoxDestoSpace(x,y,'d');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x+1);
}
else if(judge(x+2,y)==3)
{
moveBoxDestoDes(x,y,'d');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y,x+1);
}
else
gotoxy(y,x);
break;
}
case 'l':
if(!judge(x,y-1))
{
gotoxy(y,x);
break;
}
else if(judge(x,y-1)==1||judge(x,y-1)==3)
{
if(judge(x,y)==3)
{
printDestination(x,y);
printMan(x,y-1);
break;
}
else
{
printf(" ");
printMan(x,y-1);
break;
}
}
else if(judge(x,y-1)==2)
{
if(judge(x,y-2)==1)
{
moveBoxSpacetoSpace(x,y,'l');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y-1,x);
}
else if(judge(x,y-2)==3)
{
moveBoxSpacetoDes(x,y,'l');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y-1,x);
}
else
gotoxy(y,x);
break;
}
else if(judge(x,y-1)==4)
{
if(judge(x,y-2)==1)
{
moveBoxDestoSpace(x,y,'l');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y-1,x);
}
else if(judge(x,y-2)==3)
{
moveBoxDestoDes(x,y,'l');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y-1,x);
}
else
gotoxy(y,x);
break;
}
case 'r':
if(!judge(x,y+1))
{
gotoxy(y,x);
break;
}
else if(judge(x,y+1)==1||judge(x,y+1)==3)
{
if(judge(x,y)==3)
{
printDestination(x,y);
printMan(x,y+1);
break;
}
else
{
printf(" ");
printMan(x,y+1);
break;
}
}
else if(judge(x,y+1)==2)
{
if(judge(x,y+2)==1)
{
moveBoxSpacetoSpace(x,y,'r');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y+1,x);
}
else if(judge(x,y+2)==3)
{
moveBoxSpacetoDes(x,y,'r');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y+1,x);
}
else
gotoxy(y,x);
break;
}
else if(judge(x,y+1)==4)
{
if(judge(x,y+2)==1)
{
moveBoxDestoSpace(x,y,'r');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y+1,x);
}
else if(judge(x,y+2)==3)
{
moveBoxDestoDes(x,y,'r');
if(judge(x,y)==3)
printDestination(x,y);
gotoxy(y+1,x);
}
else
gotoxy(y,x);
break;
}
default:
break;
}
}
void reset(int i)
{
switch(i)
{
case 0:
init();
initStep1();
break;
case 1:
init();
initStep2();
break;
case 2:
init();
initStep3();
break;
case 3:
init();
initStep4();
break;
default:
break;
}
}
void main()
{
int key;
int x;
int y;
int s;
int i=0;
winer *win;
winer *pw;
_AL=3;
_AH=0;
geninterrupt(0x10);
init();
win=initStep1();
do{
_AH=3;
geninterrupt(0x10);
x=_DH+1;
y=_DL+1;
while(bioskey(1)==0);
key=bioskey(0);
switch(key)
{
case 0x4800:
move(x,y,'u');
break;
case 0x5000:
move(x,y,'d');
break;
case 0x4b00:
move(x,y,'l');
break;
case 0x4d00:
move(x,y,'r');
break;
case 0x3920:
reset(i);
break;
default:
break;
}
s=0;
pw=win;
while(pw)
{
if(status[pw->x][pw->y]=='m')
s++;
pw=pw->p;
}
if(s==0)
{
free(win);
gotoxy(25,2);
printf("congratulate! You have done this step!");
getch();
i++;
switch(i)
{
case 1:
init();
win=initStep2();
break;
case 2:
init();
win=initStep3();
break;
case 3:
init();
win=initStep4();
break;
case 4:
gotoxy(15,21);
printf("Congratulation! \n");
gotoxy(15,23);
printf("You have done all the steps, Welcome to play again!");
key=0x011b;
getch();
break;
default:
break;
}
}
}while(key!=0x011b);
_AL=3;
_AH=0;
geninterrupt(0x10);
}