#!/usr/bin/env python # -*- coding: utf-8 -*- import pygame from pygame.locals import * import random import time import pdb class particle: def __init__(self,x,y,z): self.x=x self.y=y self.z=z self.life=random.randint(50,100) class sprite: def __init__(self,type): self.type=type side=random.randint(0,1) if side==0: side=-1 self.x=side * 1620.0 self.y=240 messages=["Senyores i senyors...",u"L'equip LliureX esta orgullós de presentar...",u"LliureX 11.09, també conegut com a Marty","basat en Ubuntu Lucid LTS","L'equip:",u"Héctor G.H. (Cless)","Enrique M.G. (Lt_Henry)","Raul R.S. (kbut)","Alfons L.S. (turix)",u"Àngel B.V. (Xangel)","Jose Carlos G.M. (llxpad)","Pilar E.G.","Nacho V.H.","David M.F. (Volat)","Soledad G.G.","Raquel O.M.","Vicent B.D.","Miguel G.G.","Jaime M.F.","Francisco A.D.","Fernando T.A.","Jose R.C.", "Salvador A.B.","I no ens oblidem de:","Luis G.G.","Javi G.E.","Javi E.D.","Jose Alfredo M.A.","David P.L.","Elvira M.T.",u"Amb la col·laboració especial de:","Xavier G.E.",u"Gràcies a tot el món que fa servir LliureX a les aules",u"Ha sigut un camí llarg...",u"però on anem no fa falta carretera"] road=[] step=0 mode=0 hmode=0 road_x=0 road_y=0 road_curve=0 road_hop=0 ob=0 for n in range(1000): step = step + 1 if step>20: step=0 mode=random.randint(0,2) road_curve=random.randint(10,40) road_hop=random.randint(10,40) if mode==0: hmode=random.randint(0,2) ob=sprite(random.randint(0,5)) if mode==0: if hmode==1: road_y=road_y+road_hop if hmode==2: road_y=road_y-road_hop if mode==1: road_x=road_x+road_curve if mode==2: road_x=road_x-road_curve road.append( (road_x,road_y,ob) ) def main(): pygame.init() clock = pygame.time.Clock() screen = pygame.display.set_mode([640, 480],pygame.DOUBLEBUF) font = pygame.font.Font("/usr/share/llx-credits/marty/data/slicer.ttf", 22) font_bttf = pygame.font.Font("/usr/share/llx-credits/marty/data/BTTF.ttf", 40) sky_background=pygame.image.load("/usr/share/llx-credits/marty/data/sky.png") img_seto=pygame.image.load("/usr/share/llx-credits/marty/data/seto.png") img_rocks=pygame.image.load("/usr/share/llx-credits/marty/data/rocks.png") img_delorean=pygame.image.load("/usr/share/llx-credits/marty/data/delorean.png") img_banner=pygame.image.load("/usr/share/llx-credits/marty/data/banner.png") music = pygame.mixer.Sound("/usr/share/llx-credits/marty/data/bttf.ogg") done = False frame = 0 fps=0 ticks=0 depth = 18 pos = 0 step_color=0 road_pos=0 msg_pos=0 msg_accel=0 msg_status=0 msg_time=0 msg_current=-1 srf_msg = None scroll_x=0 while not done: #events caching for event in pygame.event.get(): if event.type == pygame.QUIT: done=True keys=pygame.key.get_pressed() if keys[pygame.K_ESCAPE]: done=True pos = pos + 0.05 #pygame.draw.rect(screen,[60,200,30],[0,240,640,240]) #pygame.draw.rect(screen,[40,40,200],[0,0,640,480]) #scrolling background if(scroll_x>0): if(scroll_x<640): screen.blit(sky_background,(0,0),(scroll_x,0,640,480)) else: screen.blit(sky_background,(0,0),(scroll_x,0,1280-scroll_x,480)) screen.blit(sky_background,(1280-scroll_x,0),(0,0,1280-scroll_x-640,480)) else: if(scroll_x>-640): screen.blit(sky_background,(scroll_x,0),(0,0,640-scroll_x,480)) #fps computation frame=frame + 1 if(pygame.time.get_ticks()-ticks>1000): fps = frame msg_time=msg_time+1 if msg_time>3: msg_time=0 msg_pos=0 msg_accel=0 msg_status=1 msg_current=msg_current+1 frame = 0 ticks=pygame.time.get_ticks() pygame.display.set_caption("Fps:%d"%(fps)) if not pygame.mixer.get_busy() and ticks>1500: music.play() if msg_current==len(messages): print "* Hasta la vista, baby" done=True #scenario color grass_color=[(131,185,26),(121,175,16)] tarmac_color=[(215,215,215),(185,185,185)] left_piano_color=[(245,245,245),(195,35,45)] right_piano_color=[(195,35,45),(245,245,245)] flame_color=[(32,32,32),(254,200,89)] #movement dynamics if pos > 0.99: road_pos=road_pos+1 pos=0.0 mrx,mry,tmp = road[road_pos] nrx,nry,tmp = road[road_pos+1] drx=mrx-nrx dry=mry-nry scroll_x = -(mrx - (drx*pos))/10 #track drawing for p in range(depth,-1,-1): z=(p-pos) step_color=(road_pos+p)%2 if z<=0.0: continue rx,ry,ob = road[road_pos+p] cx = 320 + rx - mrx + drx*pos cy = 240 - ry + mry - dry*pos ax=cx-(320/(z)) bx=cx+(320/(z)) ay=cy+(240/(z)) z=((p-1)-pos) if z<=0.0: continue rx,ry,tmp = road[road_pos+p-1] cx = 320 + rx - mrx cy = 240 - ry + mry - dry*pos pax=cx-(320/(z)) pbx=cx+(320/(z)) pay=cy+(240/(z)) dfy=float(pay-ay) dfx=(pax+drx*pos)-ax dfx2=(pbx+drx*pos)-bx try: ddfx=float(dfx/dfy) ddfx2=float(dfx2/dfy) except ZeroDivisionError: ddfx=0.0 ddfx2=0.0 i=0.0 ii=0.0 for j in range(int(dfy)+1): i=i+ddfx ii=ii+ddfx2 if ay+j>480: break dist_line=((bx+ii)-(ax+i))*0.10 pygame.draw.line(screen,grass_color[step_color],(0,ay+j),(ax+i,ay+j)) pygame.draw.line(screen,left_piano_color[step_color],(ax+i,ay+j),(ax+i+dist_line,ay+j)) pygame.draw.line(screen,tarmac_color[step_color],(ax+i+dist_line,ay+j),(bx+ii-dist_line,ay+j)) pygame.draw.line(screen,right_piano_color[step_color],(bx+ii-dist_line,ay+j),(bx+ii,ay+j)) pygame.draw.line(screen,grass_color[step_color],(bx+ii,ay+j),(640,ay+j)) #END track drawing #object drawing for p in range(depth,-1,-1): z=((p-1)-pos) if z<=0.0: continue rx,ry,ob = road[road_pos+p-1] cx = 320 + rx - mrx + drx*pos cy = 240 - ry + mry - dry*pos if mry>ry: continue #sprite drawing #bush if ob.type==0: cx = 320.0 + rx - mrx + drx*pos cy = 240.0 - ry + mry - dry*pos z=z+0.5 size=((depth-z)/depth) ax=cx-(ob.x/(z)) ay=cy+(ob.y/(z)) size=size*64 tmp_srf=pygame.transform.scale(img_seto,(int(size),int(size))) screen.blit(tmp_srf,(ax+(size/2),ay-(size/2))) if ob.type==1: cx = 320.0 + rx - mrx + drx*pos cy = 240.0 - ry + mry - dry*pos z=z+0.5 size=((depth-z)/depth) ax=cx-(ob.x/(z)) ay=cy+(ob.y/(z)) size=size*128 tmp_srf=pygame.transform.scale(img_rocks,(int(size),int(size))) screen.blit(tmp_srf,(ax-(size/2),ay-(size/2))) if ob.type==2: cx = 320.0 + rx - mrx + drx*pos cy = 240.0- ry + mry - dry*pos z=z+0.5 size=((depth-z)/depth) ax=cx-(ob.x/(z)) ay=cy+(ob.y/(z)) size=size*128 tmp_srf=pygame.transform.scale(img_banner,(int(size),int(size))) screen.blit(tmp_srf,(ax-(size/2),ay-(size/2))) #END object drawing if msg_status==1: if msg_pos<640: msg_accel=msg_accel+1 if msg_accel>24: msg_accel=24 msg_pos=msg_pos+msg_accel else: msg_status=2 if not srf_msg==None: srf_msg = font.render(messages[msg_current-1], 1, (255, 255, 255)) screen.blit(srf_msg, (320-(srf_msg.get_width()/2), 15)) for j in range(10,50,2): pygame.draw.line(screen,(255,255,0),(msg_pos-256,j),(msg_pos,j)) pygame.draw.line(screen,(255,255,0),(640-msg_pos,j+1),(640-msg_pos+256,j+1)) if msg_status==2: srf_msg = font.render(messages[msg_current], 1, (255, 255, 255)) screen.blit(srf_msg, (320-(srf_msg.get_width()/2), 15)) screen.blit(img_delorean,(320-(img_delorean.get_width()/2),480-img_delorean.get_height()) ) #srf_msg = font.render("Pos:"+str(road_pos)+" X"+str(mrx)+",Y:"+str(mry)+", scroll_x:"+str(scroll_x)+", dry:"+str(dry), 1, (255, 255, 255)) #screen.blit(srf_msg, (15, 5)) pygame.display.flip() clock.tick(60) #pygame.time.delay(5) #final credits done=False particles=[] for x in range(800): if random.random()>0.5: p=particle(random.randint(40,60),240.0,random.randint(1,100)) else: p=particle(-random.randint(40,60),240.0,random.randint(1,100)) particles.append(p) while not done: #events caching for event in pygame.event.get(): if event.type == pygame.QUIT: done=True keys=pygame.key.get_pressed() if keys[pygame.K_ESCAPE]: done=True if not pygame.mixer.get_busy(): done=True screen.blit(sky_background,(0,0)) #track drawing for p in range(depth,-1,-1): z=(p-pos) step_color=(road_pos+p)%2 if z<=0.0: continue rx,ry,ob = road[road_pos+p] cx = 320 + rx - mrx + drx*pos cy = 240 - ry + mry - dry*pos ax=cx-(320/(z)) bx=cx+(320/(z)) ay=cy+(240/(z)) z=((p-1)-pos) if z<=0.0: continue rx,ry,tmp = road[road_pos+p-1] cx = 320 + rx - mrx cy = 240 - ry + mry - dry*pos pax=cx-(320/(z)) pbx=cx+(320/(z)) pay=cy+(240/(z)) dfy=float(pay-ay) dfx=(pax+drx*pos)-ax dfx2=(pbx+drx*pos)-bx try: ddfx=float(dfx/dfy) ddfx2=float(dfx2/dfy) except ZeroDivisionError: ddfx=0.0 ddfx2=0.0 i=0.0 ii=0.0 for j in range(int(dfy)+1): i=i+ddfx ii=ii+ddfx2 if ay+j>480: break dist_line=((bx+ii)-(ax+i))*0.10 pygame.draw.line(screen,grass_color[step_color],(0,ay+j),(ax+i,ay+j)) pygame.draw.line(screen,left_piano_color[step_color],(ax+i,ay+j),(ax+i+dist_line,ay+j)) pygame.draw.line(screen,tarmac_color[step_color],(ax+i+dist_line,ay+j),(bx+ii-dist_line,ay+j)) pygame.draw.line(screen,right_piano_color[step_color],(bx+ii-dist_line,ay+j),(bx+ii,ay+j)) pygame.draw.line(screen,grass_color[step_color],(bx+ii,ay+j),(640,ay+j)) #END track drawing verts=[ (320+40,240+240) , (320+60,240+240) , (320+(60/12),240+(240/12)) , (320+(40/12),240+(240/12)) ] pygame.draw.polygon(screen,flame_color[0],verts,0) verts=[ (320-40,240+240) , (320-60,240+240) , (320-(60/12),240+(240/12)) , (320-(40/12),240+(240/12)) ] pygame.draw.polygon(screen,flame_color[0],verts,0) for p in particles: p.life=p.life-1 if p.life==0: p.life=random.randint(60,180) if(random.random()>0.5): p.x=random.randint(40,60) else: p.x=-random.randint(40,60) p.y=240 p.z= 1.0+(random.random()*12.0) #p.z=1.0 p.y=p.y-1 #screen.set_at(( (p.x/p.z)+320 ,(p.y/p.z)+240 ),(200,p.life%256,40)) pygame.draw.rect(screen, (200,p.life%256,40), ((p.x/p.z)+320 ,(p.y/p.z)+240,2,2) ,0) srf_msg = font.render("see you in the future, Marty", 1, (255, 60, 30)) screen.blit(srf_msg, (320-(srf_msg.get_width()/2), 15)) frame=frame + 1 if(pygame.time.get_ticks()-ticks>1000): fps = frame frame = 0 ticks=pygame.time.get_ticks() pygame.display.set_caption("Fps:%d"%(fps)) pygame.display.flip() clock.tick(60) if __name__=="__main__": main()