Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Python PSP Programming

Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Acid_Snake
Retired Mod
Posts: 3100
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

[Tutorial] Python PSP Programming

Post by Acid_Snake »

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.

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()
Let's go line by line:

Code: Select all

font = psp2d.Font("font.png")
Used to open the font file (font.png)

Code: Select all

image = psp2d.Image(480, 272)
This creates the image that will be drawn on the screen, it can also be a background:

Code: Select all

image = psp2d.Image("background.png")

Code: Select all

screen = psp2d.Screen()
The psp screen, not much to say about this.

Code: Select all

CLEAR_COLOR = psp2d.Color(0,0,0)
The color that will be painted on screen, it works on a RGB basis.

Code: Select all

image.clear(CLEAR_COLOR)
Pretty self-explaining, you clear image with the clear color.

Code: Select all

font.drawText(image, 0, 0, "Hello World")
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.

Code: Select all

screen.blit(image)
Basically, screen becomes what image is.

Code: Select all

screen.swap()
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:

Code: Select all

pad = psp2d.Controller()
and:

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
Now, you don't really call pad.x, you use it with the if statement:

Code: Select all

if pad.cross:
    do something
elif pad.circle:
    do something different
psp2d.Controller requires a loop with the while statement:

Code: Select all

while True:
    pad = psp2d.Controller()
    if pad.cross:
        do something
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:

Code: Select all

x = True
while x == True:
    pad = psp2d.Controller()
    if pad.circle:
        x = False
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:

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
The result:
psp_hello.png
psp_hello.png (4.11 KiB) Viewed 61523 times

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:
pc_psp.jpg
pc_psp.jpg (66.9 KiB) Viewed 61523 times
Next: viewtopic.php?f=5&t=13373&p=159645
Advertising
Last edited by Acid_Snake on Sun Aug 19, 2012 10:50 am, edited 2 times in total.
Nelson93
Posts: 26
Joined: Sun Apr 29, 2012 8:03 am

Re: [Tutorial] Python PSP Programming

Post by Nelson93 »

Incredible tutorial Acid_Snake! It's such an interesting read epically for people like me with no prior programming knowledge! It really does make me appreciate the work that goes into something like hbl and homebrew development. Thanks!!
Advertising
Acid_Snake
Retired Mod
Posts: 3100
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: [Tutorial] Python PSP Programming

Post by Acid_Snake »

Nelson93 wrote:Incredible tutorial Acid_Snake! It's such an interesting read epically for people like me with no prior programming knowledge! It really does make me appreciate the work that goes into something like hbl and homebrew development. Thanks!!
Thanks! Also I want to announce that the next version of pymenu will ship with a bunch of tools and libraries to make python development easier, and incorporating your own homebrew to pymenu will be even easier (adding one line to one file). Here is so far the code for a Hello World with pymenu's api:

Code: Select all

from pymenu.define import *
from pymenu.common import *
txt(["Hello World"])
Much better, right?
Nelson93
Posts: 26
Joined: Sun Apr 29, 2012 8:03 am

Re: [Tutorial] Python PSP Programming

Post by Nelson93 »

That's so great! I really haven't had the chance to mess about with vhbl yet and the alternitive menues but I can't wait to give it a shot once 1.80 is realised and I get the exploit! Thanks again! :D
Acid_Snake
Retired Mod
Posts: 3100
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: [Tutorial] Python PSP Programming

Post by Acid_Snake »

you can always try it with a psp. when I finish the next pymenu version I'll make a tuto on how to use it's api, it greatly improves python psp development.
qwikrazor87
Guru
Posts: 2874
Joined: Sat Apr 21, 2012 1:23 pm
Location: The North Pole

Re: [Tutorial] Python PSP Programming

Post by qwikrazor87 »

Hey you made the tut! Thanks Acid_Snake. :)
PSP 2001 - TA-085 - 6.61 PRO-C2
PS Vita 3G - PCH-1101 - 3.65 HENkaku Ensō
Alcatel phone - Android 8.1.0
Laptop - Toshiba Satellite L305D-S5974 - Ubuntu 16.04 LTS
Nelson93
Posts: 26
Joined: Sun Apr 29, 2012 8:03 am

Re: [Tutorial] Python PSP Programming

Post by Nelson93 »

Sadly I don't own a psp any more :( I actually don't have any experience with hbl in general since when I had my psp I used full cfw :P
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Python PSP Programming

Post by m0skit0 »

Cool tutorial, moving and sticking ;)
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
psvitainfo
Posts: 7
Joined: Fri Mar 23, 2012 3:19 pm
Contact:

Re: [Tutorial] Python PSP Programming

Post by psvitainfo »

thanks for the tutorial
Acid_Snake
Retired Mod
Posts: 3100
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: [Tutorial] Python PSP Programming

Post by Acid_Snake »

Thanks for your support guys ;)
Update: it turns out you do have access to the joystick (that's why I said I wasn't sure about that):

Code: Select all

pad.analogX
pad.analogY
I've updated the tutorial accordingly.
Locked

Return to “Programming and Security”