Advertisement
flyingfisch

Untitled

Feb 23rd, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.59 KB | None | 0 0
  1. print("starting aspirin...")
  2. --function variables
  3. local drawRectFill = zmg.drawRectFill
  4. local drawCircle = zmg.drawCircle
  5. local fastCopy = zmg.fastCopy
  6. local makeColor = zmg.makeColor
  7. local drawPoint = zmg.drawPoint
  8. local drawLine = zmg.drawLine
  9. local clear = zmg.clear
  10. local drawText = zmg.drawText
  11. local keyDirectPoll = zmg.keyDirectPoll
  12. local keyDirect = zmg.keyDirect
  13. local floor = math.floor
  14. local random = math.random
  15.  
  16. --variables
  17. local key = {F1=79, F2=69, F3=59, F4=49, F5=39, F6=29, Alpha=77, Exit=47, Optn=68, Up=28, Down=37, Left=38, Right=27}
  18. local color = {bg=makeColor("black"),fg=makeColor("blue"),fg2=makeColor("red"),line=makeColor("lightblue")}
  19. --lines={{x1,y1,direction(-1,1),direction(0=horiz,1=vert)},{...}}
  20. local lines={}
  21. local player={x=384/2,y=216/2,size=5,speed=2}
  22. local target={x=384/2,y=216/4,size=10}
  23. local linewidth=20
  24. local linespeed=2
  25. local exit=0
  26. local exitall=0
  27.  
  28. local score=0
  29.  
  30. --screen vars
  31. local SCREEN_WIDTH = 384
  32. local SCREEN_HEIGHT = 216
  33.  
  34. --game loop
  35. keyDirectPoll()
  36. while exitall~=1 do
  37.     while exit~=1 do
  38.         if keyDirect(key.Exit)>0 then
  39.             exit=1
  40.             exitall=1
  41.         end
  42.         --clear screen
  43.         drawRectFill(0,0,SCREEN_WIDTH,SCREEN_HEIGHT,color.bg)
  44.         --display score
  45.         drawText(1,1,"SCORE: " .. score,color.fg2,color.bg)
  46.        
  47.         --keys
  48.         if keyDirect(key.Left)>0 and player.x>0 then player.x=player.x-player.speed
  49.             elseif keyDirect(key.Right)>0 and player.x<SCREEN_WIDTH then player.x=player.x+player.speed
  50.         end
  51.         if keyDirect(key.Up)>0 and player.y>0 then player.y=player.y-player.speed
  52.             elseif keyDirect(key.Down)>0 and player.y<SCREEN_HEIGHT then player.y=player.y+player.speed
  53.         end
  54.        
  55.         --calculations
  56.         --check collision with target
  57.         if player.x<target.x+target.size and player.x>target.x-target.size
  58.             and player.y-player.size<target.y+target.size and player.y+player.size>target.y-target.size then
  59.             target.x=random(0,SCREEN_WIDTH)
  60.             target.y=random(0,SCREEN_HEIGHT)
  61.             score=score+100
  62.             -- create lines
  63.             if player.y>SCREEN_HEIGHT/2 then
  64.                 lines[#lines+1]={random(0,SCREEN_WIDTH/2),random(0,SCREEN_HEIGHT/2-player.size),1,0}
  65.                 else
  66.                 lines[#lines+1]={random(0,SCREEN_WIDTH/2),random(SCREEN_HEIGHT/2+player.size,SCREEN_HEIGHT),1,0}
  67.             end
  68.            
  69.             if player.x>SCREEN_WIDTH/2 then
  70.                 lines[#lines+1]={random(0,SCREEN_WIDTH/2-player.size),random(0,SCREEN_HEIGHT/2),1,1}
  71.                 else
  72.                 lines[#lines+1]={random(SCREEN_WIDTH/2+player.size,SCREEN_WIDTH),random(0,SCREEN_HEIGHT/2),1,1}
  73.                 fastCopy()
  74.             end
  75.            
  76.             --workaround to improve framerate
  77.             if player.x<6 then
  78.                 linespeed = math.floor(#lines/10+2)
  79.                 player.speed = math.floor(#lines/10+2)
  80.             end
  81.         end
  82.        
  83.         --lines
  84.         for i=1,#lines,1 do
  85.             --if horizontal
  86.             if lines[i][4]==0 then
  87.                 --reverse direction if hit edge
  88.                 if lines[i][1]>SCREEN_WIDTH-linewidth or lines[i][1]<0 then
  89.                     lines[i][3]=lines[i][3]*-1
  90.                 end
  91.                 --check collisions
  92.                 if lines[i][1]+linewidth>player.x-player.size and lines[i][1]<player.x+player.size
  93.                     and lines[i][2]>player.y-player.size and lines[i][2]<player.y+player.size then
  94.                     exit=1
  95.                 end
  96.                 --move it along
  97.                 lines[i][1]=lines[i][1]+linespeed*lines[i][3]
  98.                 --draw it
  99.                 drawLine(lines[i][1],lines[i][2],lines[i][1]+linewidth,lines[i][2],color.line)
  100.             end
  101.            
  102.             --if vertical
  103.             if lines[i][4]==1 then
  104.                 --reverse direction if hit edge
  105.                 if lines[i][2]>SCREEN_HEIGHT-linewidth or lines[i][2]<0 then
  106.                     lines[i][3]=lines[i][3]*-1
  107.                 end
  108.                 --check collisions
  109.                 if lines[i][2]+linewidth>player.y-player.size and lines[i][2]<player.y+player.size
  110.                     and lines[i][1]>player.x-player.size and lines[i][1]<player.x+player.size then
  111.                     exit=1
  112.                 end
  113.                 --move it along
  114.                 lines[i][2]=lines[i][2]+linespeed*lines[i][3]
  115.                 --draw it
  116.                 drawLine(lines[i][1],lines[i][2],lines[i][1],lines[i][2]+linewidth,color.line)
  117.             end
  118.         end
  119.        
  120.         keyDirectPoll()
  121.        
  122.         --display
  123.         drawCircle(player.x,player.y,player.size,color.fg)
  124.         drawCircle(target.x,target.y,target.size,color.fg2)
  125.         fastCopy()
  126.        
  127.         keyDirectPoll()
  128.     end
  129.     --death screen
  130.     keyDirectPoll()
  131.     --clear screen
  132.     drawRectFill(0,0,SCREEN_WIDTH,SCREEN_HEIGHT,color.bg)
  133.    
  134.     --draw text
  135.     drawText(1,1,"YOU DIED!",color.fg2,color.bg)
  136.     drawText(1,20,"SCORE: " .. score,color.fg2,color.bg)
  137.     drawText(1,40,"F1: Play again",color.fg2,color.bg)
  138.     drawText(1,60,"F6: Exit",color.fg2,color.bg)
  139.    
  140.     fastCopy()
  141.    
  142.     if keyDirect(key.F1)>0 then
  143.         --reset stuff
  144.         exit=0
  145.         lines={}
  146.         player={x=384/2,y=216/2,size=5,speed=2}
  147.         target={x=384/2,y=216/4,size=10}
  148.         linewidth=20
  149.         linespeed=2
  150.         score=0
  151.        
  152.         elseif keyDirect(key.F6)>0 then exitall=1
  153.     end
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement