Question:
My Evil Monkeys game won't work... C++?
anonymous
2009-02-14 12:15:34 UTC
this is the heart of my game:

game.cpp



#include
#include
#include

using namespace std;

#include "game.h"

#define GAME_SPEED 33.3333 //this gives 30fps

bool game::run()
{
drawArea.DrawEngine();

level = new Level(&drawArea, 30, 20);

drawArea.createBackTile(TILE_EMPTY, ' ');
drawArea.createBackTile(TILE_WALL, 219);

drawArea.createSprite(SPRITE_PLAYER, 1);
drawArea.createSprite(SPRITE_ENEMY, '$');

player = new Character(level, &drawArea, 0);

level->draw();
level->addPlayer(player);
level->addEnemies(3);

char key = ' ';

startTime = timeGetTime();
frameCount = 0;
lastTime = 0;

posx = 0;

player->move(0, 0);

while(key != 'q')
{
while(!getInput(&key))
{
timerUpdate();
}

level->keyPress(key);
}

delete player;

// cout << frameCount / ((timeGetTime() - startTime) / 1000) << " fps" << endl;

return true;
}

bool game::getInput(char *c)
{
if(_kbhit())
{
*c = _getch();
return true;
}

return false;
}

void game::timerUpdate(void)
{
double currentTime = timeGetTime() - lastTime;

level->update();
if(currentTime < GAME_SPEED)
return;

frameCount++;

lastTime = timeGetTime();
}



however, I get this when I debug:

Unhandled exception at 0x00418305 in evilMonkies.exe: 0xC0000005: Access violation reading location 0x808e6398.

It then closes the window.

Any help?
Three answers:
Franck Z
2009-02-14 12:39:23 UTC
I won't be of much help certainly. The only thing that I find weird here, is that your call to level->update in game::timerUpdate is after the one to timeGetTime and before the test if(currentTime < GAME_SPEED), so that you level->update function may end after the end of a frame but not imply frameCount++, or possibly worse, this level->update function may be called several times during a frame.



I also feel cautious about the use of _kbhit(). It's likely to have your object "level" deal with another time constant than the frame duration. I mean your player will move according to the automatic key repetition as used for instance when one types in some text. It's probably meaningless to your "level" object.



I think you should be advised to use your O.S. functions instead. (same thing when you wait for the end of a frame)



(also, although I can't be 100% sure as I don't know the rest of your program, I think it's weird that a game::run method creates a level object, and not the contrary)



Edit: usually when you start a large program without knowing much how to do it precisely, it's best to start with the concrete highest functions, starting with main(). Just try to implement something easily accessible in terms of programmation and testing. Don't rely too much on debuggers for now, you have to learn how to debug before being efficient with these tools. It's good not to lay down too quickly the objects and structures of your program (don't devise a comprehensive hollow skeleton of your program). Instead, as you observe the tiny proceedings you've been able to do, organise bit by bit your program into objects and functions, so as to be able to solve more issues progressively. Using printf's to have your program tell you, as it runs, what it's supposed to do (even if it's not yet coded) will help you find where bugs take place.



You can also flip through a book about eXtreme Programming, or bemuse on the Forth language's legacy:

http://puzzle.dl.sourceforge.net/sourceforge/thinking-forth/thinking-forth-color.pdf

(Forth has now become kind of tedious to tackle objective programming, but you may enjoy having a look at the Java Virtual Machine specifications, at the Sun website, and realise how it inspired Java...)



If your IDE isn't able to extract automatically a graphic synopsis of the objects/functions/types/variables structure of your program, change IDE! (try Eclipse at http://www.eclipse.org/downloads/ to see what I mean)



Please, if you're able to fix your issue here before your question expires, tell here what was going wrong. I don't see it right now, so I might as well do this mistake one day! ;-)
legat
2016-11-06 06:30:57 UTC
we are in a position to in basic terms fairly base a catalogue like this on what we've performed and what we view to be our favourites particularly than a definitive record. unlikely to record my finished one hundred even with the undeniable fact that that's drastically diverse to yours, heres my genuine 20: a million: Ocarina of time 2: Sonic the hedgehog 3: great mario bros. 3 4: great Mario sixty 4 5: Goldeneye 6: Pokemon 7: %guy 8: Majoras mask 9: area Invaders 10: GTA 3 11: Ape escape 12: metallic kit good 3 13: devil might Cry 3 14: very final delusion 7 15: Castlevania: Symphony of the night sixteen: Sonic journey 17: great Metroid 18: highway Fighter 2 19: Resident evil 2 20: Halo: wrestle enhanced
nitu
2009-02-14 12:29:32 UTC
expert helps for project programming and webdesign

http://experthelps.info/


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...