--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
How to make a player jump!!!
Makefile:
Code:
TARGET = Lesson7
OBJS = main.o
INCDIR =
CFLAGS = -G4 -Wall -O2
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
LIBDIR =
LDFLAGS =
STDLIBS= -losl -lpng -lz \
-lpspsdk -lpspctrl -lpspumd -lpsprtc -lpsppower -lpspgu -lpspaudiolib -lpspaudio -lm
LIBS=$(STDLIBS)$(YOURLIBS)
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Lesson7
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
Using Lesson4 sprite.
Code:
#include <oslib/oslib.h>
//Necessary to create eboot
PSP_MODULE_INFO("OSLib Sample", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
//declaration of the pointers of our images
OSL_IMAGE *background, *sprite;
//definitions
#define DOWN 0
#define UP 35
#define RIGHT 70
#define LEFT 105
//variables
int sprite_position;
int sprite_march;
int sprite_accel = 1;
int sprite_vel_y = 0;
int sprite_jumping = 0;
//function declarations
void Buttons();
void SpriteAnimate();
int main()
{
//Initialization of the Oslib library
oslInit(0);
//Initialization of the graphics mode
oslInitGfx(OSL_PF_8888, 1);
//Sets the transparency color (pink)
oslSetTransparentColor(RGB(255,0,255));
//loads our images into memory
background = oslLoadImageFile("background.png", OSL_IN_RAM, OSL_PF_5551);
sprite = oslLoadImageFile("sprite.png", OSL_IN_RAM, OSL_PF_5551);
//Disables the transpaent color (pink)
oslDisableTransparentColor();
//verification that all files are present
if (!background || !sprite)
oslDebug("It is impossible to load one or more of the images that are required in this program. Please make sure you have all the files required in the eboot folder");
//Sets the sprite's original position on the screen
sprite->x = 100; // (Or wherever floor is)
sprite->y = 210;
sprite_position = RIGHT;
//main while loop
while (!osl_quit)
{
//To be able to draw on the screen
oslStartDrawing();
//calls the Buttons() function
Buttons();
//Draw the images to the screen
oslDrawImage(background);
oslDrawImage(sprite);
//Ends drawing mode
oslEndDrawing();
//Synchronizes the screen
oslSyncFrame();
}
//Terminate the program
oslEndGfx();
oslQuit();
return 0;
}
void Buttons()
{
//initiate the PSP's buttons
oslReadKeys();
if (osl_keys->held.right)
{
sprite->x += 2;
sprite_position = RIGHT;
SpriteAnimate();
}
if (osl_keys->held.left)
{
sprite->x -= 2;
sprite_position = LEFT;
SpriteAnimate();
}
// Here it is the begin of the player jump code
if (osl_keys->pressed.cross && sprite->y == 210)
{
sprite_vel_y = 15;
sprite_jumping = 1;
}
if (sprite_jumping == 1) /* Move sprite if we are jumping */
{
sprite_vel_y = sprite_vel_y - sprite_accel;
sprite->y = sprite->y - sprite_vel_y;
}
if (sprite->y >= 210) /* Check to see if sprite has hit floor */
{
sprite_vel_y = 0;
sprite_jumping = 0;
sprite->y = 210;
}
// Here it is the end of the player jump code
//If a button is not pressed
if (!osl_keys->held.value)
{
//Start the variable over for when a button is pressed again
sprite_march = 0;
//Sets the sprite's direction
oslSetImageTileSize(sprite,0,sprite_position,22,35);
}
}
void SpriteAnimate()
{
//Moves the sprite in the row that it is in
sprite_march++;
//Moves the sprite constantly
oslSetImageTileSize(sprite,(sprite_march * 22),sprite_position,22,35);
//resets the sprite movement in that row
if (sprite_march == 6) sprite_march = 0;
}
:mrgreenthumbsup: :mrgreenthumbsup:
The only thing that i willexplaine is the jumping code!
first we need to create three variables(integers) and set their value:
int sprite_accel = 1;
the value of the integer sprite_accel is 1
int sprite_vel_y = 0;
the value of the integer sprite_vel_y is 0
int sprite_jumping = 0;
the value of the integer sprite_jumping is 0
And now we can type the player jump code:
if (osl_keys->pressed.cross && sprite->y == 210)
if the button cross is pressed and the sprite(coordinate y) is 210
{
sprite_vel_y = 15;
then the integer sprite_vel_y is 15
sprite_jumping = 1;
and the integer sprite_jumping is 1
}
if (sprite_jumping == 1)
if the integer sprite_jumping is 1
{
sprite_vel_y = sprite_vel_y - sprite_accel;
then the integer sprite_vel_y is (sprite_vel_y - sprite_accel
sprite->y = sprite->y - sprite_vel_y;
and the sprite(coordinate y) is (sprite(coordinate y) - sprite_vel_y)
}
if (sprite->y >= 210)
if the sprite(coordinate y) is greater or iqual than 210
{
sprite_vel_y = 0;
then the integer sprite_vel_y is 0
sprite_jumping = 0;
and the integer sprite_jumping is 0
sprite->y = 210;
and the sprite(coordinate y) is 210
}
First, we need to set the variables and set their value,
and second, the player only jumps when the cross button is pressed and when the sprite(coordinate y) is 210.
:D