Advertisement
flyingfisch

indy500

Jun 11th, 2012
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.15 KB | None | 0 0
  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. function initVars()
  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.     obstacles = {x = {1}, y = {40}, number = 1}
  34.     player = {x = 0, y = 0, speed = 6, topspeed = 9}
  35.     race = {finish = 10000, playersfinished = {}, score = {}}
  36. end
  37.  
  38.  
  39. function main(mode)
  40.     if mode == 0 then intro() end
  41.     setColor(1)
  42.     initVars()
  43.     menu()
  44. end
  45.  
  46. function intro()
  47.    
  48. end
  49.  
  50. function menu()
  51.     clear nil
  52.    
  53.     while done ~= 1 do
  54.         if key(key_left) then gDoorActive = gDoorActive - 1 wait(3) end
  55.         if key(key_right) then gDoorActive = gDoorActive + 1 wait(3) end
  56.         if key(key_exit) then done = 1 end
  57.         if key(key_shift) then selection = gDoorActive continue = 1 end
  58.         if gDoorActive < 1 then gDoorActive = 1 end
  59.         if gDoorActive > 3 then gDoorActive = 3 end
  60.        
  61.         if selection == 1 then play()
  62.             elseif selection == 2 then optn()
  63.             elseif selection == 3 then help()
  64.         end
  65.        
  66.         selection = 0
  67.        
  68.         for i = 1, 3, 1 do
  69.             if i == gDoorActive and gDoorPosition[i] < 30 then gDoorPosition[i] = gDoorPosition[i] + 1
  70.             elseif gDoorPosition[i] > 0 then gDoorPosition[i] = gDoorPosition[i] - 1 end
  71.         end
  72.        
  73.         for ix = 1, 3, 1 do
  74.             garageDoor(ix * 35 - 20, 25, 30, 30, gDoorPosition[ix], 5, gDoorText[ix], 5)
  75.         end
  76.        
  77.         refresh2()
  78.         wait(1)
  79.     end
  80.    
  81.     continue = 0
  82. end
  83.  
  84. function  garageDoor(x, y, width, height, position, panelHeight, text, margin)
  85.  
  86.     buffer = height/2
  87.     graytext(x + margin, y + buffer, text)  --display text
  88.  
  89.     rect(x, y, x + width, y + height, 5, 5) --draw garage
  90.    
  91.     rect(x, y, x + width, y + height - position, 4, 0) --draw door
  92.    
  93.     --draw panels
  94.     for iy = height - position, 0, -panelHeight do
  95.         line(x, y + iy, x + width, y + iy, 4)
  96.     end
  97. end
  98.  
  99. function play()
  100.     transition(2)
  101.     clear nil
  102.     var = 0
  103.     iy = 0
  104.     for i=1, ai.number, 1 do
  105.         ai.y[i] = 0
  106.         ai.x[i] = i*8+2
  107.         ai.topspeed[i] = 6 + random(4)
  108.         if i == 1 then ai.speed[i] = ai.topspeed[i] else ai.speed[i] = 1 + random(ai.topspeed[i]) end
  109.     end
  110.    
  111.     player.y = 0
  112.    
  113.     --[[--
  114.     graytext(5, 5, "Please Wait...")
  115.     refresh2()
  116.    
  117.     clear nil
  118.     for iy=1, screenHeight, 1 do
  119.         for ix = 1, screenWidth, 1 do
  120.             if i == 1 then pixel(ix, iy, 4) i = 0 else i = i + 1 end
  121.         end
  122.         if i==0 then i = 1 else i = 0 end
  123.     end
  124.     --]]--
  125.    
  126.     drawroad()
  127.    
  128.     refresh2()
  129.    
  130.    
  131.     --game loop
  132.     while not key(key_exit) do
  133.         --rect(43, 0, 83, 64, 0, 0) --clears only the road
  134.         clear nil
  135.         drawroad()
  136.        
  137.         if key(key_left) and player.x > 0 then player.x = player.x - 1
  138.             elseif key(key_right) and player.x < 40 - car.width + 1 then player.x = player.x + 1
  139.             elseif key(key_shift) then player.speed = player.speed + 1
  140.             elseif key(key_alpha) then player.speed = player.speed - 1
  141.         end
  142.        
  143.         if player.speed > player.topspeed then player.speed = player.topspeed
  144.             elseif player.speed < 0 then player.speed = 0
  145.         end
  146.        
  147.         aiRace()
  148.        
  149.         --check if player finished
  150.         if player.y > race.finish then race.score[race.playersfinished] = -1 end
  151.         --obstacleCollisions()
  152.        
  153.         spritexy player.x + 43, screenHeight - car.height, car.sprite
  154.         player.y = player.y + player.speed
  155.         line(43, race.finish, 83, race.finish, 4)
  156.         refresh2()
  157.     end
  158.    
  159.     waitKey(key_exit)
  160.     clear nil
  161. end
  162.  
  163. function aiRace()
  164.     for i=1, ai.number, 1 do
  165.         --ai and collisions with ai
  166.         for j=1, ai.number do
  167.             --if car i is not the same as car j
  168.             if i~=j then
  169.                 --if car j is in front or behind car i
  170.                 if ai.x[j] > ai.x[i] - 16 and ai.x[j] < ai.x[i] + 16 then
  171.                     --if car j is in front
  172.                     if ai.y[j] > ai.y[i] and ai.y[j] < ai.y[i] + 16 then
  173.                         if ai.speed[i] > 0 then  ai.speed[i] = ai.speed[i] - 1 else ai.x[i] = ai.x[i] + 1 end
  174.                         elseif ai.speed[i] < ai.topspeed[i] and j == ai.number then ai.speed[i] = ai.speed[i] + 1
  175.                     end
  176.                     --if car j is behind
  177.                     if ai.y[j] < ai.y[i]  and ai.y[j] > ai.y[i] - 16 then
  178.                         if ai.speed[i] < ai.topspeed[i] then ai.speed[i] = ai.speed[i] + 1 else ai.x[i] = ai.x[i] - 1 end
  179.                     end
  180.                 end
  181.                 --if car j is left or right of car i
  182.                 if ai.y[j] > ai.y[i] - 16 and ai.y[j] < ai.y[i] + 16 then
  183.                     --if car j is to the right
  184.                     if ai.x[j] > ai.x[i] and ai.x[j] < ai.x[i] + 16 then
  185.                         --if the car i is not against the left wall
  186.                         if ai.x[i] > 0 then ai.x[i] = ai.x[i] - 1 elseif ai.speed[i] < ai.topspeed[i] then ai.speed[i] = ai.speed[i] + 1 end
  187.                     end
  188.                     --if car j is to the left
  189.                     if ai.x[j] < ai.x[i] and ai.x[j] > ai.x[i] - 16 then
  190.                         --if car i is not against right wall
  191.                         if ai.x[i] < 40 then ai.x[i] = ai.x[i] + 1 elseif ai.speed[i] < ai.topspeed[i] then ai.speed[i] = ai.speed[i] + 1 end
  192.                     end
  193.                 end
  194.             end
  195.         end
  196.    
  197.     --if car i is off the road
  198.     if ai.x[i] > 32 then ai.x[i] = ai.x[i] - 1 elseif ai.x[i] < 0 then ai.x[i] = ai.x[i] + 1 end
  199.    
  200.     --if car has passed finish line
  201.     if ai.y[i] > race.finish then race.score[race.playersfinished] = i end
  202.    
  203.     --collisions and ai with player
  204.    
  205.     --if player is in front or behind
  206.     if player.x > ai.x[i] - 16 and player.x < ai.x[i] + 16 then
  207.         --if player is in front
  208.         if player.y > ai.y[i] and player.y < ai.y[i] + 16 then
  209.             if ai.speed[i] > 0 then  ai.speed[i] = ai.speed[i] - 1 else ai.x[i] = ai.x[i] + 1 end
  210.         end
  211.         --if player is behind
  212.         if player.y < ai.y[i]  and player.y > ai.y[i] - 16 then
  213.             if ai.speed[i] < ai.topspeed[i] then ai.speed[i] = ai.speed[i] + 1 else ai.x[i] = ai.x[i] - 1 end
  214.         end
  215.     end
  216.     --if player is left or right of car i
  217.     if player.y > ai.y[i] - 16 and player.y < ai.y[i] + 16 then
  218.         --if player is to the right
  219.         if player.x > ai.x[i] and player.x < ai.x[i] + 16 then
  220.             --if the car i is not against the left wall
  221.             if ai.x[i] > 0 then ai.x[i] = ai.x[i] - 1 elseif ai.speed[i] < ai.topspeed[i] then ai.speed[i] = ai.speed[i] + 1 end
  222.         end
  223.         --if player is to the left
  224.         if player.x < ai.x[i] and player.x > ai.x[i] - 16 then
  225.             --if car i is not against right wall
  226.             if ai.x[i] < 40 then ai.x[i] = ai.x[i] + 1 elseif ai.speed[i] < ai.topspeed[i] then ai.speed[i] = ai.speed[i] + 1 end
  227.         end
  228.     end
  229.    
  230.     --collisions with player
  231.     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 transistion(2) crash() end
  232.    
  233.     --display
  234.     buffer = ai.y[i] - player.y
  235.     spritexy ai.x[i] + 43, screenHeight - car.height - buffer, car.sprite
  236.     ai.y[i] = ai.y[i] + ai.speed[i]
  237.     end
  238.        
  239. end
  240.  
  241. function drawroad()
  242.     line(42, 0, 42, 64, 4)
  243.     line(84, 0, 84, 64, 4)
  244. end
  245.  
  246. --[[--function obstacleCollisions()
  247.     for i=1, obstacles.number do
  248.         --drawObstacle(obstacles.x[i], obstacles.y[i])
  249.         rect(obstacles.x[i] + 43, obstacles.y[i], obstacles.x[i] + 43 + 8, obstacles.y[i] + 8, 4, 4)
  250.         if obstacles.y[i] > player.y and obstacles.y[i] < player.y + 8 and obstacles.x[i] > player.x and obstacles.x[i] < player.x + 8 then transistion(2) crash() end
  251.     end
  252.    
  253.     if random(10) == 3 then
  254.         obstacles.y[obstacles.number] = player.y + screenHeight
  255.         obstacles.x[obstacles.number] = random(32)
  256.         obstacles.number = obstacles.number + 1
  257.     end
  258. end
  259.  
  260. function drawObstacle(x1, y1)
  261.     x1 = x1 + 43
  262.     rect(x1, y1, x1 + 8, y1 + 8, 4, 4)
  263. end--]]--
  264.  
  265. function crash()
  266.     clear nil
  267.     graytext(1, 1, "You Died!")
  268.     refresh2()
  269.     wait(100)
  270.     transition(1)
  271.     main(1)
  272. end
  273.  
  274. function transition(mode1)
  275.     if mode1 == 1 then
  276.         for i=1, screenHeight do
  277.             rect(0, 0, screenWidth, i, 5, 5)
  278.             refresh2()
  279.         end
  280.     elseif mode1 == 2 then
  281.         for i=1, 5 do
  282.             rect(0, 0, screenWidth, screenHeight, 5, 5)
  283.             refresh2()
  284.             wait(3)
  285.             clear nil
  286.             wait(3)
  287.         end
  288.     end
  289. end
  290.  
  291. function optn()
  292.     clear nil
  293.     graytext(1, 1, "Options Menu")
  294.     refresh2()
  295.     waitKey(key_exit)
  296.     clear nil
  297. end
  298.  
  299. function help()
  300.     clear nil
  301.     graytext(1, 1, "How may I help you?")
  302.     refresh2()
  303.     waitKey(key_exit)
  304.     clear nil
  305. end
  306.  
  307. function bye()
  308.     clear nil
  309.     graytext(1, 1, "Bye!")
  310.     refresh2()
  311.     wait(100)
  312. end
  313.  
  314. function waitKey(Key)
  315.     while not key(Key) do wait(3) end
  316.     wait(3)
  317. end
  318.  
  319. function refresh2()
  320.     --if invert == 1 then rect(0, 0, screenWidth, screenHeight) end
  321.     if invert == 1 then fill(5) end
  322.     refresh
  323. end
  324.  
  325. main(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement