Advertisement
xerpi

CPong vAlpha by xerpi

Nov 28th, 2011
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.67 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;
  11. int color,score;
  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. void collisions();
  39. int coli(float x1,float y1,float w1,float h1,float x2,float y2,float w2,float h2);
  40.  
  41. int main(){
  42.     oslInit(0);
  43.     oslInitGfx(OSL_PF_8888, 1);
  44.     oslInitConsole();
  45.    
  46.     //Limit
  47.         limit.x = 10;
  48.         limit.y = 10;
  49.         limit.x2 = 470;
  50.         limit.y2 = 262;
  51.         limit.color = RGB(255,255,255);
  52.     //Raquet
  53.         //Raquet 1
  54.             raquet1.x = 11;
  55.             raquet1.y = 121;
  56.             raquet1.w = 5;
  57.             raquet1.h = 30;
  58.             raquet1.speed = 4;
  59.             raquet1.color = RGB(255,0,0);
  60.         //Raquet 2
  61.             raquet2.x = 464;
  62.             raquet2.y = 121;
  63.             raquet2.w = 5;
  64.             raquet2.h = 30;
  65.             raquet2.speed = 4;
  66.             raquet2.color = RGB(255,0,0);
  67.     //Goal
  68.         //Goal 1
  69.             goal1.x = 0;
  70.             goal1.y = 101;
  71.             goal1.w = 10;
  72.             goal1.h = 70;
  73.             goal1.color = RGB(0,255,0);
  74.         //Goal 2
  75.             goal2.x = 470;
  76.             goal2.y = 101;
  77.             goal2.w = 10;
  78.             goal2.h = 70;
  79.             goal2.color = RGB(0,255,0);
  80.     //Ball
  81.         ball.w = 10;
  82.         ball.h = 10;
  83.         ball.color = RGB(0,0,255);
  84.         ball_reset();
  85.         ball_update();
  86.                
  87.     while (!osl_quit){
  88.         oslStartDrawing();
  89.         oslReadKeys();
  90.         oslCls();
  91.         //oslPrintf_xy(5,5,"Score: %i",score);
  92.  
  93.        
  94.         move_raquets();
  95.         move_ball();
  96.         collisions();
  97.         blit_all();
  98.        
  99.        
  100.        
  101.        
  102.         oslEndDrawing();
  103.         oslSyncFrame();
  104.  
  105.     }
  106.     oslEndGfx();
  107.     oslQuit();
  108.     return 0;
  109. }
  110.  
  111. // Randomer: Create a random number
  112.     int randomer(int from, int until) {
  113.         // Save random number
  114.         int random_value;
  115.         // Set random value
  116.         random_value = rand()%(until-from+1) + from;
  117.  
  118.         return random_value;
  119.     }
  120.  
  121.     void blit_all(){
  122.         //Score
  123.             oslPrintf_xy(120,0,"%i",raquet1.score);
  124.             oslPrintf_xy(360,0,"%i",raquet2.score);
  125.         //Raquets
  126.             oslDrawFillRect(raquet1.x, raquet1.y, raquet1.x+raquet1.w, raquet1.y+raquet1.h, raquet1.color);
  127.             oslDrawFillRect(raquet2.x, raquet2.y, raquet2.x+raquet2.w, raquet2.y+raquet2.h, raquet2.color);
  128.         //Goals
  129.             oslDrawFillRect(goal1.x, goal1.y, goal1.x+goal1.w, goal1.y+goal1.h, goal1.color);
  130.             oslDrawFillRect(goal2.x, goal2.y, goal2.x+goal2.w, goal2.y+goal2.h, goal2.color);
  131.         //Limits
  132.             oslDrawRect(limit.x, limit.y, limit.x2, limit.y2, limit.color);
  133.         //Ball
  134.             oslDrawFillRect(ball.x, ball.y, ball.x+ball.w, ball.y+ball.h, ball.color);
  135.     }
  136.    
  137.     void ball_reset(){
  138.         ball.x = 235;
  139.         ball.y = 131;
  140.         ball.speed = 5;
  141.         ball.angle = randomer(0,314159)/100000;
  142.     }
  143.    
  144.     void ball_update(){
  145.         ball.inc_x = cos(ball.angle);
  146.         ball.inc_y = sin(ball.angle);
  147.     }
  148.        
  149.     void move_raquets(){
  150.         //Raquet 1
  151.             if (osl_keys->held.up) {
  152.                 raquet1.y -= raquet1.speed;
  153.                 if(raquet1.y < limit.y) raquet1.y = limit.y;
  154.             }else if(osl_keys->held.down) {
  155.                 raquet1.y += raquet1.speed;
  156.                 if((raquet1.y + raquet1.h) > limit.y2) raquet1.y = (limit.y2-raquet1.h);
  157.             }
  158.         //Raquet 2
  159.             if (osl_keys->held.triangle) {
  160.                 raquet2.y -= raquet2.speed;
  161.                 if(raquet2.y < limit.y) raquet2.y = limit.y;
  162.             }else if(osl_keys->held.cross) {
  163.                 raquet2.y += raquet2.speed;
  164.                 if((raquet2.y + raquet2.h) > limit.y2) raquet2.y = (limit.y2-raquet2.h);
  165.             }
  166.         }
  167.        
  168.     void move_ball(){
  169.         ball.speed += 0.001;
  170.         ball.x += ball.inc_x*ball.speed;
  171.         ball.y += ball.inc_y*ball.speed;
  172.         if( (ball.y < limit.y) || ( (ball.y+ball.h) > limit.y2) ) ball.inc_y *= -1;
  173.         if( (ball.x < limit.x) || ( (ball.x+ball.w) > limit.x2) ) ball.inc_x *= -1;
  174.     }
  175.    
  176.     void collisions(){
  177.         //Ball-raquet
  178.             //Raquet 1
  179.                 if ( coli(raquet1.x,raquet1.y,raquet1.w,raquet1.h,ball.x,ball.y,ball.w,ball.h) ){
  180.                     ball.inc_x *= -1;
  181.                     ball.x = raquet1.x + raquet1.w+1;
  182.                 }
  183.             //Raquet 2
  184.                 if ( coli(raquet2.x, raquet2.y, raquet2.w,raquet2.h,ball.x,ball.y,ball.w,ball.h) ){
  185.                     ball.inc_x *= -1;
  186.                     ball.x = raquet2.x - ball.w-1;
  187.                 }
  188.         //Ball-goal
  189.             //Goal 1
  190.                 if ( coli( goal1.x, goal1.y, goal1.w,goal1.h,ball.x,ball.y,ball.w,ball.h) ){
  191.                     raquet2.score++;
  192.                     ball_reset();
  193.                 }
  194.             //Goal 2
  195.                 if ( coli( goal2.x, goal2.y, goal2.w,goal2.h,ball.x,ball.y,ball.w,ball.h) ){
  196.                     raquet1.score++;
  197.                     ball_reset();
  198.                 }              
  199.    
  200.     }
  201.    
  202.     int coli(float x1,float y1,float w1,float h1,float x2,float y2,float w2,float h2){
  203.         if( (x1 + w1 >= x2) && (x1 <= x2+w2)  && (y1 + h1 >= y2) && (y1 <= y2+h2) ) return 1;
  204.         return 0;
  205.     }
  206.  
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement