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

[Tutorial] Python PSP Programming Part III

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

[Tutorial] Python PSP Programming Part III

Post by Acid_Snake » Sun Aug 19, 2012 11:46 am

Working with Images.

Working with images in psp2d is essential to make your app nicer. Opening an image is simple as we saw before:

Code: Select all

image = psp2d.Image("image.png")
An image can be of any size up to the psp's screen resolution (480x272), and it can be moved around, resized, blend, cleared, etc


Placing an image:
You can place an image anywhere on the screen based on the x y coordinates.

Code: Select all

screen = psp2d.Screen()
image = psp2d.Image("image.png")
screen.blit(image, dx=50, dy=50)
the values dx and dy takes care of placing the image into the coordinates you give it, if none are given then the default (dx=0, dy=0) is used.


Redimentioning:
Just like placing, redimentioning takes place at the time of doing a screen.blit:

Code: Select all

screen.blit(image, dw=50, dh=50, blend=True)
dw and dh are values for width and height respectively.

Let's take a look at this code:

Code: Select all

import psp2d
screen = psp2d.Screen()
image = psp2d.Image("ICON0.PNG")
screen.blit(image, dx=50, dy=50)
screen.blit(image, dx=200, dy=150, dw=32, blend=True)
screen.swap()
while True:
	pad = psp2d.Controller()
	if pad.circle: break
and it's result:
example.png
example.png (24.87 KiB) Viewed 3492 times

Working with colors:
The function psp2d.Color is used to create color palettes based on an RGB basis:

Code: Select all

psp2d.Color(R,G,B)
with that said, to make the red color we use:

Code: Select all

psp2d.Color(255,0,0)
0 is minimum and 255 maximum. The same applies to green and blue:

Code: Select all

psp2d.Color(0,255,0) # Green
psp2d.Color(0,0,255) # Blue
If you have ever studied art class you probably know that mixing different colors give you another color, here is some other colors I have found through trial and error:

Code: Select all

WHITE_COLOR = psp2d.Color(255,255,255)
CLEAR_COLOR = psp2d.Color(0,0,0)
RED_COLOR   = psp2d.Color(255, 0, 0)
GREEN_COLOR = psp2d.Color(0, 255, 0)
BLUE_COLOR  = psp2d.Color(0,50,255)
YELLOW_COLOR  = psp2d.Color(255,255,0)
CLEAR_COLOR is nothing more than black. Play around with the different numbers to get different colors.

Here is the example code for psptools' pixel fixer:

Code: Select all

def pixel_fix():
	# Stuck pixel fixer
	x12 = True
	log.write("pixel fixer started\n")
	while x12 == True:
		pad = psp2d.Controller()
		time.sleep(0.07)
		img.clear(WHITE_COLOR)
		scr.blit(img)
		scr.swap()
		time.sleep(0.07)
		img.clear(RED_COLOR)
		scr.blit(img)
		scr.swap()
		time.sleep(0.07)
		img.clear(GREEN_COLOR)
		scr.blit(img)
		scr.swap()
		time.sleep(0.07)
		img.clear(BLUE_COLOR)
		scr.blit(img)
		scr.swap()
		time.sleep(0.07)
		img.clear(YELLOW_COLOR)
		scr.blit(img)
		scr.swap()
		if pad.triangle:
			x12 = False
			log.write("pixel fixer ended by user\n")
			main()
There is no much mystery here, basically the same code gets repeted over and over again until the user press circle, the main code is:

Code: Select all

time.sleep(0.07)  # Delay between screen change
img.clear(COLOR) # The color to fill the screen with, this is the only thing that changes in the entire function
scr.blit(img) # You should already know this by now
scr.swap() # # You should already know this by now
This is all for now.

Previous: viewtopic.php?f=5&t=13373
Next: viewtopic.php?f=37&t=33157
Advertising

User avatar
bronywhocodes
Posts: 21
Joined: Mon Dec 07, 2015 7:19 am

Re: [Tutorial] Python PSP Programming Part III

Post by bronywhocodes » Mon Dec 07, 2015 9:23 am

hello.

i have run into a problem while making a sprite move.

when i use this:

Code: Select all

import psp2d

screen = psp2d.Screen()
CLEAR_COLOUR = psp2d.Color(0,0,0)
pad = psp2d.Controller()



character = psp2d.Image("char1.png")


stillgame = True

while stillgame == True:	
	
	x = 50
	y = 50
	
	yChange = 0
	xChange = 0
	
	if pad.left:
		xChange = -10
		
	if pad.right:
		xChange = 10 
		
	x += xChange
	
	screen.clear(CLEAR_COLOUR)
	screen.blit(character,dx = x,dy = y)
	
	screen.swap()
nothing happens. (i am new to this so, i dont know what the D does :P)

and when i do this:

Code: Select all

import psp2d

screen = psp2d.Screen()
CLEAR_COLOUR = psp2d.Color(0,0,0)
pad = psp2d.Controller()



character = psp2d.Image("char1.png")


stillgame = True

while stillgame == True:	
	
	x = 50
	y = 50
	
	yChange = 0
	xChange = 0
	
	if pad.left:
		xChange = -10
		
	if pad.right:
		xChange = 10 
		
	x += xChange
	
	screen.clear(CLEAR_COLOUR)
	screen.blit(character,x,y)
	
	screen.swap()
i get an error:

Code: Select all

ValueError: subsurface rectangle outside surface area
what may i be doing wrong?

thks :D
Advertising
I am a coder. Python is My favourite. Also, here's my consoles that I can hack

Wii [unhacked (plz tutorial on how to code for wii)]
Psp go (6.61 pro cfw unbridled)

Post Reply

Return to “Programming and Security”