Advertisement
xerpi

CPong

Nov 28th, 2011
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <oslib/oslib.h>
  4.  
  5. PSP_MODULE_INFO("CPong by xerpi", 0, 1, 1);
  6. PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
  7.  
  8.  
  9. typedef struct{
  10. float x,y,w,h,speed,score;
  11. int color;
  12. }Raquet;
  13.  
  14. struct{
  15. float x,y,w,h,inc_x,inc_y,angle,speed;
  16. int color;
  17. }ball;
  18.  
  19. struct{
  20. float x,y,x2,y2;
  21. int color;
  22. }limit;
  23.  
  24. typedef struct{
  25. float x,y,w,h;
  26. int color;
  27. }Goal;
  28.  
  29. Raquet raquet1,raquet2;
  30. Goal goal1,goal2;
  31.  
  32. int randomer(int from, int until);
  33. void move_raquets();
  34. void move_ball();
  35. void blit_all();
  36. void ball_update();
  37. void ball_reset();
  38.  
  39. int main(){
  40.     oslInit(0);
  41.     oslInitGfx(OSL_PF_8888, 1);
  42.     oslInitConsole();
  43.    
  44.     //Limit
  45.         limit.x = 20;
  46.         limit.y = 10;
  47.         limit.x2 = 440;
  48.         limit.y2 = 256;
  49.         limit.color = RGB(255,255,255);
  50.     //Raquet
  51.         //Raquet 1
  52.             raquet1.x = 20;
  53.             raquet1.y = 121;
  54.             raquet1.w = 5;
  55.             raquet1.h = 30;
  56.             raquet1.speed = 2;
  57.             raquet1.color = RGB(255,0,0);
  58.         //Raquet 2
  59.             raquet2.x = 420;
  60.             raquet2.y = 121;
  61.             raquet2.w = 5;
  62.             raquet2.h = 30;
  63.             raquet2.speed = 2;
  64.             raquet2.color = RGB(255,0,0);
  65.     //Goal
  66.         //Goal 1
  67.             goal1.x = 0;
  68.             goal1.y = 101;
  69.             goal1.w = 20;
  70.             goal1.h = 70;
  71.             goal1.color = RGB(0,255,0);
  72.         //Goal 2
  73.             goal2.x = 460;
  74.             goal2.y = 101;
  75.             goal2.w = 20;
  76.             goal2.h = 70;
  77.             goal2.color = RGB(0,255,0);
  78.     //Ball
  79.         ball.w = 10;
  80.         ball.h = 10;
  81.         ball.color = RGB(0,0,255);
  82.         ball_reset();
  83.         ball_update();
  84.                
  85.     while (!osl_quit){
  86.         oslStartDrawing();
  87.         oslReadKeys();
  88.         oslCls();
  89.         //oslPrintf_xy(5,5,"Score: %i",score);
  90.  
  91.        
  92.         move_raquets();
  93.         move_ball();
  94.         blit_all();
  95.        
  96.        
  97.        
  98.        
  99.         oslEndDrawing();
  100.         oslSyncFrame();
  101.  
  102.     }
  103.     oslEndGfx();
  104.     oslQuit();
  105.     return 0;
  106. }
  107.  
  108. // Randomer: Create a random number
  109.     int randomer(int from, int until) {
  110.         // Save random number
  111.         int random_value;
  112.         // Set random value
  113.         random_value = rand()%(until-from+1) + from;
  114.  
  115.         return random_value;
  116.     }
  117.  
  118.     void blit_all(){
  119.         //Raquets
  120.             oslDrawFillRect(raquet1.x, raquet1.y, raquet1.x+raquet1.w, raquet1.y+raquet1.h, raquet1.color);
  121.             oslDrawFillRect(raquet2.x, raquet2.y, raquet2.x+raquet2.w, raquet2.y+raquet2.h, raquet2.color);
  122.         //Goals
  123.             oslDrawFillRect(goal1.x, goal1.y, goal1.x+goal1.w, goal1.y+goal1.h, goal1.color);
  124.             oslDrawFillRect(goal2.x, goal2.y, goal2.x+goal2.w, goal2.y+goal2.h, goal2.color);
  125.         //Limits
  126.             oslDrawRect(limit.x, limit.y, limit.x2, limit.y2, limit.color);
  127.         //Ball
  128.             oslDrawFillRect(ball.x, ball.y, ball.x+ball.w, ball.y+ball.h, ball.color);
  129.     }
  130.    
  131.     void ball_reset(){
  132.         ball.x = 235;
  133.         ball.y = 131;
  134.         ball.speed = 1;
  135.         ball.angle = randomer(0,314159)/100000;
  136.     }
  137.    
  138.     void ball_update(){
  139.         ball.inc_x = cos(ball.angle);
  140.         ball.inc_y = sin(ball.angle);
  141.     }
  142.        
  143.     void move_raquets(){
  144.         //Raquet 1
  145.             if (osl_keys->held.up) {
  146.                 raquet1.y -= raquet1.speed;
  147.                 if(raquet1.y < limit.y) raquet1.y = limit.y;
  148.             }else if(osl_keys->held.down) {
  149.                 raquet1.y += raquet1.speed;
  150.                 if((raquet1.y + raquet1.h) < limit.y2) raquet1.y = (limit.y2-raquet1.h);
  151.             }
  152.         //Raquet 2
  153.             if (osl_keys->held.triangle) {
  154.                 raquet2.y -= raquet2.speed;
  155.                 if(raquet2.y < limit.y) raquet2.y = limit.y;
  156.             }else if(osl_keys->held.cross) {
  157.                 raquet2.y += raquet2.speed;
  158.                 if((raquet2.y + raquet2.h) < limit.y2) raquet2.y = (limit.y2-raquet2.h);
  159.             }
  160.         }
  161.        
  162.     void move_ball(){
  163.         ball.speed += 0.001;
  164.         ball.x += ball.inc_x*ball.speed;
  165.         ball.y += ball.inc_y*ball.speed;
  166.         if( (ball.y < limit.y) || ( (ball.y+ball.h) > limit.y2) ) ball.inc_y *= -1;
  167.         if( (ball.x < limit.x) || ( (ball.x+ball.w) > limit.x2) ) ball.inc_x *= -1;
  168.     }
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement