un petit platformer, il ne comporte que deux niveaux, parce que flemme de le finir
from kandinsky import * from ion import * from time import * bg_color=(255,255,255) class Map: def __init__(self,rect): self.x=0 self.y=0 self.current_map=0 self.maps=[] for i in range(len(rect)): self.maps.append(unpack(rect[i])) self.rects=self.maps[self.current_map][0:-2] self.door=self.maps[self.current_map][-2] self.spawn=self.maps[self.current_map][-1] self.color=(250,90,100) self.detail_size=2 def draw(self): for rect in self.rects: fill_rect(rect[0]+self.x+self.detail_size,rect[1]+self.y,rect[2]-2*self.detail_size,rect[3],self.color) fill_rect(rect[0]+self.x,rect[1]+self.y+self.detail_size,rect[2],rect[3]-2*self.detail_size,self.color) fill_rect(self.door[0]+self.x,self.door[1]+self.y,10,15,(0,0,0)) def next_map(self,player): self.current_map+=1 if len(self.maps)!=self.current_map: self.rects=self.maps[self.current_map][0:-2] self.door=self.maps[self.current_map][-2] self.spawn=self.maps[self.current_map][-1] self.draw() player.pos=self.spawn class Player: def __init__(self): self.pos=[60,100] self.rect=[7,11] self.is_jumping=False self.is_falling=False self.jump_value=1 self.speed=4 self.color=(255,0,0) def draw(self): fill_rect(self.pos[0]+mapp.x,int(self.pos[1]+mapp.y),self.rect[0],self.rect[1],self.color) def hide(self): fill_rect(self.pos[0]+mapp.x,int(self.pos[1]+mapp.y),self.rect[0],self.rect[1],bg_color) def raytrace(self,length,x,y,debug=False): g=int(length/2) if g==0: g+=1 for k in range(g): i=k*2 if debug==True: set_pixel(self.pos[0]+mapp.x+i*x+int((1/2)*x**2+(1/2)*x)*self.rect[0],self.pos[1]+mapp.y+i*y+int((1/2)*y**2+(1/2)*y)*self.rect[1],(0,0,0)) set_pixel(self.pos[0]+mapp.x+i*x+int((1/2)*x**2+(1/2)*x)*self.rect[0]+y**2*(self.rect[0]-1),self.pos[1]+mapp.y+i*y+int((1/2)*y**2+(1/2)*y)*self.rect[1]+x**2*(self.rect[1]-1),(0,0,0)) for j in range(len(mapp.rects)): if (mapp.rects[j][0]<self.pos[0]+mapp.x+i*x+int((1/2)*x**2+(1/2)*x)*self.rect[0]<mapp.rects[j][0]+mapp.rects[j][2] and mapp.rects[j][1]<self.pos[1]+mapp.y+i*y+int((1/2)*y**2+(1/2)*y)*self.rect[1]<mapp.rects[j][1]+mapp.rects[j][3]) or (mapp.rects[j][0]<=self.pos[0]+mapp.x+i*x+int((1/2)*x**2+(1/2)*x)*self.rect[0]+y**2*(self.rect[0]-1)<=mapp.rects[j][0]+mapp.rects[j][2] and mapp.rects[j][1]<=self.pos[1]+mapp.y+i*y+int((1/2)*y**2+(1/2)*y)*self.rect[1]+x**2*(self.rect[1]-1)<=mapp.rects[j][1]+mapp.rects[j][3]): return (True,i) return (False,length) def jump(self): if self.is_jumping==False and self.is_falling==False: self.is_jumping=True self.jump_value=8 def collide(self): for i in range(len(mapp.rects)): if not (self.pos[0]>=mapp.rects[i][0]+mapp.rects[i][2] or self.pos[0]+self.rect[0]<=mapp.rects[i][0] or self.pos[1]>=mapp.rects[i][1]+mapp.rects[i][3] or self.pos[1]+self.rect[1]<=mapp.rects[i][1]): return True return False def update(self): a=keydown(3)-keydown(0) if a!=0: b=self.raytrace(self.speed,a,0) if b[1]!=0: self.hide() self.pos[0]+=a*b[1] while self.collide(): self.pos[0]-=a self.draw() if keydown(4): self.jump() if self.is_jumping: b=self.raytrace(self.jump_value,0,-1) if b[1]!=0: self.hide() self.pos[1]-=b[1] while self.collide(): self.pos[1]=int(self.pos[1]) self.pos[1]+=1 self.draw() self.jump_value=self.jump_value/1.2 if b[0]: self.jump_value=1 if self.jump_value<=1: self.is_jumping=False self.jump_value=1 if self.is_falling==False and self.is_jumping==False and self.raytrace(1,0,1,mapp)[0]==False: self.is_falling=True self.jump_value=1 if self.is_falling: b=self.raytrace(self.jump_value,0,1) if b[1]!=0: self.hide() self.pos[1]+=b[1] while self.collide(): self.pos[1]=int(self.pos[1]) self.pos[1]-=1 self.draw() self.jump_value=self.jump_value*1.2 if b[0]: self.is_falling=False self.jump_value=1 player=Player() def release_key(key): while keydown(key):pass def playLevel(): delay=monotonic() player.update() if keydown(17): # pause draw_string("PAUSE",135,101) release_key(17) while not keydown(17):pass release_key(17) mapp.draw() player.draw() fill_rect(135,101,50,20,bg_color) if 0.03-monotonic()+delay>0: sleep(0.03-monotonic()+delay) def nextLevel(): if not (player.pos[0]>mapp.door[0]+10 or player.pos[0]+player.rect[0]<mapp.door[0] or player.pos[1]>mapp.door[1]+15 or player.pos[1]+player.rect[1]<mapp.door[1]): for i in range(32): fill_rect(i*10,0,10,222,mapp.color) sleep(0.03) mapp.next_map(player) if len(mapp.maps)!=mapp.current_map: for i in range(32): fill_rect(i*10,0,10,222,bg_color) mapp.draw() if i*10<=mapp.door[0]: fill_rect(mapp.door[0],mapp.door[1],10,15,mapp.color) sleep(0.02) player.draw() def unpack(x): interval=0 new_map=[] for i in range(int(len(x)/4)): t=((ord(x[i*4+interval])-97)*14,(ord(x[i*4+1+interval])-97)*10,(ord(x[i*4+2+interval])-97)*7,(ord(x[i*4+3+interval])-97)*5) new_map.insert(0,t) if x[i*4+4+interval]=="1": u=[(ord(x[i*4+interval])-97)*14+int(((ord(x[i*4+2+interval])-97)*7)/2)-5,(ord(x[i*4+1+interval])-97)*10-15] new_map.insert(-1-interval,u) interval+=1 elif x[i*4+4+interval]=="2": u=[(ord(x[i*4+interval])-97)*14+int(((ord(x[i*4+2+interval])-97)*7)/2)-5,(ord(x[i*4+1+interval])-97)*10-15] interval+=1 new_map.insert(-1,u) new_map.insert(0,new_map[-1]) new_map.pop(-1) return new_map maps=["aqkc2fq{csqkc1","aqhc2iqecooecumec1"] mapp=Map(maps) mapp.draw() player.pos=mapp.spawn while len(mapp.maps)!=mapp.current_map: playLevel() nextLevel() draw_string("GG",150,101,(255,)*3,mapp.color) draw_string("you completed the game",50,131,(255,)*3,mapp.color)