| | |
Code mess..
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Well my main() function has become a mess... What should I do next time to prevent this?
C++ Syntax (Toggle Plain Text)
int main(int argc, char *args[]) { srand((unsigned)time(0)); if(!init()) return 1; atexit(cleanup); // to cleanup the surfaces // load images background = loadImage("background.jpg"); message = loadImage("play_again.jpg"); youWinMessage = loadImage("you_win.jpg"); youLoseMessage = loadImage("you_lose.jpg"); start: // the ball SDL_Rect bounds; bounds.x = 0; bounds.y = 0; bounds.w = SCREEN_WIDTH; bounds.h = SCREEN_HEIGHT; Sprite ball(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, loadImage("ball.gif"), bounds ); ball.setStep(BALL_SPEED); randomizeDirections(ball); //Rectangles for the player and the computer Rect player(SCREEN_WIDTH / 2 - RECT_WIDTH / 2, SCREEN_HEIGHT - int(RECT_HEIGHT * 1.5), RECT_WIDTH, RECT_HEIGHT), computer(SCREEN_WIDTH / 2 - RECT_WIDTH / 2, int(RECT_HEIGHT * 1.5) - RECT_HEIGHT, RECT_WIDTH, RECT_HEIGHT); player.setColor(rand() % 256, rand() % 256, rand() % 256); computer.setColor(rand() % 256, rand() % 256, rand() % 256); bool playerWins = false, computerWins = false; if(SDL_Flip(screen) == -1) return 1; timer.start(); // start the timers bool quit = false; // the quit flag //**************************MAIN LOOP**************************************************** while(!quit) { if(timer.getTicks() % 30 == 0) { //apply the background applySurface(0, 0, background, screen); // handle events while( SDL_PollEvent( &event ) ) { //If the user has Xed out the window if(event.type == SDL_QUIT) quit = true; if(keystates[SDLK_ESCAPE]) quit = true; if(keystates[SDLK_LEFT]) { if(player.x >= 0) player.x -= STEP; } if(keystates[SDLK_RIGHT]) { if(player.x + player.Width() <= SCREEN_WIDTH) player.x += STEP; } if(keystates[SDLK_UP]) { if(player.y == Sint16(SCREEN_HEIGHT - int(RECT_HEIGHT) * 1.5)) player.y -= STEP; } else { if(player.y < Sint16(SCREEN_HEIGHT - int(RECT_HEIGHT) * 1.5)) player.y += STEP; } if(keystates[SDLK_RETURN]) { if(playerWins || computerWins) { playerWins = false; computerWins = false; goto start; } } } if(timer.isStarted()) { //calcuate the computer's move and motion of the ball AI(computer, ball); ball.move(); if(ball.Y() + ball.H() + ball.getStep() - 2 >= SCREEN_HEIGHT) { computerWins = true; timer.stop(); } if(ball.Y() - ball.getStep() + 5 <= 0) { playerWins = true; timer.stop(); } //check for collision bool thereWasCollision = false; if(collision(ball.getOffset(), player.getSDL_Rect())) { ball.setDirectionY(Sprite::TO_ZERO); changeDirectionOnCollision(ball, player.getSDL_Rect()); thereWasCollision = true; } if(collision(ball.getOffset(), computer.getSDL_Rect())) { ball.setDirectionY(Sprite::TO_INFINITY); changeDirectionOnCollision(ball, computer.getSDL_Rect()); thereWasCollision = true; } if(thereWasCollision) if(ball.getStep() < BALL_SPEED_MAX) ball.setStep(ball.getStep() + 1); } //draw player.draw(); computer.draw(); applySurface(ball.X(), ball.Y(), ball.getImage(), screen); if(playerWins || computerWins) { applySurface( (SCREEN_WIDTH - message->w) / 2, (SCREEN_HEIGHT + message->h + 10) / 2, message, screen); if(playerWins) applySurface( (SCREEN_WIDTH - youWinMessage->w) / 2, (SCREEN_HEIGHT - youWinMessage->h - 10) / 2, youWinMessage, screen); if(computerWins) applySurface( (SCREEN_WIDTH - youLoseMessage->w) / 2, (SCREEN_HEIGHT - youLoseMessage->h - 10) / 2, youLoseMessage, screen); } if(SDL_Flip(screen) == -1) return 1; } } SDL_Quit(); return 0; }
> What should I do next time to prevent this?
Start with a design.
Even simple things like
Also, the moment you can no longer see the { and } of the whole of main (or any other function) on screen, then STOP and think about your code structure for a few minutes, rather than just carrying on typing.
Start with a design.
Even simple things like
C++ Syntax (Toggle Plain Text)
do { if ( inputPresent ) { doInput(); } moveEnemies(); dead = checkGameOver(); redrawScreen(); } while ( !dead );
Also, the moment you can no longer see the { and } of the whole of main (or any other function) on screen, then STOP and think about your code structure for a few minutes, rather than just carrying on typing.
![]() |
Similar Threads
- code mess-up (PHP)
- C++ to Delphi CheckSum code (Pascal and Delphi)
- create textbox in code and set it equal to one on form. (C#)
- the form cant remember me ????? (PHP)
Other Threads in the C++ Forum
- Previous Thread: Updating a object from a binary file.
- Next Thread: exceptions fall back to std::exception
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







