[Tutor] a pyball python app

dean garrad thankyouforthevenom1971 at hotmail.co.uk
Thu Apr 3 19:04:10 CEST 2008


hi i signed onto the site it asked me to. :).


can i talk to ya now more ? 

im not sure if i am in wrong place to send e mail to you still


i still ahving problems with the ball script this is the script at the moment how it stands with 1 ball working fine and i have been trying to add another ball to the script.

i coppied the txt and added a location2 aswell as location. but it didnt seam to work :S 


import appuifw
from graphics import *
import e32

import sensor
class SensorConnection(object):
    delta = []
    def __init__(self):
        """Connect to the sensor."""
        sens = sensor.sensors()['AccSensor']
        self.s = sensor.Sensor(sens['id'], sens['category'])
        self.s.connect(self.callme)
    
    def callme(self, state):
        self.delta = []
        for key in ['data_1', 'data_2', 'data_3']:
            val = state[key]
            self.delta.append(int(val + 40)/80)

    def cleanup(self):
        """Cleanup after yourself. *Must be called* before exiting."""
        self.s.disconnect()

sense_conn = SensorConnection()

appuifw.app.screen='full'
img=None
def handle_redraw(rect):
    if img:
        canvas.blit(img)
appuifw.app.body=canvas=appuifw.Canvas(
    redraw_callback=handle_redraw)
img=Image.new(canvas.size)

running=1
def quit():
    global running
    running=0

appuifw.app.exit_key_handler=quit

location=[img.size[0]/2,img.size[1]/2]
speed=[0.,0.]
blobsize=16
xs,ys=img.size[0]-blobsize,img.size[1]-blobsize
acceleration=0.05
friction = 0.993

import time
start_time=time.clock()
n_frames=0

# To speed things up, we prerender the text.
labeltext=u'Tilt the phone to move'
textrect=img.measure_text(labeltext, font='normal')[0]
text_img=Image.new((textrect[2]-textrect[0],textrect[3]-textrect[1]))
text_img.clear(0)
text_img.text((-textrect[0],-textrect[1]),labeltext,fill=0xffffff,font='normal')

while running:
    img.clear(0)
    img.blit(text_img, (0,0))
    img.point((location[0]+blobsize/2,location[1]+blobsize/2),
              0x00ff00,width=blobsize)
    handle_redraw(())
    e32.ao_yield()
    e32.reset_inactivity()
    speed[0]*=friction
    speed[1]*=friction
    location[0]+=speed[0]
    location[1]+=speed[1]
    
    n_frames+=1
    
    if not sense_conn: continue

    if not len(sense_conn.delta): continue    
    
    x_bounce_factor = .8 * (1 - min(6,abs(sense_conn.delta[0])) / 9) 
    y_bounce_factor = .8 * (1 - min(6,abs(sense_conn.delta[1])) / 9) 

    if location[0]>xs:
        location[0]=xs-(location[0]-xs)
        speed[0]= -x_bounce_factor * speed[0]
        speed[1]=0.90*speed[1]
    if location[0]<0:
        location[0]=-location[0]
        speed[0]= -x_bounce_factor * speed[0]
        speed[1]=0.90*speed[1]
    if location[1]>ys:
        location[1]=ys-(location[1]-ys)
        speed[0]=0.90*speed[0]
        speed[1]= -y_bounce_factor * speed[1]
    if location[1]<0:
        location[1]=-location[1]
        speed[0]=0.90*speed[0]
        speed[1]= -y_bounce_factor * speed[1]
    
    speed[0] -= (sense_conn.delta[1]) * acceleration
    speed[1] -= (sense_conn.delta[0]) * acceleration

    speed[0] = max(min(xs / 2, speed[0]), -xs/2) 
    speed[1] = max(min(ys / 2, speed[1]), -ys/2) 
    
end_time=time.clock()
total=end_time-start_time

sense_conn.cleanup()

print "%d frames, %f seconds, %f FPS, %f ms/frame."%(n_frames,total,
                                                     n_frames/total,
                                                     total/n_frames*1000.)
                                                     



> Date: Thu, 3 Apr 2008 10:34:26 -0400
> From: kent37 at tds.net
> To: thankyouforthevenom1971 at hotmail.co.uk
> CC: tutor at python.org
> Subject: Re: [Tutor] a pyball python app
> 
> dean garrad wrote:
> > 
> > 
> > this is the part im lil confused about to add the extra ball
> > 
> > location=[img.size[0]/2,img.size[1]/2]
> > speed=[0.,0.]
> 
> > i tried copying but it did nothing do i need to copy it and change 
> > something so the python app dont read the same txt twice and jus do nothing?
> 
> If you want to just copy/paste, you will have to copy all the lines 
> using location and speed and change the copies to something like 
> location2 and speed2. Of course you should also change the starting 
> location or speed so the two balls don't draw in the same place!
> 
> > also on the txt bellow if i add another bal somehow i dotn have to copy 
> > or change the txt bellow do i? it should create same value for both balls?
> 
> You have to duplicate all the location and speed code, using the new names.
> 
> > also could you link advise any good tuturials that would help me ive 
> > read some on the net but need more help really.
> 
> The first two tutorials listed here are popular:
> http://wiki.python.org/moin/BeginnersGuide/NonProgrammers
> 
> Kent
> 
> PS Please subscribe to the list so I don't have to moderate all your 
> requests through.

_________________________________________________________________
The next generation of Windows Live is here
http://www.windowslive.co.uk/get-live
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080403/90acc349/attachment.htm 


More information about the Tutor mailing list