from math import sqrt from kandinsky import * from ion import * from time import sleep,monotonic from random import * dino_1=[ [0,0,0,0,0,0,0,1,1,1,1,0], [0,0,0,0,0,0,1,1,0,1,1,1], [0,0,0,0,0,0,1,1,1,1,1,1], [0,0,0,0,0,0,1,1,1,0,0,0], [0,0,0,0,0,0,1,1,1,1,1,0], [1,0,0,0,0,1,1,1,0,0,0,0], [1,0,0,0,1,1,1,1,1,1,0,0], [1,1,0,1,1,1,1,1,0,1,0,0], [1,1,1,1,1,1,1,1,0,0,0,0], [0,1,1,1,1,1,1,1,0,0,0,0], [0,0,1,0,0,1,1,0,0,0,0,0], [0,0,1,1,0,0,1,0,0,0,0,0], [0,0,0,0,0,0,1,1,0,0,0,0], ] dino_2=[ [0,0,0,0,0,0,0,1,1,1,1,0], [0,0,0,0,0,0,1,1,0,1,1,1], [0,0,0,0,0,0,1,1,1,1,1,1], [0,0,0,0,0,0,1,1,1,0,0,0], [0,0,0,0,0,0,1,1,1,1,1,0], [1,0,0,0,0,1,1,1,0,0,0,0], [1,0,0,0,1,1,1,1,1,1,0,0], [1,1,0,1,1,1,1,1,0,1,0,0], [1,1,1,1,1,1,1,1,0,0,0,0], [0,1,1,1,1,1,1,1,0,0,0,0], [0,0,1,1,0,0,1,0,0,0,0,0], [0,0,1,0,0,0,1,1,0,0,0,0], [0,0,1,1,0,0,0,0,0,0,0,0], ] cac_1=[ [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 1, 1], [0, 0, 0, 1, 1, 1, 0, 1, 1], [1, 1, 0, 1, 1, 1, 0, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 0, 0] ] cac_2=[ [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 1, 1], [0, 0, 0, 1, 1, 1, 0, 1, 1], [1, 1, 0, 1, 1, 1, 0, 1, 1], [1, 1, 0, 1, 1, 1, 0, 1, 1], [1, 1, 0, 1, 1, 1, 0, 1, 1], [1, 1, 0, 1, 1, 1, 0, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1, 0], [1, 1, 0, 1, 1, 1, 0, 0, 0], [1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 0, 0], [0, 1, 0, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0] ] blk=color(0,0,0) wt=color(255,255,255) red=color(255,0,0) gre=color(92,117,94) sky=color(212,253,255) sand=color(237,201,175) obj_speed=2 def draw(sprite,x_pos,y_pos,color,scale,pre_x,pre_y): y_pos-=len(sprite)*scale preo_y=pre_y-len(sprite)*scale o_x=(x_pos-pre_x) o_y=(y_pos-pre_y) for lineNum,line in enumerate(sprite): for index,i in enumerate(line): p_x=pre_x+index*scale p_y=preo_y+lineNum*scale if 0 <= lineNum + o_y < len(sprite): if 0 <= index + o_x < len(sprite[lineNum + o_y]): pre_co = sprite[lineNum + o_y][index + o_x] else: pre_co = wt else: pre_co = wt x=x_pos+index*scale y=y_pos+lineNum*scale p_px=get_pixel(p_x,p_y) if p_px==color and pre_co==wt: fill_rect(p_x,p_y,scale,scale,wt) px=get_pixel(x,y) if i==1 and px!=color: fill_rect(x,y,scale,scale,color) if i==0 and px!=wt: fill_rect(x,y,scale,scale,wt) class Plr: def __init__(self): self.pos=120 self.vel=0 self.can_jump=True self.sco=0 self.v=0 def draw_plr(self,x_pos,y_pos,color,pre_x): self.v=(self.v+1)%(17-obj_speed) if self.v<(17-obj_speed)/2: sprite=dino_1 else: sprite=dino_1 draw(sprite,x_pos,y_pos,color,2,60,pre_x) def upd(self): if keydown(KEY_UP) and self.can_jump: self.can_jump=False self.vel=7.5*sqrt(dt) new_pos=self.pos-int(self.vel) # fill_rect(60,self.pos-26,24,26,wt) if new_pos<=120: self.draw_plr(60,new_pos,blk,self.pos) self.pos=new_pos else: self.draw_plr(60,120,blk,self.pos) self.pos=120 self.vel=0 self.can_jump=True if get_pixel(84,self.pos-1)==blk or get_pixel(60,self.pos-1)==blk: return False self.vel-=(.5*dt) obs=[] class Obj: def __init__(self): obs.append(self) self.size=randint(1,2) self.pos=360 self.pre_p=360 def upd(self): self.pos=self.pos-obj_speed*dt # fill_rect(self.pre_p,120-32,14,32,wt) if self.pos<=-10: obs.remove(self) fill_rect(0,84,36,36,wt) return 0 if self.size==1: sprite=cac_1 elif self.size==2: sprite=cac_2 draw(sprite,int(self.pos),120,blk,2,int(self.pre_p),120) self.pre_p=self.pos plr = Plr() originTime=int(monotonic()-1) pre_t=monotonic() jsp=monotonic() i=0 while True: fill_rect(0,120,360,1,blk) fps= float(i)/(float(monotonic())-originTime) dt=60/fps if fps!=0 else 0 if monotonic()-jsp>3*dt: jsp=monotonic() if obj_speed<15: obj_speed+=1 if monotonic()-pre_t>(3+obj_speed)/(2*obj_speed)*(random()/2+.75)*dt: pre_t=monotonic() Obj() for o in obs: if o.upd()==0: plr.sco+=1 draw_string(str(plr.sco),10,10) if plr.upd()==False: break i+=1