Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import appuifw as A
- import graphics as G
- import random
- rand=random.randint
- class Datagram:
- def __init__(self):
- self.b,self.maxY=5,150
- self.x0=W # offset x
- self.y0=150+self.b # offset y
- self.t=2
- self.step=1
- self.max=0
- self.data=[
- [self.x0-self.b,self.y0,self.x0-self.b,self.y0]
- ] # data awal
- self.data2=[
- [self.x0-self.b,self.y0,self.x0-self.b,self.y0]
- ] # data awal
- def update(self):
- self.t-=1
- if self.t<0:
- newY=rand(self.y0-(self.maxY+1),self.y0)
- newY2=rand(self.y0-10,self.y0)
- if (self.y0-self.data[-1][3])>=self.maxY:
- self.max+=1
- self.data.append([self.data[-1][2],self.data[-1][3],self.x0-self.b,newY])
- self.data2.append([self.data2[-1][2],self.data2[-1][3],self.x0-self.b,newY2])
- self.t=2
- if self.data[0][0] < self.b*4:self.data.pop(0)
- if self.data2[0][0] < self.b*4:self.data2.pop(0)
- def draw(self,g):
- g.rectangle((self.b*4-1,4,self.x0-self.b+1,5+self.maxY+1),0x222222,0x555555)
- for i in range(0,self.maxY+10,10):
- g.line((8,5+i,14,5+i),0x696969)
- for y in range(len(self.data)):
- self.data[y][0]-=self.step
- self.data[y][2]-=self.step
- g.line((self.data[y][0],self.data[y][1],self.data[y][2],self.data[y][3]),0x00ff00)
- for y in range(len(self.data2)):
- self.data2[y][0]-=self.step
- self.data2[y][2]-=self.step
- g.line((self.data2[y][0],self.data2[y][1],self.data2[y][2],self.data2[y][3]),0xff0000)
- def get_last(self):
- return self.y0-self.data[-1][3]
- def get_max(self):
- return self.max
- def get_avg(self):
- sample=len(self.data)
- n=0
- for i in range(sample):
- n+=self.y0-self.data[i][3]
- return float(n/sample)
- class App:
- def __init__(self):
- self.run=0
- A.app.screen='full'
- self.c = A.Canvas(redraw_callback=self.redraw,event_callback=None)
- A.app.body=self.c
- A.app.exit_key_handler = self.stop
- def stop(self):
- self.run=0
- def redraw(self,x):
- if BG:
- self.c.blit(BG)
- def play(self):
- self.run=1
- while self.run:
- av=dat.get_avg()
- vmax=dat.get_max()
- last=dat.get_last()
- BG.clear(0x333333)
- BG.rectangle((5,H-22,W-5,H-4),0x222222,0x555555)
- # last
- BG.text((10,H-8),u'Current: %d' %last,0xdadada)
- # total berapa kali mencapai nilai maksimum
- BG.text((70,H-8),u'Max: %d' %vmax,0xdadada)
- # nilai rata2
- BG.text((120,H-8),u'Avg: %0.1f' %av,0xdadada)
- dat.update()
- dat.draw(BG)
- self.redraw(0)
- A.e32.ao_sleep(0.01)
- W,H=G.sysinfo.display_pixels()
- BG=G.Image.new((W,H))
- dat=Datagram()
- App().play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement