C語(yǔ)言實(shí)現(xiàn)控制臺(tái)版貪吃蛇游戲
用c語(yǔ)言寫的期末作業(yè):C語(yǔ)言實(shí)現(xiàn)控制臺(tái)版貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下
/*
{
conio.h
阻塞式: getch(): 從無(wú)回顯的控制臺(tái)獲取字符。無(wú)緩沖區(qū),只有當(dāng)按下一個(gè)鍵才會(huì)執(zhí)行后面的程序。
非阻塞式: kbhit() 檢測(cè)緩沖區(qū)中是否有字符;執(zhí)行該函數(shù)后程序不會(huì)停下,而是繼續(xù)執(zhí)行下面的代碼
由于getch()和kbhit()已棄用,
在編譯時(shí)會(huì)產(chǎn)生警告,
可以用_getch()和_kbhit()替換它們,
或者在包含頭文件前加上#pragma warning(disable : 4996)關(guān)閉警告
}
*/
#pragma warning(disable : 4996) //用來(lái)關(guān)閉警告
#include<stdio.h>
#include <stdlib.h>
#include <conio.h> //_kbhit()函數(shù)獲取鍵盤事件
#include <time.h>
#include<Windows.h>
#define WIN_HEIGH 30 //窗口高度
#define WIN_WIDTH 105 //窗口寬度
#define G_HEIGHT 30 //游戲界面高度
#define G_WIDTH 80 //游戲界面寬度
#define STATUS int //函數(shù)返回狀態(tài)屬性
#define SUCCESS 1 //執(zhí)行成功
#define FAIL 0 //執(zhí)行失敗
typedef struct snake_node{ //蛇的某個(gè)點(diǎn)的信息
char direct; //即將前進(jìn)的方向 udlr分別對(duì)應(yīng)上下左右,剛吃掉的身體部分方向?yàn)閛
COORD postion; //該點(diǎn)所在位置
struct snake_node* next_node; //下個(gè)節(jié)點(diǎn)的指針
}*Ps_node,S_node;
typedef struct snake {
char head_type; //蛇頭的樣子
char body_type; //蛇身的樣子
WORD color; //顏色
int level; //等級(jí)
int speed; //速度
int score; //成績(jī)
int control; //控制器 即按wasd進(jìn)行移動(dòng),或者按上下左右進(jìn)行移動(dòng)
S_node head; //蛇頭
}*Psnake,Snake; //蛇結(jié)構(gòu)
// 0輸出空格 -1輸出#
char canvas[G_HEIGHT][G_WIDTH] ; //畫布
int bean_count; //已創(chuàng)建食物的數(shù)量
POINT reward_position; //獎(jiǎng)勵(lì)的位置
int reward_time = -1000; //獎(jiǎng)勵(lì)剩余時(shí)間
int run_snake_threads; //正在運(yùn)行的蛇的數(shù)量
char keyboard_out[2]; //鍵盤的輸出控制器 keyboard_out[0],代表控制器1,keyboard_out[1],代表控制器2,
int init_snake_speed = 400; //蛇的初始速度 ,例=400時(shí),蛇0.4s前進(jìn)一步
//關(guān)于顏色取值,請(qǐng)查看help.txt
void gotoxy(int x, int y); //設(shè)置光標(biāo)到指定位置
void setcolor(WORD wAttributes); //設(shè)置輸出顏色
void prc_toxy(char c, int x, int y); //指定位置輸出字符
void prs_toxy(char* str, int x, int y); //指定位置輸出字符串
void prc_colortoxy(char str, int x, int y, WORD wAttributes); //在指定位置,以wAttributes顏色輸出字符
void prs_colortoxy(char* str, int x, int y, WORD wAttributes); //在指定位置,以wAttributes顏色輸出字符字符串
void one_play_mod(); //進(jìn)入單人模式
void two_play_mod(); //進(jìn)入雙人模式
/***********************
函數(shù)名:init_system
功能:
獲取窗口句柄
設(shè)置控制臺(tái)顏色和大小
隱藏光標(biāo)顯示
************************/
void init_system();
/***********************
函數(shù)名:init_snode
功能:初始化新的身體節(jié)點(diǎn)
輸入?yún)?shù):
direct:這個(gè)節(jié)點(diǎn)的下一個(gè)運(yùn)動(dòng)方向;
snake_x,snake_y:這個(gè)節(jié)點(diǎn)的位置(x,y)
next_node:下一個(gè)節(jié)點(diǎn)的指針
返回值:
Ps_node:返回已初始化的節(jié)點(diǎn)的指針
************************/
Ps_node init_snode(Ps_node n_snode,char drect,int x,int y,Ps_node next_node);
/************************
函數(shù)名:init_snake
功能:初始化蛇的相關(guān)參數(shù),并為其創(chuàng)建身體,長(zhǎng)度為三
輸入?yún)?shù):
she: 要初始化的蛇指針
color: 蛇的顏色
head_type: 用該字符顯示蛇的身體
x,y: 蛇最開(kāi)始的位置
direct: 默認(rèn)前進(jìn)方向
control: 移動(dòng)蛇的方式:默認(rèn)1為WASD鍵,2為方向鍵盤上下左右鍵
返回值:初始化后的蛇指針
**************************/
Psnake init_snake(Psnake she ,WORD color,char head_type,char body_type,int x,int y, char direct, int control); //初始化小蛇
DWORD WINAPI Run_Snake_Thread(LPVOID lpParam); //線程:運(yùn)行一條蛇
DWORD WINAPI Del_reward_Thread(LPVOID lpParam); //線程:生成獎(jiǎng)勵(lì)后,一段時(shí)間內(nèi)不遲到獎(jiǎng)勵(lì)會(huì)消失
DWORD WINAPI Render_Thread(LPVOID lpParam); //線程:用來(lái)渲染,游戲運(yùn)行時(shí),所有的打印顯示由該進(jìn)程完成
DWORD WINAPI Keyboard_Thread(LPVOID lpParam); //線程:游戲運(yùn)行時(shí),監(jiān)聽(tīng)按鍵輸入,并設(shè)置keyboard_out
/************************
函數(shù)名:get_canvas_toxy
功能: 獲取畫布上該點(diǎn)的值
輸入?yún)?shù):postion
返回值:char類型,畫布上該點(diǎn)的值
************************/
char get_canvas_toxy(COORD postion);
/***********************
函數(shù)名:snode_gonext
功能: 蛇身體上的點(diǎn)移動(dòng)到下個(gè)位置
輸入?yún)?shù):
snode:要移動(dòng)的點(diǎn)
direct:要移動(dòng)的方向
返回值:char ,未移動(dòng)前,該點(diǎn)要前進(jìn)的方向
************************/
char snode_gonext(Ps_node snode, char direct);
/***********************
函數(shù)名:snake_gonext
功能: 蛇移動(dòng)到下個(gè)位置
輸入?yún)?shù):
she:要移動(dòng)的蛇
direct:蛇頭移動(dòng)的方向
返回值:無(wú)
************************/
void snake_gonext(Psnake she, char direct);
/********************
函數(shù)名:init_canvas
功能: 初始化畫布,并為畫布設(shè)置邊框
輸入?yún)?shù): 無(wú)
返回值: 無(wú)
********************/
void init_canvas();
void draw_wall(); //在canvas上設(shè)置邊框位置
/***********************
函數(shù)名:snaketocanvas
功能:將蛇的坐標(biāo)信息拷貝到畫布canvas上,在canvas上刪除原來(lái)的位置信息,
輸入?yún)?shù):she :要拷貝的蛇的指針
***********************/
void snaketocanvas(Psnake she);
/**************************
函數(shù)名:eat_node
功能:蛇吃食物,并把它加入蛇身,并增加成績(jī),觸發(fā)升級(jí)加速
(注:加入到蛇身后該點(diǎn)的位置還在原來(lái)的地方,需要身體都經(jīng)過(guò)那里后長(zhǎng)度才會(huì)增加)
輸入?yún)?shù):
she:吃食物的那條蛇
position:食物的位置
**************************/
void eat_node(Psnake she, COORD postion);
/**********************
函數(shù)名:create_bean
功能:在畫布的一個(gè)隨機(jī)位置創(chuàng)建豆
返回值:
status:返回是否創(chuàng)建成功
**********************/
STATUS createrand_bean();
/**********************
函數(shù)名:free_snake
功能:釋放小蛇的內(nèi)存
輸入?yún)?shù):
she:要釋放內(nèi)存的小蛇
************************/
void free_snake(Psnake she);
void menu(); //開(kāi)始菜單, 用來(lái)選擇模式,單人或者雙人
void show_menu(int flag); //根據(jù)不同的flag顯示不同的菜單
HANDLE handle;
int main() {
init_system();
menu();
return 0;
}
void init_system() {
system("color F0 ");
handle = GetStdHandle(STD_OUTPUT_HANDLE);//獲取窗口句柄
char chCmd[32];
sprintf(chCmd, "mode con cols=%d lines=%d", WIN_WIDTH, WIN_HEIGH); //設(shè)置控制臺(tái)大小
system(chCmd);
CONSOLE_CURSOR_INFO cursor_info = { 1,0 }; //設(shè)置光標(biāo)不可見(jiàn),第一個(gè)值為光標(biāo)厚度,第二個(gè)值為光標(biāo)是否顯示
SetConsoleCursorInfo(handle, &cursor_info); //
srand((int)time(NULL)); //初始化隨機(jī)數(shù)種子
}
void menu() {
int ch1, ch2;
int flag = 1; //標(biāo)志當(dāng)前選擇模式
show_menu(flag);
while (1) {
ch1 = _getch();
if (ch1 == 13) {
printf("此處是進(jìn)入函數(shù)\n");
if (flag == 1)
one_play_mod();
else if (flag == 2)
two_play_mod();
else {
system("cls");
printf("退出\n");
break;
}
}
if (ch1==224) {
ch2 = _getch();
if (ch2 == 72) {
flag = (flag + 2)% 3;
}
else if (ch2 == 80) {
flag = (flag + 4) % 3;
}
}
show_menu(flag);
}
}
void show_menu(int flag) {
char gamename[30] = "貪 吃 蛇";
char mod1[20] = "單人模式";
char mod2[20] = "雙人模式";
char modexit[20] = "退出";
int xstr = (WIN_WIDTH - strlen(mod1)) / 2; //要打印選項(xiàng)的位置x列
int ystr; //要打印選項(xiàng)的位置y行
WORD color[3] = {0xf0,0xf0,0xf0}; //要設(shè)置的顏色
system("cls");
setcolor(0xf0);
prs_toxy(gamename, (WIN_WIDTH - strlen(gamename) )/ 2, 5);
color[flag] = FOREGROUND_RED | 0xf0;
prs_colortoxy(mod1, (WIN_WIDTH - strlen(mod1)) / 2, 11, color[1]);
prs_colortoxy(mod2, (WIN_WIDTH - strlen(mod2)) / 2, 13, color[2]);
prs_colortoxy(modexit, (WIN_WIDTH - strlen(modexit)) / 2, 15, color[0]);
if (flag == 1) ystr = 11; else if (flag == 2) ystr = 13; else ystr = 15; //設(shè)置箭頭位于哪一行
prs_colortoxy("→", xstr - 4, ystr, color[flag]);
gotoxy(0, 0);
}
void gotoxy(int x, int y) {
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void setcolor(WORD wAttributes) {
SetConsoleTextAttribute(handle, wAttributes);
}
void prc_toxy(char c, int x, int y) {
gotoxy(x, y);
printf("%c", c);
}
void prs_toxy(char* str, int x, int y) {
gotoxy(x, y);
printf("%s", str);
}
void prc_colortoxy(char c, int x, int y, WORD wAttributes) {
gotoxy(x, y);
SetConsoleTextAttribute(handle, wAttributes);
printf("%c", c);
}
void prs_colortoxy(char* str, int x, int y, WORD wAttributes) {
gotoxy(x, y);
SetConsoleTextAttribute(handle, wAttributes);
printf("%s", str);
}
Ps_node init_snode(Ps_node n_snode, char drect, int x, int y, Ps_node next_node) {
n_snode->direct = drect;
n_snode->postion.X = x;
n_snode->postion.Y = y;
n_snode->next_node = next_node;
return n_snode;
}
Psnake init_snake(Psnake she , WORD color, char head_type, char body_type, int x, int y, char direct,int control) {
int ax=x,ay=y,bx=x,by=y;
she->head_type = head_type;
she->body_type = body_type;
she->color = color;
she->control = control;
she->level = 1;
she->speed = init_snake_speed;
she->score = 0;
Ps_node s1 = (Ps_node)malloc(sizeof(S_node));
Ps_node s2 = (Ps_node)malloc(sizeof(S_node));
switch (direct)
{
case 'l':
ax = x + 2; bx = x + 1;
break;
case 'r':
ax = x - 2; bx = x - 1;
break;
case 'u':
ay = y + 2; by = y + 1;
break;
case 'd':
ay = y - 2; by = y - 1;
break;
default:
break;
}
init_snode(s2, direct, ax, ay, NULL); //要根據(jù)方向設(shè)置下一個(gè)節(jié)點(diǎn)的位置
init_snode(s1, direct, bx , by, s2);
init_snode(&she->head, direct, x, y,s1);
return she;
}
void one_play_mod() {
HANDLE hThread;
HANDLE hThread_render;
HANDLE hT_getkey;
Snake xiaoshe;
system("cls");
bean_count = 0;
init_canvas();
init_snake(&xiaoshe,0xf2, '@','*', G_WIDTH/2, G_HEIGHT/2, 'r', 2);
snaketocanvas(&xiaoshe);
createrand_bean();
run_snake_threads = 1;
draw_wall();
hThread = CreateThread(NULL, 0, Run_Snake_Thread,(LPVOID)&xiaoshe, 0, NULL);
hThread_render = CreateThread(NULL, 0, Render_Thread, (LPVOID)&xiaoshe, 0, NULL);
hT_getkey= CreateThread(NULL, 0,Keyboard_Thread, NULL, 0, NULL);
WaitForSingleObject(hThread, INFINITE);
run_snake_threads = 0;
WaitForSingleObject(hT_getkey, INFINITE);
WaitForSingleObject(hThread_render, INFINITE);
CloseHandle(hThread);
CloseHandle(hT_getkey);
CloseHandle(hThread_render);
gotoxy(G_WIDTH / 2-2, G_HEIGHT / 2);
printf("游戲結(jié)束\n");
Sleep(2000);
}
void two_play_mod() {
HANDLE hThread_a;
HANDLE hThread_b;
HANDLE hThread_render;
HANDLE hT_getkey;
Snake she[2];
system("cls");
bean_count = 0;
init_canvas();
init_snake(&she[0], 0xf2, '@', '*', G_WIDTH / 2-4, G_HEIGHT / 2 -4, 'r', 2);
init_snake(&she[1], 0xE5, 'U', 'S', G_WIDTH / 2, G_HEIGHT / 2, 'l', 1);
snaketocanvas(&she[0]);
snaketocanvas(&she[1]);
draw_wall();
createrand_bean();
/*createrand_bean(); //創(chuàng)建多個(gè)食物
createrand_bean();
createrand_bean();*/
run_snake_threads = 2;
hThread_b = CreateThread(NULL, 0, Run_Snake_Thread, (LPVOID)&she[0], 0, NULL);
hThread_a = CreateThread(NULL, 0, Run_Snake_Thread, (LPVOID)&she[1], 0, NULL);
hThread_render= CreateThread(NULL, 0, Render_Thread, (LPVOID)&she, 0, NULL);
hT_getkey = CreateThread(NULL, 0, Keyboard_Thread, NULL, 0, NULL);
WaitForSingleObject(hThread_a, INFINITE);
WaitForSingleObject(hThread_b, INFINITE);
run_snake_threads = 0;
WaitForSingleObject(hT_getkey, INFINITE);
WaitForSingleObject(hThread_render, INFINITE);
CloseHandle(hThread_a);
CloseHandle(hThread_b);
CloseHandle(hT_getkey);
CloseHandle(hThread_render);
gotoxy(G_WIDTH / 2, G_HEIGHT / 2);
printf("游戲結(jié)束\n");
Sleep(1000);
}
DWORD WINAPI Keyboard_Thread(LPVOID lpParam) {
int ch1;
int ch2;
keyboard_out[0] = 'n'; keyboard_out[1] = 'n';
while (run_snake_threads > 0) {
while (_kbhit())
{
ch1 = _getch();
switch (ch1)
{
case 'w':
keyboard_out[0] = 'u';
break;
case 's':
keyboard_out[0] = 'd';
break;
case 'd':
keyboard_out[0] = 'r';
break;
case 'a':
keyboard_out[0] = 'l';
break;
case 224:
ch2 = _getch();
switch (ch2)
{
case 72:
keyboard_out[1] = 'u';
break;
case 80:
keyboard_out[1] = 'd';
break;
case 77:
keyboard_out[1] = 'r';
break;
case 75:
keyboard_out[1] = 'l';
break;
default:
break;
}
break;
default:
break;
}
}
Sleep(50);
}
}
char get_canvas_toxy(COORD postion) {
return canvas[postion.Y][postion.X];
}
char snode_gonext(Ps_node snode, char direct) {
char old_direct = snode->direct;
snode->direct = direct;
switch (direct)
{
case 'u':
snode->postion.Y = snode->postion.Y - 1;
break;
case 'd':
snode->postion.Y = snode->postion.Y + 1;
break;
case 'l':
snode->postion.X = snode->postion.X - 1;
break;
case 'r':
snode->postion.X = snode->postion.X + 1;
break;
default:
break;
}
return old_direct;
}
void snake_gonext(Psnake she, char direct) {
Ps_node temp=&she->head;
Ps_node end = temp;
char dta=direct; //方向
while (temp!=NULL) //不處理最后一個(gè)節(jié)點(diǎn)
{
if(temp->next_node!=NULL && temp->next_node->direct=='n'){
if (temp->postion.X == temp->next_node->postion.X && temp->postion.Y == temp->next_node->postion.Y) {
dta = snode_gonext(temp, dta);
temp->next_node->direct = dta;
}else
dta = snode_gonext(temp, dta);
break;
}
dta=snode_gonext(temp, dta);
temp = temp->next_node;
}
snaketocanvas(she);
}
void snaketocanvas(Psnake she) {
Ps_node temp=&she->head;
int i, j,x,y;
char body_type = she->body_type;
for (i = 1; i < G_HEIGHT - 1; i++)
for (j = 1; j < G_WIDTH - 1; j++)
if (canvas[i][j] == she->head_type || canvas[i][j] == she->body_type)
canvas[i][j] = '0';
x = temp->postion.X;
y = temp->postion.Y;
while (temp->next_node != NULL) {
canvas[temp->next_node->postion.Y][temp->next_node->postion.X] = body_type;
temp = temp->next_node;
}
canvas[y][x] = she->head_type; //單獨(dú)處理蛇頭,放在最后防止被覆蓋
}
void draw_wall() {
int i, j;
setcolor(0xf0);
gotoxy(0, 0);
for (i = 0; i < G_HEIGHT; i++) {
gotoxy(0, i);
for (j = 0; j < G_WIDTH; j++) {
if (canvas[i][j] == '#')
printf("#");
else
printf(" ");
}
}
}
DWORD WINAPI Render_Thread(LPVOID lpParam) {
Psnake shea = (Psnake)lpParam;
Psnake sheb = (run_snake_threads==2)?((Psnake)lpParam+1):(shea);
WORD color;
char heada = shea->head_type;
char headb = shea->head_type;
char pr;
char pr_score[50];
char show_time[30];
int snakes_counts = run_snake_threads;
int i, j;
if (snakes_counts == 1) { //單人模式
prs_colortoxy("player 1", G_WIDTH + 3, 6, 0xf0);
prs_colortoxy("↑", G_WIDTH + 15, 5, 0xf0);
prs_colortoxy("←↓→",G_WIDTH + 13, 6, 0xf0);
}
else {
prs_colortoxy("player 1", G_WIDTH + 3, 6, 0xf0);
prs_colortoxy("↑", G_WIDTH + 15, 5, 0xf0);
prs_colortoxy("←↓→", G_WIDTH + 13, 6, 0xf0);
prs_colortoxy("player 2", G_WIDTH + 3, 19, 0xf0);
prs_colortoxy("W", G_WIDTH + 14, 18, 0xf0);
prs_colortoxy("ASD", G_WIDTH + 13, 19, 0xf0);
}
while (run_snake_threads != 0) //根據(jù)snake線程數(shù)結(jié)束循環(huán)
{
for (i = 1; i < G_HEIGHT-1; i++) {
gotoxy(1, i);
for (j = 1; j < G_WIDTH-1; j++) {
switch (canvas[i][j])
{
case '@': //蛇頭
color = (heada == '@') ? shea->color : sheb->color;
pr = '@';
break;
case '*': //蛇身
color = (shea->body_type == '*') ? shea->color : sheb->color;
pr = '*';
break;
case 'U': //蛇頭
color = (shea->head_type == 'U') ? shea->color : sheb->color;
pr = 'U';
break;
case 'S': //蛇身
color = (shea->body_type == 'S') ? shea->color : sheb->color;
pr = 'S';
break;
case '$': //豆子
color = 0xf6;
pr = '$';
break;
case '&': //獎(jiǎng)勵(lì)
color = 0xa4;
pr = '&';
break;
case '0':
color = 0xf0;
pr = ' ';
break;
default:
break;
}
setcolor(color);
printf("%c", pr);
}
}
sprintf(pr_score, "分?jǐn)?shù): %d", shea->score);
prs_colortoxy(pr_score, G_WIDTH + 3, 8, 0xf0);
if (snakes_counts == 2) {
sprintf(pr_score, "分?jǐn)?shù): %d", sheb->score);
prs_colortoxy(pr_score, G_WIDTH + 3, 21, 0xf0);
}
if (reward_time >= 0) {
sprintf(show_time, "獎(jiǎng)勵(lì)剩余時(shí)間:%ds ", reward_time / 1000);
prs_colortoxy(show_time, G_WIDTH + 5, G_HEIGHT - 3, 0xf4);
}
else {
prs_colortoxy(" ", G_WIDTH + 5, G_HEIGHT - 3, 0xf4);
}
Sleep(50); //顯示頻率
}
}
STATUS createrand_bean() {
int ran; //canvas的第n個(gè)點(diǎn)為獎(jiǎng)勵(lì)位置
int i,j,count,sum=0;
char c = '$';
HANDLE delrewarthread;
for (i = 1; i < G_HEIGHT - 1; i++)
for (j = 1; j < G_WIDTH - 1; j++)
if (canvas[i][j] == '0')
sum++;
if (sum == 0)
return FAIL;
if (bean_count % 5 == 0 && bean_count!=0) {
ran = rand() % sum;
count = 0;
for (i = 1; i < G_HEIGHT - 1; i++)
for (j = 1; j < G_WIDTH - 1; j++)
if (canvas[i][j] == '0') {
if (count == ran) {
reward_position.x = j;
reward_position.y = i;
canvas[i][j] = '&';
delrewarthread= CreateThread(NULL, 0, Del_reward_Thread, NULL, 0, NULL);
CloseHandle(delrewarthread);
}
count++;
}
}
bean_count++;
ran = rand() % sum;
count = 0;
for (i = 1; i < G_HEIGHT - 1; i++)
for (j = 1; j < G_WIDTH - 1; j++)
if (canvas[i][j] == '0') {
if (count == ran) {
canvas[i][j] = c;
return SUCCESS;
}
count++;
}
return FAIL;
}
void eat_node(Psnake she, COORD postion) {
Ps_node end = &she->head;
Ps_node s = (Ps_node)malloc(sizeof(S_node));
while (end->next_node != NULL)
end = end->next_node;
init_snode(s,'n', postion.X, postion.Y, NULL);
end->next_node = s;
canvas[postion.Y][postion.X]=she->head_type;
she->score++;
if (she->score / 10 >= she->level) {
she->level++;
she->speed = (int)(she->speed * 0.75);
}
}
void free_snake(Psnake she) {
Ps_node body = she->head.next_node;
Ps_node temp;
while (body!=NULL)
{
temp = body->next_node;
free(body);
body = temp;
}
}
DWORD WINAPI Run_Snake_Thread(LPVOID lpParam) {
Psnake pshe = (Psnake)lpParam;
char direct;
COORD next_node; //下一個(gè)蛇頭位置
char v_canvas;
boolean end = 0;
while (!end) {
direct = pshe->head.direct;
if (keyboard_out[pshe->control - 1] != 'n') { //獲取鍵盤輸入的目的是,獲取蛇要前往的下一個(gè)方向
switch (keyboard_out[pshe->control - 1]) //輸入方向與運(yùn)行方向相反
{
case 'u':
direct = (direct != 'd') ? 'u' : direct;
break;
case 'd':
direct = (direct != 'u') ? 'd' : direct;
break;
case 'l':
direct = (direct != 'r') ? 'l' : direct;
break;
case 'r':
direct = (direct != 'l') ? 'r' : direct;
break;
default:
break;
}
keyboard_out[pshe->control - 1] = 'n';
}
next_node = pshe->head.postion;
switch (direct)
{
case 'u':
next_node.Y = next_node.Y - 1;
break;
case 'd':
next_node.Y = next_node.Y + 1;
break;
case 'l':
next_node.X = next_node.X - 1;
break;
case 'r':
next_node.X = next_node.X + 1;
break;
default:
break;
}
v_canvas = get_canvas_toxy(next_node);
switch (v_canvas)
{
case '0': //表示該點(diǎn)安全,沒(méi)有東西
snake_gonext(pshe, direct);
break;
case '$':
eat_node(pshe, next_node);
snake_gonext(pshe, direct);
end = !createrand_bean();
break;
case '&': //吃下道具,觸發(fā)減速
eat_node(pshe, next_node);
snake_gonext(pshe, direct);
pshe->speed =(int) (pshe->speed * 1.25);
break;
default:
end = 1;
break;
}
Sleep(pshe->speed);
}
free_snake(pshe); //這里只釋放了蛇的身體其他信息仍保留
Sleep(500);
return 0;
}
DWORD WINAPI Del_reward_Thread(LPVOID lpParam) {
reward_time = 10000;
while (reward_time >=0)
{
if (canvas[reward_position.y][reward_position.x] == '&' && run_snake_threads>0) {
Sleep(1000);
reward_time = reward_time - 1000;
}
else
break;
}
canvas[reward_position.y][reward_position.x] = '0';
reward_time = -1000;
return 0;
}
void init_canvas() {
int i, j;
for (i = 0; i < G_HEIGHT; i++)
for (j = 0; j < G_WIDTH; j++)
canvas[i][j] = '0';
for (i = 0; i < G_HEIGHT ; i++) {
canvas[i][0] = '#';
canvas[i][G_WIDTH - 1] = '#';
}
for (j = 0; j < G_WIDTH; j++) {
canvas[0][j] = '#';
canvas[G_HEIGHT - 1][j] = '#';
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲
- 用純C語(yǔ)言實(shí)現(xiàn)貪吃蛇游戲
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇小游戲
- Linux下C語(yǔ)言實(shí)現(xiàn)貪吃蛇小游戲
- C語(yǔ)言代碼鏈表實(shí)現(xiàn)貪吃蛇游戲
- 基于C語(yǔ)言實(shí)現(xiàn)的貪吃蛇游戲完整實(shí)例代碼
- 貪吃蛇C語(yǔ)言代碼實(shí)現(xiàn)(難度可選)
- C語(yǔ)言貪吃蛇經(jīng)典小游戲
- C語(yǔ)言實(shí)現(xiàn)貪吃蛇游戲
- C語(yǔ)言實(shí)現(xiàn)貪吃蛇游戲演示
相關(guān)文章
C++設(shè)計(jì)模式中的觀察者模式一起來(lái)看看
這篇文章主要為大家詳細(xì)介紹了C++觀察者模式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03
C#?CLR學(xué)習(xí)?C++使用namespace實(shí)例詳解
這篇文章主要為大家介紹了C#?CLR學(xué)習(xí)?C++使用namespace實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
C語(yǔ)言新手練習(xí)題之求第n個(gè)斐波那契數(shù)
斐波那契數(shù)列這一個(gè)大一上C語(yǔ)言就有的問(wèn)題大家應(yīng)該都不陌生,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言新手練習(xí)題之求第n個(gè)斐波那契數(shù)的相關(guān)資料,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
vs2022啟動(dòng)一個(gè)CmakeLists.txt項(xiàng)目的實(shí)踐
本文主要介紹了vs2022啟動(dòng)一個(gè)CmakeLists.txt項(xiàng)目的實(shí)踐,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06
C++實(shí)現(xiàn)LeetCode(228.總結(jié)區(qū)間)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(228.總結(jié)區(qū)間),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語(yǔ)言文字藝術(shù)之?dāng)?shù)據(jù)輸入輸出
這篇文章主要介紹了C語(yǔ)言文字藝術(shù)之?dāng)?shù)據(jù)輸入輸出,C語(yǔ)言的語(yǔ)句用來(lái)向計(jì)算機(jī)系統(tǒng)發(fā)出操作指令。一條語(yǔ)句編寫完成經(jīng)過(guò)編譯后產(chǎn)生若干條機(jī)器指2022-07-07
C++?std::copy與memcpy區(qū)別小結(jié)
本文主要介紹了C++?std::copy與memcpy區(qū)別小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
c語(yǔ)言程序設(shè)計(jì)文件操作方法示例(CreateFile和fopen)
c主要的文件操作函數(shù)有:CreateFile,CloseHandle,ReadFile,WriteFile,SetFilePointer,GetFileSize。其中的讀寫操作是以字符為單位,獲得文件大小也是以字符為單位。2013-12-12

