Advertisement
Guest User

Untitled

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