當前位置:編程學習大全網 - 編程語言 - 求壹個用C語言編寫的小遊戲代碼

求壹個用C語言編寫的小遊戲代碼

#include <graphics.h>

#include <conio.h>

#include <time.h>

/////////////////////////////////////////////

// 定義常量、枚舉量、結構體、全局變量

/////////////////////////////////////////////

#define WIDTH 10 // 遊戲區寬度

#define HEIGHT 22 // 遊戲區高度

#define SIZE 20 // 每個遊戲區單位的實際像素

// 定義操作類型

enum CMD

{

CMD_ROTATE, // 方塊旋轉

CMD_LEFT, CMD_RIGHT, CMD_DOWN, // 方塊左、右、下移動

CMD_SINK, // 方塊沈底

CMD_QUIT // 退出遊戲

};

// 定義繪制方塊的方法

enum DRAW

{

SHOW, // 顯示方塊

HIDE, // 隱藏方塊

FIX // 固定方塊

};

// 定義七種俄羅斯方塊

struct BLOCK

{

WORD dir[4]; // 方塊的四個旋轉狀態

COLORREF color; // 方塊的顏色

} g_Blocks[7] = { {0x0F00, 0x4444, 0x0F00, 0x4444, RED}, // I

{0x0660, 0x0660, 0x0660, 0x0660, BLUE}, // 口

{0x4460, 0x02E0, 0x0622, 0x0740, MAGENTA}, // L

{0x2260, 0x0E20, 0x0644, 0x0470, YELLOW}, // 反L

{0x0C60, 0x2640, 0x0C60, 0x2640, CYAN}, // Z

{0x0360, 0x4620, 0x0360, 0x4620, GREEN}, // 反Z

{0x4E00, 0x4C40, 0x0E40, 0x4640, BROWN}}; // T

// 定義當前方塊、下壹個方塊的信息

struct BLOCKINFO

{

byte id; // 方塊 ID

char x, y; // 方塊在遊戲區中的坐標

byte dir:2; // 方向

} g_CurBlock, g_NextBlock;

// 定義遊戲區

BYTE g_World[WIDTH][HEIGHT] = {0};

/////////////////////////////////////////////

// 函數聲明

/////////////////////////////////////////////

void Init(); // 初始化遊戲

void Quit(); // 退出遊戲

void NewGame(); // 開始新遊戲

void GameOver(); // 結束遊戲

CMD GetCmd(); // 獲取控制命令

void DispatchCmd(CMD _cmd); // 分發控制命令

void NewBlock(); // 生成新的方塊

bool CheckBlock(BLOCKINFO _block); // 檢測指定方塊是否可以放下

void DrawBlock(BLOCKINFO _block, DRAW _draw = SHOW); // 畫方塊

void OnRotate(); // 旋轉方塊

void OnLeft(); // 左移方塊

void OnRight(); // 右移方塊

void OnDown(); // 下移方塊

void OnSink(); // 沈底方塊

/////////////////////////////////////////////

// 函數定義

/////////////////////////////////////////////

// 主函數

void main()

{

Init();

CMD c;

while(true)

{

c = GetCmd();

DispatchCmd(c);

// 按退出時,顯示對話框咨詢用戶是否退出

if (c == CMD_QUIT)

{

HWND wnd = GetHWnd();

if (MessageBox(wnd, _T("您要退出遊戲嗎?"), _T("提醒"), MB_OKCANCEL | MB_ICONQUESTION) == IDOK)

Quit();

}

}

}

// 初始化遊戲

void Init()

{

initgraph(640, 480);

srand((unsigned)time(NULL));

// 顯示操作說明

setfont(14, 0, _T("宋體"));

outtextxy(20, 330, _T("操作說明"));

outtextxy(20, 350, _T("上:旋轉"));

outtextxy(20, 370, _T("左:左移"));

outtextxy(20, 390, _T("右:右移"));

outtextxy(20, 410, _T("下:下移"));

outtextxy(20, 430, _T("空格:沈底"));

outtextxy(20, 450, _T("ESC:退出"));

// 設置坐標原點

setorigin(220, 20);

// 繪制遊戲區邊界

rectangle(-1, -1, WIDTH * SIZE, HEIGHT * SIZE);

rectangle((WIDTH + 1) * SIZE - 1, -1, (WIDTH + 5) * SIZE, 4 * SIZE);

// 開始新遊戲

NewGame();

}

// 退出遊戲

void Quit()

{

closegraph();

exit(0);

}

