daily pastebin goal
30%
SHARE
TWEET

Untitled

flyingfisch Jun 9th, 2012 29 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local rect = graydraw.rect
  2. local setColor = graydraw.setcolor
  3. local wait = misc.wait
  4. local line = graydraw.line
  5. local graytext = graydraw.text
  6. local fill = graydraw.fill
  7. local pixel = graydraw.pixel
  8. local random = misc.random
  9.  
  10.  
  11. car = {height = 8, width = 8, sprite = "\000\000<((f^fz(<V<(jvNnr8<V\000"}
  12.  
  13.  
  14. screenWidth = 128
  15. screenHeight = 64
  16.  
  17. ai = {x = {}, y = {}, speed = {}, topspeed = {}, number = 3}
  18. continue = 0
  19. done = 0
  20. gDoorPosition = {0,0,0}
  21. gDoorText = {"Play", "Optn", "Help"}
  22. gDoorActive = 1
  23. i = 0
  24. invert = 0
  25. Key = 0
  26. key_alpha = 33
  27. key_left = 37
  28. key_right = 40
  29. key_down = 38
  30. key_up = 39
  31. key_exit = 36
  32. key_shift = 41
  33. player = {x = 0, y = 0, speed = 0, topspeed = 6}
  34.  
  35.  
  36.  
  37. function main()
  38.         setColor(1)
  39.         menu()
  40. end
  41.  
  42. function menu()
  43.         clear nil
  44.        
  45.         while done ~= 1 do
  46.                 if key(key_left) then gDoorActive = gDoorActive - 1 wait(3) end
  47.                 if key(key_right) then gDoorActive = gDoorActive + 1 wait(3) end
  48.                 if key(key_exit) then done = 1 end
  49.                 if key(key_shift) then selection = gDoorActive continue = 1 end
  50.                 if gDoorActive < 1 then gDoorActive = 1 end
  51.                 if gDoorActive > 3 then gDoorActive = 3 end
  52.                
  53.                 if selection == 1 then play()
  54.                         elseif selection == 2 then optn()
  55.                         elseif selection == 3 then help()
  56.                 end
  57.                
  58.                 selection = 0
  59.                
  60.                 for i = 1, 3, 1 do
  61.                         if i == gDoorActive and gDoorPosition[i] < 30 then gDoorPosition[i] = gDoorPosition[i] + 1
  62.                         elseif gDoorPosition[i] > 0 then gDoorPosition[i] = gDoorPosition[i] - 1 end
  63.                 end
  64.                
  65.                 for ix = 1, 3, 1 do
  66.                         garageDoor(ix * 35 - 20, 25, 30, 30, gDoorPosition[ix], 5, gDoorText[ix], 5)
  67.                 end
  68.                
  69.                 refresh2()
  70.                 wait(1)
  71.         end
  72.        
  73.         continue = 0
  74. end
  75.  
  76. function  garageDoor(x, y, width, height, position, panelHeight, text, margin)
  77.  
  78.         buffer = height/2
  79.         graytext(x + margin, y + buffer, text)  --display text
  80.  
  81.         rect(x, y, x + width, y + height, 5, 5) --draw garage
  82.        
  83.         rect(x, y, x + width, y + height - position, 4, 0) --draw door
  84.        
  85.         --draw panels
  86.         for iy = height - position, 0, -panelHeight do
  87.                 line(x, y + iy, x + width, y + iy, 4)
  88.         end
  89. end
  90.  
  91. function play()
  92.         clear nil
  93.         var = 0
  94.         iy = 0
  95.         for i=1, ai.number, 1 do
  96.                 ai.y[i] = 0
  97.                 ai.x[i] = i*8+1
  98.                 ai.topspeed[i] = 6
  99.                 if i == 1 then ai.speed[i] = ai.topspeed[i] else ai.speed = 1 + random(ai.topspeed[i]) end
  100.         end
  101.        
  102.         player.y = 0
  103.        
  104.         --[[--
  105.         graytext(5, 5, "Please Wait...")
  106.         refresh2()
  107.        
  108.         clear nil
  109.         for iy=1, screenHeight, 1 do
  110.                 for ix = 1, screenWidth, 1 do
  111.                         if i == 1 then pixel(ix, iy, 4) i = 0 else i = i + 1 end
  112.                 end
  113.                 if i==0 then i = 1 else i = 0 end
  114.         end
  115.         --]]--
  116.        
  117.         line(42, 0, 42, 64, 4)
  118.         line(84, 0, 84, 64, 4)
  119.        
  120.         refresh2()
  121.        
  122.        
  123.         --game loop
  124.         while not key(key_exit) do
  125.                 rect(43, 0, 83, 64, 0, 0) --clears only the road
  126.                
  127.                 if key(key_left) and player.x > 0 then player.x = player.x - 1
  128.                         elseif key(key_right) and player.x < 40 - car.width + 1 then player.x = player.x + 1
  129.                         elseif key(key_shift) then player.speed = player.speed + 1
  130.                         elseif key(key_alpha) then player.speed = player.speed - 1
  131.                 end
  132.                
  133.                 if player.speed > player.topspeed then player.speed = player.topspeed
  134.                         elseif player.speed < 0 then player.speed = 0
  135.                 end
  136.                
  137.                 aiRace()
  138.                
  139.                 spritexy player.x + 43, screenHeight - car.height, car.sprite
  140.                 player.y = player.y + player.speed
  141.                 refresh2()
  142.         end
  143.        
  144.         waitKey(key_exit)
  145.         clear nil
  146. end
  147.  
  148. function aiRace()
  149.         for i=1, ai.number, 1 do
  150.                 --ai and collisions with ai
  151.                 for j=1, ai.number do
  152.                         --if car i is not the same as car j
  153.                         if i~=j then
  154.                                 --if car j is in front or behind car i
  155.                                 if ai.x[j] > ai.x[i] - 16 and ai.x[j] < ai.x[i] + 16 then
  156.                                         --if car j is in front
  157.                                         if ai.y[j] > ai.y[i] and ai.y[j] < ai.y[i] + 16 then
  158.                                                 if ai.speed[i] > 0 then  ai.speed[i] = ai.speed[i] - 1 else ai.x[i] = ai.x[i] + 1 end
  159.                                         end
  160.                                         --if car j is behind
  161.                                         if ai.y[j] < ai.y[i]  and ai.y[j] > ai.y[i] - 16 then
  162.                                                 if ai.speed[i] < ai.topspeed[i] then ai.speed[i] = ai.speed[i] + 1 else ai.x[i] = ai.x - 1 end
  163.                                         end
  164.                                 end
  165.                                 --if car j is left or right of car i
  166.                                 if ai.y[j] > ai.y[i] - 16 and ai.y[j] < ai.y[i] + 16 then
  167.                                         --if car j is to the right
  168.                                         if ai.x[j] > ai.x[i] and ai.x[j] < ai.x[i] + 16 then
  169.                                                 --if the car i is not against the left wall
  170.                                                 if ai.x[i] > 0 then ai.x[i] = ai.x[i] - 1 elseif ai.speed > 0 then ai.speed[i] = ai.speed[i] - 1 end
  171.                                         end
  172.                                         --if car j is to the left
  173.                                         if ai.x[j] < ai.x[i] and ai.x[j] > ai.x[i] - 16 then
  174.                                                 --if car i is not against right wall
  175.                                                 if ai.x[i] < 40 then ai.x[i] = ai.x[i] + 1 elseif ai.speed > 0 then ai.speed[i] = ai.speed[i] - 1 end
  176.                                         end
  177.                                 end
  178.                         end
  179.                 end
  180.         --collisions with player
  181.         if ai.y[i] > player.y and ai.y[i] < player.y + 8 and ai.x[i] > player.x and ai.x[i] < player.x + 8 then crash() end
  182.        
  183.         --display
  184.         buffer = ai.y[i] - player.y
  185.         spritexy ai.x[i] + 43, screenHeight - car.height - buffer, car.sprite
  186.         ai.y[i] = ai.y[i] + ai.speed[i]
  187.         end
  188.                
  189. end
  190.  
  191. function crash()
  192.         clear nil
  193.         graytext(1, 1, "You Died!")
  194.         refresh2()
  195.         wait(100)
  196.         menu()
  197. end
  198.  
  199. function optn()
  200.         clear nil
  201.         graytext(1, 1, "Options Menu")
  202.         refresh2()
  203.         waitKey(key_exit)
  204.         clear nil
  205. end
  206.  
  207. function help()
  208.         clear nil
  209.         graytext(1, 1, "How may I help you?")
  210.         refresh2()
  211.         waitKey(key_exit)
  212.         clear nil
  213. end
  214.  
  215. function bye()
  216.         clear nil
  217.         graytext(1, 1, "Bye!")
  218.         refresh2()
  219.         wait(100)
  220. end
  221.  
  222. function waitKey(Key)
  223.         while not key(Key) do wait(3) end
  224.         wait(3)
  225. end
  226.  
  227. function refresh2()
  228.         --if invert == 1 then rect(0, 0, screenWidth, screenHeight) end
  229.         if invert == 1 then fill(5) end
  230.         refresh
  231. end
  232.  
  233. main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top