Forum rules: Forum rule Nº 15 is strictly enforced in this subforum.
#158753 by Acid_Snake
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.
Code: Select allimport 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 allfont = psp2d.Font("font.png")
Used to open the font file (font.png)

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


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

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

Code: Select allimage.clear(CLEAR_COLOR)
Pretty self-explaining, you clear image with the clear color.

Code: Select allfont.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 allscreen.blit(image)
Basically, screen becomes what image is.

Code: Select allscreen.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 allpad = psp2d.Controller()
and:
Code: Select allpad.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 allif pad.cross:
    do something
elif pad.circle:
    do something different

psp2d.Controller requires a loop with the while statement:
Code: Select allwhile 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 allx = 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 allimport 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 14914 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 14914 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.
#158870 by Nelson93
Tue Aug 14, 2012 12:56 pm
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
#158907 by Acid_Snake
Tue Aug 14, 2012 4:38 pm
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 allfrom pymenu.define import *
from pymenu.common import *
txt(["Hello World"])
Much better, right?
#158937 by Nelson93
Tue Aug 14, 2012 7:16 pm
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
#159153 by qwikrazor87
Wed Aug 15, 2012 10:05 pm
Hey you made the tut! Thanks Acid_Snake. :)

Who is online

Users browsing this forum: No registered users and 2 guests