Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2013
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  26. local score=0
  27.  
  28. --screen vars
  29. local SCREEN_WIDTH = 384
  30. local SCREEN_HEIGHT = 216
  31.  
  32. --game loop
  33. keyDirectPoll()
  34. while exit~=1 do
  35.     if keyDirect(key.Exit)>0 then
  36.         exit=1
  37.     end
  38.     --clear screen
  39.     drawRectFill(0,0,SCREEN_WIDTH,SCREEN_HEIGHT,color.bg)
  40.     --display score
  41.     drawText(1,1,"SCORE: " .. score,color.fg2,color.bg)
  42.     --[[debug
  43.     drawText(1,20,"DEBUG: " .. #lines,color.fg2,color.bg)
  44.     --]]
  45.    
  46.     --keys
  47.     if keyDirect(key.Left)>0 and player.x>0 then player.x=player.x-player.speed
  48.         elseif keyDirect(key.Right)>0 and player.x<SCREEN_WIDTH then player.x=player.x+player.speed
  49.     end
  50.     if keyDirect(key.Up)>0 and player.y>0 then player.y=player.y-player.speed
  51.         elseif keyDirect(key.Down)>0 and player.y<SCREEN_HEIGHT then player.y=player.y+player.speed
  52.     end
  53.    
  54.     --calculations
  55.     --check collision with target
  56.     if player.x<target.x+target.size and player.x>target.x-target.size
  57.         and player.y-player.size<target.y+target.size and player.y+player.size>target.y-target.size then
  58.         target.x=random(0,SCREEN_WIDTH)
  59.         target.y=random(0,SCREEN_HEIGHT)
  60.         score=score+100
  61.         -- create lines
  62.         if player.y>SCREEN_HEIGHT/2 then
  63.             lines[#lines+1]={random(0,SCREEN_WIDTH/2),random(0,SCREEN_HEIGHT/2-player.size),1,0}
  64.             else
  65.             lines[#lines+1]={random(0,SCREEN_WIDTH/2),random(SCREEN_HEIGHT/2+player.size,SCREEN_HEIGHT),1,0}
  66.         end
  67.        
  68.         if player.x>SCREEN_WIDTH/2 then
  69.             lines[#lines+2]={random(0,SCREEN_WIDTH/2-player.size),random(0,SCREEN_HEIGHT/2),1,1}
  70.             else
  71.             lines[#lines+2]={random(SCREEN_WIDTH/2+player.size,SCREEN_WIDTH),random(0,SCREEN_HEIGHT/2),1,1}
  72.             fastCopy()
  73.         end
  74.     end
  75.    
  76.     --lines
  77.     for i=1,#lines,1 do
  78.         --if horizontal
  79.         if lines[i][4]==0 then
  80.             --reverse direction if hit edge
  81.             if lines[i][1]>SCREEN_WIDTH-linewidth or lines[i][1]<0 then
  82.                 lines[i][3]=lines[i][3]*-1
  83.             end
  84.             --check collisions
  85.             if lines[i][1]+linewidth>player.x-player.size and lines[i][1]<player.x+player.size
  86.                 and lines[i][2]>player.y-player.size and lines[i][2]<player.y+player.size then
  87.                 print("you lose")
  88.                 exit=1
  89.             end
  90.             --move it along
  91.             lines[i][1]=lines[i][1]+linespeed*lines[i][3]
  92.             --draw it
  93.             drawLine(lines[i][1],lines[i][2],lines[i][1]+linewidth,lines[i][2],color.line)
  94.         end
  95.        
  96.         --if vertical
  97.         if lines[i][4]==1 then
  98.             --reverse direction if hit edge
  99.             if lines[i][2]>SCREEN_HEIGHT-linewidth or lines[i][2]<0 then
  100.                 lines[i][3]=lines[i][3]*-1
  101.             end
  102.             --check collisions
  103.             if lines[i][2]+linewidth>player.y-player.size and lines[i][2]<player.y+player.size
  104.                 and lines[i][1]>player.x-player.size and lines[i][1]<player.x+player.size then
  105.                 print("you lose")
  106.                 exit=1
  107.             end
  108.             --move it along
  109.             lines[i][2]=lines[i][2]+linespeed*lines[i][3]
  110.             --draw it
  111.             drawLine(lines[i][1],lines[i][2],lines[i][1],lines[i][2]+linewidth,color.line)
  112.         end
  113.     end
  114.    
  115.     keyDirectPoll()
  116.    
  117.     --display
  118.     drawCircle(player.x,player.y,player.size,color.fg)
  119.     drawCircle(target.x,target.y,target.size,color.fg2)
  120.     fastCopy()
  121.    
  122.     keyDirectPoll()
  123. end
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement