日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

c语言版本双人贪吃蛇

發布時間:2023/12/14 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言版本双人贪吃蛇 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <queue>

using namespace std;

#define WIDTH 64 // 游戲區域網格寬度
#define HEIGHT 48 // 游戲區域網格高度
#define ITEMSIZE 10 // 游戲元素大小
#define CMD_A_UP 0x1 // 控制命令:游戲者 A 向上
#define CMD_A_DOWN 0x2 // 控制命令:游戲者 A 向下
#define CMD_A_LEFT 0x4 // 控制命令:游戲者 A 向左
#define CMD_A_RIGHT 0x8 // 控制命令:游戲者 A 向右
#define CMD_B_UP 0x10 // 控制命令:游戲者 B 向上
#define CMD_B_DOWN 0x20 // 控制命令:游戲者 B 向下
#define CMD_B_LEFT 0x40 // 控制命令:游戲者 B 向左
#define CMD_B_RIGHT 0x80 // 控制命令:游戲者 B 向右
#define CMD_QUIT 0x100 // 控制命令:退出游戲

// 定義游戲元素
enum ITEM { EMPTY = 0, WALL, PLAYER_A, PLAYER_B, PLAYER_DEAD, PLAYER_A_NEXT, PLAYER_B_NEXT };

// 全局變量
ITEM g_world[WIDTH][HEIGHT]; // 保存游戲區
POINT g_player_a; // 游戲者 A 的坐標
POINT g_player_b; // 游戲者 B 的坐標
POINT g_offset_a; // 游戲者 A 的移動偏移方向
POINT g_offset_b; // 游戲者 B 的移動偏移方向

// 繪制游戲元素
void DrawItem(int x, int y, ITEM item)
{
switch(item)
{
case EMPTY: setfillcolor(BLACK); break;
case WALL: setfillcolor(LIGHTGRAY); break;
case PLAYER_A: setfillcolor(BLUE); break;
case PLAYER_B: setfillcolor(RED); break;
case PLAYER_DEAD: setfillcolor(MAGENTA); break;
}
bar(x * ITEMSIZE + 1, y * ITEMSIZE + 1, (x + 1) * ITEMSIZE - 2, (y + 1) * ITEMSIZE - 2);
g_world[x][y] = item;
}

// 初始化游戲
void init()
{
int x, y;


// 繪制墻壁
for(x = 0; x < WIDTH; x++)
{
DrawItem(x, 0, WALL);
DrawItem(x, HEIGHT - 1, WALL);
}
for(y = 1; y < HEIGHT - 1; y++)
{
DrawItem(0, y, WALL);
DrawItem(WIDTH - 1, y, WALL);
}
// 繪制游戲區域
for (x = 1; x < WIDTH - 1; x++)
for (y = 1; y < HEIGHT - 1; y++)
DrawItem(x, y, EMPTY);
// 隨機產生兩個游戲者的位置(至少間隔 5 格)
do
{
g_player_a.x = rand() % (WIDTH - 6) + 3; g_player_a.y = rand() % (HEIGHT - 6) + 3;
g_player_b.x = rand() % (WIDTH - 6) + 3; g_player_b.y = rand() % (HEIGHT - 6) + 3;
}while ( ?(g_player_b.x - g_player_a.x) * (g_player_b.x - g_player_a.x)
+ (g_player_b.y - g_player_a.y) * (g_player_b.y - g_player_b.x) <= 25);
// 畫出兩個游戲者的位置
DrawItem(g_player_a.x, g_player_a.y, PLAYER_A);
DrawItem(g_player_b.x, g_player_b.y, PLAYER_B);

// 隨機產生兩個游戲者的移動方向
// 該方法的原理詳見:http://www.easyx.cn/skills/View.aspx?id=115
int n;
n = (rand() % 4) * 2 + 1; g_offset_a.x = n / 3 - 1; g_offset_a.y = n % 3 - 1;
n = (rand() % 4) * 2 + 1; g_offset_b.x = n / 3 - 1; g_offset_b.y = n % 3 - 1;

// 繪制 Player A 空心方塊提示移動方向
int tx = g_player_a.x + g_offset_a.x;
int ty = g_player_a.y + g_offset_a.y;
setcolor(BLUE);
rectangle(tx * ITEMSIZE + 1, ty * ITEMSIZE + 1, (tx + 1) * ITEMSIZE - 2, (ty + 1) * ITEMSIZE - 2);
// 繪制 Player B 空心方塊提示移動方向
tx = g_player_b.x + g_offset_b.x;
ty = g_player_b.y + g_offset_b.y;
setcolor(RED);
rectangle(tx * ITEMSIZE + 1, ty * ITEMSIZE + 1, (tx + 1) * ITEMSIZE - 2, (ty + 1) * ITEMSIZE - 2);

// 按確定開始游戲
MessageBox(GetHWnd(), _T("對戰貪吃蛇 游戲說明:\n\n") \
_T("游戲目標:兩條蛇,先碰到墻壁或碰到任何蛇的身體就算輸。\n") \
_T("Player A 使用 A S D W 控制藍色小蛇移動方向。\n") \
_T("Player B 使用上下左右控制紅色小蛇移動方向。\n\n") \
_T("點“確定”按鈕開始游戲。"), _T("游戲說明"), MB_OK | MB_ICONINFORMATION);
}

// 獲取用戶命令
int GetCmd()
{
// 定義兩個用戶的命令隊列
static queue<int> PLAYER_A_CMD;
static queue<int> PLAYER_B_CMD;


// 定義每次返回的命令
int cmd = 0;


// 處理按鍵
while(_kbhit())
{
switch(_getch())
{
case ?27: cmd ?= CMD_QUIT; break;
case 'W': case 'w': if (PLAYER_A_CMD.size() < 16) PLAYER_A_CMD.push(CMD_A_UP); break;
case 'S': case 's': if (PLAYER_A_CMD.size() < 16) PLAYER_A_CMD.push(CMD_A_DOWN); break;
case 'A': case 'a': if (PLAYER_A_CMD.size() < 16) PLAYER_A_CMD.push(CMD_A_LEFT); break;
case 'D': case 'd': if (PLAYER_A_CMD.size() < 16) PLAYER_A_CMD.push(CMD_A_RIGHT); break;
case ?0 : case 0xE0:
switch(_getch())
{
case 72: if (PLAYER_B_CMD.size() < 16) PLAYER_B_CMD.push(CMD_B_UP); break;
case 80: if (PLAYER_B_CMD.size() < 16) PLAYER_B_CMD.push(CMD_B_DOWN); break;
case 75: if (PLAYER_B_CMD.size() < 16) PLAYER_B_CMD.push(CMD_B_LEFT); break;
case 77: if (PLAYER_B_CMD.size() < 16) PLAYER_B_CMD.push(CMD_B_RIGHT); break;
}
}
}

// 讀取 Player A 的命令
int c = 0;
while(!PLAYER_A_CMD.empty())
{
c = PLAYER_A_CMD.front();
PLAYER_A_CMD.pop();
if ((c == CMD_A_UP || c == CMD_A_DOWN) ?&& g_offset_a.x != 0) break;
if ((c == CMD_A_LEFT || c == CMD_A_RIGHT) && g_offset_a.y != 0) break;
}
if (c != 0)
cmd |= c;

// 讀取 Player B 的命令
c = 0;
while(!PLAYER_B_CMD.empty())
{
c = PLAYER_B_CMD.front();
PLAYER_B_CMD.pop();
if ((c == CMD_B_UP || c == CMD_B_DOWN) ?&& g_offset_b.x != 0) break;
if ((c == CMD_B_LEFT || c == CMD_B_RIGHT) && g_offset_b.y != 0) break;
}
if (c != 0) cmd |= c;


// 返回命令
return cmd;
}

// 處理命令
bool DealCmd(int cmd)
{
if ((cmd & CMD_A_UP) && g_offset_a.x != 0) { g_offset_a.x = 0; g_offset_a.y = -1; }
if ((cmd & CMD_A_DOWN) && g_offset_a.x != 0) { g_offset_a.x = 0; g_offset_a.y = 1; }
if ((cmd & CMD_A_LEFT) && g_offset_a.y != 0) { g_offset_a.x = -1; g_offset_a.y = 0; }
if ((cmd & CMD_A_RIGHT) && g_offset_a.y != 0) { g_offset_a.x = 1; g_offset_a.y = 0; }
if ((cmd & CMD_B_UP) && g_offset_b.x != 0) { g_offset_b.x = 0; g_offset_b.y = -1; }
if ((cmd & CMD_B_DOWN) && g_offset_b.x != 0) { g_offset_b.x = 0; g_offset_b.y = 1; }
if ((cmd & CMD_B_LEFT) && g_offset_b.y != 0) { g_offset_b.x = -1; g_offset_b.y = 0; }
if ((cmd & CMD_B_RIGHT) && g_offset_b.y != 0) { g_offset_b.x = 1; g_offset_b.y = 0; }
if (cmd & CMD_QUIT)
if (MessageBox(GetHWnd(), _T("您要退出游戲嗎?"), _T("游戲暫停"), MB_OKCANCEL) == IDOK)
return false;


return true;
}
// 處理游戲
bool DealGame()
{
// Player A、B 前進
g_player_a.x += g_offset_a.x;
g_player_a.y += g_offset_a.y;
g_player_b.x += g_offset_b.x;
g_player_b.y += g_offset_b.y;


// 判斷 Player A、B 的生死狀態
bool dead_a = false, dead_b = false, dead_ab = false;


if (g_player_a.x == g_player_b.x && g_player_a.y == g_player_b.y)
{
DrawItem(g_player_a.x, g_player_a.y, PLAYER_DEAD);
dead_ab = true;
}
else if (g_world[g_player_a.x][g_player_a.y] != EMPTY)
{
DrawItem(g_player_a.x, g_player_a.y, PLAYER_DEAD);
dead_a = true;
}
else if (g_world[g_player_b.x][g_player_b.y] != EMPTY)
{
DrawItem(g_player_b.x, g_player_b.y, PLAYER_DEAD);
dead_b = true;
}
else
{
DrawItem(g_player_a.x, g_player_a.y, PLAYER_A);
DrawItem(g_player_b.x, g_player_b.y, PLAYER_B);
return true;
}

// 判斷是否要重新開始
bool restart = false;

if (dead_ab || (dead_a && dead_b))
restart = MessageBox(GetHWnd(), _T("Player A 和 Player B 都死了。\n要再來一局嗎?"),
_T("GAME OVER"), MB_YESNO | MB_ICONINFORMATION) == IDYES;
else if (dead_a)
restart = MessageBox(GetHWnd(), _T("Player A 死了。\n要再來一局嗎?"),
_T("GAME OVER"), MB_YESNO | MB_ICONINFORMATION) == IDYES;
else if(dead_b)
restart = MessageBox(GetHWnd(), _T("Player B 死了。\n要再來一局嗎?"),
_T("GAME OVER"), MB_YESNO | MB_ICONINFORMATION) == IDYES;

if (restart)
{
init();
return true;
}
else
return false;
}

// 主函數
void main()
{
initgraph(640, 480);
srand((unsigned)time(NULL));


// 初始化
init();


// 游戲主循環
while(true)
{
int cmd = GetCmd(); // 獲取用戶命令
if (!DealCmd(cmd)) break; // 處理命令
if (!DealGame()) break; // 處理游戲
Sleep(200); // 延時
}


// 關閉繪圖窗口
closegraph();
}

總結

以上是生活随笔為你收集整理的c语言版本双人贪吃蛇的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。