forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Pong and the right wall of the screen

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Producted



Joined: 04 Jun 2009
Posts: 56

PostPosted: Mon Aug 31, 2009 10:17 am    Post subject: Pong and the right wall of the screen Reply with quote

Okay, I managed to print out one bat and one ball on the screen. The whole upper wall of the screen is the other bat (till I created the second bat). I managed to move the bat on the down-wall of the screen with the shoulder buttons. I also managed to let the ball bounce on the bat, bounce 'naturally' on the upper wall and the left wall.

There's my problem; it doesn't bounces naturally on the right wall. If you don't understand what I mean, just see for yourself. Please only tell me or show me what the solution is. Don't go wild and create the second bat etc. That's something I want to do myself. :)

(the same bug exists for the down-wall, but with a fix for the right-wall I'm able to implent it for the down-wall too).

Code:

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspctrl.h>
#include <graphics.h>
#include <stdio.h>

#define BAT_WIDTH 100
#define BALL_WIDTH 32

#define RGB(r, g, b) ((r)|((g)<<8)|((b)<<16))

PSP_MODULE_INFO("POPong", 0, 1, 1);

/* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
          sceKernelExitGame();
          return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp) {
          int cbid;

          cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
          sceKernelRegisterExitCallback(cbid);

          sceKernelSleepThreadCB();

          return 0;
}
int SetupCallbacks(void) {
          int thid = 0;

          thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
          if(thid >= 0) {
                    sceKernelStartThread(thid, 0, 0);
          }

          return thid;
}

Color White = RGB(255, 255, 255);
Color Red = RGB(255, 0, 0);

int batPosX = 190;
   
int ballPosX = 224;
int ballPosY = 116;

int ballVecX = 2;
int ballVecY = 1;

int loop = 1;
int score = 0;

SceCtrlData pad;
int analog;

char buffer1[100];
char buffer2[100];
char buffer3[100];

int main(void)
{
   SetupCallbacks();
   initGraphics();
   
   Image* batImage = loadImage("bat.png");
   Image* ballImage = loadImage("ball.png");
   
   sceCtrlSetSamplingCycle(0);
   
   sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
   
   while(1)
   {
      sceCtrlReadBufferPositive(&pad,1);
      
      if (pad.Buttons & PSP_CTRL_LTRIGGER)
      {
         batPosX-=5;
      }
      
      if (pad.Buttons & PSP_CTRL_RTRIGGER)
      {
         batPosX+=5;
      }
      
      analog = pad.Lx>>4;
      analog -= 8;
      batPosX += analog/2;
      
      if (batPosX > 480 - BAT_WIDTH)
      {
         batPosX = 480 - BAT_WIDTH;
      }
      
      if (batPosX < 0)
      {
         batPosX = 0;
      }
      
      ballPosX += ballVecX;
      ballPosY += ballVecY;
      
      if (ballPosX <= 0)
      {
         ballPosX =- ballPosX;
         ballVecX =+ 1;
      }
      
      // -> THIS NEEDS A FIX <-
      if (ballPosX >= 472)
      {
         ballPosX = 944 - ballPosX;
         ballVecX =- 1;
      }
      
      if (ballPosY <= 0)
      {
         ballPosY =-  ballPosY;
         ballVecY =+ 1;
      }
      
      if (ballPosY >= 272)
      {
         if ( (ballPosX >= batPosX) && (ballPosX <= batPosX + BAT_WIDTH - BALL_WIDTH) )
         {
            ballPosY = 544 - ballPosY;
            ballVecY =- 1;
            
            score++;
            
            sprintf(buffer3, "%i", score);
         }
         else
         {
            fillScreenRect(0xff0000ff,0,0,480,272);
            printTextScreen(15, 15, "GAME OVER", White);
            flipScreen();
            sceKernelDelayThread(5000000);
            sceKernelExitGame();
         }
      }
      
      sprintf(buffer1, "%i", ballPosX);
      sprintf(buffer2, "%i", ballPosY);
      
      fillScreenRect(White, 0, 0, 480, 272);
      
      blitAlphaImageToScreen(0, 0, BAT_WIDTH, 10, batImage, batPosX, 262);
      blitAlphaImageToScreen(0, 0, BALL_WIDTH, BALL_WIDTH, ballImage, ballPosX, ballPosY);
      
      printTextScreen(15, 15, buffer1, Red);
      printTextScreen(15, 30, buffer2, Red);
      printTextScreen(15, 45, buffer3, Red);
      
      sceDisplayWaitVblankStart();
      flipScreen();
   }
   
   sceKernelSleepThread();
}


WIP (play long enough till the ball goes against the right wall): http://producted.net/temp/POPong.rar

Thanks in advance.
_________________
Producted.net
Back to top
View user's profile Send private message
Producted



Joined: 04 Jun 2009
Posts: 56

PostPosted: Mon Aug 31, 2009 10:52 am    Post subject: Reply with quote

Inspired by the source of superpong, I came with this fix;

Code:

      // -> THIS HAS BEEN FIXED <-
      if (ballPosX + BALL_WIDTH >= 472)
      {
         ballPosX = 472 - BALL_WIDTH;
         ballVecX =- 1;
      }

_________________
Producted.net
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group