Pong and the right wall of the screen

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Producted
Posts: 56
Joined: Thu Jun 04, 2009 12:16 am

Pong and the right wall of the screen

Post by Producted »

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: Select all

#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&#40;r, g, b&#41; &#40;&#40;r&#41;|&#40;&#40;g&#41;<<8&#41;|&#40;&#40;b&#41;<<16&#41;&#41;

PSP_MODULE_INFO&#40;"POPong", 0, 1, 1&#41;;

/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123;
          sceKernelExitGame&#40;&#41;;
          return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41; &#123;
          int cbid;

          cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
          sceKernelRegisterExitCallback&#40;cbid&#41;;

          sceKernelSleepThreadCB&#40;&#41;;

          return 0;
&#125;
int SetupCallbacks&#40;void&#41; &#123;
          int thid = 0;

          thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
          if&#40;thid >= 0&#41; &#123;
                    sceKernelStartThread&#40;thid, 0, 0&#41;;
          &#125;

          return thid;
&#125;

Color White = RGB&#40;255, 255, 255&#41;;
Color Red = RGB&#40;255, 0, 0&#41;;

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&#91;100&#93;;
char buffer2&#91;100&#93;;
char buffer3&#91;100&#93;;

int main&#40;void&#41;
&#123;
	SetupCallbacks&#40;&#41;;
	initGraphics&#40;&#41;;
	
	Image* batImage = loadImage&#40;"bat.png"&#41;;
	Image* ballImage = loadImage&#40;"ball.png"&#41;;
	
	sceCtrlSetSamplingCycle&#40;0&#41;;
	
	sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_ANALOG&#41;;
	
	while&#40;1&#41;
	&#123;
		sceCtrlReadBufferPositive&#40;&pad,1&#41;;
		
		if &#40;pad.Buttons & PSP_CTRL_LTRIGGER&#41;
		&#123;
			batPosX-=5;
		&#125;
		
		if &#40;pad.Buttons & PSP_CTRL_RTRIGGER&#41;
		&#123;
			batPosX+=5;
		&#125;
		
		analog = pad.Lx>>4;
		analog -= 8;
		batPosX += analog/2;
		
		if &#40;batPosX > 480 - BAT_WIDTH&#41;
		&#123;
			batPosX = 480 - BAT_WIDTH;
		&#125;
		
		if &#40;batPosX < 0&#41;
		&#123;
			batPosX = 0;
		&#125;
		
		ballPosX += ballVecX;
		ballPosY += ballVecY;
		
		if &#40;ballPosX <= 0&#41;
		&#123;
			ballPosX =- ballPosX;
			ballVecX =+ 1;
		&#125;
		
		// -> THIS NEEDS A FIX <-
		if &#40;ballPosX >= 472&#41;
		&#123;
			ballPosX = 944 - ballPosX;
			ballVecX =- 1;
		&#125;
		
		if &#40;ballPosY <= 0&#41;
		&#123;
			ballPosY =-  ballPosY;
			ballVecY =+ 1;
		&#125;
		
		if &#40;ballPosY >= 272&#41;
		&#123;
			if &#40; &#40;ballPosX >= batPosX&#41; && &#40;ballPosX <= batPosX + BAT_WIDTH - BALL_WIDTH&#41; &#41;
			&#123;
				ballPosY = 544 - ballPosY;
				ballVecY =- 1;
				
				score++;
				
				sprintf&#40;buffer3, "%i", score&#41;;
			&#125;
			else
			&#123;
				fillScreenRect&#40;0xff0000ff,0,0,480,272&#41;;
				printTextScreen&#40;15, 15, "GAME OVER", White&#41;;
				flipScreen&#40;&#41;;
				sceKernelDelayThread&#40;5000000&#41;;
				sceKernelExitGame&#40;&#41;;
			&#125;
		&#125;
		
		sprintf&#40;buffer1, "%i", ballPosX&#41;;
		sprintf&#40;buffer2, "%i", ballPosY&#41;;
		
		fillScreenRect&#40;White, 0, 0, 480, 272&#41;;
		
		blitAlphaImageToScreen&#40;0, 0, BAT_WIDTH, 10, batImage, batPosX, 262&#41;;
		blitAlphaImageToScreen&#40;0, 0, BALL_WIDTH, BALL_WIDTH, ballImage, ballPosX, ballPosY&#41;;
		
		printTextScreen&#40;15, 15, buffer1, Red&#41;;
		printTextScreen&#40;15, 30, buffer2, Red&#41;;
		printTextScreen&#40;15, 45, buffer3, Red&#41;;
		
		sceDisplayWaitVblankStart&#40;&#41;;
		flipScreen&#40;&#41;;
	&#125;
	
	sceKernelSleepThread&#40;&#41;;
&#125;
WIP (play long enough till the ball goes against the right wall): http://producted.net/temp/POPong.rar

Thanks in advance.
Producted
Posts: 56
Joined: Thu Jun 04, 2009 12:16 am

Post by Producted »

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

Code: Select all

		// -> THIS HAS BEEN FIXED <-
		if &#40;ballPosX + BALL_WIDTH >= 472&#41;
		&#123;
			ballPosX = 472 - BALL_WIDTH;
			ballVecX =- 1;
		&#125;
Post Reply