Advertisement
phjoe

Datagram

Jul 26th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import appuifw as A
  2. import graphics as G
  3. import random
  4. rand=random.randint
  5.  
  6. class Datagram:
  7.  def __init__(self):
  8.   self.b,self.maxY=5,150
  9.   self.x0=W               # offset x
  10.   self.y0=150+self.b    # offset y
  11.   self.t=2
  12.   self.step=1
  13.   self.max=0
  14.   self.data=[
  15.    [self.x0-self.b,self.y0,self.x0-self.b,self.y0]
  16.     ]  # data awal
  17.   self.data2=[
  18.    [self.x0-self.b,self.y0,self.x0-self.b,self.y0]
  19.     ]  # data awal
  20.  
  21.  def update(self):
  22.   self.t-=1
  23.   if self.t<0:
  24.    newY=rand(self.y0-(self.maxY+1),self.y0)
  25.    newY2=rand(self.y0-10,self.y0)
  26.    if (self.y0-self.data[-1][3])>=self.maxY:
  27.     self.max+=1
  28.    self.data.append([self.data[-1][2],self.data[-1][3],self.x0-self.b,newY])
  29.    self.data2.append([self.data2[-1][2],self.data2[-1][3],self.x0-self.b,newY2])
  30.    self.t=2
  31.   if self.data[0][0] < self.b*4:self.data.pop(0)
  32.   if self.data2[0][0] < self.b*4:self.data2.pop(0)
  33.  
  34.  def draw(self,g):
  35.   g.rectangle((self.b*4-1,4,self.x0-self.b+1,5+self.maxY+1),0x222222,0x555555)
  36.   for i in range(0,self.maxY+10,10):
  37.    g.line((8,5+i,14,5+i),0x696969)
  38.   for y in range(len(self.data)):
  39.    self.data[y][0]-=self.step
  40.    self.data[y][2]-=self.step
  41.    g.line((self.data[y][0],self.data[y][1],self.data[y][2],self.data[y][3]),0x00ff00)
  42.   for y in range(len(self.data2)):
  43.    self.data2[y][0]-=self.step
  44.    self.data2[y][2]-=self.step
  45.    g.line((self.data2[y][0],self.data2[y][1],self.data2[y][2],self.data2[y][3]),0xff0000)
  46.  
  47.  def get_last(self):
  48.   return self.y0-self.data[-1][3]
  49.  
  50.  def get_max(self):
  51.   return self.max
  52.  
  53.  def get_avg(self):
  54.   sample=len(self.data)
  55.   n=0
  56.   for i in range(sample):
  57.     n+=self.y0-self.data[i][3]
  58.   return float(n/sample)
  59.  
  60.  
  61.  
  62.  
  63. class App:
  64.  def __init__(self):
  65.   self.run=0
  66.   A.app.screen='full'
  67.   self.c = A.Canvas(redraw_callback=self.redraw,event_callback=None)
  68.   A.app.body=self.c
  69.   A.app.exit_key_handler = self.stop
  70.  
  71.  def stop(self):
  72.   self.run=0
  73.  
  74.  def redraw(self,x):
  75.   if BG:
  76.    self.c.blit(BG)
  77.  
  78.  def play(self):
  79.   self.run=1
  80.   while self.run:
  81.    av=dat.get_avg()
  82.    vmax=dat.get_max()
  83.    last=dat.get_last()
  84.    BG.clear(0x333333)
  85.    BG.rectangle((5,H-22,W-5,H-4),0x222222,0x555555)
  86.  
  87.    # last
  88.    BG.text((10,H-8),u'Current: %d' %last,0xdadada)
  89.    # total berapa kali mencapai nilai maksimum
  90.    BG.text((70,H-8),u'Max: %d' %vmax,0xdadada)
  91.    # nilai rata2
  92.    BG.text((120,H-8),u'Avg: %0.1f' %av,0xdadada)
  93.    dat.update()
  94.    dat.draw(BG)
  95.    self.redraw(0)
  96.    A.e32.ao_sleep(0.01)
  97.  
  98.  
  99. W,H=G.sysinfo.display_pixels()
  100. BG=G.Image.new((W,H))
  101. dat=Datagram()
  102. App().play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement