所以我正在用线程制作C控制台游戏,我一直想做蛇。我首先制作的代码没有线程,但是现在我应该实现它们,因此我不得不对函数进行大量编辑。我很确定我钉住了蛇的活动,但由于某种原因它不会。
请帮帮我,因为截止日期临近,在过去的五个小时里我一直在梳头。
以下是有关机芯的全部代码,谢谢!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#include <unistd.h>
#include <stdbool.h>
#include <pthread.h>
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
boolean snakevivo = false, snakespostato = false;
int len, length, bend_no, vite;
char key;
typedef struct
{
int x;
int y;
int dir;
} Coordinate;
Coordinate head, bend[500], cibo, body[30];
// Funzioni per andare in una certa posizione
void gotoxy(int x, int y) {
COORD dwCursorPosition = {x, y};
SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), dwCursorPosition);
}
void GotoXY(int x, int y) {
HANDLE a;
COORD b;
fflush(stdout);
b.X = x;
b.Y = y;
a = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(a,b);
}
void iniziaGioco() {
system("cls");
// Inizializziamo le prime variabili del giocatore
length = 5; // Lunghezza iniziale
head.x = 25;// Coordinate inilziali
head.y = 20;
head.dir = RIGHT; // Direzione iniziale
vite = 3; // Numero di vite
bend[0] = head;
snakevivo = true;
// Inizializzazione threads
// tMuro = Muri, collisioni, vite e game over
// tCibo = Genera cibo quando è mangiato
// tPunteggio = Controlla e disegna punteggio
// tSnake = Controlla Snake
pthread_t tSnake;
pthread_create(&tSnake, NULL, &snake, NULL);
pthread_join(tSnake, NULL);
//movimentoGiocatore();*/
}
// Funzioni per il movimento
/// SOPRA
void sopra() {
for (int i = 0 ;i <= (bend[bend_no].y - head.y) && len < length; i++) {
GotoXY(head.x,head.y+i);
if(len==0) printf("^");
else printf("*");
body[len].x = head.x;
body[len].y = head.y+i;
len++;
}
gestionePiegamento();
if(!kbhit()) head.y--;
}
/// SOTTO
void sotto() {
for (int i = 0; i <= (head.y - bend[bend_no].y) && len < length; i++) {
GotoXY(head.x,head.y-i);
if(len==0) printf("v");
else printf("*");
body[len].x = head.x;
body[len].y = head.y-i;
len++;
}
gestionePiegamento();
if(!kbhit()) head.y++;
}
/// SINISTRA
void sinistra() {
for (int i = 0; i <= (bend[bend_no].x - head.x) && len < length; i++) {
GotoXY((head.x+i),head.y);
if(len == 0) printf("<");
else printf("*");
body[len].x = head.x+i;
body[len].y = head.y;
len++;
}
gestionePiegamento();
if(!kbhit()) head.x--;
}
/// DESTRA
void destra() {
for (int i = 0; i <= (head.x - bend[bend_no].x) && len < length; i++) {
GotoXY(body[len].x, body[len].y);
if(len == 0) printf(">");
else printf("*");
body[len].x = head.x-i;
body[len].y = head.y;
len++;
}
gestionePiegamento();
if (!kbhit()) head.x++;
}
// Funzione per il movimento e l'input
void * snake()
{
while (snakevivo) {
fflush(stdin); // Utilizziamo fflush(stdin) per svuotare il buffer
len = 0;
for (int i=0;i<30;i++)
{
body[i].x = 0;
body[i].y = 0;
if (i == length) break;
}
if (head.dir == RIGHT) destra();
else if (head.dir == LEFT) sinistra();
else if (head.dir == DOWN) sotto();
else if (head.dir == UP) sopra();
// Input movimento
key = getch();
// Controlla se l'input permette un movimento valido
if((key == RIGHT && head.dir != LEFT &&head.dir != RIGHT)||
(key == LEFT && head.dir != RIGHT && head.dir != LEFT)||
(key == UP && head.dir != DOWN && head.dir != UP)||
(key == DOWN && head.dir != UP && head.dir != DOWN)) {
bend_no++;
bend[bend_no] = head;
head.dir = key;
switch (key) {
case UP:
head.y--;
break;
case DOWN:
head.y++;
break;
case RIGHT:
head.x++;
case LEFT:
head.x--;
break;
}
}
if (getch() == 27) gameOver();
snakespostato = true;
sleep(200);
}
}
// Funzione per gestire la coda del serpente al cambio di direzione (bend)
void gestionePiegamento()
{
int i, j, diff;
for(i = bend_no; i >= 0 && len < length; i--)
{
if(bend[i].x == bend[i-1].x)
{
diff=bend[i].y-bend[i-1].y;
if(diff < 0) {
for(j = 1; j <= (-diff); j++) {
body[len].x = bend[i].x;
body[len].y = bend[i].y+j;
GotoXY(body[len].x,body[len].y);
printf("*");
len++;
if (len == length) break;
}
}
else if (diff > 0) {
for(j = 1; j <= diff;j++)
{
body[len].x = bend[i].x;
body[len].y = bend[i].y-j;
GotoXY(body[len].x,body[len].y);
printf("*");
len++;
if (len == length) break;
}
}
}
else if (bend[i].y == bend[i-1].y)
{
diff = bend[i].x - bend[i-1].x;
if (diff < 0) {
for(j = 1;j <= (-diff) && len < length; j++)
{
body[len].x = bend[i].x+j;
body[len].y = bend[i].y;
GotoXY(body[len].x,body[len].y);
printf("*");
len++;
if (len == length) break;
}
}
else if (diff > 0) {
for(j = 1; j <= diff && len < length; j++)
{
body[len].x = bend[i].x-j;
body[len].y = bend[i].y;
GotoXY(body[len].x,body[len].y);
printf("*");
len++;
if (len == length) break;
}
}
}
}
}
根据睡眠功能,我希望蛇出现并且我能够每200毫秒左右移动一次。但是它实际上根本不会显示。