Advertisement
phjoe

Powerbar 2 test

Jan 2nd, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # powerbar
  2. # joe, 2/01/2014
  3.  
  4. import appuifw as A
  5. import graphics as G
  6. import math
  7.  
  8. class Keyboard(object):
  9.  def __init__(self,onevent=lambda:None):
  10.   self._keyboard_state={}
  11.   self._downs={}
  12.   self._onevent=onevent
  13.  
  14.  def handle_event(self,event):
  15.   if event['type']==3:
  16.    code=event['scancode']
  17.    if not self.is_down(code):
  18.     self._downs[code]=self._downs.get(code,0)+1
  19.    self._keyboard_state[code]=1
  20.   elif event['type']==2:
  21.    self._keyboard_state[event['scancode']]=0
  22.   self._onevent()
  23.  
  24.  def is_down(self,scancode):
  25.   return self._keyboard_state.get(scancode,0)
  26.  
  27.  def pressed(self,scancode):
  28.   if self._downs.get(scancode,0):
  29.    self._downs[scancode]-=1
  30.    return True
  31.   return False
  32.  
  33.  def release(self,scancode):
  34.   if self._downs.get(scancode,0):
  35.    self._downs[scancode]-=2
  36.    return True
  37.   return False
  38.  
  39.  
  40. class Ball:
  41.  def __init__(self,px,py):
  42.   self.px=px
  43.   self.py=py
  44.   self.state=0
  45.   self.angle=80
  46.   self.v0=8.3
  47.   self.min_b=2
  48.   self.min_speed=0.1
  49.   self.friction=0.99
  50.   self.radius=4
  51.   self.t,self.dt=0.0,0.002
  52.   self.gravity=1.0
  53.   self.vx=self.v0 * math.cos(self.angle*math.pi/180);
  54.   self.vy=-self.v0 * math.sin(self.angle*math.pi/180)
  55.  
  56.  def move(self):
  57.   if self.state:
  58.    self.vy+=self.gravity*self.t
  59.    self.px+=self.vx
  60.    self.py+=self.vy
  61.    if self.px > W-self.radius:
  62.     self.vx=-self.vx
  63.     self.px=W-self.radius
  64.    elif self.px < 0 and self.py < bar.max:
  65.     self.vx=-self.vx
  66.     self.px=0
  67.    elif self.px < (2+(bar.w/2)) and self.py > bar.max:
  68.     self.vx=-self.vx
  69.     self.px=bar.w+6
  70.    elif self.py > H-self.radius:
  71.     self.vy=-self.vy
  72.     self.py=H-self.radius
  73.     if self.vy >-self.min_b:
  74.      self.vx*=self.friction
  75.    elif self.py < 0:
  76.     self.vy=-self.vy
  77.     self.py=0
  78.    self.t+=self.dt
  79.  
  80.  def draw(self,g):
  81.   g.ellipse((self.px-self.radius, self.py-self.radius,self.px+self.radius, self.py+self.radius),fill=0xffffff)
  82.  
  83.  
  84. class Bar:
  85.  def __init__(self):
  86.   self.w,self.max=4,100
  87.   self.v=0
  88.  
  89.  def update(self):
  90.   self.v+=1
  91.   if self.v>self.max-1:self.v=self.max
  92.  
  93.  def full(self):
  94.   self.v=0
  95.  
  96.  def draw(self,g):
  97.   px,py=2,H
  98.   g.rectangle((px-1,py-self.max-1,px+self.w+1,py+1),0xffffff)
  99.   g.rectangle((px,py-(self.max-self.v),px+self.w,py),fill=0x00ee00)
  100.  
  101.  
  102. class App:
  103.  def __init__(self):
  104.   self.run=0
  105.   A.app.screen='full'
  106.   self.c = A.Canvas(redraw_callback=self.redraw,event_callback=kb.handle_event)
  107.   A.app.body=self.c
  108.   A.app.menu=[(u'Replay',self.replay)]
  109.   A.app.exit_key_handler = self.stop
  110.  
  111.  def stop(self):
  112.   self.run=0
  113.  
  114.  def redraw(self,x):
  115.   if bg:
  116.    self.c.blit(bg)
  117.  
  118.  def replay(self):
  119.   ball.__init__(BX,BY)
  120.   kb._downs={}
  121.  
  122.  def play(self):
  123.   self.run=1
  124.   while self.run:
  125.    bg.clear(0)
  126.    ball.move()
  127.    ball.draw(bg)
  128.    bar.draw(bg)
  129.    self.redraw(0)
  130.    A.e32.ao_sleep(1e-04)
  131.    if not ball.state:
  132.     if kb.is_down(KEY):
  133.      bar.update()
  134.      ball.vy-=0.1
  135.     elif kb.release(KEY):
  136.      bar.full()
  137.      ball.state=1
  138.  
  139.  
  140. W,H=G.sysinfo.display_pixels()
  141. bg=G.Image.new((W,H))
  142. KEY=0x38
  143. kb=Keyboard()
  144. bar=Bar()
  145.  
  146. BX,BY=2+(bar.w/2),H-bar.max-bar.w
  147. ball=Ball(BX,BY)
  148. ap=App()
  149. ap.play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement