我寫了一個貪吃蛇小游戲,在最后的收尾作業上出現了問題,大致是我在不隱藏游標的情況下程式可以完整運行,但是,我一加入隱藏游標,確實隱藏了,隱藏之后我又讓他顯示出來就是將那個函式的引數改為1,但程式就一直卡在退出界面,不能正常退出了,這個游戲做得比較簡單還請見諒!!
,以下是代碼,問題主要是在有隱藏游標函式的地方(圖案不能打)。1
#ifndef __3_H__
#define __3_H__
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define U 1
#define D 2
#define L 3
#define R 4
typedef struct snake
{
int x;
int y;
struct snake *next;
}SNAKE;
void gotoxy(int x, int y);
int color(int c);
void HideCursor();
void menu();
void start();
void off();
void map();
void snakemove();
void creatsnake();
void wall();
int self();
void food();
void direct();
void score();
#endif
2
#include "3.h"
extern int max_score;
extern int status, sleeptime, result, biao;
extern SNAKE *head, *q, *food_2;
void snakemove()
{
status = R;
char ch;
while (1)
{
if (kbhit())
{
ch = getch();
if (ch == 'w' && (status != D))
{
status = U;
}
else if (ch == 's' && (status != U))
{
status = D;
}
else if (ch == 'a' && (status != R))
{
status = L;
}
else if (ch == 'd' && (status != L))
{
status = R;
}
if (ch == ' ')
{
while (1)
{
Sleep(300);
if (ch == ' ')
{
break;
}
}
}
else if (ch == 27)
{
score();
}
}
Sleep(sleeptime);
direct();
if (result == 3)
{
biao = 1;
break;
}
}
}
3
#include "3.h"
extern int max_score;
extern int status, sleeptime, result;
extern SNAKE *head, *q, *food_2;
void menu()
{
gotoxy(54, 10);
color(14);
printf("貪吃蛇");
gotoxy(48, 13);
color(3);
printf("1.開始游戲 ");
gotoxy(60, 13);
printf("2.設定");
gotoxy(48, 15);
printf("3.退出");
gotoxy(48, 17);
color(15);
}
4
#include "3.h"
extern int max_score;
extern int status, sleeptime, result;
extern SNAKE *head, *q, *food_2;
int self()
{
SNAKE *snake;
snake = head->next;
while (snake != NULL)
{
if (snake->x == head->x && snake->y == head->y)
{
return 1;
}
snake = snake->next;
}
return 0;
}
5
#include "3.h"
extern int max_score;
extern int status, sleeptime, result;
extern SNAKE *head, *q, *food_2;
void map()
{
int i, j, x = 21;
system("cls");
for (i = 20; i <= 120; i += 2)
{
color(4);
gotoxy(i, 20);
printf("圖案");
gotoxy(i, 62);
printf("圖案");
}
for (i = 21; i < 62; i++)
{
gotoxy(20, i);
printf("圖案");
gotoxy(120, i);
printf("圖案");
}
for (i = 0; i < 41; i++)
{
color(0);
gotoxy(22, x);
for (j = 0; j < 98; j += 2)
{
printf("圖案");
}
x++;
}
}
6
#include "3.h"
extern int max_score;
extern int status, sleeptime, result;
extern SNAKE *head, *q, *food_2;
void direct()
{
SNAKE *nexthead;
wall();
nexthead = (SNAKE *)malloc(sizeof(SNAKE));
HideCursor();
if (status == U)
{
nexthead->x = head->x;
nexthead->y = head->y - 1;
nexthead->next = head;
head = nexthead;
q = head;
if (nexthead->x == food_2->x && nexthead->y == food_2->y)
{
while (q != NULL)
{
gotoxy(q->x, q->y);
color(11);
printf("圖案");
q = q->next;
}
max_score = max_score + 1;
food();
}
else
{
while (q->next->next != NULL)
{
gotoxy(q->x, q->y);
color(11);
printf("圖案");
q = q->next;
}
gotoxy(q->next->x, q->next->y);
color(0);
printf("圖案");
free(q->next);
q->next = NULL;
}
}
if (status == D)
{
nexthead->x = head->x;
nexthead->y = head->y + 1;
nexthead->next = head;
head = nexthead;
q = head;
if (nexthead->x == food_2->x && nexthead->y == food_2->y)
{
while (q != NULL)
{
gotoxy(q->x, q->y);
color(11);
printf("圖案");
q = q->next;
}
max_score = max_score + 1;
food();
}
else
{
while (q->next->next != NULL)
{
gotoxy(q->x, q->y);
color(11);
printf("圖案");
q = q->next;
}
gotoxy(q->next->x, q->next->y);
color(0);
printf("圖案");
free(q->next);
q->next = NULL;
}
}
if (status == L)
{
nexthead->x = head->x - 2;
nexthead->y = head->y;
nexthead->next = head;
head = nexthead;
q = head;
if (nexthead->x == food_2->x && nexthead->y == food_2->y)
{
while (q != NULL)
{
gotoxy(q->x, q->y);
color(11);
printf("圖案");
q = q->next;
}
max_score = max_score + 1;
food();
}
else
{
while (q->next->next != NULL)
{
gotoxy(q->x, q->y);
color(11);
printf("圖案");
q = q->next;
}
gotoxy(q->next->x, q->next->y);
color(0);
printf("圖案");
free(q->next);
q->next = NULL;
}
}
if (status == R)
{
nexthead->x = head->x + 2;
nexthead->y = head->y;
nexthead->next = head;
head = nexthead;
q = head;
if (nexthead->x == food_2->x && nexthead->y == food_2->y)
{
while (q != NULL)
{
gotoxy(q->x, q->y);
color(11);
printf("圖案");
q = q->next;
}
max_score = max_score + 1;
food();
}
else
{
while (q->next->next != NULL)
{
gotoxy(q->x, q->y);
color(11);
printf("圖案");
q = q->next;
}
gotoxy(q->next->x, q->next->y);
color(0);
printf("圖案");
free(q->next);
q->next = NULL;
}
}
if (self() == 1)
{
result = 3;
}
}
7
#include "3.h"
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
8
#include "3.h"
extern int max_score;
extern int status, sleeptime;
extern SNAKE *head, *q, *food_2;
void wall()
{
if (head->x == 21 || head->x == 119 || head->y == 20 || head->y == 62)
{
score();
}
}
9
#include "3.h"
extern int max_score;
extern int status, sleeptime;
extern SNAKE *head, *q, *food_2;
void creatsnake()
{
SNAKE *tail;
int i;
tail = (SNAKE *)malloc(sizeof(SNAKE));
tail->x = 24;
tail->y = 25;
tail->next = NULL;
for (i = 1; i <= 4; i++)
{
head = (SNAKE *)malloc(sizeof(SNAKE));
head->next = tail;
head->x = 24 + 2 * i;
head->y = 25;
tail = head;
}
while(tail->next != NULL)
{
gotoxy(tail->x, tail->y);
color(11);
printf("圖案");
tail = tail->next;
}
}
10
#include "3.h"
extern int max_score;
extern int status, sleeptime;
extern SNAKE *head, *q, *food_2;
void food()
{
SNAKE *food_1;
srand((unsigned)time(NULL));
food_1 = (SNAKE *)malloc(sizeof(SNAKE));
while ((food_1->x % 2) != 0)
{
food_1->x = rand() % 97 + 22;
}
food_1->y = rand() % 41 + 21;
q = head;
while (q->next == NULL)
{
if (q->x == food_1->x && q->y == food_1->y)
{
free(food_1);
food();
}
q = q->next;
}
gotoxy(food_1->x, food_1->y);
food_2 = food_1;
color(6);
printf("圖案");
}
11
#include "3.h"
extern int max_score;
extern int status, sleeptime;
extern SNAKE *head, *q, *food_2;
void off()
{
exit(0);
}
12
#include "3.h"
int color(int c)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
return 0;
}
13
#include "3.h"
extern int biao;
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = { 1, biao };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
14
#include "3.h"
extern int max_score;
extern int status, sleeptime, result;
extern SNAKE *head, *q, *food_2;
void start()
{
map();
creatsnake();
food();
snakemove();
score();
}
15
#include "3.h"
SNAKE *head, *q, *food_2;
int max_score = 0, result = 0;
int status = 0, sleeptime = 200;
int biao = 0;
void main()
{
while (1)
{
system("cls");
menu();
printf("請輸入你想要的操作: \n");
scanf("%d", &result);
switch (result)
{
case 1: start();
case 2: off();
case 3: off();
default: printf("error!!\n"); break;
}
}
}
16
#include "3.h"
extern int max_score;
extern int status, sleeptime, result, biao;
extern SNAKE *head, *q, *food_2;
void score()
{
int i = 0;
HideCursor();
system("cls");
gotoxy(20, 20);
printf("您的得分為%d", max_score);
max_score = 0;
while (1)
{
system("cls");
printf("游戲結束您已死亡,是否繼續:1.YES 2.NO\n");
scanf("%d", &i);
switch (i)
{
case 1: result = 1; break;
case 2: result = 3; break;
default: printf("error!!!\n"); break;
}
}
system("cls");
}
uj5u.com熱心網友回復:
僅供參考:#include <windows.h>
#include <stdio.h>
void ConPrint(char *CharBuffer, int len);
void ConPrintAt(int x, int y, char *CharBuffer, int len);
void gotoXY(int x, int y);
void ClearConsole(void);
void ClearConsoleToColors(int ForgC, int BackC);
void SetColorAndBackground(int ForgC, int BackC);
void SetColor(int ForgC);
void HideTheCursor(void);
void ShowTheCursor(void);
int main(int argc, char* argv[])
{
HideTheCursor();
ClearConsoleToColors(15, 1);
ClearConsole();
gotoXY(1, 1);
SetColor(14);
printf("This is a test...\n");
Sleep(5000);
ShowTheCursor();
SetColorAndBackground(15, 12);
ConPrint("This is also a test...\n", 23);
SetColorAndBackground(1, 7);
ConPrintAt(22, 15, "This is also a test...\n", 23);
gotoXY(0, 24);
SetColorAndBackground(7, 1);
return 0;
}
//This will clear the console while setting the forground and
//background colors.
void ClearConsoleToColors(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
//Get the handle to the current output buffer...
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
//This is used to reset the carat/cursor to the top left.
COORD coord = {0, 0};
//A return value... indicating how many chars were written
//not used but we need to capture this since it will be
//written anyway (passing NULL causes an access violation).
DWORD count;
//This is a structure containing all of the console info
// it is used here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Here we will set the current color
SetConsoleTextAttribute(hStdOut, wColor);
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//This fills the buffer with a given character (in this case 32=space).
FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
//This will set our cursor position for the next print statement.
SetConsoleCursorPosition(hStdOut, coord);
}
}
//This will clear the console.
void ClearConsole()
{
//Get the handle to the current output buffer...
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
//This is used to reset the carat/cursor to the top left.
COORD coord = {0, 0};
//A return value... indicating how many chars were written
// not used but we need to capture this since it will be
// written anyway (passing NULL causes an access violation).
DWORD count;
//This is a structure containing all of the console info
// it is used here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Here we will set the current color
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//This fills the buffer with a given character (in this case 32=space).
FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
//This will set our cursor position for the next print statement.
SetConsoleCursorPosition(hStdOut, coord);
}
}
//This will set the position of the cursor
void gotoXY(int x, int y)
{
//Initialize the coordinates
COORD coord = {x, y};
//Set the position
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
//This will set the forground color for printing in a console window.
void SetColor(int ForgC)
{
WORD wColor;
//We will need this handle to get the current background attribute
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
//We use csbi for the wAttributes word.
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
}
//This will set the forground and background color for printing in a console window.
void SetColorAndBackground(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
//Direct console output
void ConPrint(char *CharBuffer, int len)
{
DWORD count;
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), CharBuffer, len, &count, NULL);
}
//Direct Console output at a particular coordinate.
void ConPrintAt(int x, int y, char *CharBuffer, int len)
{
DWORD count;
COORD coord = {x, y};
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hStdOut, coord);
WriteConsole(hStdOut, CharBuffer, len, &count, NULL);
}
//Hides the console cursor
void HideTheCursor()
{
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if(GetConsoleCursorInfo(hStdOut, &cciCursor))
{
cciCursor.bVisible = FALSE;
SetConsoleCursorInfo(hStdOut, &cciCursor);
}
}
//Shows the console cursor
void ShowTheCursor()
{
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if(GetConsoleCursorInfo(hStdOut, &cciCursor))
{
cciCursor.bVisible = TRUE;
SetConsoleCursorInfo(hStdOut, &cciCursor);
}
}
uj5u.com熱心網友回復:
大哥,可以給我說說我問題的原因嗎
,這么多我看的暈啊
uj5u.com熱心網友回復:
void score()
{
int i = 0;
color(3);
biao = 1;
HideCursor();
system("cls");
gotoxy(20, 20);
printf("您的得分為%d", max_score);
max_score = 0;
while (result != 3)
{
system("cls");
printf("游戲結束您已死亡,是否繼續:1.YES 2.NO\n");
scanf("%d", &i);
switch (i)
{
case 1: result = 1; break;
case 2: result = 3; break;
default: printf("error!!!\n"); break;
}
}
system("cls");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/8162.html
標籤:C語言
上一篇:C++請教指標的前中括號啥作用
下一篇:請問為什么運行超時?
