| Reply | « Previous Thread | Next Thread » |
|
Hi all guys!
I'm making a game in phyton and have done all the hard part of the game. For test i've used canvas.rectangle instead of real image. Now i'm switching these rectangles to images with canvas.blit(image,target=(x,y),mask=testmask). I have to draw 5 5x5 pixel image and 1 10x10 image. This is into a while loop with a sleep timer at the end. I've inserted the drawing code just after the canvas.clear(), but when i'm running the game it seems that the draw function is too slow and the screen is cleared before it finish to draw my images. i post some part of code: automask code for images (called once at the beggining of program just after the image loading) Code:
def automask(im):
width, height = im.size
mask = Image.new(im.size, '1') # black and white
tran = im.getpixel((0,0))[0] # transparent top-left
for y in range(height):
line = im.getpixel([(x, y) for x in range(width)])
for x in range(width):
if line[x] == tran:
mask.point((x,y), 0) # mask on the point
return mask
Code:
canvas.clear(0x000000) canvas.rectangle((ostacolox, ostacolo, (ostacolox + 10), (ostacolo + 30)), fill=color) canvas.rectangle((ostacolo2x, ostacolo2, (ostacolo2x + 10), (ostacolo2 + 30)), fill=color) spunti= unicode(int(punti)) slivello= unicode(livello) canvas.text((290,18), spunti ,fill=0xffff00) canvas.text((2,18), "Livello " + slivello, fill=0xffff00) canvas.blit(corpo, target=(x1,y1)) canvas.blit(corpo, target=(x3,y3)) canvas.blit(corpo, target=(x5,y5)) canvas.blit(corpo, target=(x7,y7)) canvas.blit(corpo, target=(x9,y9)) canvas.blit(testa, target=(x11,y11),mask=masktesta) If you want you can watch my game working with canvas.rectagle here -> http://www.youtube.com/watch?v=OTqyHoNIlOw Sorry for my bad english! I hope in some reply!! Thank you Michele |
|
Quote:
Code:
# Build offscreen buffer
buffer = graphics.Image.new(canvas.size, 'RGB')
# Main loop
while 1:
# Draw next frame to buffer
buffer.clear(0x000000)
buffer.rectangle((ostacolox, ostacolo, (ostacolox + 10), (ostacolo + 30)), fill=color)
buffer.rectangle((ostacolo2x, ostacolo2, (ostacolo2x + 10), (ostacolo2 + 30)), fill=color)
spunti= unicode(int(punti))
slivello= unicode(livello)
buffer.text((290,18), spunti ,fill=0xffff00)
buffer.text((2,18), "Livello " + slivello, fill=0xffff00)
buffer.blit(corpo, target=(x1,y1))
buffer.blit(corpo, target=(x3,y3))
buffer.blit(corpo, target=(x5,y5))
buffer.blit(corpo, target=(x7,y7))
buffer.blit(corpo, target=(x9,y9))
buffer.blit(testa, target=(x11,y11),mask=masktesta)
# Now blit buffer onto canvas
canvas.blit(buffer)
# Sleep for a bit
e32.ao_sleep(0.1)
|
|
Great Job man! Now it's working fine. Sorry for my stupid question but i'm very newbie to phyton and i didn't know about the possibility to create an external buffer
![]() Now the game runs a little slower but there is not flickering at all!!! I'm running this game on a N95 8gb.. do you think there will be speed problem with other s60 3rd edition? i'm asking this because i know there are other 3rd edition with lower cpu speed.. |
|
Ok, now i have solved the flickering problem, but i'm having another problem. With the buffer method i have draw 2 moving 320x240 png that are the background of the game. I need to import 2 times the png because when the first reach x=-320 and it's totally out of the screen i need the second one to be at x=0 and then set the first image to be x=320 se they do a infinite cycle giving the look of an infitine moving background. The problem is that this is very heavy: the game fps fall down and it's near to unplayable. Someone have ideas on how to speed up this code or make the same effect with a lighte code?
I post the code i use to draw the background: Code:
bx1=0
bx2=320
back1 = Image.open("E:\\back2.png")
res_x, res_y = canvas.size
buffer = graphics.Image.new(canvas.size, 'RGB')
#on the while loop:
buffer.blit(back1, target=(bx1,0))
buffer.blit(back1, target=(bx2,0))
...
...
canv.blit(buffer)
...
...
bx1 = bx1-2
if bx1<= -320:
bx1=320
bx2 = bx2-2
if bx2<= -320:
bx2=320
Michele |
|
Quote:
Code:
back = Image.open("E:\\back2.png")
w, h = back.size
dblBack = Image.new((w*2, h), 'RGB')
dblBack.blit(back, target=(0, 0))
dblBack.blit(back, target=(w, 0))
x = 0
while 1:
buffer.blit(dblBack, target=(-x,0))
canvas.blit(buffer)
x = (x + 2) % w
e32.ao_sleep(0.1)
|
|
Quote:
i'm very newbie with python and i want to learn it better as i can!! |
|
Quote:
When you start getting performance issues, you need to profile your existing code, by calculating how many milliseconds each line of code is taking to execute (time.clock() can help there), working out which lines are taking the longest to run, and trying to optimize those lines. For example, for the scrolling background, you may find that using the 'source' parameter for 'blit' to reduce the source rectangle size may yield a performance advantage. If that's still too slow, you might be able to (given sufficient RAM) pre-compute all 160 possible background states, and use them in sequence. However, before you start optimizing that section, make sure that it's definately that bit which is taking up most of the time, otherwise your time would be better spent optimizing the part that is. |
|
thank you very much man. I can't say enough thanks to you because i'm not very good with english language
however i will post there if i need more help!! when my game will be ready i will send you a copy and i would like you to tell me your opinion..if someone is interested this is the latest video i've recorded...unluckely the framerate is slow cause i've registered it on the pc screen and framerate is very low there. http://theray.altervista.org/wordpre...a-una-grafica/ |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|
| Rate This Thread | |
| Thread | Thread Starter | Forum | Replies | Last Post |
|---|---|---|---|---|
| canvas.blit() problem - scaling does not go together with picture masks | Zamoth_bg | Python | 2 | 2008-06-29 21:40 |
| canvas.blit() problem of scaling | Zamoth_bg | Python | 3 | 2008-06-29 13:19 |
| Problem with eglSwapBuffers and heap corruption | greatape | Symbian Media (Graphics & Sounds) | 2 | 2007-05-24 04:35 |
| netcards - Problem with opening adapter | kernj | Symbian Tools & SDKs | 5 | 2007-01-10 09:56 |
| Problem: S60 SDK for CW in PC with .NET | anttij | Carbide.c++ IDE and plug-ins | 1 | 2005-02-28 12:36 |