Reputation: 1806
Say we have (pseudo) code like this:
GetInput()
{
//insert keyboard stuff here
}
Update()
{
//insert rendering stuff here
}
void game::loop()
{
game.Update();
player.GetInput();
}
How will I go about waiting for the player to give input before updating whats on screen?
Upvotes: 1
Views: 5161
Reputation: 195
To make the game turn-based, I recommend an array of objects with methods that represent a player's abilities in the game. You can have a game loop, a nested round loop, a nested turn loop, and a boolean that exits the round and turn loops when a player wins.
With your current pseudocode, you could use a switch and integer in the Update method to equate these nested loops.
Example Tic Tac Toe AI:
#include "Shared.h"
#include "Board.h"
#ifndef AI_H_
#define AI_H_
class AI{
public:
AI();
enum difficulty{Easy, Medium, Hard};
void setAlgorithm(difficulty h);
void applyAlgorithm(Board* b, int columns, int rows);
private:
Board::spot type;
difficulty harder;
void easy(Board* b, int columns, int rows);
void medium(Board* b, int columns, int rows);
void hard(Board* b, int columns, int rows);
};
#endif /*AI_H_*/
SDL_WaitEvent() is ridiculously more efficient than SDL_PollEvent(), if you don't want to hog the CPU.
while(!quit){
if(SDL_WaitEvent(&event)){
if(event.type == SDL_QUIT){
quit = true;
}else if(event.type == SDL_MOUSEBUTTONDOWN){
if(!menu.isClicked()){
if(menu.handleEvent(&event)){
}
}
}
}
}
Upvotes: 0
Reputation: 9785
Are you sure you really want to wait? Don't mean to be intrusive, but usually in games it's best to continue drawing stuff on the screen while waiting for any kind of input. Because players don't want to see the very same picture all the time.
Upvotes: 1
Reputation: 6540
Why not switch the order (GetInput
first), then block in GetInput
and don't return until the user has entered something acceptable?
Upvotes: 2