⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 实现贪吃蛇等功能
💻 C
📖 第 1 页 / 共 2 页
字号:
void DrawBknd(HWND *pWnd)
{
        HDC dc = GetDC(*pWnd);
        RECT rect;
        GetClientRect(*pWnd, &rect);       
        HBRUSH temp_brush = (HBRUSH)SelectObject(dc, GetStockObject(BLACK_BRUSH)); 
	Rectangle(dc, rect.left, rect.top, rect.right, rect.bottom);
	SetTextColor(dc, RGB(255,0,0));
	SetBkMode(dc, TRANSPARENT);
	
	char str[10];
	sprintf(str, "第%d关 得分:%d", hard, mark); 
	TextOut(dc, 2, 2, str, strlen(str));
	
	char tim[10];
	sprintf(tim, "用时:%d秒", totaltime/1000); 
	TextOut(dc, 2, 20, tim, strlen(tim));
	
	TextOut(dc, 2, 40, "空格暂停", strlen("空格暂停")); 
	//TextOut(dc, 2, 60, "F2重新开始", strlen("F2重新开始")); 
	
	char *help = "共六关";
        TextOut(dc, rect.right - 50, 2, help, strlen(help)); 
        
	char *info1 = "C/SDK/C++/MFC/算法/ 讨论群欢迎你的加入!期待高手,打造精英团队!";
	char *info2 = "我们的口号:超越自己,共同进步. 主群号:21035626 联盟论坛:http://yzfy.org";
	SetTextColor(dc,RGB(200,200,200));
	TextOut(dc, 2, rect.bottom - 44,  info1, strlen(info1));
	TextOut(dc, 2, rect.bottom - 22,  info2, strlen(info2));
	
}

/****得到当前运动方向****/ 
int GetNowWay()
{
        return nowway;
}

/****关卡****/
void HardStep(HWND *pWnd) 
{
        if (mark == 50)
        {
                isstop = true;
                MessageBox(*pWnd, "恭喜你进入第二关\n获得100分奖励", ":-)", 
                        MB_OK|MB_ICONINFORMATION);
                isstop = false;
                mark += 100;
                hard = 2;
                SetTimer(*pWnd, 1, speed - 100, NULL);                 
        }
        else if (mark == 250)
        {
                isstop = true;
                MessageBox(*pWnd, "恭喜你进入第三关\n获得200分奖励", ":-)", 
                        MB_OK|MB_ICONINFORMATION);
                isstop = false;
                mark += 200;
                hard = 3;
                SetTimer(*pWnd, 1, speed - 200, NULL);                
        }
        else if (mark == 650)
        {
                isstop = true;
                MessageBox(*pWnd, "恭喜你进入第四关\n获得300分奖励", ":-)", 
                        MB_OK|MB_ICONINFORMATION);
                isstop = false;
                mark += 300;
                hard = 4;
                snake_len = 5;
                SetTimer(*pWnd, 1, speed - 250, NULL);                 
        }
        else if (mark == 1050)
        {
                isstop = true;
                MessageBox(*pWnd, "恭喜你进入第五关\n获得500分奖励", ":-)", 
                        MB_OK|MB_ICONINFORMATION);
                isstop = false;
                mark += 500;
                hard = 5;
                SetTimer(*pWnd, 1, speed - 270, NULL);              
        }
        else if (mark == 1600)
        {
                isstop = true;
                MessageBox(*pWnd, "恭喜你进入第六关\n获得1000分奖励", ":-)", 
                        MB_OK|MB_ICONINFORMATION);
                isstop = false;
                mark += 1000;
                hard = 6;
                SetTimer(*pWnd, 1, speed - 290, NULL);               
        }
        else if (mark == 2650)
        {
                mark += 2350;
                char str[100];
                sprintf(str, "你羸了!\nYou are greate!\n得分%d用时 %d 秒\n欢迎你到QQ群:21035626来坐坐", totaltime/1000, mark); 
                MessageBox(*pWnd, str, "贪吃蛇", MB_OK|MB_ICONINFORMATION); 
                isstop = true;
        }
                
}

/****统计游戏时间****/ 
void StTime()
{
        if (hard == 1)
                totaltime += speed;
        else if (hard == 2)
                totaltime += (speed - 100);
        else if (hard == 3)
                totaltime += (speed - 200);
        else if (hard == 4)
                totaltime += (speed - 350);
        else if (hard == 5)
                totaltime += (speed - 270);
        else if (hard == 6)
                totaltime += (speed - 290);        
}

/****开始移动****/ 
void Move(HWND *pWnd)
{
        HardStep(pWnd); // 注意: 不要写成了*pWnd 
        StTime();                 
        int x = randPt.x;
        int y = randPt.y;             
        if (x == pt[0].x &&  y == pt[0].y)      //如果蛇头和食物相遇 
        {
                ++snake_len;
                int ix = 1;                       
        	for ( ; ix != snake_len; ++ix)
        	{
        		pt[snake_len-ix].x = pt[snake_len-ix-1].x; 
        		pt[snake_len-ix].y = pt[snake_len-ix-1].y; 
        	}  
        	SetRandomPt(pWnd); //食物被吃掉以后重新设置食物位置 
        	mark += 10;
        }
}

/****向上移动****/ 
void MoveUp()
{
        int ix = 1;
        for ( ; ix != snake_len; ++ix)
	{
		pt[snake_len-ix].x = pt[snake_len-ix-1].x; 
		pt[snake_len-ix].y = pt[snake_len-ix-1].y; 
	}
	--pt[0].y;
	nowway = 1;        
}

/****向下移动****/ 
void MoveDown()
{
        int ix = 1;
        for ( ; ix != snake_len; ++ix)
	{
		pt[snake_len-ix].x = pt[snake_len-ix-1].x; 
		pt[snake_len-ix].y = pt[snake_len-ix-1].y; 
	}
	++pt[0].y;
	nowway = 2;        
}

/****向左移动****/ 
void MoveLeft()
{
        int ix = 1;
        for ( ; ix != snake_len; ++ix)
	{
		pt[snake_len-ix].x = pt[snake_len-ix-1].x; 
		pt[snake_len-ix].y = pt[snake_len-ix-1].y; 
	}
	--pt[0].x;
	nowway = 3;        
}

/****向右移动****/ 
void MoveRight()
{
        int ix = 1;
        for ( ; ix != snake_len; ++ix)
	{
		pt[snake_len-ix].x = pt[snake_len-ix-1].x; 
		pt[snake_len-ix].y = pt[snake_len-ix-1].y; 
	}
	++pt[0].x;
	nowway = 4;
}

/****设置当前运动方向****/ 
void SetNowWay(int nw)
{
        nowway = nw;
}

/****随机移动的食物 2008年2月9日 晚添加****/ 
void MoveRandomPt(HWND *pWnd)
{
        RECT rect;
        GetClientRect(*pWnd, &rect);
	int x, y;
	if (hard ==2)
	{
        re1:	x = rand() % 17;
        	y = rand() % 13;
        }
        else 
        {
        re2:    x = rand() % rect.right / 30;
                y = rand() % rect.bottom / 30;
        }
	int ix = 0;
	for ( ; ix != snake_len; ++ix)
		if (x == pt[ix].x && y == pt[ix].y && x != randPt.x && y != randPt.y)
		      if (hard == 2)
		              goto re1;
		      else
		              goto re2;		
	if (x < randPt.x)
		--randPt.x;
	else if (randPt.x < x)
		++randPt.x;
	if (y < randPt.y)
		--randPt.y;
	else if (y > randPt.y)
		++randPt.y;

}
/****判断是否结束****/ 
int GameOver(HWND *pWnd)
{
        RECT rect;
        GetClientRect(*pWnd, &rect);
        char str[50];
        sprintf(str, "GameOver\n第%d关,得分%d,用时 %d 秒\n按F2键重新开始", 
                hard, mark, totaltime/1000); 
        if (pt[0].x < 0 || pt[0].x > rect.right / 30 || pt[0].y < 0 ||
                 pt[0].y > rect.bottom / 30)
        {
                KillTimer(*pWnd, 1);
                MessageBox(*pWnd, str, ":-(", MB_OK|MB_ICONINFORMATION);
                isstop = true; 
        }
        else
        {
                int x = pt[0].x, y = pt[0].y;
                int ix = 1;
                for ( ; ix != snake_len ; ++ix)
                        if (x == pt[ix].x && y == pt[ix].y)      //如果与蛇身重叠
                        {
                                KillTimer(*pWnd, 1);                                
                                MessageBox(*pWnd, str, ":-(", MB_OK|MB_ICONINFORMATION);
                                isstop = true;   
                        }                         
        }
        return isstop;
}
/////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -