Code mess..

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Code mess..

 
0
  #1
Jan 10th, 2009
Well my main() function has become a mess... What should I do next time to prevent this?


  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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Code mess..

 
0
  #2
Jan 10th, 2009
> What should I do next time to prevent this?
Start with a design.

Even simple things like
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: Code mess..

 
0
  #3
Jan 10th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Code mess..

 
0
  #4
Jan 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Code mess..

 
0
  #5
Jan 10th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC