Ejemplo n.º 1
0
void CGame::Play()
{
	CBoard *Board = new CBoard;
	Board->DrawBoard();
	delete Board;
	CSnake *Snake = new CSnake;
	//sets difficulty
	switch(dif_lvl)
	{
	case easy:
		CSnake::delay = 160;
		CSnake::multiplier = 1;
		break;
	case medium:
		CSnake::delay = 140;
		CSnake::multiplier = 1;
		break;
	case hard:
		CSnake::delay = 120;
		CSnake::multiplier = 0.9;
		break;
	case extreme:
		CSnake::delay = 100;
		CSnake::multiplier = 0.8;
		break;
	}
	while(Snake->Move() != 0)
	{
		if(GetAsyncKeyState(VK_UP) & 0x8000)
		{
			Snake->ChangeDirection(up);
		}
		else if(GetAsyncKeyState(VK_DOWN) & 0x8000)
		{
			Snake->ChangeDirection(down);
		}
		else if(GetAsyncKeyState(VK_LEFT) & 0x8000)
		{
			Snake->ChangeDirection(static_cast<Direction>(2));
		}
		else if(GetAsyncKeyState(VK_RIGHT) & 0x8000)
		{
			Snake->ChangeDirection(static_cast<Direction>(3));
		}
		else if(GetAsyncKeyState(VK_ESCAPE) & 0x8000)
			break; 
	}
	int score = Snake->GetScore();
	delete Snake;
	GameOver(score);
}
Ejemplo n.º 2
0
int main(int args, char** argc)
{
	try
	{
		Window::Init("FPS");
	}
	catch (const std::runtime_error &g)
	{
		std::cout << g.what() << std::endl;
		Window::Quit();
		return -1;
	}
	Timer timer;
	int frame = 0;
		
	//setting a position to draw image in
	SDL_Texture *img = Window::LoadImage("../img/squares.png");

	//set up board
	CBoard Gameboard;
	SDL_Rect pos = {Window::mBox.w / 2 - (5*TILE_SIZE),
		Window::mBox.h - (17*TILE_SIZE),TILE_SIZE*16,TILE_SIZE*10};
	//Gameboard.Init(10,16,pos);
	Gameboard.LoadForMenus();
	
	bool quit = false;
	
	SDL_Event e;

	CTetromino piece;
	CTetromino *tetrapiece;
	tetrapiece = &piece;

	int fpsCount = 0;
	
	//main loop
	timer.Start();

	while(!quit)
	{
		//read input, handle it
		while(SDL_PollEvent(&e))
		{
			switch (e.type)
			{
			case SDL_QUIT:
				quit = true;
				break;
			case SDL_KEYDOWN:
				
				switch (e.key.keysym.sym)
				{
				case SDLK_UP:
					if (Gameboard.GameState == MENUS)
					{
						Gameboard.SelectorPos -= SELECTOR_JUMP;
					}
					break;
				case SDLK_DOWN:
					if (Gameboard.GameState == GAME)
					{
						if (piece.can_move_down() && Gameboard.is_clear_down(tetrapiece))
						{
							//grays out old piece position
							Gameboard.GrayOutOldPosition(tetrapiece);
							//moves coordinates
							piece.MoveDown();
							//updates piece
							moves += 10;	
						}
						else
						{
							Gameboard.landed = true;
						}
					}
					else if (Gameboard.GameState == MENUS)
					{
						Gameboard.SelectorPos += SELECTOR_JUMP;
					}
					break;
				case SDLK_LEFT:
					if (Gameboard.GameState == GAME)
					{
						if (piece.can_move_left() && Gameboard.is_clear_left(tetrapiece))
						{
							Gameboard.GrayOutOldPosition(tetrapiece);
							piece.MoveLeft();
							moves -=1;
						}
					}
					break;
				case SDLK_RIGHT:
					if (Gameboard.GameState == GAME)
					{
						if (piece.can_move_right() && Gameboard.is_clear_right(tetrapiece))
						{
							Gameboard.GrayOutOldPosition(tetrapiece);
							piece.MoveRight();
							moves += 1;
						}
					}
					break;
				case SDLK_r:
					if (Gameboard.GameState == GAME)
					{
						Gameboard.GrayOutOldPosition(tetrapiece);
						Gameboard.RotateTetromino(tetrapiece, moves, CLOCKWISE);
					}
					break;
				case SDLK_c:
					if (Gameboard.GameState == GAME)
					{
						Gameboard.GrayOutOldPosition(tetrapiece);
						Gameboard.RotateTetromino(tetrapiece, moves, COUNTERCLOCKWISE);
					}
					break;
				case SDLK_RETURN:
					if (Gameboard.GameState == MENUS)
					{
						if (Gameboard.SelectorPos == 0)
						{
							Gameboard.GameState = GAME;
							Gameboard.Init(10,16,pos);
						}
						else if (Gameboard.SelectorPos == 3*SELECTOR_JUMP)
						{
							quit = true;
						}
						else if (Gameboard.SelectorPos == 2*SELECTOR_JUMP)
						{
							Gameboard.GameState = HISCORES;
						}
						else if(Gameboard.SelectorPos == 1*SELECTOR_JUMP)
						{
							Gameboard.GameState = OPTIONS;
						}
					}
					
					break;
				case SDLK_ESCAPE:
					Gameboard.LoadForMenus();
					Gameboard.GameState = MENUS;
					break;
				}
				break;
			case SDL_KEYUP:
				switch (e.key.keysym.sym)
				{
					case SDLK_RIGHT:
						break;
					case SDLK_LEFT:
						break;
					case SDLK_UP:
						break;
					case SDLK_DOWN:
						break;
				}
				break;
			}
		}
		//render the scene
		//std::cout << moving_tile << std::endl;
		Window::Clear();
		switch (Gameboard.GameState)
		{
			case MENUS:
			{
				Gameboard.DrawMenus(Window::mRenderer, Window::font);
				break;
			}
			case GAME:
			{
				if (Gameboard.landed)
				{
					Gameboard.landed = true;

					Gameboard.MarkLanded();
					Gameboard.EatLine();

					Gameboard.CreateTetromino(tetrapiece);
					Gameboard.GetNewPieceOnBoard(tetrapiece);
					moves = 0;
				}
				Gameboard.Update(tetrapiece);

				Gameboard.DrawBoard(Window::mRenderer);
				Gameboard.DrawScore(Window::mRenderer, Window::font);
				break;
			}
			case GAMEOVER:			
			{
				//TODO: this should run only once
				//Gameboard.landed = true;
				
				Gameboard.DrawGameOverScreen(Window::mRenderer, Window::font);
				Gameboard.DrawHiscores(Window::mRenderer, Window::font);
				break;
			}
			case HISCORES:		
			{
				Gameboard.DrawHiscores(Window::mRenderer, Window::font);
				//std::cout << "drawn";
				break;
			}
		};
		Window::Present();

		frame += 1; // increment rendered frames
		if (frame > 62)
			SDL_Delay(17);
		if (timer.Ticks() > 1000)
		{
			if (Gameboard.GameState == GAME)
			{
				if (piece.can_move_down() && Gameboard.is_clear_down(tetrapiece))
				{
					Gameboard.GrayOutOldPosition(tetrapiece);
					piece.MoveDown();
					moves += 10;
				}
				else
				{
					Gameboard.landed = true;
				}
			}

			fpsCount = frame;
			
			timer.Restart();
			Window::ChangeWindowTitle("FPS: " + std::to_string(fpsCount));
			frame = 0;
		}
	}

	//SDL CLEANUP and QUIT
	
	SDL_DestroyTexture(img);
	
	Window::Quit();
	return 0;
}