[Tutorial] Python PSP Programming
Posted: Mon Aug 13, 2012 1:38 pm
Python Programming on the psp is very similar to pc programming, the only new thing here is the module psp2d.
The Interpreter:
First you need stackless python psp: http://code.google.com/p/pspstacklesspy ... p&can=2&q=
Script:
Your main application will be stored in a file called script.py, placed in the same place as eboot.pbp (the interpreter).
Psp2d:
This module provides a 2d graphics library for all your python needs. Make sure psp2d.py is in the same place as script.py. You can find it here: http://code.google.com/p/pspstacklesspy ... p&can=2&q=
Font:
Psp2d uses sfonts which you can find by google searching. Here is an example: http://code.google.com/p/pspstacklesspy ... p&can=2&q=
Hello World:
Let's start with the basics, and what better way to learn than with our typical hello world.
Let's go line by line:
Used to open the font file (font.png)
This creates the image that will be drawn on the screen, it can also be a background:
The psp screen, not much to say about this.
The color that will be painted on screen, it works on a RGB basis.
Pretty self-explaining, you clear image with the clear color.
This is the meat of the code, it draws the text on screen. It works on a coordinate basis, the first 0 is the x coordinate (left-right), the second 0 is the y coordinate (up-down). Play around with it a bit.
Basically, screen becomes what image is.
Update screen.
Controller:
Psp2d has access to all the buttons on the psp except the note key, the screen key, the volume keys, and the home key. It is pretty simple to use: and:
Now, you don't really call pad.x, you use it with the if statement:
psp2d.Controller requires a loop with the while statement:
Now this is not the way to do this, while True is an endless loop because True is always True, so there is no way to end it, we don't want that, we want to be able to end the loop and to do so we use a variable since it's modifiable:
by pressing the circle button we modify x to become False, by becoming false the condition is no longer met and therefore the loop breaks.
Let's put all the above into a live example:
The result:
Running psp python homebrews on pc:
Since python is cross-platform and the psp2d.py file acts as a muck-up of pygame you can play and debug all psp homebrews written in python on a pc. To do this you need to install pygame (google it). Here is a screenshot of various psp python homebrews running on a pc: Next: viewtopic.php?f=5&t=13373&p=159645
The Interpreter:
First you need stackless python psp: http://code.google.com/p/pspstacklesspy ... p&can=2&q=
Script:
Your main application will be stored in a file called script.py, placed in the same place as eboot.pbp (the interpreter).
Psp2d:
This module provides a 2d graphics library for all your python needs. Make sure psp2d.py is in the same place as script.py. You can find it here: http://code.google.com/p/pspstacklesspy ... p&can=2&q=
Font:
Psp2d uses sfonts which you can find by google searching. Here is an example: http://code.google.com/p/pspstacklesspy ... p&can=2&q=
Hello World:
Let's start with the basics, and what better way to learn than with our typical hello world.
Code: Select all
import psp2d
font = psp2d.Font("font.png")
image = psp2d.Image(480, 272)
screen = psp2d.Screen()
CLEAR_COLOR = psp2d.Color(0,0,0)
image.clear(CLEAR_COLOR)
font.drawText(image, 0, 0, "Hello World")
screen.blit(image)
screen.swap()
Code: Select all
font = psp2d.Font("font.png")
Code: Select all
image = psp2d.Image(480, 272)
Code: Select all
image = psp2d.Image("background.png")
Code: Select all
screen = psp2d.Screen()
Code: Select all
CLEAR_COLOR = psp2d.Color(0,0,0)
Code: Select all
image.clear(CLEAR_COLOR)
Code: Select all
font.drawText(image, 0, 0, "Hello World")
Code: Select all
screen.blit(image)
Code: Select all
screen.swap()
Controller:
Psp2d has access to all the buttons on the psp except the note key, the screen key, the volume keys, and the home key. It is pretty simple to use:
Code: Select all
pad = psp2d.Controller()
Code: Select all
pad.up
pad.down
pad.left
pad.right
pad.cross
pad.circle
pad.square
pad.triangle
pad.l
pad.r
pad.start
pad.select
pad.analogX
pad.analogY
Code: Select all
if pad.cross:
do something
elif pad.circle:
do something different
Code: Select all
while True:
pad = psp2d.Controller()
if pad.cross:
do something
Code: Select all
x = True
while x == True:
pad = psp2d.Controller()
if pad.circle:
x = False
Let's put all the above into a live example:
Code: Select all
import psp2d
font = psp2d.Font("font.png")
image = psp2d.Image(480, 272)
screen = psp2d.Screen()
CLEAR_COLOR = psp2d.Color(0,0,0)
image.clear(CLEAR_COLOR)
font.drawText(image, 0, 0, "Hello World!")
font.drawText(image, 0, 30, "Press Circle to exit")
screen.blit(image)
screen.swap()
x = True
while x == True:
pad = psp2d.Controller()
if pad.circle:
font.drawText(image, 0, 60, "Goodbye!")
screen.blit(image)
screen.swap()
x = False
Running psp python homebrews on pc:
Since python is cross-platform and the psp2d.py file acts as a muck-up of pygame you can play and debug all psp homebrews written in python on a pc. To do this you need to install pygame (google it). Here is a screenshot of various psp python homebrews running on a pc: Next: viewtopic.php?f=5&t=13373&p=159645