Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # powerbar
- # joe, 2/01/2014
- import appuifw as A
- import graphics as G
- import math
- class Keyboard(object):
- def __init__(self,onevent=lambda:None):
- self._keyboard_state={}
- self._downs={}
- self._onevent=onevent
- def handle_event(self,event):
- if event['type']==3:
- code=event['scancode']
- if not self.is_down(code):
- self._downs[code]=self._downs.get(code,0)+1
- self._keyboard_state[code]=1
- elif event['type']==2:
- self._keyboard_state[event['scancode']]=0
- self._onevent()
- def is_down(self,scancode):
- return self._keyboard_state.get(scancode,0)
- def pressed(self,scancode):
- if self._downs.get(scancode,0):
- self._downs[scancode]-=1
- return True
- return False
- def release(self,scancode):
- if self._downs.get(scancode,0):
- self._downs[scancode]-=2
- return True
- return False
- class Ball:
- def __init__(self,px,py):
- self.px=px
- self.py=py
- self.state=0
- self.angle=80
- self.v0=8.3
- self.min_b=2
- self.min_speed=0.1
- self.friction=0.99
- self.radius=4
- self.t,self.dt=0.0,0.002
- self.gravity=1.0
- self.vx=self.v0 * math.cos(self.angle*math.pi/180);
- self.vy=-self.v0 * math.sin(self.angle*math.pi/180)
- def move(self):
- if self.state:
- self.vy+=self.gravity*self.t
- self.px+=self.vx
- self.py+=self.vy
- if self.px > W-self.radius:
- self.vx=-self.vx
- self.px=W-self.radius
- elif self.px < 0 and self.py < bar.max:
- self.vx=-self.vx
- self.px=0
- elif self.px < (2+(bar.w/2)) and self.py > bar.max:
- self.vx=-self.vx
- self.px=bar.w+6
- elif self.py > H-self.radius:
- self.vy=-self.vy
- self.py=H-self.radius
- if self.vy >-self.min_b:
- self.vx*=self.friction
- elif self.py < 0:
- self.vy=-self.vy
- self.py=0
- self.t+=self.dt
- def draw(self,g):
- g.ellipse((self.px-self.radius, self.py-self.radius,self.px+self.radius, self.py+self.radius),fill=0xffffff)
- class Bar:
- def __init__(self):
- self.w,self.max=4,100
- self.v=0
- def update(self):
- self.v+=1
- if self.v>self.max-1:self.v=self.max
- def full(self):
- self.v=0
- def draw(self,g):
- px,py=2,H
- g.rectangle((px-1,py-self.max-1,px+self.w+1,py+1),0xffffff)
- g.rectangle((px,py-(self.max-self.v),px+self.w,py),fill=0x00ee00)
- class App:
- def __init__(self):
- self.run=0
- A.app.screen='full'
- self.c = A.Canvas(redraw_callback=self.redraw,event_callback=kb.handle_event)
- A.app.body=self.c
- A.app.menu=[(u'Replay',self.replay)]
- A.app.exit_key_handler = self.stop
- def stop(self):
- self.run=0
- def redraw(self,x):
- if bg:
- self.c.blit(bg)
- def replay(self):
- ball.__init__(BX,BY)
- kb._downs={}
- def play(self):
- self.run=1
- while self.run:
- bg.clear(0)
- ball.move()
- ball.draw(bg)
- bar.draw(bg)
- self.redraw(0)
- A.e32.ao_sleep(1e-04)
- if not ball.state:
- if kb.is_down(KEY):
- bar.update()
- ball.vy-=0.1
- elif kb.release(KEY):
- bar.full()
- ball.state=1
- W,H=G.sysinfo.display_pixels()
- bg=G.Image.new((W,H))
- KEY=0x38
- kb=Keyboard()
- bar=Bar()
- BX,BY=2+(bar.w/2),H-bar.max-bar.w
- ball=Ball(BX,BY)
- ap=App()
- ap.play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement