I am developing a game in xcode using cocos2d-x. I want to display the highscore in game over scene.when i run the game, score is displaying in the game over scene instead of highscore, even if the score is less than highscore. I think highscore is not stored. I am using the following code. Please help me to solve this.
CCUserDefault *def=CCUserDefault::sharedUserDefault();
long int high_score=0;
if(score>high_score)
{
def->setIntegerForKey(HIGH_SCORE, score);
//def->flush();
//high_score=def->getIntegerForKey(HIGH_SCORE);
}
else if(score<high_score)
{
def->setIntegerForKey(HIGH_SCORE, high_score);
//def->flush();
//high_score=def->getIntegerForKey(HIGH_SCORE);
}
high_score=def->getIntegerForKey(HIGH_SCORE);
char s[7];
sprintf(s,"%ld", high_score);
CCLabelTTF *high_label=CCLabelTTF::create(s, "Arial.fnt", 20);
high_label->setPosition(ccp(winwsize - 800, winhsize - 50));
this->addChild(high_label,2);
Make the high_core variable global.
Pay attention that if score<high_score nothing is really needed so you can drop the def->setIntegerForKey(HIHG_SCORE, high_score); statement.
Related
I am making a multiplayer tic tac toe game with a semi-graphical interface. I have made the code and most of it works.The only part of it that doesn't work is the draw function.
I do understand that I am using TurboC++,which is a highly out of date compiler,but the Indian education system follows only TurboC++,so I have to make my project in it.(The syllabus was changed to have Python instead of C++ recently but I happened to be in the last batch of students that will not be taught Python)
The problem is in the last part of the result() function. I was unable to find what was wrong with it.I have not used graphics.h because it is not in my syllabus.
result function alone:
struct mat //To store the positions and what is present in the 9 boxes
{
int x,y;char ch;
};
struct xo //To keep track of the match
{
int actp,actx,acty;
mat pos[3][3];
char flag;
void setup() //To create the boxes/bars
{
actp=1,actx=1,acty=1;
flag=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
pos[i][j].ch=0;
}
}
void result() //To find the result
{
char flage;
for(int i=0;i<3;i++) //Column win
{
if(pos[i][0].ch==pos[i][1].ch&&pos[i][1].ch==pos[i][2].ch)
flage=pos[i][0].ch;
}
for(i=0;i<3;i++) //Row win
{
if(pos[0][i].ch==pos[1][i].ch&&pos[1][i].ch==pos[2][i].ch)
flage=pos[0][i].ch;
}
if(pos[0][0].ch==pos[1][1].ch&&pos[1][1].ch==pos[2][2].ch) //Diagonal win
flage=pos[0][0].ch;
if(pos[0][2].ch==pos[1][1].ch&&pos[1][1].ch==pos[2][0].ch) //Other diagonal win
flage=pos[0][2].ch;
if(flage=='X')flag='1';
else if(flage=='O')flag='2';
else flag='0';
int chk=1;
for(i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{if(pos[i][j].ch=='0'){chk=0;gotoxy(3,15);cout<<chk;} }//Added cout statement for debugging
}
if(flag=='0'&&chk==0)flag='D';//I understand that the condition is supposed to be chk==1,but it works only if I have this condition
}
}a;
Here is the whole code,if necessary:
https://drive.google.com/open?id=19WMexp3Hw_p9hO3qiYm0HRj-GGAJeaTr
A screenshot:
https://i.stack.imgur.com/wGh7a.jpg
If I use the correct condition, the program says that the match is drawn just after 1 move.
With this wrong condition, the program works to a certain extent and is able to find winners but never declares a match drawn even if it happens.
Thanks for the help!!
I have corrected the errors, will elaborate soon. Thanks for the help.
I am, for lack of a better word, an absolute programming novice. And as it stands, I've been flung head first into programming something in two or three languages.
As part of an assignment, we have been tasked with essentially recreating a heads/tails flip, in the form of a random chance game.
The way it works is the player has ten credits to start off with. They are asked to input a wager, and then they are asked heads or tails. Picking either heads or tails sets off a number generator, and depending on the value, they may well end up winning or losing their wager.
I coded a similar version of this using HTML/CSS/JS, and it works. I'll leave a puush link to the file so you can view it yourselves to get an idea of what I'm trying to do: http://puu.sh/bP2V7/2ef63f4a1c.html
I'm trying to do, functionally, the same thing in C++ in the form of a command application. I know the code I'm using works fine, and it compiles without much of a hitch. It's a bit annoying that it closes down rather than resetting to a previous line, but that's a hurdle I'll jump over when I get to it.
I had a look around, and to be honest, whilst a few of the things may well work, I'm admittedly relatively clueless and some of the programmer speak kinda flies over my head.
It's probably because I'm being thrown into it and I'm not used to it yet, but as it stands, I need your help.
My code simply works as follows (simplified to save time):
int main()
{
int points, wager;
points = 10;
output "HEADS OR TAILS?";
if (player has 1 point or more)
{
output "Input wager";
input wager value;
output "Wager is (player input)";
output "Heads or Tails?";
input h or t; //This was what I wanted
if (player selects 'heads') //For sake of simplicity, the code
{ //here will account for both heads
int heads; //and tails.
srand(NULL);
heads = random number 1 and 2;
if (heads = 1)
{
output "HEADS!";
output "You win 'wager'!";
points = points + wager;
}
if (heads = 2)
{
output "TAILS!";
output "You lose 'wager'!";
points = points - wager;
}
}
}
if (player has 0 points)
{
output "GAME OVER";
}
}
What I want to do is have the user input either an 'h' or a 't' to determine whether or not they want heads or tails.
In your programming class, they will have told you what they expect you to use as tools for input and output, eg char inputChar; cin >> inputChar; or similar. Use whatever they told you to use in the style they want you to use, eg
cout << HEADS_OR_TAILS_PROMPT;
char inputChar;
cin >> inputChar;
switch(inputChar) {
case 'h':
{
... // code for the heads case
break;
}
case 't':
{
... // code for the tails case
break;
}
default:
// whatever you want to do if they didn't input a valid option
}
Although, to be honest, asking your professor is going to get you a better answer for what the grader is expecting than asking us is.
I'm working on a game in C++, and have a player object and a bullet object. I have a std::vector collection for both kinds of object, defined earlier in my code like so:
std::vector<Player*> players;
std::vector<Bullet*> bullets;
My problem happens in my main game loop, where I do all of my update logic. Using a for loop to check each player and update him, I get any bullets he has spawned and add them to the bullets vector.
// update players
for (int i = 0; i < players.size(); ++i){
Player *player = players[i];
player->Update(delta);
// get bullets
for (int b = 0; b < player->Bullets.size(); ++b){
// THIS IS WHERE THINGS GET WEIRD FOR PLAYER 2
Bullet *bull = player->Bullets[i];
bullets.push_back(bull);
}
player->Bullets.clear();
}
While my first player works fine - he can shoot as much as he wants - when you get to the second player, making him shoot causes an EXC_BAD_ACCESS later on when I iterate through the bullets vector. As I was stepping through with a debugger (using Xcode), when I get to the part below my comment ("// THIS IS WHERE THINGS GET WEIRD FOR PLAYER 2"), I notice that the bullet is NULL and the player is NULL as well. Somewhere between calling that player's update method and where I pull the bullets out, things go horribly wrong.
I've been debugging this for several hours now, and I'm wondering what I am missing.
Thanks!
It's a simple typo:
Bullet *bull = player->Bullets[b];
^ not i
You could avoid this kind of mistake using range-based loops (C++11 or later):
for (Player * player : players) {
player->Update(delta);
for (Bullet * bullet : player->Bullets) {
bullets.push_back(bullet);
}
player->Bullets.clear();
}
and/or replacing the inner loop with
bullets.insert(bullets.end(), player->Bullets.begin(), player->Bullets.end());
I have made a simple score4 game. It was a project so I made a CLI IO system.
Wanting to upgrade in the furure I made the whole IO Interface (move choosing, board show) a bunch of virtual functions in a class. So I made the CLI Interface just by implementing those functions with iostream usage. I initialize this Interface like this:
IOInterface *ii = new IOConsoleInterface();
Now I want to make wxWidgets Interface of the game and run it with a -gui switch. I made the frames quite easily but now that I want to finally connect the pieces, when I initialize the GUI it enters an infinite loop and doesnt return to the Score4 game loop.
The game loop calls ii functions so i dont need the control in the wxWidgets. I want it just to be there and do the stuff it is ordered to do by the game loop.
thank you in advance
Edit: The Score4 wasn't a wx Application. I just want to apply some wxGUI classes to it.
Edit2: The game loop code:
"ii" is InputInterface, an object that takes information
like move and play-again answer
"oi" is OutpuInterface, an object that draws the board and
tells the player that it is his turn
void Mechanics::gameLoop(){
bool newGame = true;
int gameCounter = 0;
while(newGame){
short choice = 0;
short turn;
gameCounter++;
currentPlayer = decideStart(gameCounter);
board.clear();
oi->refreshTable(board, p1,p2);
for (turn = 1; turn <= ROWS*COLS; turn++){
oi->playerPrompt(currentPlayer);
do{
if (currentPlayer->isAI == 0)
choice = ii->getPlayersMove(currentPlayer);
else{
GameInstance gi = exportGameInstance();
choice = currentPlayer->chooseMove(gi); // AI's movement
}
} while (!(board.checkMoveValidity(choice))); //breaks when move is valid--> when condition is >0
board.move(choice,currentPlayer);
oi->moveAnimation(choice,currentPlayer);
oi->refreshTable(board, p1,p2);
if (board.winConditionCheck(currentPlayer))
break;
changeCurrentPlayer();
}
if (turn > ROWS*COLS) //draw
oi->draw_conclusion();
else
oi->win_loss_conclusion(true,currentPlayer);
newGame = ii->newGamePrompt();
}
delete ii;
delete oi;
// GAME ENDS HERE
}
You need to run the event loop in the GUI application, there is no (reasonable) way around that. So while you can initialize wxWidgets without entering the event loop using e.g. wxEntryStart(), you still do need to run it later.
If you want to have both console and GUI interfaces the best would be to run the event loop in both cases, except you'd run your own event loop in the former case and wx one in the latter.
I making a game in Cocos2D. I made a score counter on my screen, and when I hit an enemy it adds a point to the score. When I run it, and when I hit an enemy the project terminates.
It is probably because of this: Format specifies type 'id' but the argument has type 'int', referring to this line of code:
- (void)addPoint
{
score = score + 1; // score++; will also work.
[scoreLabel setString:[NSString stringWithFormat:#"%#", score]];
}
It says it wants me to replace the #"%#" to #"%d", because that will not make the score work, please help me fix this.
Thank you!
Yes, replace %# with %d. Score is integer, your question itself has answer..why simply asked here?
[scoreLabel setString:[NSString stringWithFormat:#"%d", score]];