943,620 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 458
  • C++ RSS
Jan 10th, 2009
0

Code mess..

Expand Post »
Well my main() function has become a mess... What should I do next time to prevent this?


C++ Syntax (Toggle Plain Text)
  1. int main(int argc, char *args[])
  2. {
  3. srand((unsigned)time(0));
  4.  
  5. if(!init())
  6. return 1;
  7.  
  8. atexit(cleanup); // to cleanup the surfaces
  9.  
  10. // load images
  11. background = loadImage("background.jpg");
  12. message = loadImage("play_again.jpg");
  13. youWinMessage = loadImage("you_win.jpg");
  14. youLoseMessage = loadImage("you_lose.jpg");
  15.  
  16. start:
  17. // the ball
  18. SDL_Rect bounds; bounds.x = 0; bounds.y = 0; bounds.w = SCREEN_WIDTH; bounds.h = SCREEN_HEIGHT;
  19. Sprite ball(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, loadImage("ball.gif"), bounds );
  20. ball.setStep(BALL_SPEED);
  21. randomizeDirections(ball);
  22.  
  23. //Rectangles for the player and the computer
  24. Rect player(SCREEN_WIDTH / 2 - RECT_WIDTH / 2, SCREEN_HEIGHT - int(RECT_HEIGHT * 1.5), RECT_WIDTH, RECT_HEIGHT),
  25. computer(SCREEN_WIDTH / 2 - RECT_WIDTH / 2, int(RECT_HEIGHT * 1.5) - RECT_HEIGHT, RECT_WIDTH, RECT_HEIGHT);
  26.  
  27. player.setColor(rand() % 256, rand() % 256, rand() % 256);
  28. computer.setColor(rand() % 256, rand() % 256, rand() % 256);
  29.  
  30. bool playerWins = false, computerWins = false;
  31.  
  32. if(SDL_Flip(screen) == -1)
  33. return 1;
  34.  
  35. timer.start(); // start the timers
  36. bool quit = false; // the quit flag
  37.  
  38. //**************************MAIN LOOP****************************************************
  39. while(!quit)
  40. {
  41. if(timer.getTicks() % 30 == 0)
  42. {
  43. //apply the background
  44. applySurface(0, 0, background, screen);
  45.  
  46. // handle events
  47. while( SDL_PollEvent( &event ) )
  48. {
  49. //If the user has Xed out the window
  50. if(event.type == SDL_QUIT)
  51. quit = true;
  52. if(keystates[SDLK_ESCAPE])
  53. quit = true;
  54.  
  55. if(keystates[SDLK_LEFT])
  56. {
  57. if(player.x >= 0)
  58. player.x -= STEP;
  59. }
  60. if(keystates[SDLK_RIGHT])
  61. {
  62. if(player.x + player.Width() <= SCREEN_WIDTH)
  63. player.x += STEP;
  64. }
  65. if(keystates[SDLK_UP])
  66. {
  67. if(player.y == Sint16(SCREEN_HEIGHT - int(RECT_HEIGHT) * 1.5))
  68. player.y -= STEP;
  69. }
  70. else
  71. {
  72. if(player.y < Sint16(SCREEN_HEIGHT - int(RECT_HEIGHT) * 1.5))
  73. player.y += STEP;
  74. }
  75.  
  76. if(keystates[SDLK_RETURN])
  77. {
  78. if(playerWins || computerWins)
  79. {
  80. playerWins = false;
  81. computerWins = false;
  82. goto start;
  83. }
  84. }
  85. }
  86.  
  87. if(timer.isStarted())
  88. {
  89. //calcuate the computer's move and motion of the ball
  90. AI(computer, ball);
  91. ball.move();
  92.  
  93. if(ball.Y() + ball.H() + ball.getStep() - 2 >= SCREEN_HEIGHT)
  94. {
  95. computerWins = true;
  96. timer.stop();
  97. }
  98. if(ball.Y() - ball.getStep() + 5 <= 0)
  99. {
  100. playerWins = true;
  101. timer.stop();
  102. }
  103.  
  104. //check for collision
  105. bool thereWasCollision = false;
  106. if(collision(ball.getOffset(), player.getSDL_Rect()))
  107. {
  108. ball.setDirectionY(Sprite::TO_ZERO);
  109. changeDirectionOnCollision(ball, player.getSDL_Rect());
  110. thereWasCollision = true;
  111. }
  112. if(collision(ball.getOffset(), computer.getSDL_Rect()))
  113. {
  114. ball.setDirectionY(Sprite::TO_INFINITY);
  115. changeDirectionOnCollision(ball, computer.getSDL_Rect());
  116. thereWasCollision = true;
  117. }
  118.  
  119. if(thereWasCollision)
  120. if(ball.getStep() < BALL_SPEED_MAX)
  121. ball.setStep(ball.getStep() + 1);
  122. }
  123.  
  124. //draw
  125. player.draw();
  126. computer.draw();
  127. applySurface(ball.X(), ball.Y(), ball.getImage(), screen);
  128.  
  129. if(playerWins || computerWins)
  130. {
  131. applySurface( (SCREEN_WIDTH - message->w) / 2, (SCREEN_HEIGHT + message->h + 10) / 2, message, screen);
  132.  
  133. if(playerWins)
  134. applySurface( (SCREEN_WIDTH - youWinMessage->w) / 2, (SCREEN_HEIGHT - youWinMessage->h - 10) / 2, youWinMessage, screen);
  135. if(computerWins)
  136. applySurface( (SCREEN_WIDTH - youLoseMessage->w) / 2, (SCREEN_HEIGHT - youLoseMessage->h - 10) / 2, youLoseMessage, screen);
  137. }
  138.  
  139.  
  140. if(SDL_Flip(screen) == -1) return 1;
  141. }
  142. }
  143.  
  144. SDL_Quit();
  145. return 0;
  146. }
Similar Threads
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Jan 10th, 2009
0

Re: Code mess..

> What should I do next time to prevent this?
Start with a design.

Even simple things like
C++ Syntax (Toggle Plain Text)
  1. do {
  2. if ( inputPresent ) {
  3. doInput();
  4. }
  5. moveEnemies();
  6. dead = checkGameOver();
  7. redrawScreen();
  8. } 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 10th, 2009
0

Re: Code mess..

One thing about collisions. At the moment I'm using functions that take 2 arguments. Would it be better if each object had a collision function so I could say:

Sprite s;
s.collision(some_rect_type rect); // returns bool

Is there a better alternative?
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Jan 10th, 2009
0

Re: Code mess..

I dunno - you need to look at the overall design and implementation, before deciding whether specific local improvements are a good idea.

When you're into writing larger programs, there isn't a single "best way" of doing anything.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 10th, 2009
0

Re: Code mess..

Except for not using goto's

edit: and using functions/methods is always a plus.
Last edited by Comatose; Jan 10th, 2009 at 9:03 am.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Updating a object from a binary file.
Next Thread in C++ Forum Timeline: exceptions fall back to std::exception





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC