?? game_russia.c
字號:
#include <graphics.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
/*定義左上角點在屏幕上的位置*/
#define MAPXOFT 9
#define MAPYOFT 5
/*定義下一個方塊顯示的位置*/
#define MAPXOFT1 13
#define MAPYOFT1 -2
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000 /*此鍵為加速鍵*/
#define UP 0x4800 /*此鍵為變形鍵*/
#define ESC 0x011b /*此鍵為退出鍵*/
#define ENTER 0x1c0d
#define TIMER 0x1c /* 時鐘中斷的中斷號 */
/* 中斷處理函數(shù)在C和C++中的表示略有不同。
如果定義了_cplusplus則表示在C++環(huán)境下,否則是在C環(huán)境下。 */
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
int TimerCounter=0; /* 計時變量,每秒鐘增加18。 */
/* 指向原來時鐘中斷處理過程入口的中斷處理函數(shù)指針(句柄) */
void interrupt ( *oldhandler)(__CPPARGS);
/* 新的時鐘中斷處理函數(shù) */
void interrupt newhandler(__CPPARGS)
{
/* increase the global counter */
TimerCounter++;
/* call the old routine */
oldhandler();
}
/* 設置新的時鐘中斷處理過程 */
void SetTimer(void interrupt (*IntProc)(__CPPARGS))
{
oldhandler=getvect(TIMER);
disable(); /* 設置新的時鐘中斷處理過程時,禁止所有中斷 */
setvect(TIMER,IntProc);
enable(); /* 開啟中斷 */
}
/* 恢復原有的時鐘中斷處理過程 */
void KillTimer()
{
disable();
setvect(TIMER,oldhandler);
enable();
}
struct shape
{
int xy[8],next;
};
struct shape shapes[19]=
{
/*x1,y1,x2,y2,x3,y3,x4,y4 指四個小方塊的相對坐標,next指此方塊變形后應變?yōu)槟膫€小方塊
{ x1,y1,x2,y2,x3,y3,x4,y4,next}*/
{ 0,-2, 0,-1, 0, 0, 1, 0, 1},
{-1, 0, 0, 0, 1,-1, 1, 0, 2},
{ 0,-2, 1,-2, 1,-1, 1, 0, 3},
{-1,-1,-1, 0, 0,-1, 1,-1, 0},
{ 0,-2, 0,-1, 0, 0, 1,-2, 5},
{-1,-1, 0,-1, 1,-1, 1, 0, 6},
{ 0, 0, 1,-2, 1,-1, 1, 0, 7},
{-1,-1,-1, 0, 0, 0, 1, 0, 4},
{-1, 0, 0,-1, 0, 0, 1, 0, 9},
{ 0,-2, 0,-1, 0, 0, 1,-1,10},
{-1,-1, 0,-1, 1,-1, 0, 0,11},
{ 0,-1, 1,-2, 1,-1, 1, 0, 8},
{-1, 0, 0,-1, 0, 0, 1,-1,13},
{ 0,-2, 0,-1, 1,-1, 1, 0,12},
{-1,-1, 0,-1, 0, 0, 1, 0,15},
{ 0,-1, 0, 0, 1,-2, 1,-1,14},
{ 0,-3, 0,-2, 0,-1, 0, 0,17},
{-1, 0, 0, 0, 1, 0, 2, 0,16},
{ 0,-1, 0, 0, 1,-1, 1, 0,18}
};
int board[10][20]={0};/*定義游戲板初始化為0*/
char sp[]="0",le[]="0",sc[]="00000";
int speed,speed0,level,score;
int sign,flag;
int style,style1; /*style為當前方塊的種類,style1為即將輸出的方塊的種類*/
void draw_block(int x,int y,int style,int way);
void draw_little_block(int x,int y);
void init();
void initialize();
void speed_change(void);
void score_change(int);
void kill_line(int y);
void fill_board(int x,int y, int style);
int change(int *i,int *j,int key);
void renovate(void);
void ajustment(void);
void level_change(void);
main()
{
int i,j,key,x0=5,y0=1;
randomize();
while(!flag) /*flag為0表示重新開始游戲*/
{
level=score=speed=0;
strcpy(le,"0");
strcpy(sp,"0");
strcpy(sc,"00000");
for(i=0;i<10;i++)
for(j=0;j<20;j++)
board[i][j]=0; /*初始化一些變量為0*/
initialize(); /*初始化進入圖形模式*/
init(); /*初始化游戲板記分器等*/
SetTimer(newhandler); /* 修改時鐘中斷 */
ajustment(); /*開始游戲前調(diào)整速度和高度*/
if(level>0)
level_change(); /*根據(jù)高度隨機確定方塊是否存在*/
style=random(19); /*隨機確定方塊種類*/
while(1)
{
i=x0,j=y0;
style1=random(19); /*隨機確定即將出現(xiàn)的方塊種類*/
setcolor(WHITE);
sign=1;
draw_block(MAPXOFT1,MAPYOFT1,style1,1);
/*畫出即將出現(xiàn)的方塊*/
for(j=y0;j<=20;j++) /*使方塊下降*/
{
if(!check_block(i,j,style))
break;
draw_block(i,j,style,1);
while(1)
{
if(speed0==0) /*未按下加速鍵時的處理*/
{
if (TimerCounter>18/(speed+1))
{
/* 恢復計時變量 */
TimerCounter=0;
break;
}
}
else if(TimerCounter>18/(9+1))/*按下加速鍵時的處理*/
{
/* 恢復計時變量 */
TimerCounter=0;
speed0=0;
break;
}
if(bioskey(1))
{
key=bioskey(0);
if(change(&i,&j,key))/*根據(jù)按鍵值做調(diào)整*/
{
flag=1;
goto end;
}
}
}
draw_block(i,j,style,0);
renovate(); /*刷新屏幕*/
}
if(j==y0)
break;
j--;
draw_block(i,j,style,1);
fill_board(i,j,style);
sign=1;
draw_block(MAPXOFT1,MAPYOFT1,style1,0);
style=style1;
kill_line(j); /*消去的函數(shù),消去若干行并改變分數(shù)和速度*/
while(bioskey(1)) /*清除內(nèi)存中的按鍵*/
key=bioskey(0);
}
setcolor(CYAN);
settextstyle(0,0,2);
TimerCounter=0;
while(1)
if(TimerCounter>54)
{
TimerCounter=0;
break;
}
while(bioskey(1)) /*清除內(nèi)存中的按鍵*/
key=bioskey(0);
outtextxy(400,340,"Game over!");
outtextxy(360,360,"Enter to replay.");
outtextxy(360,380,"Esc to quit.");
while(bioskey(1)==0);
key=bioskey(0);
end:;
closegraph();
KillTimer();
if(key==ESC||flag)
break;
}
}
void initialize()/*初始化進入圖形模式*/
{
int gdriver = VGA, gmode=VGAHI, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "f:\\tc\\BGI");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
}
void init(void)/*初始化游戲板記分器等*/
{
int x1;
x1=5+MAPXOFT;
setcolor(GREEN);
circle((x1+0.5)*16,(MAPYOFT-2)*16,3*16);
setcolor(WHITE);
line((x1-0.6)*16,(MAPYOFT-3.2)*16,x1*16,(MAPYOFT-3.2)*16);
line((x1+1.4)*16,(MAPYOFT-3.2)*16,(x1+2.0)*16,(MAPYOFT-3.2)*16);
line((x1+0.5)*16,(MAPYOFT-2)*16,(x1+0.5)*16,(MAPYOFT-1.7)*16);
circle((x1+0.5)*16,(MAPYOFT-0.9)*16,0.3*16);
setcolor(CYAN);
line((MAPXOFT+3)*16,(MAPYOFT+21)*16,MAPXOFT*16,(MAPYOFT+23.5)*16);
line((MAPXOFT+5.5)*16,(MAPYOFT+21)*16,MAPXOFT*16,(MAPYOFT+26)*16);
line((MAPXOFT+9)*16,(MAPYOFT+21)*16,(MAPXOFT+12)*16,(MAPYOFT+23.5)*16);
line((MAPXOFT+6.5)*16,(MAPYOFT+21)*16,(MAPXOFT+12)*16,(MAPYOFT+26)*16);
setcolor(MAGENTA);
ellipse((MAPXOFT+1)*16,(MAPYOFT+11)*16,90,270,7*16,7*16);
ellipse((MAPXOFT+11)*16,(MAPYOFT+11)*16,-90,90,7*16,7*16);
circle((MAPXOFT+24)*16,(MAPYOFT+6)*16,6*16);
setcolor(WHITE);
rectangle((x1-0.5)*16,(MAPYOFT-2.9)*16,(x1-0.1)*16,(MAPYOFT-2.5)*16);
rectangle((x1+1.5)*16,(MAPYOFT-2.9)*16,(x1+1.9)*16,(MAPYOFT-2.5)*16);
setcolor(YELLOW);
rectangle(10*16,6*16,20*16,26*16);
settextstyle(0,0,2);
outtextxy(5*16,13.5*16,"level");
outtextxy(20.4*16,13.5*16,"speed");
outtextxy((MAPXOFT+22)*16,(MAPYOFT+3)*16,"score");
settextstyle(0,0,4);
outtextxy(6.5*16,15*16,"0");
outtextxy(21.9*16,15*16,"0");
settextstyle(0,0,3);
outtextxy((MAPXOFT+21)*16,(MAPYOFT+6)*16,sc);
}
void ajustment(void) /*開始游戲前調(diào)整速度和高度*/
{
int key,boo=1,left=1;
setcolor(YELLOW);
settextstyle(0,0,2);
outtextxy(500,340,"PRESS");
outtextxy(500,360,"ENTER");
outtextxy(500,380," TO ");
outtextxy(500,400,"START");
while(1)
{
if(TimerCounter>8)
{
TimerCounter=0;
boo*=-1;
if(boo==-1)
setcolor(BLACK);
else
setcolor(YELLOW);
if(left==1)
{
line(6*16,17*16,8.5*16,17*16);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -