當前位置:編程學習大全網 - 編程語言 - 俄羅斯方塊c語言的代碼

俄羅斯方塊c語言的代碼

(1)第壹個cpp:

#include "colorConsole.h"

HANDLE initiate()

{

HANDLE hOutput;

hOutput = GetStdHandle(STD_OUTPUT_HANDLE);

return hOutput;

}

BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString)

{

DWORD cWritten;

BOOL fSuccess;

COORD coord;

coord.X = x; // start at first cell

coord.Y = y; // of first row

fSuccess = WriteConsoleOutputCharacter(

hOutput, // screen buffer handle

lpszString, // pointer to source string

lstrlen(lpszString), // length of string

coord, // first cell to write to

&cWritten); // actual number written

if (! fSuccess)

cout<<"error:WriteConsoleOutputCharacter"<<endl;

for (;fSuccess && coord.X < lstrlen(lpszString)+x; coord.X += nColors)

{

fSuccess = WriteConsoleOutputAttribute(

hOutput, // screen buffer handle

wColors, // pointer to source string

nColors, // length of string

coord, // first cell to write to

&cWritten); // actual number written

}

if (! fSuccess)

cout<<"error:WriteConsoleOutputAttribute"<<endl;

return 0;

}

(2)第二個cpp

#include <conio.h>

#include <stdlib.h>

#include<stdio.h>

#include <windows.h>

#include <mmsystem.h>

#pragma comment(lib,"winmm.lib") //播放背景音樂的頭文件

#include "colorConsole.h"

#include<time.h>

#define SQUARE_COLOR BACKGROUD_BLUE|BACKGROUD_RED| BACKGROUD_INTENSITY //背景顏色

#define SQUARE_COLOR FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY //方塊的顏色

#define up 72

#define down 80

#define left 75

#define right 77

#define esc 27

#define MAPW 15 //地圖的寬度

#define MAPH 25 //地圖的高度

void initiate1();

int * build(); //創建方塊 //初始化工作

BOOL isavailable(int a[],int x,int y,int w,int h); //判定是否能放下

void drawblocks(int a[],int w,int h,int x,int y,WORD wColors[],int nColors);

void delete_cache(); //清除鍵盤緩沖區

void revolve(int a[][4],int w,int h,int *x,int y); //轉動方塊

void pro();

void end();

void delete_blocks(int *a,int w,int h,int x,int y);

void gameover();

void deletefull_line(int m[][MAPW],int row,int w,int h); //消除壹行

int dx=30,dy=5; //屏幕上的偏移量

int score=0,level=0;

int map[MAPH][MAPW];

int a1[4][4]={{1},{1,1,1}};

int a2[4][4]={{0,1},{1,1,1}};

int a3[4][4]={{1,1},{0,1,1}};

int a4[4][4]={{0,0,1},{1,1,1}};

int a5[4][4]={{0,1,1},{1,1}};

int a6[4][4]={{1,1,1,1}};

int a7[4][4]={{1,1},{1,1}};

int a[4][4];

int main()

{

HANDLE handle;

handle=initiate();

WORD wColors[1]={FOREGROUND_BLUE| FOREGROUND_GREEN|FOREGROUND_INTENSITY };

while(1)

{

sndPlaySound("Resource\\Just Dance.wav",SND_LOOP|SND_ASYNC);//用異步方式播放音樂,PlaySound函數在開始播放後立即返回

system("CLS");

int n=0;

printf("目錄\n1.開始遊戲\n2.退出遊戲\n\n\n");

scanf("%d",&n);

switch(n)

{

case 1:

system("CLS");

textout(handle,22,6,wColors+2,1,"請選擇遊戲等級:");

textout(handle,32,8,wColors+2,1,"1.初級");

textout(handle,32,10,wColors+2,1,"2.中級");

textout(handle,32,12,wColors+2,1,"3.高級");

while(1)

{

char choice;

choice=_getch();

if(choice=='1')

{

textout(handle,22,6,wColors+2,1,"開始遊戲,初級");

textout(handle,32,8,wColors+2,1," ");

textout(handle,32,10,wColors+2,1," ");

textout(handle,32,12,wColors+2,1," ");

level=0,score=0;

Sleep(2000);

textout(handle,22,6,wColors+2,1," ");

break;

}

else if(choice=='2')

{

textout(handle,22,6,wColors+2,1,"開始遊戲,中級");

textout(handle,32,8,wColors+2,1," ");

textout(handle,32,10,wColors+2,1," ");

textout(handle,32,12,wColors+2,1," ");

level=2,score=20;

Sleep(2000);

textout(handle,22,6,wColors+2,1," ");

break;

}

else if(choice=='3')

{

textout(handle,22,6,wColors+2,1,"開始遊戲,高級");

textout(handle,32,8,wColors+2,1," ");

textout(handle,32,10,wColors+2,1," ");

textout(handle,32,12,wColors+2,1," ");

level=4,score=40;

Sleep(2000);

textout(handle,22,6,wColors+2,1," ");

break;

}

else if(choice!='1'&&choice!='2'&&choice!='3')

continue;

}

pro();

break;

case 2:

return 0;

default:

printf("錯誤,按鍵繼續");

while(!_kbhit());

}

}

}

void pro() //遊戲主題

{

initiate1();

int *b=NULL;

b=build(); //創建方塊

int sign,blank,x,y;

while(1)

{

for(int i=0;i<4;i++) //復制方塊

for(int j=0;j<4;j++)

if(a[i][j]=*(b+i*4+j)) blank=i;

y=1-blank;x=4;

delete_blocks(&a[0][0],4,4,16,10);

b=build();

HANDLE handle;

handle=initiate();

WORD wColors[1]={FOREGROUND_BLUE| FOREGROUND_GREEN|FOREGROUND_INTENSITY };

drawblocks(b,4,4,16,10,wColors,1);

wColors[0]=SQUARE_COLOR;

drawblocks(&a[0][0],4,4,x,y,wColors,1);

delete_cache();

char string[5];

wColors[0]=FOREGROUND_RED| FOREGROUND_GREEN|FOREGROUND_INTENSITY;

textout(handle,dx-10,8+dy,wColors,1,itoa(score,string,10));

textout(handle,dx-10,14+dy,wColors,1,itoa(level,string,10));

sign=1;

while(sign)

{

int delay=0,max_delay=100-10*level; //延遲量

while(delay<max_delay)

{

if(_kbhit()) //用if避免按住鍵使方塊卡住

{

int draw=0;

int key=_getch();

switch (key)

{

case up:

delete_blocks(&a[0][0],4,4,x,y);

revolve(a,4,4,&x,y);

draw=1;

break;

case down:

delay=max_delay;

break;

case left:

if(isavailable(&a[0][0],x-1,y,4,4))

{

delete_blocks(&a[0][0],4,4,x,y);

x--;

draw=1;

}

break;

case right:

if(isavailable(&a[0][0],x+1,y,4,4))

{

delete_blocks(&a[0][0],4,4,x,y);

x++;

draw=1;

}

break;

case 32://32 是空格鍵的ASCII碼,按空格鍵暫停

while(1)

{

textout(handle,dx,-2+dy,wColors,1,"Press any key to continue");

Sleep(200);

textout(handle,dx,-2+dy,wColors,1," ");

Sleep(200);

if(_kbhit())

{

draw=1;

break;

}

}

break;

case esc://按鍵退出遊戲

exit(EXIT_SUCCESS);

}

if(draw)

{

HANDLE handle;

handle=initiate();

WORD wColors[1]={SQUARE_COLOR};

drawblocks(&a[0][0],4,4,x,y,wColors,1);

draw=0;

}

}

_sleep(5);delay++;

}

if(isavailable(&a[0][0],x,y+1,4,4)) //是否能下移

{

delete_blocks(&a[0][0],4,4,x,y);

y++;

HANDLE handle;

handle=initiate();

WORD wColors[1]={SQUARE_COLOR};

drawblocks(&a[0][0],4,4,x,y,wColors,1);

}

else

{

sign=0; //標記,使跳出 while(sign) 循環,產生新方塊

if(y<=1)

{

system("CLS");

HANDLE handle;

handle=initiate();

WORD wColors[1]={FOREGROUND_RED| FOREGROUND_GREEN};

textout(handle,4+dx,6+dy,wColors,1,"GAME OVER!!!");

textout(handle,4+dx,8+dy,wColors,1,"分數:");

textout(handle,10+dx,8+dy,wColors,1,itoa(score,string,10));

textout(handle,4+dx,10+dy,wColors,1,"制作者:***");

delete_cache();

exit(EXIT_SUCCESS);

} //是否結束

for(int i=0;i<4;i++) //放下方塊

for(int j=0;j<4;j++)

if(a[i][j]&&((i+y)<MAPH-1)&&((j+x)<MAPW-1))

map[i+y][j+x]=a[i][j];

int full,k=0;

for( i=y;i<min(y+4,MAPH-1);i++)

{

full=1;

for(int j=1;j<14;j++)

if(!map[i][j]) full=0;

if(full) //消掉壹行

{

deletefull_line(map,i,MAPW,MAPH);

k++;

score=score+k;

level=min(score/10,9);

}

}

}

}

}

}

void initiate1() //初始化

{

int i;

for(i=0;i<25;i++)

{

map[i][0]=-2;

map[i][14]=-2;

}

for(i=0;i<15;i++)

{

map[0][i]=-1;

map[24][i]=-1;

}

map[0][0]=-3;

map[0][14]=-3;

map[24][0]=-3;

map[24][14]=-3;

HANDLE handle;

handle=initiate();

WORD wColors[1]={FOREGROUND_GREEN| FOREGROUND_BLUE|FOREGROUND_INTENSITY};

textout(handle,dx-10,6+dy,wColors,1,"SCORE");

textout(handle,dx-10,12+dy,wColors,1,"LEVEL");

textout(handle,32+dx,8+dy,wColors,1,"NEXT");

wColors[0]=FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY;

drawblocks(&map[0][0],15,25,0,0,wColors,1);

textout(handle,dx,dy,wColors,1,"◎═════════════◎");

wColors[0]=FOREGROUND_BLUE| FOREGROUND_GREEN|FOREGROUND_INTENSITY;

textout(handle,dx+8,dy+5,wColors,1,"按任意鍵開始");

wColors[0]=FOREGROUND_BLUE|FOREGROUND_RED|FOREGROUND_INTENSITY ;

textout(handle,dx+7,dy-3,wColors,1,"制作者:***");

int x=_getch();

srand(time(NULL));

textout(handle,dx+8,dy+5,wColors,1," ");

}

int * build() //創建方塊

{

int * a=NULL;

int c=rand()%7;

switch(c)

{

case 0:

a=&a1[0][0];break;

case 1:

a=&a2[0][0];break;

case 2:

a=&a3[0][0];break;

case 3:

a=&a4[0][0];break;

case 4:

a=&a5[0][0];break;

case 5:

a=&a6[0][0];break;

case 6:

a=&a7[0][0];break;

}

return a;

}

void drawblocks(int a[],int w,int h,int x,int y,WORD wColors[],int nColors) //畫出方塊

{

HANDLE handle;

handle = initiate();

int temp;

for(int i=0;i<h;i++)

for(int j=0;j<w;j++)

if((temp=a[i*w+j])&&y+i>0)

{

if(temp==-3)

{

textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"◎");

_sleep(30);

}

else if(temp==-2)

{

textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"║");

_sleep(30);

}

else if(temp==1)

textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"◎");

else if(temp==-1)

{

textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,"═");

_sleep(30);

}

}

}

void delete_cache() //清除緩沖區

{

while(_kbhit())

{

_getch();

}

}

void delete_blocks(int *a,int w,int h,int x,int y) //覆蓋方塊

{

HANDLE handle;

handle=initiate();

WORD wColors[1]={SQUARE_COLOR};

for(int i=0;i<h;i++)

for(int j=0;j<w;j++)

if(a[i*w+j]&&i+y>0)

textout(handle,2*(x+j)+dx,y+i+dy,wColors,1," ");

}

void revolve(int a[][4],int w,int h,int *x,int y) //轉動方塊

{

int b[4][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};

int sign=0,line=0;

for(int i=h-1;i>=0;i--)

{

for(int j=0;j<w;j++)

if(a[i][j])

{

b[j][line]=a[i][j];

sign=1;

}

if(sign)

{

line++;

sign=0;

}

}

for(i=0;i<4;i++)

if(isavailable(&b[0][0],*x-i,y,w,h))

{

*x-=i;

for(int k=0;k<h;k++)

for(int j=0;j<w;j++)

a[k][j]=b[k][j];

break;

}

}

void deletefull_line(int m[][MAPW],int row,int w,int h) //消除滿行的方塊

{

HANDLE handle;

handle=initiate();

WORD wColors[1]={SQUARE_COLOR};

textout(handle,2+dx,row+dy,wColors,1,"﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌");

_sleep(100);

int i;

for(i=row;i>1;i--)

{

delete_blocks(&m[i][1],MAPW-2,1,1,i);

for(int j=1;j<MAPW-1;j++)

m[i][j]=m[i-1][j];

drawblocks(&m[i][1],MAPW-2,1,1,i,wColors,1);

}

for(i=1;i<MAPW-1;i++)

m[1][i]=0;

}

BOOL isavailable(int a[],int x,int y,int w,int h)

{

for(int i=max(y,1);i<y+h;i++)

for(int j=x;j<x+w;j++)

if(map[i][j]&&a[w*(i-y)+j-x])

return 0;

return 1;

}

  • 上一篇:動態規劃算法程序示例
  • 下一篇:網頁設計制作詳細流程
  • copyright 2024編程學習大全網