// 開始新遊戲

void NewGame()

{

// 清空遊戲區

setfillstyle(BLACK);

bar(0, 0, WIDTH * SIZE - 1, HEIGHT * SIZE - 1);

ZeroMemory(g_World, WIDTH * HEIGHT);

// 生成下壹個方塊

g_NextBlock.id = rand() % 7;

g_NextBlock.dir = rand() % 4;

g_NextBlock.x = WIDTH + 1;

g_NextBlock.y = HEIGHT - 1;

// 獲取新方塊

NewBlock();

}

// 結束遊戲

void GameOver()

{

HWND wnd = GetHWnd();

if (MessageBox(wnd, _T("遊戲結束。\n您想重新來壹局嗎?"), _T("遊戲結束"), MB_YESNO | MB_ICONQUESTION) == IDYES)

NewGame();

else

Quit();

}

// 獲取控制命令

DWORD m_oldtime;

CMD GetCmd()

{

// 獲取控制值

while(true)

{

// 如果超時,自動下落壹格

DWORD newtime = GetTickCount();

if (newtime - m_oldtime >= 500)

{

m_oldtime = newtime;

return CMD_DOWN;

}

// 如果有按鍵,返回按鍵對應的功能

if (kbhit())

{

switch(getch())

{

case 'w':

case 'W': return CMD_ROTATE;

case 'a':

case 'A': return CMD_LEFT;

case 'd':

case 'D': return CMD_RIGHT;

case 's':

case 'S': return CMD_DOWN;

case 27: return CMD_QUIT;

case ' ': return CMD_SINK;

case 0:

case 0xE0:

switch(getch())

{

case 72: return CMD_ROTATE;

case 75: return CMD_LEFT;

case 77: return CMD_RIGHT;

case 80: return CMD_DOWN;

}

}

}

// 延時 (降低 CPU 占用率)

Sleep(20);

}

}

// 分發控制命令

void DispatchCmd(CMD _cmd)

{

switch(_cmd)

{

case CMD_ROTATE: OnRotate(); break;

case CMD_LEFT: OnLeft(); break;

case CMD_RIGHT: OnRight(); break;

case CMD_DOWN: OnDown(); break;

case CMD_SINK: OnSink(); break;

case CMD_QUIT: break;

}

}

// 生成新的方塊

void NewBlock()

{

g_CurBlock.id = g_NextBlock.id, g_NextBlock.id = rand() % 7;

g_CurBlock.dir = g_NextBlock.dir, g_NextBlock.dir = rand() % 4;

g_CurBlock.x = (WIDTH - 4) / 2;

g_CurBlock.y = HEIGHT + 2;

// 下移新方塊直到有局部顯示

WORD c = g_Blocks[g_CurBlock.id].dir[g_CurBlock.dir];

while((c & 0xF) == 0)

{

g_CurBlock.y--;

c >>= 4;

}

// 繪制新方塊

DrawBlock(g_CurBlock);

// 繪制下壹個方塊

setfillstyle(BLACK);

bar((WIDTH + 1) * SIZE, 0, (WIDTH + 5) * SIZE - 1, 4 * SIZE - 1);

DrawBlock(g_NextBlock);

// 設置計時器,用於判斷自動下落

m_oldtime = GetTickCount();

}

// 畫方塊

void DrawBlock(BLOCKINFO _block, DRAW _draw)

{

WORD b = g_Blocks[_block.id].dir[_block.dir];

int x, y;

int color = BLACK;

switch(_draw)

{

case SHOW: color = g_Blocks[_block.id].color; break;

case HIDE: color = BLACK; break;

case FIX: color = g_Blocks[_block.id].color / 3; break;

}

setfillstyle(color);

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

{

if (b & 0x8000)

{

x = _block.x + i % 4;

y = _block.y - i / 4;

if (y < HEIGHT)

{

if (_draw != HIDE)

bar3d(x * SIZE + 2, (HEIGHT - y - 1) * SIZE + 2, (x + 1) * SIZE - 4, (HEIGHT - y) * SIZE - 4, 3, true);

else

bar(x * SIZE, (HEIGHT - y - 1) * SIZE, (x + 1) * SIZE - 1, (HEIGHT - y) * SIZE - 1);

}

}

b <<= 1;

}

}

// 檢測指定方塊是否可以放下

bool CheckBlock(BLOCKINFO _block)

{

WORD b = g_Blocks[_block.id].dir[_block.dir];

int x, y;

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

{

if (b & 0x8000)

{

x = _block.x + i % 4;

y = _block.y - i / 4;

if ((x < 0) || (x >= WIDTH) || (y < 0))

return false;

if ((y < HEIGHT) && (g_World[x][y]))

return false;

}

b <<= 1;

}

return true;

}

// 旋轉方塊

void OnRotate()

{

// 獲取可以旋轉的 x 偏移量

int dx;

BLOCKINFO tmp = g_CurBlock;

tmp.dir++; if (CheckBlock(tmp)) { dx = 0; goto rotate; }

tmp.x = g_CurBlock.x - 1; if (CheckBlock(tmp)) { dx = -1; goto rotate; }

tmp.x = g_CurBlock.x + 1; if (CheckBlock(tmp)) { dx = 1; goto rotate; }

tmp.x = g_CurBlock.x - 2; if (CheckBlock(tmp)) { dx = -2; goto rotate; }

tmp.x = g_CurBlock.x + 2; if (CheckBlock(tmp)) { dx = 2; goto rotate; }

return;

rotate:

// 旋轉

DrawBlock(g_CurBlock, HIDE);

g_CurBlock.dir++;

g_CurBlock.x += dx;

DrawBlock(g_CurBlock);

}

// 左移方塊

void OnLeft()

{

BLOCKINFO tmp = g_CurBlock;

tmp.x--;

if (CheckBlock(tmp))

{

DrawBlock(g_CurBlock, HIDE);

g_CurBlock.x--;

DrawBlock(g_CurBlock);

}

}

// 右移方塊

void OnRight()

{

BLOCKINFO tmp = g_CurBlock;

tmp.x++;

if (CheckBlock(tmp))

{

DrawBlock(g_CurBlock, HIDE);

g_CurBlock.x++;

DrawBlock(g_CurBlock);

}

}

// 下移方塊

void OnDown()

{

BLOCKINFO tmp = g_CurBlock;

tmp.y--;

if (CheckBlock(tmp))

{

DrawBlock(g_CurBlock, HIDE);

g_CurBlock.y--;

DrawBlock(g_CurBlock);

}

else

OnSink(); // 不可下移時,執行“沈底方塊”操作

}

// 沈底方塊

void OnSink()

{

int i, x, y;

// 連續下移方塊

DrawBlock(g_CurBlock, HIDE);

BLOCKINFO tmp = g_CurBlock;

tmp.y--;

while (CheckBlock(tmp))

{

g_CurBlock.y--;

tmp.y--;

}

DrawBlock(g_CurBlock, FIX);

// 固定方塊在遊戲區

WORD b = g_Blocks[g_CurBlock.id].dir[g_CurBlock.dir];

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

{

if (b & 0x8000)

{

if (g_CurBlock.y - i / 4 >= HEIGHT)

{ // 如果方塊的固定位置超出高度,結束遊戲

GameOver();

return;

}

else

g_World[g_CurBlock.x + i % 4][g_CurBlock.y - i / 4] = 1;

}

b <<= 1;

}

// 檢查是否需要消掉行,並標記

int row[4] = {0};

bool bRow = false;

for(y = g_CurBlock.y; y >= max(g_CurBlock.y - 3, 0); y--)

{

i = 0;

for(x = 0; x < WIDTH; x++)

if (g_World[x][y] == 1)

i++;

if (i == WIDTH)

{

bRow = true;

row[g_CurBlock.y - y] = 1;

setfillstyle(WHITE, DIAGCROSS2_FILL);

bar(0, (HEIGHT - y - 1) * SIZE + SIZE / 2 - 2, WIDTH * SIZE - 1, (HEIGHT - y - 1) * SIZE + SIZE / 2 + 2);

}

}

if (bRow)

{

// 延時 200 毫秒

Sleep(200);

// 擦掉剛才標記的行

IMAGE img;

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

{

if (row[i])

{

for(y = g_CurBlock.y - i + 1; y < HEIGHT; y++)

for(x = 0; x < WIDTH; x++)

{

g_World[x][y - 1] = g_World[x][y];

g_World[x][y] = 0;

}

getimage(&img, 0, 0, WIDTH * SIZE, (HEIGHT - (g_CurBlock.y - i + 1)) * SIZE);

putimage(0, SIZE, &img);

}

}

}

// 產生新方塊

NewBlock();

}

  • 上一篇:福州哪裏能考計算機操作員以及考試時間?
  • 下一篇:五年級作文____,妳真了不起。開頭結尾,謝謝了!
  • copyright 2024編程學習大全網