C语言编写的俄罗斯方块
Tc2.0
Tc2.0
ASCII25
80
Tc2.0initgraph()closegraph()
initgraph()closegraph()
graphics.h
void far initgraph(int far *graphdriver,int far *graphmode,char far *pathtodriver);graphdri
vergraphmodegraphdriver
pathtodriver
Tc2.0
VGAVGAHI640*480(
0-6390-479)16
pathtodriver
VGAegavga.bgiegavga.bgiTc
void far closegraph(void);
initgraph()closegraph()
int gdriver = VGA, gmode=VGAHI, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, e:tc2);
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf(Graphics error: %sn, grapherrormsg(errorcode));
printf(Press any key to halt:);
getch();
exit(1); /* return with error code */
}
/* return to text mode */
closegraph();
Tc2.0
setcolor();
line();
rectangle();
settextjustify();
outtextxy();
setfillstyle();
bar();
void far setcolor(int color);
line()rectangle()
outtextxy()
color
BLACK ? 0
BLUE ? 1
GREEN ? 2
CYAN ? 3
RED ? 4
MAGENTA ? 5
BROWN ? 6
LIGHTGRAY ? 7
DARKGRAY ? 8
LIGHTBLUE ? 9
LIGHTGREEN ?10
LIGHTCYAN ?11
LIGHTRED ?12
LIGHTMAGENTA ?13
YELLOW ?14
WHITE ?15
void far line(int x1,int y1,int x2,int y2);
(x1,y1)(x2,y2)
void far rectangle(int left,int top,int right,int bottom);
(left,top)(right,bottom)
void far settextjustify(int horz,int vert);
outtextxy() horizvert
horiz ?LEFT_TEXT ? 0 ?Left-justify text
?CENTER_TEXT ? 1 ?Center text
?RIGHT_TEXT ? 2 ?Right-justify text
vert ?BOTTOM_TEXT ? 0 ?Justify from bottom
?CENTER_TEXT ? 1 ?Center text
?TOP_TEXT ? 2 ?Justify from top
void far outtextxy(int x,int y,char * textstring);
(x,y)(DEFAULT_FONT)textstring
settextjustify()
void far setfillstyle(int pattern,int color);
bar() patternSOLID_FILL,colorsetcolor(int color)color
prog1.c
Tc2.0bioskey();
int bioskey(int cmd);
cmd1bioskey()0
(0)
cmd0bioskey()
Escape0x11b,
for (;;)
{
key=bioskey(0); /* wait for a keystroke */
printf(0x%xn,key);
if (key==0x11b) break; /* Escape */
}
#define VK_LEFT 0x4b00
#define VK_RIGHT 0x4d00
#define VK_DOWN 0x5000
#define VK_UP 0x4800
#define VK_HOME 0x4700
#define VK_END 0x4f00
#define VK_SPACE 0x3920
#define VK_ESC 0x011b
#define VK_ENTER 0x1c0d
prog2.cprog3.c
prog2.cEscape prog3.cEscape
prog4.c
()
18.2
118
getvect()setvect()
?void interrupt (*getvect(int interruptno))();
?void setvect(int interruptno, void interrupt (*isr) ( ));
interrupt
iretret
getvect()interruptnointerruptno
setvect()interruptnoisr()
isr()
prog5.cprog5.c
/* prog5.c */
This is an interrupt service routine. You can NOT compile this
program with Test Stack Overflow turned on and get an executable
file which will operate correctly. */
/* 110 escape */
#include
#include
#include
/* Escape key */
#define VK_ESC 0x11b
#define TIMER 0x1c /* */
/* CC++
_cplusplusC++C */
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
int TimerCounter=0; /* 18 */
/* () */
void interrupt ( *oldhandler)(__CPPARGS);
/* */
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();
}
void main(void)
{
int key,time=0;
SetTimer(newhandler); /* */
for (;;)
{
if (bioskey(1))
{
key=bioskey(0);
if (key==VK_ESC) /* escape */
{
printf(User cancel!n);
break;
}
}
if (TimerCounter>18) /* 1 */
{
/* */
TimerCounter=0;
time++;
printf(%dn,time);
if (time==10) /* 10 */ {
printf(Program terminated normally!n);
break;
}
}
}
KillTimer(); /* */
}
???? ???? ???? ????
???? ???? ???? ????
???? ???? ???? ????
???? ???? ???? ????
???? ???? ????
???? ???? ????
???? ???? ????
???? ???? ????
struct shape
{
int xy[8];
int color;
int next;
}
-1 0 1 2
-3????
-2????
-1????
0????
4x4(0,0)(
)4
xy
(
)(-2,0)(-1,0)(0,0)(1,0)shape
xy4xy[8]xy[0]xy[1]
xy[2]xy[3]
shapecolor
19(
)shapenext
???? ???? ???? ????
???? ???? ???? ????
???? ???? ???? ????
???? ???? ???? ????
struct shape shapes[19]=
{
/*{x1,y1,x2,y2,x3,y3,x4,y4, color, next}*/
{ 0,-2, 0,-1, 0, 0, 1, 0, CYAN, 1}, /* */
{-1, 0, 0, 0, 1,-1, 1, 0, CYAN, 2}, /* # */
{ 0,-2, 1,-2, 1,-1, 1, 0, CYAN, 3}, /* # */
{-1,-1,-1, 0, 0,-1, 1,-1, CYAN, 0}, /* ## */
……
}
(
)1020
board[12][22]board[x][y]1
(x,y)board[x][y] 0
1board[0][y],board[11][y](y021)1
board[x][21](x110)1 1 2 3 4 5 6 7 8 910
1??????????
2??????????
3??????????
4??????????
5??????????
6??????????
7??????????
8??????????
9??????????
10??????????
11??????????
12??????????
13??????????
14??????????
15??????????
16??????????
17??????????
18??????????
19??????????
20??????????
prog6.c
SIZE16640×480
40×30(640/16=40480/16=30)40×30
DrawBlock(int x,int y)
-8-7-6-5-4-3-2-1 0 1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031
-4???????????????????????????????????????? -3???????????????????????????????????????? -2???????????????????????????????????????? -1???????????????????????????????????????? 0???????????????????????????????????????? 1???????????????????????????????????????? 2???????????????????????????????????????? 3???????????????????????????????????????? 4???????????????????????????????????????? 5???????????????????????????????????????? 6???????????????????????????????????????? 7???????????????????????????????????????? 8???????????????????????????????????????? 9???????????????????????????????????????? 10???????????????????????????????????????? 11???????????????????????????????????????? 12???????????????????????????????????????? 13???????????????????????????????????????? 14???????????????????????????????????????? 15???????????????????????????????????????? 16???????????????????????????????????????? 17???????????????????????????????????????? 18???????????????????????????????????????? 19???????????????????????????????????????? 20????????????????????????????????????????
21????????????????????????????????????????
22????????????????????????????????????????
23????????????????????????????????????????
24????????????????????????????????????????
25????????????????????????????????????????
26????????????????????????????????????????
1
10120“”1417
36
struct shape shapes[19]=
{
/*{x1,y1,x2,y2,x3,y3,x4,y4, color, next}*/
{ 0,-2, 0,-1, 0, 0, 1, 0, CYAN, 1},
……
}
(0,-2)(0,-1)(0,0)(1,0)
4x4
(x,y)
(x+0,y-2)(x+0,y-1)(x+0,y+0)(x+1,y+0)
4x4
-1 0 1 2
-3????
-2????
-1???? (0,-2)(0,-1)(0,0)(1,0) 0????
1 2 3 4 5 6 7 8 910
1?????????? (2,3)
2?????????? 3?????????? (2+0,3-2)(2+0,3-1)(2+0,3-0)(2+1,3-0) 4?????????? (2,1)(2,2)(2,3)(3,3)
5??????????
6??????????
7?????????? (1,9) 8?????????? (1+0,9-2)(1+0,9-1)(1+0,9-0)(1+1,9-0) 9?????????? (1,7)(1,8)(1,9)(2,9)
10??????????
11??????????
12??????????
13??????????
14??????????
15??????????
16??????????
17??????????
18?????????? (9,20) 19?????????? (9+0,20-2)(9+0,20-1)(9+0,20-0)(9+1,20-0) 20?????????? (9,18)(9,19)(9,20)(10,20)
Russia.cboard[12][22]board[x][y]x011y1211(x,y)
(x,y)board[x][y]x110y120
(x,y)
board[0][y]board[11][y]y1211board[x][21]
x1101“”
“”
“”
ShapeIndex(x,y)
enum bool Confilict(int ShapeIndex,int x,int y)
{
int i;
/* ShapeIndex */
for (i=0;i<=7;i++,i++) /* i0,2,4,6 */
{
/* x110
*/
if (shapes[ShapeIndex].xy[i]+x<1 ||
shapes[ShapeIndex].xy[i]+x>10) return True;
/* y1
*/
if (shapes[ShapeIndex].xy[i+1]+y<1) continue;
/* (x,y)
*/
if (board[shapes[ShapeIndex].xy[i]+x][shapes[ShapeIndex].xy[i+1]+y])
return True;
}
/* (x,y)
*/
return False;
}
shapes[ShapeIndex].xy[i]i0,2,4,6ShapeIndex
x(i01xi22xi43xi64x
shapes[ShapeIndex].xy[i]i1,3,5,7ShapeIndex
y(i11yi32yi53yi74y
shapes[ShapeIndex].xy[i]+xi0,2,4,6ShapeIndex
(x,y)x(i01x
i22xi43x
i64x
shapes[ShapeIndex].xy[i]+yi1,3,5,7ShapeIndex
(x,y)y
(i11yi32y
i53yi74y
board[shapes[ShapeIndex].xy[i]+x][shapes[ShapeIndex].xy[i+1]+y]
ActualX=shapes[ShapeIndex].xy[i]+x/* x0,2,4,6 */
x
ActualY=[shapes[ShapeIndex].xy[i+1]+y
y
board[ActualX][ActualY]0
10
“”
x
/* x110
*/
if (shapes[ShapeIndex].xy[i]+x<1 ||
shapes[ShapeIndex].xy[i]+x>10) return True;
??
? 2 3 4 5 6 7 8 910
1?????????? 2?????????? “” 3?????????? “” 4?????????? “” 5??????????
6??????????
7??????????
8?????????? “” 9?????????? “” 10?????????? “” 11??????????
12??????????
13??????????
14?????????? 15?????????? 16?????????? 17?????????? 18?????????? 19?????????? 20??????????