コード例 #1
0
ファイル: main.cpp プロジェクト: SophiaWoo/Snake
void CreateFood() {
	Food.pos_x = rand() % 10 + 1;
	Food.pos_y = rand() % 10 + 1;
	if (SnakeHead.pos_x == Food.pos_x && SnakeHead.pos_y == Food.pos_y)
		CreateFood();
	for (int i = 0; i < length; i++)
	if (Food.pos_x == SnakeBody[i].pos_x && Food.pos_y == SnakeBody[i].pos_y)
		CreateFood();
}
コード例 #2
0
ファイル: game_opration.c プロジェクト: Anna-YJ/samples
// 游戏的初始化,
// 创建游戏的内部数据结构和系统对象。
void CreateGame(HWND hwnd, // 主窗口句柄
	DWORD dwInitTimerElapse, //
	unsigned int one_level_scores,
	DOUBLE level_speedup_ratio,
	int boundary_x, int boundary_y,
	int init_x, int init_y, 
	int init_len,
	dirction init_dir)
{
	// 设置随机数种子
	// 需要使用随机数生成食物的位置等。
	FILETIME ft;
	GetSystemTimeAsFileTime(&ft);
	srand(ft.dwLowDateTime);

	dbLevelSpeedupRatio = level_speedup_ratio;
	dwTimerElapse = dwInitTimerElapse;
	dwOneLevelScores = one_level_scores;

	// 设置游戏的边界
	SetBoundary(boundary_x, boundary_y);

	// 创建表示贪吃蛇的数据结构
	CreateSnake(init_dir, init_x, init_y, init_len);

	// 创建表示食物的数据结构
	CreateFood();

	// 创建一个计时器
	// 每经过 dwTimerElapse 毫秒,hwnd窗口(主窗口)就会收到一个WM_TIMER消息。
	// 计时器是驱动本游戏进行的主要时间线。
	// dwTimerElapse变量影响游戏进行的快慢变化。
	SetTimer(hwnd, TIMER_ID, dwTimerElapse, NULL);

}
コード例 #3
0
ファイル: SimpleSnake.c プロジェクト: hewllet/xialieyang
void Init(){
	char kb = 's', default_kb = 's';
	Randomxy(&snake->posx, &snake->posy);
	snake->next = snake; snake->prv = snake;	tail = snake;//构成循环
	Paint(body); CreateFood();
	while (1){
		if (kbhit())kb = getch(); if (kb < 97) kb += 32;
		switch (kb){
		case'w': if (default_kb != 's'){ Move; tail->posy--; }else kb = default_kb; break;
		case 's':if (default_kb != 'w') { Move; tail->posy++; }else kb = default_kb; break;
		case'a':  if (default_kb != 'd') { Move; tail->posx--; }else kb = default_kb; break;
		case'd':  if (default_kb != 'a'){ Move; tail->posx++; }else  kb = default_kb; break;
		}	
		default_kb = kb; snake = tail;  tail = tail->prv; //新的头结点,要一个节点标记蛇身最后一个	
		if (IsEat()) {CreateFood(); CreateNode(); Paint(create);}
		Paint(body);
	}
}
コード例 #4
0
ファイル: game_opration.c プロジェクト: Anna-YJ/samples
// 游戏控制的一个主要流程。
// 当计时器发生时进行的处理逻辑。
void OnTimer(HWND hwnd)
{
	// 计时器到时以后,蛇移动一步。
	// 根据以后以后的状态,进行后续处理。
	switch (SnakeMove())
	{
	// 如果蛇已经死了
	case SNAKE_DEAD:
		// 首先关闭计时器,避免在计时器的驱动下再次调用本函数
		KillTimer(hwnd, TIMER_ID);
		// 然后通知玩家,Game Over了,退出进程。
		MessageBox(0, "Game Over", "Game Over", 0);
		ExitProcess(0);
		break;
	// 如果蛇吃到了食物
	case SNAKE_EATEN_FOOD:
		// 计分;
		score++;
		// 创建新的食物。食物是一个全局唯一变量。
		CreateFood();
		// 蛇进行生长,然后判断蛇生长以后的状态,进行后续处理。
		switch (SnakeGorwup())
		{
			// 玩家赢了,停止计时器。
		case SNAKE_COMPLETE:		
			KillTimer(hwnd, TIMER_ID);
			// 通知玩家,退出进程,结束游戏。
			MessageBox(0, "You Win!", "You Win", 0);
			ExitProcess(0);
			break;
			// 发生错误,停止计时器,并退出。
		case SNAKE_ERROR:
			KillTimer(hwnd, TIMER_ID);
			MessageBox(hwnd, "Error!!", "Error!1", MB_OK);
			ExitProcess(0);
			break;
			// 没有结束,也没有错误,正常吃到食物。
		case SNAKE_GROWUP:
			// 判断计分,是否升级。
			if (IS_SPEEDUP(score))
			{
				level++;
				SpeedUp(hwnd);
			}
			break;
		} //switch (SnakeGorwup()) 结束
		break;
	case SNAKE_MOVED:
		break;
	}
	return;

}
コード例 #5
0
ファイル: main.cpp プロジェクト: SophiaWoo/Snake
int main() {
	SnakeHead.pos_y = 5;
	derection = 'd';
	for (int i = 0; i < length; i++)
		SnakeBody[i].pos_y = 4 - i;
	CreateFood();
	Map(SNAKE_HEAD, SNAKE_BODY, SNAKE_FOOD);
	for (int i = 0; i < 12; i++)
		printf("%s\n", map[i]);
	while (SnakeHead.life) {
		Map(BLANK_CELL, BLANK_CELL, SNAKE_FOOD);
		Last = SnakeBody[length - 1];
		if (kbhit())
			derection = getch();
		move();
		if (SnakeHead.pos_x == Food.pos_x && SnakeHead.pos_y == Food.pos_y) {
			CreateFood();
			Grow();
			Score++;
			if (Score != 0 && Score % 3 == 0)
				speed = speed * 0.8;
		}
		Map(SNAKE_HEAD, SNAKE_BODY, SNAKE_FOOD);
		system("CLS");
		for (int i = 0; i < 12; i++)
			printf("%s\n", map[i]);
		Sleep(speed);
		if (SnakeHead.pos_x == 0 || SnakeHead.pos_x == 11 || SnakeHead.pos_y == 0 || SnakeHead.pos_y == 11)
			SnakeHead.life = 0;
		for (int i = 0; i < length; i++)
		if ((SnakeHead.pos_x == SnakeBody[i].pos_x && SnakeHead.pos_y == SnakeBody[i].pos_y) || length == SNAKE_MAX_LENGTH)
			SnakeHead.life = 0;
	}
	if (length == SNAKE_MAX_LENGTH)
		printf("You win!\n");
	else
		printf("Game Over!\nScore = %d\n", Score);
	system("pause");
	return 0;
}
コード例 #6
0
ファイル: 贪吃蛇.cpp プロジェクト: chenwp/shuax
void DispatchMessage()
{
	InitSnake();
	InitWall();

	while (1)
	{
		CreateFood();
		CreateAward();

		DrawString(10, 0, " Score:%04d ", score);
		AppendSnakeNodeList(Head, Orientation);
		CheckAward();
		if (CheckSnake() || CheckWall())
		{
			DrawString(2, 2, "你死啦! 按任意键重新开始玩!");
			getch();
			break;
		}
		DrawString(Head->Location.x, Head->Location.y, "◎");

		if (!CheckFood())
		{
			DrawString(Tail->Location.x, Tail->Location.y, " ");
            DeleteSnakeNodeList(Tail);
		}
		int keep = 0;

		if (kbhit())
		{
			switch (getch())
			{
			case 0x1b://按下ESC,暂停游戏
				getch();
				continue;
			case 0xE0://按下特殊键
				switch (getch())
				{
				case 75: if(Orientation==0) keep = 1;if (Orientation%2!=0) Orientation=0; break;
				case 72: if(Orientation==1) keep = 1;if (Orientation%2==0) Orientation=1; break;
				case 77: if(Orientation==2) keep = 1;if (Orientation%2!=0) Orientation=2; break;
				case 80: if(Orientation==3) keep = 1;if (Orientation%2==0) Orientation=3; break;
				default: ;
				}
				break;
			}
		}

		if(!keep&&(100-score*2)>10)Sleep(100-score*2);//难度提升 
		else Sleep(10);
	}
}
コード例 #7
0
ファイル: Level1.cpp プロジェクト: matheusportela/Poiesis
void Level1::CreateAllEntities()
{
    // EntityFactory::CreateBackground();
    EntityFactory::CreateCamera(CFG_GETF("LEVEL_1_CAMERA_HEIGHT"));

    CreateButtons();
    CreateAreas();

    // Cells and food must be created after areas to be rendered above them.
    auto player = EntityFactory::CreatePlayer();
    Engine::GetInstance().AddComponent(
        std::make_shared<AIComponent>("EatableComponent"), player);

    // CreateCells();
    CreateBacteria();
    CreateFood();
}
コード例 #8
0
ファイル: snake.cpp プロジェクト: qhw/Snake
//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc,hMemDC;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
	
	switch (message) 
	{
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;
				case IDM_UP:
					SendMessage(hWnd,WM_KEYDOWN,(WPARAM)VK_UP,0);
					break;
				case IDM_LEFT:
					SendMessage(hWnd,WM_KEYDOWN,(WPARAM)VK_LEFT,0);
					break;
				case IDM_RIGHT:
					SendMessage(hWnd,WM_KEYDOWN,(WPARAM)VK_RIGHT,0);
					break;
				case IDM_DOWN:
					SendMessage(hWnd,WM_KEYDOWN,(WPARAM)VK_DOWN,0);
					break;
				case IDM_START_PAUSE:
					SendMessage(hWnd,WM_KEYDOWN,(WPARAM)VK_SPACE,0);
					break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_CREATE:
			m = 20;
			n = 15;
			for(i = 0; i < n_body; i++)
			{
				Position[i].x = m-i;
				Position[i].y = n;
				Matrix[Position[i].x][Position[i].y] = 1;
			}
			CreateFood();
			CreateUI(hWnd);
			SetTimer(hWnd,100,speed,NULL);
				break;
		case WM_KEYDOWN:
			switch(wParam)
			{
			case VK_UP:
				//KillTimer(hWnd,100);
				if(direction != 2 && n>0 && CheckOver(hWnd,m,n-1))
				{
					
					for(i = 0; i < n_body; i++)
					{
						Matrix[Position[i].x][Position[i].y] = 0;
					}
					n--;
					CheckFood();
					for(i = n_body-1; i > 0; i--)
					{
						Position[i] = Position[i-1];
					}
					Position[0].y = n;
					for(i = 0; i < n_body; i++)
					{
						Matrix[Position[i].x][Position[i].y] = 1;
					}
					CreateFood();
					direction = 0;
					DrawUI(hWnd);
				}
				else
					MessageBeep(0);
			//	SetTimer(hWnd,100,500,NULL);
				break;
			case VK_LEFT:
			//	KillTimer(hWnd,100);
				if(direction != 1 && m>0 && CheckOver(hWnd,m-1,n))
				{
					for(i = 0; i < n_body; i++)
					{
						Matrix[Position[i].x][Position[i].y] = 0;
					}
					m--;
					CheckFood();
					for(i = n_body-1; i > 0; i--)
					{
						Position[i] = Position[i-1];
					}
					Position[0].x = m;
					for(i = 0; i < n_body; i++)
					{
						Matrix[Position[i].x][Position[i].y] = 1;
					}	
					CreateFood();
					direction = 3;
					DrawUI(hWnd);
				}
				else
					MessageBeep(0);
			//	SetTimer(hWnd,100,500,NULL);
				break;
			case VK_RIGHT:
				//KillTimer(hWnd,100);
				if(direction != 3 && m<39 && CheckOver(hWnd,m+1,n))
				{
					for(i = 0; i < n_body; i++)
					{
						Matrix[Position[i].x][Position[i].y] = 0;
					}
					m++;
					CheckFood();
					for(i = n_body-1; i > 0; i--)
					{
						Position[i] = Position[i-1];
					}
					Position[0].x = m;
					for(i = 0; i < n_body; i++)
					{
						Matrix[Position[i].x][Position[i].y] = 1;
					}	
					CreateFood();
					direction = 1;
					DrawUI(hWnd);
				}
				else
					MessageBeep(0);
			//	SetTimer(hWnd,100,500,NULL);
				break;
			case VK_SPACE:
				if(start_or_pause)
				{
					KillTimer(hWnd,100);
					start_or_pause = false;
				}else
				{
					SetTimer(hWnd,100,speed,NULL);
					start_or_pause = true;
				}
				break;
			case VK_DOWN:
			//	KillTimer(hWnd,100);
				if(direction != 0 && n<31 && CheckOver(hWnd,m,n+1) )
				{
					for(i = 0; i < n_body; i++)
					{
						Matrix[Position[i].x][Position[i].y] = 0;
					}
					n++;
					CheckFood();
					for(i = n_body-1; i > 0; i--)
					{
						Position[i] = Position[i-1];
					}
					Position[0].y = n;
					for(i = 0; i < n_body; i++)
					{
						Matrix[Position[i].x][Position[i].y] = 1;
					}
					CreateFood();
					direction = 2;
					DrawUI(hWnd);
				}
				else
					MessageBeep(0);
			//	SetTimer(hWnd,100,500,NULL);
				break;
			}
			break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			// TODO: Add any drawing code here...
			
			//GetClientRect(hWnd, &rt);
			hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
			hBmp  =   LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
			hMemDC  =  CreateCompatibleDC(hdc);   
			hBmpOld  =   (HBITMAP)SelectObject(hMemDC,hBmp);

			//Matrix[10][10] = 1;
			
			for(i = 0; i < 40; i++)
				for(j =0; j <32; j++)
				{
						rt.left = i * 16;
					rt.top = 24 + j *16;
					switch(Matrix[i][j])
					{
						case 0:
							{
								rt.right = rt.left + 16;
								rt.bottom = rt.top + 16 ;
								FillRect(hdc,&rt,hbr);
							}
							break;
						case 1:
							{
								hBmp  =   LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
								hMemDC  =  CreateCompatibleDC(hdc);   
								hBmpOld  =   (HBITMAP)SelectObject(hMemDC,hBmp);
								BitBlt(hdc,rt.left,rt.top,16,16,hMemDC,0,0,SRCCOPY);
								SelectObject(hMemDC, hBmpOld);   
								DeleteObject(hBmp);   
								DeleteObject(hMemDC); 
							}
							break;
						case 2:
							{
								hBmp  =   LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP2));
								hMemDC  =  CreateCompatibleDC(hdc);   
								hBmpOld  =   (HBITMAP)SelectObject(hMemDC,hBmp);
								BitBlt(hdc,rt.left,rt.top,16,16,hMemDC,0,0,SRCCOPY);
								SelectObject(hMemDC, hBmpOld);   
								DeleteObject(hBmp);   
								DeleteObject(hMemDC); 
							}
							break;
					}
				}
			SelectObject(hMemDC, hBmpOld);   
			DeleteObject(hBmp);   
			DeleteObject(hMemDC); 
			EndPaint(hWnd, &ps);
			break;
		case WM_TIMER:
			//KillTimer(hWnd,100);
			switch(direction)
			{
			case 0://UP
				for(i = 0; i < n_body; i++)
				{
					Matrix[Position[i].x][Position[i].y] = 0;
				}
				if(n>0)
				{
					n--;
				}
				else
				{
					//MessageBox(NULL,"Game Over!",NULL,NULL);
					KillTimer(hWnd,100);
					gameover = false;
				}
				CheckFood();
				for(i = n_body-1; i > 0; i--)
				{
					Position[i] = Position[i-1];
				}
				Position[0].y = n;
				for(i = 0; i < n_body; i++)
				{
					Matrix[Position[i].x][Position[i].y] = 1;
				}
				CreateFood();
				DrawUI(hWnd);
				break;
			case 1://RIGHT
				for(i = 0; i < n_body; i++)
				{
					Matrix[Position[i].x][Position[i].y] = 0;
				}
				if(m<39)
					m++;
				else
				{
					//MessageBox(NULL,"Game Over!",NULL,NULL);
					KillTimer(hWnd,100);
					gameover = false;
				}
				CheckFood();
				for(i = n_body-1; i > 0; i--)
				{
					Position[i] = Position[i-1];
				}
				Position[0].x = m;
				for(i = 0; i < n_body; i++)
				{
					Matrix[Position[i].x][Position[i].y] = 1;
				}
				CreateFood();
				DrawUI(hWnd);
				break;
			case 2://DOWN
				for(i = 0; i < n_body; i++)
				{
					Matrix[Position[i].x][Position[i].y] = 0;
				}
				if(n<31)
				  n++;
				else
				{
				//	MessageBox(NULL,"Game Over!",NULL,NULL);
					KillTimer(hWnd,100);
					gameover = false ;
				}
				CheckFood();
				for(i = n_body-1; i > 0; i--)
				{
					Position[i] = Position[i-1];
				}	
				Position[0].y = n;
				for(i = 0; i < n_body; i++)
				{
					Matrix[Position[i].x][Position[i].y] = 1;
				}
				CreateFood();
				DrawUI(hWnd);
				break;
			case 3://LEFT
				for(i = 0; i < n_body; i++)
				{
					Matrix[Position[i].x][Position[i].y] = 0;
				}
				if(m>0)
					m--;
				else
				{
					//MessageBox(NULL,"Game Over!",NULL,NULL);
					KillTimer(hWnd,100);
					gameover = false;
				}
				CheckFood();
				for(i = n_body-1; i > 0; i--)
				{
					Position[i] = Position[i-1];
				}
				Position[0].x = m;
				for(i = 0; i < n_body; i++)
				{
					Matrix[Position[i].x][Position[i].y] = 1;
				}
				CreateFood();
				DrawUI(hWnd);
				break;
			}
			//SetTimer(hWnd,100,500,NULL);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}