01/10/2018, 10:57

Phần code khi rắn đụng thân nó cứ bị lỗi nên ai có thể giúp em thêm vào đoạn code rắn đụng thân rắn

#include<stdio.h>
#include<conio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
void gotoxy(int x, int y)
{
    static HANDLE h = NULL;  
    if(!h)
        h = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD c = { x, y };  
    SetConsoleCursorPosition(h,c);
}
void clrscr(void)
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
HANDLE hConsoleOut;
COORD Home = {0,0};
DWORD dummy;

hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);

FillConsoleOutputCharacter(hConsoleOut,' ',csbiInfo.dwSize.X * csbiInfo.dwSize.Y,Home,&dummy);
csbiInfo.dwCursorPosition.X = 0; 
csbiInfo.dwCursorPosition.Y = 0; 
SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition); 
}

enum PHIMDUYCHUYEN
{
	LEFT,
	RIGHT,
	DOWN,
	UP,
};
typedef struct
{
	int x;
	int y;
}ToaDoRan;
typedef struct
{
	int x;
	int y;
}ToaDoF;
typedef struct
{
	ToaDoRan sodottoida[100];
	int sodothientai;
	PHIMDUYCHUYEN MOVE;
	
}CauTaoConRan;


CauTaoConRan ConRan;
ToaDoF F;
const int w=32;
const int h=32;
bool gameover;
///InLOGO
void  logo()

{   int count=0;
    int speed=1;
    int boss=0;
  
	gotoxy(201,200);
	
	for(int i=0;i<ConRan.sodothientai-1;i++)
	{
	count++;
	if(count==5||count==25||count==45||count==65||count==100)
	{
	speed++;
	}

	
    }
    
    
    
	 
    
	printf("
Diem:= %d",count);
	printf("
Level= %d",speed);
}
int  speed(int *a)

{   int count=0;
    int speedt=1;
    int boss=0;
    a=&boss;
    a[100];
	gotoxy(201,200);
	
	for(int i=0;i<ConRan.sodothientai-1;i++)
	{
	count++;
	if(count==5||count==25||count==45||count==65||count==100)
	{
	speedt++;
	if(speedt==2)
	boss=boss+100;
	else
	if(speedt==3)
	boss=boss+200;
	else
	if(speedt==4)
	boss=boss+100;
	else
	if(speedt==5)
	boss=boss+50;
	}
    }
    return boss;
    
	
	
    
    
	 
    

}
 
// ?n hi?n con tr? nh?p nháy trong c?a s? Console
void ShowCur(bool CursorVisibility)
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cursor = {1, CursorVisibility};
    SetConsoleCursorInfo(handle, &cursor);
}
// Đ?t màu cho ch?
void SetColor(WORD color)
{
    HANDLE hConsoleOutput;
    hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
 
    CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info;
    GetConsoleScreenBufferInfo(hConsoleOutput, &screen_buffer_info);
 
    WORD wAttributes = screen_buffer_info.wAttributes;
    color &= 0x000f;
    wAttributes &= 0xfff0;
    wAttributes |= color;
 
    SetConsoleTextAttribute(hConsoleOutput, wAttributes);
}
 
// Đ?t màu n?n cho ch?

 
 
void KhoiTao()
{
	gameover=false;
	ConRan.sodothientai=1;
	ConRan.MOVE = LEFT;
	ConRan.sodottoida[0].x=10;
	ConRan.sodottoida[0].y=10;
	F.x=20;
	F.y=20;
	// xuat hien o ban dau
	gotoxy(ConRan.sodottoida[0].x,ConRan.sodottoida[0].y);
	printf("O");
	
	
}
void Vetuong( )
{    
	for(int i=0;i<=32;i++)
	{
		gotoxy(0,i);
		putchar('|');
		
	}
		for(int i=0;i<=32;i++)
	{
		gotoxy(i,0);
		putchar('|');
		
	}
		for(int i=0;i<=32;i++)
	{
		gotoxy(32,i);
		putchar('|');
		
	}
		for(int i=0;i<=32;i++)
	{
		gotoxy(i,32);
		putchar('|');
		logo();
	}
}

void HinhAnh()
{   
	 Vetuong();
	////
      ////
  ////
	////
    // khoi tao vi tri va an moi
	for(int i=0;i<ConRan.sodothientai;i++)
		
	{
	gotoxy(ConRan.sodottoida[i].x,ConRan.sodottoida[i].y);
    if(i==0)
	printf("O");  
	else 
	printf("%c",4);
    }
    gotoxy(F.x,F.y);
    putchar('8');
	
}


void DieuKhien()
{
	
	
	//khoi tao vi tri moi
		for(int i=ConRan.sodothientai-1;i>0;i--)
	{
		ConRan.sodottoida[i]=ConRan.sodottoida[i-1];
	}
	
	if(_kbhit())
	{
		switch(_getch())
		{
			case 'a':if(ConRan.MOVE= LEFT)
			ConRan.MOVE= LEFT;
			
			break;
			  case 'd':
		    
		      ConRan.MOVE=   RIGHT;
		      
			   
			   
			
			
			break;
			case 'w':
		
			 
			   ConRan.MOVE = UP;
			   
			   
			
			
			
			break;
			case 's':
			
			   
			   ConRan.MOVE=DOWN;
			   
			   
			break;

			default :
			break;
			
		}
    }   
    
        // gameover
        
	    
		
		
		
		
	}

	
	
	
	
	
	
	


void TinhToan( )
{  
    
	switch(ConRan.MOVE)
	{
		case LEFT:
		ConRan.sodottoida[0].x--;
			break;
			
		case RIGHT:
		ConRan.sodottoida[0].x++;
			break;
		case UP:
		ConRan.sodottoida[0].y--;
			break;
		case DOWN:
		ConRan.sodottoida[0].y++;
			break;
		default : break;
	}
       
	    int i=0;
	    // an thuc an o day 
		
		if(ConRan.sodottoida[0].x==F.x&&ConRan.sodottoida[0].y==F.y)
		
	{   
		   
			for(i=ConRan.sodothientai;i>0;i--)
          	{ 
          	
	        ConRan.sodottoida[i]=ConRan.sodottoida[i-1];
         	}
	
		
	    ConRan.sodothientai++;
	    srand(time(NULL));
	    F.x=rand()%29+1;
	    F.y=rand()%29+1;
	    
	} 

	// chay xuyen tuong
	     
	    for( i=0;i<ConRan.sodothientai;i++)
	    {
	    	
	    if (ConRan.sodottoida[i].x >= w)ConRan.sodottoida[i].x = 1;
        if (ConRan.sodottoida[i].x < 0) ConRan.sodottoida[i].x = w - 2;
        if (ConRan.sodottoida[i].y >= h) ConRan.sodottoida[i].y = 1;
        if (ConRan.sodottoida[i].y < 0)ConRan.sodottoida[i].y = h - 2;
        
        }
        
       
	    
	  
}///ThangThua o day 
bool ThangThua()
{   int i;
	for( i=0;i<ConRan.sodothientai-1;i++)
	if(ConRan.sodottoida[0].y==ConRan.sodottoida[i].y&&ConRan.sodottoida[0].x==ConRan.sodottoida[i].x)
	{
		return true;
	}
	return false;
}
////

	
	
	
	
	
	


int main()
 
{   
    
    ShowCur(false);
    int *a;
    int count=0;
    a=&count;
    
    SetColor(14);
    KhoiTao();
    


	
	speed(a);
	if(ThangThua()==false)
	while(!gameover)
	{
	
	clrscr();
	HinhAnh();
	DieuKhien();
	TinhToan();
	ThangThua();
	Sleep(500-speed(a));
	
	}
    

    getch();
	return 0;
}
Bài liên quan
0