1. #include <iostream>
  2. #include "objects.h"
  3. #include "SDL/SDL.h"
  4. #include "SDL/SDL_draw.h"
  5.  
  6. SDL_Surface* window = NULL;
  7. SDL_Event event;
  8. bool quit = false;
  9.  
  10. ball one;
  11.  
  12. paddle player;
  13.  
  14. int update(int background_color, int paddle_color, SDL_Rect background)
  15. {
  16. if (SDL_FillRect(window,&background,background_color) == -1)
  17. {
  18. return 1;
  19. }
  20. Draw_FillCircle(window,one.x,one.y,10,paddle_color);
  21. player.pad.y += player.velocity;
  22. if (player.pad.y < 0)
  23. {
  24. player.pad.y = 0;
  25. }
  26. if (player.pad.y > 400)
  27. {
  28. player.pad.y = 400;
  29. }
  30. if (SDL_FillRect(window,&player.pad,paddle_color) == -1)
  31. {
  32. return 1;
  33. }
  34. if (SDL_Flip(window) == -1)
  35. {
  36. return 1;
  37. }
  38. return 0;
  39. }
  40.  
  41. int main()
  42. {
  43. if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
  44. {
  45. return 1;
  46. }
  47. window = SDL_SetVideoMode(620, 470, 32, SDL_SWSURFACE);
  48. if (window == NULL)
  49. {
  50. return 1;
  51. }
  52.  
  53. int background_color = SDL_MapRGB(window->format,23,54,200);
  54. SDL_Rect background;
  55. background.x = 0;
  56. background.y = 0;
  57. background.w = 620;
  58. background.h = 470;
  59. int paddle_color = SDL_MapRGB(window->format, 235,4,55);
  60.  
  61. SDL_WM_SetCaption("Ping",NULL);
  62. int begin = 0;
  63.  
  64. while (quit == false)
  65. {
  66. begin = SDL_GetTicks();
  67. while(SDL_PollEvent(&event))
  68. {
  69. if (event.type == SDL_KEYDOWN)
  70. {
  71. switch (event.key.keysym.sym)
  72. {
  73. case SDLK_UP:
  74. player.velocity -= 5;
  75. break;
  76. case SDLK_DOWN:
  77. player.velocity += 5;
  78. break;
  79. default:
  80. break;
  81. }
  82. }
  83. if (event.type == SDL_KEYUP)
  84. {
  85. switch (event.key.keysym.sym)
  86. {
  87. case SDLK_UP:
  88. player.velocity += 5;
  89. break;
  90. case SDLK_DOWN:
  91. player.velocity -= 5;
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. if (event.type == SDL_QUIT)
  98. {
  99. quit = true;
  100. }
  101. }
  102. if(update(background_color, paddle_color, background) == 1)
  103. {
  104. return 0;
  105. }
  106. if ((SDL_GetTicks() - begin) < 20)
  107. {
  108. SDL_Delay(20 - SDL_GetTicks() + begin);
  109. }
  110. }
  111. SDL_Quit();
  112. return 0;
  113. }
  114.