Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.35 KB | None | 0 0
  1. /*
  2.   TSEA83 project
  3.  
  4.   Test for simple pong game
  5. */
  6.  
  7. #include ../std_lib/std_lib.cmm
  8. #include ../std_lib/shift.cmm
  9. #include ../std_lib/math.cmm
  10. #include ../std_lib/video.cmm
  11. #include ../std_lib/string.cmm
  12. #include ../std_lib/print.cmm
  13. #include ../std_lib/keyboard.cmm
  14. #include ../std_lib/sleep.cmm
  15. #include ../std_lib/palettes/colors.cmm
  16.  
  17. // Game constants
  18. int width = 40;
  19. int height = 30;
  20.  
  21. int p_height = 5;
  22.  
  23. int score1_x = 1;
  24. int score1_y = 0;
  25. int score2_x = 38;
  26. int score2_y = 0;
  27.  
  28. int padding = 2;
  29.  
  30. int win_score = 3;
  31.  
  32. // tile versions
  33. char bg = 0;
  34. char wall = 1;
  35. char p1 = 2;
  36. char p2 = 3;
  37. char ball = 4;
  38. char score1 = 5;
  39. char score2 = 6;
  40. char point_zone1 = 7;
  41. char point_zone2 = 8;
  42.  
  43. // Output strings
  44. char starting[] = "STARTING...";
  45. char player_won[] = "PLAYER X WON";
  46. char play_again[] = "PRESS ENTER TO PLAY AGAIN";
  47. char goal[] = "GOAL";
  48. char remaking[] = "REMAKING...";
  49. char goodbye[] = "GOODBYE";
  50.  
  51. int main() {
  52.   // Input variables
  53.   char keyboard = 0;
  54.  
  55.   // Game variables
  56.   char board[1200];
  57.  
  58.     // Priting variables
  59.     char print_y = 8;
  60.     char print_x = 19;
  61.  
  62.     palette_index_write(0 TEMPLE_BLUE PURE_BLACK);     // background
  63.   palette_index_write(1 PURE_PINK TEMPLE_BLUE);    // wall
  64.   palette_index_write(2 PURE_PINK PURE_BLACK);       // ball
  65.   palette_index_write(3 TEMPLE_YELLOW PURE_BLACK);   // score
  66.     palette_index_write(4 TEMPLE_GREEN TEMPLE_GREEN); // player 1 score zone
  67.     palette_index_write(5 PURE_TEAL PURE_TEAL);           // player 2 score zone
  68.     palette_index_write(6 ATOM_BLUE PURE_BLACK);    // printing colors
  69.  
  70.   // Main program loop
  71.   while (1) {
  72.     // Setup variables
  73.     int game_running = 1;
  74.     int p1score = 0;
  75.     int p2score = 0;
  76.     char winner = 0;
  77.  
  78.     int ball_x = 19;
  79.     int ball_y = 14;
  80.     int ball_n_x = 0;
  81.     int ball_n_y = 0;
  82.     int ball_d_x = 1;
  83.     int ball_d_y = 1;
  84.  
  85.     int p1_x = 2;
  86.     int p1_y = 12;
  87.    
  88.     int p2_x = 37;
  89.     int p2_y = 12;
  90.  
  91.         // Loop variables
  92.         int i = 0; 
  93.  
  94.         // Set all tiles to bg first
  95.         i = 0;
  96.         while (i < 1200) {
  97.             board[i] = bg;
  98.             i = i + 1;
  99.         }  
  100.  
  101.         out(0x1111 0);
  102.         // Setup player
  103.         i = 2;
  104.         if (i < (height - padding)) {
  105.             out(i 0);
  106.         }
  107.  
  108.         // Setup point zones
  109.         while (i < (height - padding)) {
  110.             board[(i*width)] = point_zone1;
  111.             board[(i*width) + 1] = point_zone1;
  112.  
  113.             board[(i*width) + (width-2)] = point_zone2;
  114.             board[(i*width) + (width-1)] = point_zone2;
  115.             i = i + 1;
  116.         }  
  117.  
  118.         // Setup wall
  119.         i = 0;
  120.         while (i < width) {
  121.             // Upper wall
  122.             board[i] = wall;
  123.             board[(width) + i] = wall;
  124.  
  125.             board[((height-2) * width) + i] = wall;
  126.             board[((height-1) * width) + i] = wall;
  127.             i = i + 1;
  128.         }
  129.  
  130.         // Setup scores
  131.         board[(score1_y*width) + score1_x] = score1;
  132.         board[(score2_y*width) + score2_x] = score2;
  133.  
  134.         // Setup player
  135.         out(0xfefe 0);
  136.         i = 0;
  137.         if (i == 0) {
  138.             out(i 0);
  139.         }
  140.         while (i < p_height) {
  141.             board[((p1_y+i)*width) + p1_x] = p1;
  142.             board[((p2_y+i)*width) + p2_x] = p2;
  143.             out(0x0A55 0);
  144.             i = i + 1;
  145.         }
  146.  
  147.  
  148.         // Setup ball
  149.         board[(ball_y*width) + ball_x] = ball;
  150.  
  151.     // Main game loop
  152.         out(0x000F 0);
  153.        
  154.         set_cursor((print_x-5) (print_y-2));
  155.         BG_COLOR = 3;
  156.         FG_COLOR = 3;
  157.         print(starting);
  158.         sleep_ms(2000);
  159.     while (game_running == 1) {
  160.       // Read keyboard, update player pos
  161.       keyboard = read_char();
  162.       if (keyboard == 27) {
  163.         // ESCAPE => quit program
  164.                 BG_COLOR = 3;
  165.                 FG_COLOR = 3;
  166.                 set_cursor((print_x-3) print_y);
  167.                 print(goodbye);
  168.                 sleep_ms(2000);
  169.         return 0;
  170.       }
  171.             if (keyboard == 's') {
  172.         if (p1_y < (height - p_height - padding)) {
  173.           // P1 move down
  174.           p1_y = p1_y + 1;
  175.  
  176.           // Update board
  177.           board[((p1_y-1)*width) + p1_x] = bg;
  178.           board[((p1_y + (p_height-1))*width) + p1_x] = p1;
  179.         }
  180.       } else if (keyboard == 'w') {
  181.         if (p1_y > padding) {
  182.           // P1 move up
  183.           p1_y = p1_y - 1;
  184.  
  185.           // Update board
  186.           board[(p1_y*width) + p1_x] = p1;
  187.           board[((p1_y + p_height)*width) + p1_x] = bg;
  188.         }
  189.       }
  190.             if (keyboard == 'l') {
  191.         if (p2_y < (height - p_height - padding)) {
  192.           // P2 move down
  193.           p2_y = p2_y + 1;
  194.  
  195.           // Update board
  196.           board[((p2_y-1)*width) + p2_x] = bg;
  197.           board[((p2_y + (p_height-1))*width) + p2_x] = p2;
  198.         }
  199.       } else if (keyboard == 'o') {
  200.         if (p2_y > padding) {
  201.           // P2 move up
  202.           p2_y = p2_y - 1;
  203.  
  204.           // Update board
  205.           board[(p2_y*width) + p2_x] = p2;
  206.           board[((p2_y + p_height)*width) + p2_x] = bg;
  207.         }
  208.       }
  209.  
  210.       // Calc new ball position
  211.       ball_n_x = ball_x + ball_d_x;
  212.       ball_n_y = ball_y + ball_d_y;
  213.  
  214.       // Check collision, update score if necessary
  215.       char ball_n_pos = board[(ball_n_y*width) + ball_n_x];
  216.      
  217.       if (ball_n_pos == wall) {
  218.         // Collide with wall, bounce in y direction
  219.         ball_d_y = -ball_d_y;
  220.         ball_n_x = ball_x + ball_d_x;
  221.         ball_n_y = ball_y + ball_d_y;
  222.       } else if ((ball_n_pos == p1) | (ball_n_pos == p2)) {
  223.                 ball_d_x = -ball_d_x;
  224.         ball_n_x = ball_x + ball_d_x;
  225.         ball_n_y = ball_y + ball_d_y;
  226.             } else if (ball_n_pos == point_zone1) {
  227.         // Player 2 made a goal
  228.         p2score = p2score + 1;
  229.         // Reset ball position and direction
  230.         ball_n_x = 19;
  231.         ball_n_y = 14;
  232.         ball_d_x = 1;
  233.         ball_d_y = 1;
  234.                 BG_COLOR = 3;
  235.                 FG_COLOR = 3;
  236.                 set_cursor((print_x+1) print_y);
  237.                 print(goal);
  238.                 sleep_ms(1000);
  239.       } else if (ball_n_pos == point_zone2) {
  240.         // Player 1 made a goal
  241.         p1score = p1score + 1;
  242.         // Reset ball position and direction
  243.         ball_n_x = 19;
  244.         ball_n_y = 14;
  245.         ball_d_x = -1;
  246.         ball_d_y = -1;
  247.                 BG_COLOR = 3;
  248.                 FG_COLOR = 3;
  249.                 set_cursor((print_x-5) print_y);
  250.                 print(goal);
  251.                 sleep_ms(1000);
  252.       }
  253.  
  254.       // Update board
  255.       board[(ball_y*width) + ball_x] = bg;
  256.       board[(ball_n_y*width) + ball_n_x] = ball;
  257.      
  258.       // Update ball p  osition
  259.       ball_x = ball_n_x;
  260.       ball_y = ball_n_y;
  261.  
  262.             // Draw board and score
  263.             i = 0;
  264.             char curr = 0;
  265.             while (i < (width*height)) {
  266.                 curr = board[i];
  267.                 if (curr == bg) {
  268.                     // Draw blank bg
  269.                     BG_COLOR = 0;
  270.                     print_c_at(' ' i);
  271.                 } else if (curr == point_zone1) {
  272.                     // Draw score zone 1
  273.                     BG_COLOR = 4;
  274.                     print_c_at(' ' i);
  275.                 } else if (curr == point_zone2) {
  276.                     // Draw score zone 2
  277.                     BG_COLOR = 5;
  278.                     print_c_at(' ' i);
  279.                 } else if ((curr == wall) | (curr == p1) | (curr == p2)) {
  280.                     // Draw wall
  281.                     BG_COLOR = 1;
  282.                     print_c_at(' ' i);
  283.                 } else if (curr == ball) {
  284.                     // Draw ball
  285.                     FG_COLOR = 2;
  286.                     BG_COLOR = 2;
  287.                     print_c_at(4 i); // @ at i pos
  288.                 } else if (curr == score1) {
  289.                     // Draw score 1
  290.                     FG_COLOR = 3;
  291.                     BG_COLOR = 3;
  292.                     print_c_at((p1score + 0x30) i); // print p1 score
  293.                 } else if (curr == score2) {
  294.                     // Draw score 2
  295.                     FG_COLOR = 3;
  296.                     BG_COLOR = 3;
  297.                     print_c_at((p2score + 0x30) i); // print p2 score
  298.                 }
  299.                 i = i + 1;
  300.             }
  301.             out(0x00FF 0);
  302.            
  303.       // Check points, end game if should
  304.       if (p1score >= win_score) {
  305.         // P1 wins
  306.         winner = 1;
  307.         game_running = 0;
  308.       } else if (p2score >= win_score) {
  309.         // P2 wins
  310.         winner = 2;
  311.         game_running = 0;
  312.       }
  313.             sleep_ms(120);
  314.     }
  315.    
  316.     // Print after game message and play again?
  317.     out(winner 0);
  318.         player_won[7] = (winner + 0x30);
  319.         BG_COLOR = 3;
  320.         FG_COLOR = 3;
  321.         set_cursor((print_x-8) print_y);
  322.         print(player_won);
  323.  
  324.         set_cursor((print_x-10) (print_y + 3));
  325.         print(play_again);
  326.  
  327.     // Check if play again or quit
  328.     int play_again = 0;
  329.         if (play_again == 0) {
  330.             out(play_again 0);
  331.         }
  332.     while (play_again == 0) {
  333.       keyboard = read_char();
  334.             out(0xeaea 0);
  335.       if (keyboard == '\n') {
  336.         play_again = 1;
  337.       } else if (keyboard == 27) {
  338.         // ESCAPE
  339.                 BG_COLOR = 3;
  340.                 FG_COLOR = 3;
  341.                 set_cursor((print_x-3) print_y);
  342.                 print(goodbye);
  343.                 sleep_ms(2000);
  344.                 out(0xEEEE 0);
  345.         return 0;
  346.       }
  347.     }
  348.         sleep_ms(500);
  349.         out(0xFFFF 0);
  350.         BG_COLOR = 3;
  351.         FG_COLOR = 3;
  352.         set_cursor((print_x-4) (print_y-2));
  353.         print(remaking);
  354.         sleep_ms(500);
  355.   }
  356.  
  357.     // If somehow comes here, exit
  358.   return 0;
  359. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement