Advertisement
Guest User

thisone

a guest
May 24th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 KB | None | 0 0
  1. x1,y1 = term.getSize() --Returns the size of your terminal
  2. local char = "^" --makes charachter face up
  3. local currentX = 25 --Saves the current X of the cursor
  4. local currentY = 15 --Saves the current Y of the cursor
  5. local running = true
  6. score = 0 --holds the score value
  7. hit = false --becomes true if bullet interacts with target
  8. plus1 = "+1"
  9. --this function draws the cursor to the screen
  10. function drawCursor(x,y)
  11.   x = currentX
  12.   y = currentY
  13.  
  14.   term.setCursorPos(currentX, currentY)  --Draws a cursor at the current X/Y
  15.   write(char)
  16.  end  
  17.  
  18. --this is the main gameloop
  19. function gameLoop()
  20.  
  21. --these variables generate a randome nomber to set as the coordinates
  22. --for the target boxes
  23. alienRandX = math.random(50)
  24. alienRandY = math.random(10)
  25.  
  26. --this function generates the bullet  
  27. function fireShell()
  28.   fire = true
  29.  
  30.   --makes a new variable so the value doesnt change
  31.   xshell = currentX
  32.   yshell = currentY
  33.  
  34.   while fire == true do
  35.     sleep(0.05)--regulates the speed
  36.     yshell = yshell - 1
  37.     --draws a box and fills in the one behind it
  38.     paintutils.drawFilledBox(xshell, yshell-1, xshell, yshell-1, 2)
  39.     paintutils.drawFilledBox(xshell, yshell, xshell, yshell, 32768)
  40.    
  41.    
  42.     check_x_1 = xshell <= alienRandX + 1
  43.     check_x_2 = xshell >= alienRandX
  44.     --check_x_1 and check_x_2 check if the bullet is in beetween ether ends of the target
  45.     check_y_1 = yshell <= alienRandY
  46.     --check_y_1 checks if bullet is above bottom of target  
  47.    
  48.    
  49.     -- if all variables are true then hit = true
  50.     if check_x_1 == true and check_x_2 == true and check_y_1 == true then
  51.       hit = true
  52.       score = score + 1 --adds one to score
  53.     end
  54.    
  55.     --if the bullet pases the top of the screen it will stop
  56.     if yshell < 0 then
  57.       fire = false
  58.      end
  59.  
  60.     --if bullet hits then fire stops
  61.     if hit == true then
  62.       fire = false
  63.     end
  64.  
  65.   end  
  66. end
  67.  
  68. while running == true do
  69.  
  70.  --this clears the screen
  71.   paintutils.drawFilledBox(0, 0, x1,y1, (32768))
  72.  
  73.    --this writes the score
  74.    write("Score:")
  75.    write(score)
  76.    --draws cursor
  77.    drawCursor(currentX, currentY)
  78.    --draws target
  79.    paintutils.drawFilledBox(alienRandX, alienRandY, alienRandX +1, alienRandY, (2))
  80.   --makes plus1 nothing so once hit if statement finishes it goes back to nothing
  81.     plus1 = " "
  82.   ----------------------------------
  83.   --returns value of event and key pressed
  84.   local event,key = os.pullEvent( "key" )
  85.  
  86.          --keys 'a' or left arrow
  87.    if key == 203 or key == 30 then --left
  88.                 sleep(0.01)
  89.                 currentX = currentX -2
  90.          --keys 'b' or right arrow      
  91.    elseif key == 205 or key == 32 then --right
  92.                 sleep(0.01)
  93.                 currentX = currentX +2
  94.          --key spacebar      
  95.    elseif key == 57 then
  96.    --fires bullet if spacebar is pressed
  97.      fireShell()
  98.  
  99.      
  100.     end
  101.      
  102.    --spawns a new target once hit == true
  103.      if hit == true then
  104.        alienRandX = math.random(50)
  105.        alienRandY = math.random(10)
  106.        --sets plus1 to +1!
  107.        plus1 = "+1!"
  108.        write(plus1)
  109.        --sleeps for .5 sec to let player see +1
  110.        sleep(0.5)
  111.        hit = false
  112.      end
  113.      
  114. end
  115. end
  116. --runs gameloop
  117. gameLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement