Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <oslib/oslib.h>
- PSP_MODULE_INFO("CPong by xerpi", 0, 1, 1);
- PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
- typedef struct{
- float x,y,w,h,speed;
- int color,score;
- }Raquet;
- struct{
- float x,y,w,h,inc_x,inc_y,angle,speed;
- int color;
- }ball;
- struct{
- float x,y,x2,y2;
- int color;
- }limit;
- typedef struct{
- float x,y,w,h;
- int color;
- }Goal;
- Raquet raquet1,raquet2;
- Goal goal1,goal2;
- int randomer(int from, int until);
- void move_raquets();
- void move_ball();
- void blit_all();
- void ball_update();
- void ball_reset();
- void collisions();
- int coli(float x1,float y1,float w1,float h1,float x2,float y2,float w2,float h2);
- int main(){
- oslInit(0);
- oslInitGfx(OSL_PF_8888, 1);
- oslInitConsole();
- //Limit
- limit.x = 10;
- limit.y = 10;
- limit.x2 = 470;
- limit.y2 = 262;
- limit.color = RGB(255,255,255);
- //Raquet
- //Raquet 1
- raquet1.x = 11;
- raquet1.y = 121;
- raquet1.w = 5;
- raquet1.h = 30;
- raquet1.speed = 4;
- raquet1.color = RGB(255,0,0);
- //Raquet 2
- raquet2.x = 464;
- raquet2.y = 121;
- raquet2.w = 5;
- raquet2.h = 30;
- raquet2.speed = 4;
- raquet2.color = RGB(255,0,0);
- //Goal
- //Goal 1
- goal1.x = 0;
- goal1.y = 101;
- goal1.w = 10;
- goal1.h = 70;
- goal1.color = RGB(0,255,0);
- //Goal 2
- goal2.x = 470;
- goal2.y = 101;
- goal2.w = 10;
- goal2.h = 70;
- goal2.color = RGB(0,255,0);
- //Ball
- ball.w = 10;
- ball.h = 10;
- ball.color = RGB(0,0,255);
- ball_reset();
- ball_update();
- while (!osl_quit){
- oslStartDrawing();
- oslReadKeys();
- oslCls();
- //oslPrintf_xy(5,5,"Score: %i",score);
- move_raquets();
- move_ball();
- collisions();
- blit_all();
- oslEndDrawing();
- oslSyncFrame();
- }
- oslEndGfx();
- oslQuit();
- return 0;
- }
- // Randomer: Create a random number
- int randomer(int from, int until) {
- // Save random number
- int random_value;
- // Set random value
- random_value = rand()%(until-from+1) + from;
- return random_value;
- }
- void blit_all(){
- //Score
- oslPrintf_xy(120,0,"%i",raquet1.score);
- oslPrintf_xy(360,0,"%i",raquet2.score);
- //Raquets
- oslDrawFillRect(raquet1.x, raquet1.y, raquet1.x+raquet1.w, raquet1.y+raquet1.h, raquet1.color);
- oslDrawFillRect(raquet2.x, raquet2.y, raquet2.x+raquet2.w, raquet2.y+raquet2.h, raquet2.color);
- //Goals
- oslDrawFillRect(goal1.x, goal1.y, goal1.x+goal1.w, goal1.y+goal1.h, goal1.color);
- oslDrawFillRect(goal2.x, goal2.y, goal2.x+goal2.w, goal2.y+goal2.h, goal2.color);
- //Limits
- oslDrawRect(limit.x, limit.y, limit.x2, limit.y2, limit.color);
- //Ball
- oslDrawFillRect(ball.x, ball.y, ball.x+ball.w, ball.y+ball.h, ball.color);
- }
- void ball_reset(){
- ball.x = 235;
- ball.y = 131;
- ball.speed = 5;
- ball.angle = randomer(0,314159)/100000;
- }
- void ball_update(){
- ball.inc_x = cos(ball.angle);
- ball.inc_y = sin(ball.angle);
- }
- void move_raquets(){
- //Raquet 1
- if (osl_keys->held.up) {
- raquet1.y -= raquet1.speed;
- if(raquet1.y < limit.y) raquet1.y = limit.y;
- }else if(osl_keys->held.down) {
- raquet1.y += raquet1.speed;
- if((raquet1.y + raquet1.h) > limit.y2) raquet1.y = (limit.y2-raquet1.h);
- }
- //Raquet 2
- if (osl_keys->held.triangle) {
- raquet2.y -= raquet2.speed;
- if(raquet2.y < limit.y) raquet2.y = limit.y;
- }else if(osl_keys->held.cross) {
- raquet2.y += raquet2.speed;
- if((raquet2.y + raquet2.h) > limit.y2) raquet2.y = (limit.y2-raquet2.h);
- }
- }
- void move_ball(){
- ball.speed += 0.001;
- ball.x += ball.inc_x*ball.speed;
- ball.y += ball.inc_y*ball.speed;
- if( (ball.y < limit.y) || ( (ball.y+ball.h) > limit.y2) ) ball.inc_y *= -1;
- if( (ball.x < limit.x) || ( (ball.x+ball.w) > limit.x2) ) ball.inc_x *= -1;
- }
- void collisions(){
- //Ball-raquet
- //Raquet 1
- if ( coli(raquet1.x,raquet1.y,raquet1.w,raquet1.h,ball.x,ball.y,ball.w,ball.h) ){
- ball.inc_x *= -1;
- ball.x = raquet1.x + raquet1.w+1;
- }
- //Raquet 2
- if ( coli(raquet2.x, raquet2.y, raquet2.w,raquet2.h,ball.x,ball.y,ball.w,ball.h) ){
- ball.inc_x *= -1;
- ball.x = raquet2.x - ball.w-1;
- }
- //Ball-goal
- //Goal 1
- if ( coli( goal1.x, goal1.y, goal1.w,goal1.h,ball.x,ball.y,ball.w,ball.h) ){
- raquet2.score++;
- ball_reset();
- }
- //Goal 2
- if ( coli( goal2.x, goal2.y, goal2.w,goal2.h,ball.x,ball.y,ball.w,ball.h) ){
- raquet1.score++;
- ball_reset();
- }
- }
- int coli(float x1,float y1,float w1,float h1,float x2,float y2,float w2,float h2){
- if( (x1 + w1 >= x2) && (x1 <= x2+w2) && (y1 + h1 >= y2) && (y1 <= y2+h2) ) return 1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement