WinAPI双人贪吃蛇

(上学期写的,确实很冗余的代码,菜鸟一枚还请大家包涵)

2个文件:snakeList.h和snake.cpp

首先是第一个:

#include <windows.h>
#pragma once

typedef struct tagNODE
{
	POINT apt[4];
	struct tagNODE * previous, *next;
}NODE;

typedef struct tagSNAKE
{
	int length;
	NODE *head, *tail;
}SNAKE;
其次:

/*********************************************************
    Wy										05/21/2013
**********************************************************/

#include <windows.h>
#include <cstdlib>
#include <ctime>
#include "snakeList.h"

#define UNIT 30
#define ID_TIMER_SPEED 1
#define ID_TIMER_TIME  3
enum direction
{//记录蛇移动的方向
	UP, DOWN, RIGHT, LEFT
}dire1,dire2;

//判断是否吃到食物,作为全局变量,是因为如果写过程函数中,则后面的函数中
//要用到eat,则需要给许多函数传入eat这个参数,太麻烦,所以设为全局变量。
bool eat1 = false;
//定义一个全局变量,当蛇遇到墙壁或者自身时,将其设置为true
bool game_over = false;

void square (HDC, POINT[]);
void border (HDC);
void drawSnake (HDC, SNAKE*);
bool isBorder (POINT[]);
bool isFood (POINT[], POINT[]);
bool isSelf (SNAKE*, SNAKE*, POINT[]);
void drawUp (SNAKE*, SNAKE*, POINT[]);
void drawDown (SNAKE*, SNAKE*, POINT[]);
void drawRight (SNAKE*, SNAKE*, POINT[]);
void drawLeft (SNAKE*, SNAKE*, POINT[]);
void runDef (SNAKE*, SNAKE*, direction*, POINT[]);
void creatFood (SNAKE*, SNAKE*, POINT[]);
void initializeSnake1(SNAKE*);
void initializeSnake2(SNAKE*);
void drawFood (HDC,POINT[]);
void clearSnake(SNAKE*);
void initializeGame (SNAKE*, SNAKE*);

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
					PSTR szCmdLine, int iCmdShow)
{
//注册窗口类	
	static TCHAR	szAppName[] = TEXT ("snake");
	HWND			hwnd;
	MSG				msg;
	WNDCLASS		wcls;
	wcls.cbClsExtra		= 0;
	wcls.cbWndExtra		= 0;
	wcls.hbrBackground	= (HBRUSH) GetStockObject (WHITE_BRUSH);
	wcls.hCursor		= LoadCursor (NULL, IDC_ARROW);
	wcls.hIcon			= LoadIcon (NULL, IDI_APPLICATION);
	wcls.hInstance		= hInstance;
	wcls.lpfnWndProc	= WndProc;
	wcls.lpszClassName	= szAppName;
	wcls.lpszMenuName	= NULL;
	wcls.style			= CS_HREDRAW | CS_VREDRAW;

	if (!RegisterClass (&wcls))
	{
		MessageBox (NULL, TEXT ("Program requires WinNT!"), szAppName, MB_ICONERROR);
	}
//创建窗口	
	hwnd = CreateWindow (szAppName, TEXT("snake"),
						WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX,
						CW_USEDEFAULT, CW_USEDEFAULT,
						23*UNIT-13, 15*UNIT,
						NULL, NULL, hInstance, NULL);
//显示和更新窗口
	ShowWindow (hwnd, iCmdShow);
	UpdateWindow (hwnd);
//消息循环
	while (GetMessage (&msg, NULL, 0, 0))
	{
		TranslateMessage (&msg);
		DispatchMessage (&msg);
	}

	return msg.wParam;
}

//过程函数
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static int		cxClient, cyClient;
	HDC				hdc;
	PAINTSTRUCT		ps;
	static SNAKE	snake1,snake2;//定义蛇结构体
	static POINT	food[4];//食物
	static bool		isKeyDown = false;//是否有方向键按下
	static int		time;
	TCHAR			szBuffer[1000];
	
	switch (message)
	{
	case WM_SIZE:
		cxClient = LOWORD (lParam);
		cyClient = HIWORD (lParam);
		return 0;

	case WM_CREATE:
		SetTimer (hwnd, ID_TIMER_SPEED, 200, NULL);//设定游戏速度定时器
		SetTimer (hwnd, ID_TIMER_TIME, 1000, NULL);//设定游戏时间定时器
		initializeSnake1(&snake1);//初始化蛇1
		initializeSnake2(&snake2);//初始化蛇2
		dire2 = RIGHT;//初始化为向右移动
		dire1 = RIGHT;//初始化为向右移动
		creatFood(&snake1,&snake2,food);//创建食物
		return 0;

	case WM_TIMER:
		switch (wParam)
		{
		case ID_TIMER_SPEED:
			InvalidateRect (hwnd, NULL, TRUE);//如果设为FALSE,则蛇移动后会留下痕迹
			return 0;
		case ID_TIMER_TIME:
			++time;
			return 0;
		default:
			return 0;
		}

	case WM_CHAR:
		{
			switch (wParam)
			{
			case 'w':
				if (dire2 != DOWN)//防止反向移动,以下类似
				{
					isKeyDown = true;
					dire2 = UP;	//将方向更新
					drawUp(&snake2, &snake1, food);//更行蛇的结点坐标
				}
				return 0;
			case 's':
				if (dire2 != UP)
				{
					isKeyDown = true;
					dire2 = DOWN;	
					drawDown(&snake2, &snake1, food);
				}
				return 0;
			case 'd':
				if (dire2 != LEFT)
				{
					isKeyDown = true;
					dire2 = RIGHT;	
					drawRight(&snake2, &snake1, food);
				}
				return 0;
			case 'a':
				if (dire2 != RIGHT)
				{
					isKeyDown = true;
					dire2 = LEFT;	
					drawLeft(&snake2, &snake1, food);
				}
				return 0;
			}
			return 0;
		}

	case WM_KEYDOWN://按键控制
	
		{
			switch (wParam)
			{
			case VK_UP:
				if (dire1 != DOWN)//防止反向移动,以下类似
				{
					isKeyDown = true;
					dire1 = UP;	//将方向更新
					drawUp(&snake1, &snake2, food);//更行蛇的结点坐标
				}
				return 0;
			case VK_DOWN:
				if (dire1 != UP)
				{
					isKeyDown = true;
					dire1 = DOWN;	
					drawDown(&snake1, &snake2, food);
				}
				return 0;
			case VK_RIGHT:
				if (dire1 != LEFT)
				{
					isKeyDown = true;
					dire1 = RIGHT;	
					drawRight(&snake1, &snake2, food);
				}
				return 0;
			case VK_LEFT:
				if (dire1 != RIGHT)
				{
					isKeyDown = true;
					dire1 = LEFT;	
					drawLeft(&snake1, &snake2, food);
				}
				return 0;
			case VK_SPACE:
				KillTimer(hwnd,1);
				if (IDNO == MessageBox(hwnd,TEXT("回到游戏?"),TEXT("暂停游戏"),MB_YESNO))
				{
					PostQuitMessage(0);	
				}
				else
				{
					SetTimer (hwnd, ID_TIMER_SPEED, 200, NULL);//恢复定时器	
				}
				return 0;				
			case VK_ESCAPE:
				KillTimer(hwnd,1);
				if (IDYES == MessageBox(hwnd,TEXT("退出游戏?"),TEXT("退出游戏"),MB_YESNO))
				{
					PostQuitMessage(0);	
				}
				else
				{
					SetTimer (hwnd, ID_TIMER_SPEED, 200, NULL);//恢复定时器	
				}
				return 0;				
				
			default:
				return 0;
			}

			InvalidateRect (hwnd, NULL, TRUE);//使客户区无效
			return 0;
		}

	case WM_PAINT:
		hdc = BeginPaint (hwnd, &ps);

		border(hdc);//画边框

		if (!isKeyDown)//判断是否有键按下
		{
			runDef(&snake1,&snake2,&dire1,food);//如果没有,则按默认方式,即原方向移动
			runDef(&snake2,&snake2,&dire2,food);//如果没有,则按默认方式,即原方向移动
		}

		if (game_over)
		{
			KillTimer(hwnd,1);//暂停定时器
			if (IDNO == MessageBox(hwnd,TEXT("Game Over! Try Again?"),TEXT("Snake!"),MB_YESNO))
			{
				PostQuitMessage(0);
			}
			else
			{
				initializeGame(&snake1,&snake2);
				time = 0;
				SetTimer (hwnd, ID_TIMER_SPEED, 200, NULL);//恢复定时器
			}
		}

		drawSnake (hdc, &snake1);//画蛇1
		drawSnake (hdc, &snake2);//画蛇2

		drawFood(hdc,food);//画食物

		TextOut(hdc,10,12*UNIT+3,TEXT("玩家1控制:上W,下S,左A,右D                玩家2控制:上↑,下↓,左←,右→"),50);
		TextOut(hdc,10,12*UNIT+25,TEXT("空格键:暂停游戏"),8);
		TextOut(hdc,150,12*UNIT+25,TEXT("Esc键:退出游戏"),9);
		TextOut(hdc,300,12*UNIT+25,szBuffer,wsprintf(szBuffer,TEXT("Player1 Score: %d"),snake1.length));
		TextOut(hdc,450,12*UNIT+25,szBuffer,wsprintf(szBuffer,TEXT("Player2 Score: %d"),snake2.length));
		TextOut(hdc,600,12*UNIT+25,szBuffer,wsprintf(szBuffer,TEXT("time: %d"),time));

		EndPaint (hwnd, &ps);

		isKeyDown = false;//将其恢复为false
		return 0;

	case WM_DESTROY:
		KillTimer (hwnd, ID_TIMER_SPEED);
		PostQuitMessage (0);
		return 0;
	}

	return DefWindowProc (hwnd, message, wParam, lParam);
}

void square (HDC hdc, POINT apt[4])
{//paint a square based on 4 points
	
	MoveToEx (hdc, apt[0].x, apt[0].y, NULL);
	LineTo (hdc, apt[1].x, apt[1].y);

	MoveToEx (hdc, apt[1].x, apt[1].y, NULL);
	LineTo (hdc, apt[2].x, apt[2].y);

	MoveToEx (hdc, apt[2].x, apt[2].y, NULL);
	LineTo (hdc, apt[3].x, apt[3].y);

	MoveToEx (hdc, apt[3].x, apt[3].y, NULL);
	LineTo (hdc, apt[0].x, apt[0].y);

	return;
}

void border (HDC hdc)
{//画出边框	
	POINT	border[22][4];

	for (int i=0; i!=22; ++i)
	{//初始化上边框的每个方格的四个点坐标

		border[i][0].x = i*UNIT;
		border[i][0].y = 0;

		border[i][1].x = (i+1)*UNIT;
		border[i][1].y = 0;

		border[i][2].x = (i+1)*UNIT;
		border[i][2].y = UNIT;

		border[i][3].x = i*UNIT;
		border[i][3].y = UNIT;
	}

	for (int i=0; i!=22; ++i)
	{//画出上边框
		square (hdc, border[i]);
	}

	for (int i=0; i!=22; ++i)
	{//初始化下边框每个方格的四个点坐标

		border[i][0].x = i*UNIT;
		border[i][0].y = 11*UNIT;

		border[i][1].x = (i+1)*UNIT;
		border[i][1].y = 11*UNIT;

		border[i][2].x = (i+1)*UNIT;
		border[i][2].y = 12*UNIT;

		border[i][3].x = i*UNIT;
		border[i][3].y = 12*UNIT;
	}

	for (int i=0; i!=22; ++i)
	{//画出下边框
		square (hdc, border[i]);
	}

	for (int i=0; i!=10; ++i)
	{//初始化左边框每个方格的四个点坐标

		border[i][0].x = 0;
		border[i][0].y = (i+1)*UNIT;

		border[i][1].x = UNIT;
		border[i][1].y = (i+1)*UNIT;

		border[i][2].x = UNIT;
		border[i][2].y = (i+2)*UNIT;

		border[i][3].x = 0;
		border[i][3].y = (i+2)*UNIT;
	}

	for (int i=0; i!=22; ++i)
	{//画出左边框
		square (hdc, border[i]);
	}

	for (int i=0; i!=10; ++i)
	{//初始化右边框每个方格的四个点坐标

		border[i][0].x = 21*UNIT;
		border[i][0].y = (i+1)*UNIT;

		border[i][1].x = 22*UNIT;
		border[i][1].y = (i+1)*UNIT;

		border[i][2].x = 22*UNIT;
		border[i][2].y = (i+2)*UNIT;

		border[i][3].x = 21*UNIT;
		border[i][3].y = (i+2)*UNIT;
	}

	for (int i=0; i!=22; ++i)
	{//画出右边框
		square (hdc, border[i]);
	}
	
	return;
}

void drawSnake (HDC hdc, SNAKE* s)
{
	NODE* iter = s->head;
	for (int i=0; i!=s->length; ++i)
	{
		square (hdc, iter->apt);
		iter = iter->next;
	}
	iter = NULL;
	free (iter);
	return;
}

bool isBorder (POINT apt[4])
{
	if (apt[0].x==0 || apt[0].x==21*UNIT 
		|| apt[0].y==0 || apt[0].y==11*UNIT)
		return true;
	else
		return false;
}

bool isFood (POINT food[4], POINT apt[4])
{
	if (apt[0].x == food[0].x && apt[0].y == food[0].y)
		return true;
	else
		return false;
}

bool isSelf (SNAKE* s1, SNAKE* s2, POINT apt[4])
{
	NODE* current = s1->head;
	for (int i=0; i!=s1->length; ++i)
	{
		if (current->apt[0].x==apt[0].x && current->apt[0].y==apt[0].y)
			return true;
		current = current->next;
	}
	current = s2->head;
	for (int i=0; i!=s2->length; ++i)
	{
		if (current->apt[0].x==apt[0].x && current->apt[0].y==apt[0].y)
			return true;
		current = current->next;
	}

	return false;
}

void drawUp (SNAKE* s1, SNAKE* s2, POINT food[4])
{
	POINT temp[4];//蛇头上方的方格
	temp[0].x = s1->head->apt[0].x;
	temp[0].y = s1->head->apt[0].y - UNIT;
	temp[1].x = s1->head->apt[1].x; 
	temp[1].y = s1->head->apt[1].y - UNIT;
	temp[2] = s1->head->apt[1];
	temp[3] = s1->head->apt[0];
	if (isBorder(temp) || isSelf(s1, s2, temp))
	{//判断是否为墙或者自身
		game_over = true;
	}
	else if (isFood(food, temp))
	{//如果是食物,则只添加头结点
		NODE* new_head = (NODE*)malloc(sizeof(NODE));//在函数结束时,能否添加进去?
		new_head->apt[0] = temp[0];
		new_head->apt[1] = temp[1];
		new_head->apt[2] = temp[2];
		new_head->apt[3] = temp[3];
				
		new_head->next = s1->head;
		s1->head->previous = new_head;
		s1->head = new_head;
		eat1 = true;
		s1->length++;

		if (eat1)
			creatFood(s1,s2,food);
		eat1 = false;
	}
	else
	{//为空。则添加头结点,删除尾结点
		NODE* new_head = (NODE*)malloc(sizeof(NODE));//在函数结束时,能否添加进去?
		new_head->apt[0] = temp[0];
		new_head->apt[1] = temp[1];
		new_head->apt[2] = temp[2];
		new_head->apt[3] = temp[3];

		new_head->next = s1->head;
		s1->head->previous = new_head;
		s1->head	= new_head;

		NODE* temp_tail;
		temp_tail = s1->tail;
		s1->tail = s1->tail->previous;
		s1->tail->next = NULL;
		free (temp_tail);
	}
}

void drawDown (SNAKE* s1, SNAKE* s2, POINT food[4])
{
	POINT temp[4];//蛇头下方的方格
	temp[3].x = s1->head->apt[3].x;
	temp[3].y = s1->head->apt[3].y + UNIT;
	temp[2].x = s1->head->apt[2].x; 
	temp[2].y = s1->head->apt[2].y + UNIT;
	temp[0] = s1->head->apt[3];
	temp[1] = s1->head->apt[2];
	if (isBorder(temp) || isSelf(s1,s2,temp))
	{//判断是否为墙或者自身
		game_over = true;
	}
	else if (isFood(food, temp))
	{//如果是食物,则只添加头结点
		NODE* new_head = (NODE*)malloc(sizeof(NODE));//在函数结束时,能否添加进去?
		new_head->apt[0] = temp[0];
		new_head->apt[1] = temp[1];
		new_head->apt[2] = temp[2];
		new_head->apt[3] = temp[3];
				
		new_head->next = s1->head;
		s1->head->previous = new_head;
		s1->head	= new_head;
		eat1 = true;
		s1->length++;

		if (eat1)
			creatFood(s1,s2,food);
		eat1 = false;
	}
	else
	{//为空。则添加头结点,删除尾结点
		NODE* new_head = (NODE*)malloc(sizeof(NODE));//在函数结束时,能否添加进去?
		new_head->apt[0] = temp[0];
		new_head->apt[1] = temp[1];
		new_head->apt[2] = temp[2];
		new_head->apt[3] = temp[3];

		new_head->next = s1->head;
		s1->head->previous = new_head;
		s1->head	= new_head;

		NODE* temp_tail;
		temp_tail = s1->tail;
		s1->tail = s1->tail->previous;
		s1->tail->next = NULL;
		free (temp_tail);
	}
}

void drawRight (SNAKE* s1, SNAKE* s2, POINT food[4])
{
	POINT temp[4];//蛇头右方的方格
	temp[1].x = s1->head->apt[1].x + UNIT;
	temp[1].y = s1->head->apt[1].y;
	temp[2].x = s1->head->apt[2].x + UNIT; 
	temp[2].y = s1->head->apt[2].y;
	temp[0] = s1->head->apt[1];
	temp[3] = s1->head->apt[2];
	if (isBorder(temp) || isSelf(s1, s2, temp))
	{//判断是否为墙或者自身
		game_over = true;
	}
	else if (isFood(food, temp))
	{//如果是食物,则只添加头结点
		NODE* new_head = (NODE*)malloc(sizeof(NODE));//在函数结束时,能否添加进去?
		new_head->apt[0] = temp[0];
		new_head->apt[1] = temp[1];
		new_head->apt[2] = temp[2];
		new_head->apt[3] = temp[3];
				
		new_head->next = s1->head;
		s1->head->previous = new_head;
		s1->head = new_head;
		eat1 = true;
		s1->length++;

		if (eat1)
			creatFood(s1,s2,food);
		eat1 = false;
	}
	else
	{//为空。则添加头结点,删除尾结点
		NODE* new_head = (NODE*)malloc(sizeof(NODE));//在函数结束时,能否添加进去?
		new_head->apt[0] = temp[0];
		new_head->apt[1] = temp[1];
		new_head->apt[2] = temp[2];
		new_head->apt[3] = temp[3];

		new_head->next = s1->head;
		s1->head->previous = new_head;
		s1->head	  = new_head;

		NODE* temp_tail;
		temp_tail = s1->tail;
		s1->tail = s1->tail->previous;
		s1->tail->next = NULL;
		free (temp_tail);
	}
}

void drawLeft (SNAKE* s1, SNAKE* s2, POINT food[4])
{
	POINT temp[4];//蛇头上方的方格
	temp[0].x = s1->head->apt[0].x - UNIT;
	temp[0].y = s1->head->apt[0].y;
	temp[3].x = s1->head->apt[3].x - UNIT; 
	temp[3].y = s1->head->apt[3].y;
	temp[2] = s1->head->apt[3];
	temp[1] = s1->head->apt[0];
	if (isBorder(temp) || isSelf(s1, s2, temp))
	{//判断是否为墙或者自身
		game_over = true;
	}
	else if (isFood(food, temp))
	{//如果是食物,则只添加头结点
		NODE* new_head = (NODE*)malloc(sizeof(NODE));//在函数结束时,能否添加进去?
		new_head->apt[0] = temp[0];
		new_head->apt[1] = temp[1];
		new_head->apt[2] = temp[2];
		new_head->apt[3] = temp[3];
				
		new_head->next = s1->head;
		s1->head->previous = new_head;
		s1->head	= new_head;
		eat1 = true;
		s1->length++;

		if (eat1)
			creatFood(s1, s2, food);
		eat1 = false;
	}
	else
	{//为空。则添加头结点,删除尾结点
		NODE *new_head = (NODE*)malloc(sizeof(NODE));//在函数结束时,能否添加进去?
		new_head->apt[0] = temp[0];
		new_head->apt[1] = temp[1];
		new_head->apt[2] = temp[2];
		new_head->apt[3] = temp[3];

		new_head->next = s1->head;
		s1->head->previous = new_head;
		s1->head = new_head;

		NODE* temp_tail;
		temp_tail = s1->tail;
		s1->tail = s1->tail->previous;
		s1->tail->next = NULL;
		free (temp_tail);
	}
}

void runDef (SNAKE* s1, SNAKE* s2, direction* d, POINT food[4])
{//默认移动方式,即按原方向移动
	switch (*d)
	{
	case UP:
		drawUp(s1,s2,food);break;
	case DOWN:
		drawDown(s1,s2,food);break;
	case RIGHT:
		drawRight(s1,s2,food);break;
	case LEFT:
		drawLeft(s1,s2,food);break;
	default:
		break;
	}
}

void creatFood (SNAKE* snake1, SNAKE* snake2, POINT food[4])
{
	do
	{//防止食物出现在蛇身
		srand((unsigned)time(NULL));//初始化时间种子
		int seedx = rand()%20 + 1;//设置食物随即出现的x坐标
		int seedy = rand()%10 + 1;//设置食物随即出现的y坐标
		food[0].x = seedx*UNIT;
		food[0].y = seedy*UNIT;
		food[1].x = (seedx + 1)*UNIT;
		food[1].y = seedy*UNIT;
		food[2].x = (seedx + 1)*UNIT;
		food[2].y = (seedy + 1)*UNIT;
		food[3].x = seedx*UNIT;
		food[3].y = (seedy + 1)*UNIT;
	} while (isSelf(snake1,snake2,food));
}

void drawFood (HDC hdc,POINT food[4])
{
	square(hdc,food);
}

void initializeSnake1(SNAKE* snake1)
{
	NODE*			one = (NODE*)malloc(sizeof(NODE));
	NODE*			two = (NODE*)malloc(sizeof(NODE));
	NODE*			three = (NODE*)malloc(sizeof(NODE));
	
//初始化蛇的三个节点链接
	one->next		= two;
	two->next		= three;
	three->next		= NULL;
	three->previous	= two;
	two->previous	= one;
	one->previous	= NULL;
//初始化蛇的三个节点坐标
	one->apt[0].x	= 6*UNIT;
	one->apt[0].y	= 3*UNIT;
	one->apt[1].x	= 7*UNIT;
	one->apt[1].y	= 3*UNIT;
	one->apt[2].x	= 7*UNIT;
	one->apt[2].y	= 4*UNIT;
	one->apt[3].x 	= 6*UNIT;
	one->apt[3].y 	= 4*UNIT;

	two->apt[0].x 	= 5*UNIT;
	two->apt[0].y 	= 3*UNIT;
	two->apt[1].x	= 6*UNIT;
	two->apt[1].y	= 3*UNIT;
	two->apt[2].x	= 6*UNIT;
	two->apt[2].y	= 4*UNIT;
	two->apt[3].x	= 5*UNIT;
	two->apt[3].y 	= 4*UNIT;

	three->apt[0].x	= 4*UNIT;
	three->apt[0].y	= 3*UNIT;
	three->apt[1].x	= 5*UNIT;
	three->apt[1].y	= 3*UNIT;
	three->apt[2].x	= 5*UNIT;
	three->apt[2].y	= 4*UNIT;
	three->apt[3].x	= 4*UNIT;
	three->apt[3].y	= 4*UNIT;

//初始化蛇
	snake1->length	= 3;
	snake1->head		= one;
	snake1->tail		= three;

}

void initializeSnake2(SNAKE* snake2)
{
	NODE* one = (NODE*)malloc(sizeof(NODE));
	NODE* two = (NODE*)malloc(sizeof(NODE));
	NODE* three = (NODE*)malloc(sizeof(NODE));
	
//初始化蛇的三个节点链接
	one->next		= two;
	two->next		= three;
	three->next		= NULL;
	three->previous	= two;
	two->previous	= one;
	one->previous	= NULL;
//初始化蛇的三个节点坐标
	one->apt[0].x	= 6*UNIT;
	one->apt[0].y	= 6*UNIT;
	one->apt[1].x	= 7*UNIT;
	one->apt[1].y	= 6*UNIT;
	one->apt[2].x	= 7*UNIT;
	one->apt[2].y	= 7*UNIT;
	one->apt[3].x 	= 6*UNIT;
	one->apt[3].y 	= 7*UNIT;

	two->apt[0].x 	= 5*UNIT;
	two->apt[0].y 	= 6*UNIT;
	two->apt[1].x	= 6*UNIT;
	two->apt[1].y	= 6*UNIT;
	two->apt[2].x	= 6*UNIT;
	two->apt[2].y	= 7*UNIT;
	two->apt[3].x	= 5*UNIT;
	two->apt[3].y 	= 7*UNIT;

	three->apt[0].x	= 4*UNIT;
	three->apt[0].y	= 6*UNIT;
	three->apt[1].x	= 5*UNIT;
	three->apt[1].y	= 6*UNIT;
	three->apt[2].x	= 5*UNIT;
	three->apt[2].y	= 7*UNIT;
	three->apt[3].x	= 4*UNIT;
	three->apt[3].y	= 7*UNIT;

//初始化蛇
	snake2->length	= 3;
	snake2->head		= one;
	snake2->tail		= three;
}


void clearSnake(SNAKE* snake)
{//删除蛇所有结点
	NODE* current = snake->head;
	while (current)
	{
		NODE* temp = current;
		current = current->next;
		delete temp;
		temp = NULL;
	}
}

void initializeGame (SNAKE* snake1, SNAKE* snake2)
{//初始化游戏
	clearSnake(snake1);//删除原来蛇1所有结点
	clearSnake(snake2);//删除原来蛇2所有结点
	initializeSnake1(snake1);//初始化蛇1
	initializeSnake2(snake2);//初始化蛇2
	game_over = false;//恢复游戏
	dire1 = RIGHT;//初始化方向
	dire2 = RIGHT;//初始化方向
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
LabVIEW是一种基于图形化编程的系统设计平台,而WinAPI是Windows应用程序开发接口。LabVIEW与WinAPI结合使用,可以为LabVIEW提供更多的功能和灵活性。 首先,WinAPI提供了许多Windows操作系统的功能接口,可以让LabVIEW能够与操作系统进行交互,实现更多底层的操作。例如,通过WinAPI,LabVIEW可以调用Windows的文件管理接口,实现对文件的创建、移动、删除等操作。而且还可以通过调用WinAPI的网络接口,实现与网络设备的通信。 其次,LabVIEW通过与WinAPI结合,能够实现更高级的用户界面设计。WinAPI提供了丰富的界面控件和事件处理接口,与LabVIEW的图形化编程特性相结合,可以更加自由地设计出复杂而交互性强的用户界面。同时,利用WinAPI的窗口管理功能,还可以更好地控制图形界面的显示与隐藏,提高界面的用户体验。 此外,通过调用WinAPI函数,LabVIEW可以直接访问Windows的系统资源,如注册表、系统设置等。这为LabVIEW扩展了更大的功能范围,可以实现更多高级的应用。例如,通过WinAPI函数,LabVIEW可以实现与其他已经安装在Windows系统中的应用程序的交互,如调用微软的Office套件进行数据处理等。 综上所述,LabVIEW与WinAPI结合使用,为LabVIEW的开发者提供了更多丰富的功能和灵活性。通过调用WinAPI的接口,可以实现与操作系统的交互、实现更复杂和优化的用户界面以及实现更多高级的应用功能。这样,LabVIEW能够更好地满足各种应用场景的需求,并提供更好的开发体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值