Main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <stdio.h>
  2. #include <altera_avalon_pio_regs.h>
  3. #include <system.h>
  4. #include <math.h>
  5. #include "Display.h"
  6. #include "structs.h"
  7. #define ACCELERATION 0.001f
  8. #define TICK 0.001f
  9. #define LEFT_UP_BUTTON 1
  10. #define LEFT_DOWN_BUTTON 0
  11. #define RIGTH_UP_BUTTON 3
  12. #define RIGTH_DOWN_BUTTON 2
  13. #define MIN(a,b) (((a)<(b))?(a):(b))
  14. #define MAX(a,b) (((a)>(b))?(a):(b))
  15. struct Ball ball;
  16. struct Paddle left_paddle;
  17. struct Paddle right_paddle;
  18. void reset_pos() {
  19. ball.x = COLS/2 - 1;
  20. ball.y = ROWS/2 - 1;
  21. ball.vel_x = - 1;
  22. ball.vel_y = - 1;
  23. left_paddle.y = 0;
  24. left_paddle.length = 3;
  25. right_paddle.y = 0;
  26. right_paddle.length = 3;
  27. }
  28. void game() {
  29. int i,k;
  30. unsigned buttons;
  31. k=0;
  32. reset_pos();
  33. while (1){
  34. buttons = ~IORD_ALTERA_AVALON_PIO_DATA(PIO_BUTTON_BASE);
  35. //buttons = 10;
  36. if (buttons & (1<<LEFT_UP_BUTTON)){
  37. left_paddle.y = MAX(left_paddle.y - (10 * TICK),0);
  38. printf("%f\n",left_paddle.y);
  39. }
  40. if (buttons & (1<<LEFT_DOWN_BUTTON)){
  41. left_paddle.y = MIN(left_paddle.y + (10 * TICK), ROWS - (left_paddle.length));
  42. printf("%f\n",left_paddle.y);
  43. }
  44. if (buttons & (1<<RIGTH_UP_BUTTON)){
  45. right_paddle.y = MAX(right_paddle.y - (10 * TICK), 0);
  46. }
  47. if (buttons & (1<<RIGTH_DOWN_BUTTON)){
  48. right_paddle.y = MIN(right_paddle.y + (10 * TICK), ROWS - (right_paddle.length));
  49. }
  50. ball.y = MAX(MIN(ball.y + ball.vel_y * TICK,ROWS - 1),0);
  51. if (ball.y == 0 || ball.y == ROWS - 1){
  52. ball.vel_y = -ball.vel_y;
  53. }
  54. ball.x = MAX(MIN(ball.x + ball.vel_x * TICK,COLS - 1),0);
  55. if (ball.x <= 1){
  56. printf("ballx: %f, ball y: %f\n",ball.x,ball.y);
  57. printf("left_paddlex: %f\n",left_paddle.y);
  58. if (round(ball.y) >= round(left_paddle.y) && round(ball.y) <= (round(left_paddle.y) + left_paddle.length -1)){
  59. ball.vel_x = - ball.vel_x;
  60. } else {
  61. printf("ballx: %f, ball y: %f\n",ball.x,ball.y);
  62. printf("rigth player lost\n");
  63. reset_pos();
  64. }
  65. }
  66. if (ball.x >= COLS - 2){
  67. if (round(ball.y) >= round(right_paddle.y) && round(ball.y) <= (round(right_paddle.y) + right_paddle.length -1)){
  68. ball.vel_x = - ball.vel_x;
  69. } else {
  70. printf("ballx: %f, ball y: %f\n",ball.x,ball.y);
  71. printf("left player lost\n");
  72. reset_pos();
  73. }
  74. }
  75. if (ball.vel_x < 0) {
  76. ball.vel_x = ball.vel_x - ACCELERATION;
  77. } else {
  78. ball.vel_x = ball.vel_x + ACCELERATION;
  79. }
  80. if (ball.vel_y < 0) {
  81. ball.vel_y = ball.vel_y - ACCELERATION;
  82. } else {
  83. ball.vel_y = ball.vel_y + ACCELERATION;
  84. }
  85. reset_screen();
  86. for (i=0;i<left_paddle.length;i++) {
  87. set_pixel( 0, round(left_paddle.y) + i);
  88. }
  89. for (i=0;i<right_paddle.length;i++) {
  90. set_pixel(COLS - 1, round(right_paddle.y) + i);
  91. }
  92. set_pixel(round(ball.x),round(ball.y));
  93. for (k=0;k<1000;k++){}
  94. draw();
  95. }
  96. }
  97. void test_picture() {
  98. int i,j,k;
  99. unsigned buttons;
  100. while (1){
  101. for (i=0;i<ROWS;i++) {
  102. for (j=0;j<COLS;j++) {
  103. buttons = ~IORD_ALTERA_AVALON_PIO_DATA(PIO_BUTTON_BASE);
  104. //printf("%d,%d, set\n",j,i);
  105. if (buttons & (1<<LEFT_UP_BUTTON)){
  106. printf("left button up");
  107. }
  108. if (buttons & (1<<LEFT_DOWN_BUTTON)){
  109. printf("left button down");
  110. }
  111. if (buttons & (1<<RIGTH_UP_BUTTON)){
  112. printf("rigth button up");
  113. }
  114. if (buttons & (1<<RIGTH_DOWN_BUTTON)){
  115. printf("rigth button down");
  116. }
  117. printf("\n");
  118. set_pixel(j,i);
  119. draw();
  120. for(k=0;k<100000;k++) {
  121. }
  122. }
  123. }
  124. reset_screen();
  125. }
  126. }
  127. int main() {
  128. //test_picture();
  129. game();
  130. }