Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local rect = graydraw.rect
- local setColor = graydraw.setcolor
- local wait = misc.wait
- local line = graydraw.line
- local graytext = graydraw.text
- local fill = graydraw.fill
- local pixel = graydraw.pixel
- local random = misc.random
- function initVars()
- car = {height = 8, width = 8, sprite = "\000\000<((f^fz(<V<(jvNnr8<V\000"}
- screenWidth = 128
- screenHeight = 64
- ai = {x = {}, y = {}, speed = {}, topspeed = {}, number = 3}
- continue = 0
- done = 0
- gDoorPosition = {0,0,0}
- gDoorText = {"Play", "Optn", "Help"}
- gDoorActive = 1
- i = 0
- invert = 0
- Key = 0
- key_alpha = 33
- key_left = 37
- key_right = 40
- key_down = 38
- key_up = 39
- key_exit = 36
- key_shift = 41
- obstacles = {x = {1}, y = {40}, number = 1}
- player = {x = 0, y = 0, speed = 6, topspeed = 9}
- race = {finish = 10000, playersfinished = {}, score = {}}
- end
- function main(mode)
- if mode == 0 then intro() end
- setColor(1)
- initVars()
- menu()
- end
- function intro()
- end
- function menu()
- clear nil
- while done ~= 1 do
- if key(key_left) then gDoorActive = gDoorActive - 1 wait(3) end
- if key(key_right) then gDoorActive = gDoorActive + 1 wait(3) end
- if key(key_exit) then done = 1 end
- if key(key_shift) then selection = gDoorActive continue = 1 end
- if gDoorActive < 1 then gDoorActive = 1 end
- if gDoorActive > 3 then gDoorActive = 3 end
- if selection == 1 then play()
- elseif selection == 2 then optn()
- elseif selection == 3 then help()
- end
- selection = 0
- for i = 1, 3, 1 do
- if i == gDoorActive and gDoorPosition[i] < 30 then gDoorPosition[i] = gDoorPosition[i] + 1
- elseif gDoorPosition[i] > 0 then gDoorPosition[i] = gDoorPosition[i] - 1 end
- end
- for ix = 1, 3, 1 do
- garageDoor(ix * 35 - 20, 25, 30, 30, gDoorPosition[ix], 5, gDoorText[ix], 5)
- end
- refresh2()
- wait(1)
- end
- continue = 0
- end
- function garageDoor(x, y, width, height, position, panelHeight, text, margin)
- buffer = height/2
- graytext(x + margin, y + buffer, text) --display text
- rect(x, y, x + width, y + height, 5, 5) --draw garage
- rect(x, y, x + width, y + height - position, 4, 0) --draw door
- --draw panels
- for iy = height - position, 0, -panelHeight do
- line(x, y + iy, x + width, y + iy, 4)
- end
- end
- function play()
- transition(2)
- clear nil
- var = 0
- iy = 0
- for i=1, ai.number, 1 do
- ai.y[i] = 0
- ai.x[i] = i*8+2
- ai.topspeed[i] = 6 + random(4)
- if i == 1 then ai.speed[i] = ai.topspeed[i] else ai.speed[i] = 1 + random(ai.topspeed[i]) end
- end
- player.y = 0
- --[[--
- graytext(5, 5, "Please Wait...")
- refresh2()
- clear nil
- for iy=1, screenHeight, 1 do
- for ix = 1, screenWidth, 1 do
- if i == 1 then pixel(ix, iy, 4) i = 0 else i = i + 1 end
- end
- if i==0 then i = 1 else i = 0 end
- end
- --]]--
- drawroad()
- refresh2()
- --game loop
- while not key(key_exit) do
- --rect(43, 0, 83, 64, 0, 0) --clears only the road
- clear nil
- drawroad()
- if key(key_left) and player.x > 0 then player.x = player.x - 1
- elseif key(key_right) and player.x < 40 - car.width + 1 then player.x = player.x + 1
- elseif key(key_shift) then player.speed = player.speed + 1
- elseif key(key_alpha) then player.speed = player.speed - 1
- end
- if player.speed > player.topspeed then player.speed = player.topspeed
- elseif player.speed < 0 then player.speed = 0
- end
- aiRace()
- --check if player finished
- if player.y > race.finish then race.score[race.playersfinished] = -1 end
- --obstacleCollisions()
- spritexy player.x + 43, screenHeight - car.height, car.sprite
- player.y = player.y + player.speed
- line(43, race.finish, 83, race.finish, 4)
- refresh2()
- end
- waitKey(key_exit)
- clear nil
- end
- function aiRace()
- for i=1, ai.number, 1 do
- --ai and collisions with ai
- for j=1, ai.number do
- --if car i is not the same as car j
- if i~=j then
- --if car j is in front or behind car i
- if ai.x[j] > ai.x[i] - 16 and ai.x[j] < ai.x[i] + 16 then
- --if car j is in front
- if ai.y[j] > ai.y[i] and ai.y[j] < ai.y[i] + 16 then
- if ai.speed[i] > 0 then ai.speed[i] = ai.speed[i] - 1 else ai.x[i] = ai.x[i] + 1 end
- elseif ai.speed[i] < ai.topspeed[i] and j == ai.number then ai.speed[i] = ai.speed[i] + 1
- end
- --if car j is behind
- if ai.y[j] < ai.y[i] and ai.y[j] > ai.y[i] - 16 then
- if ai.speed[i] < ai.topspeed[i] then ai.speed[i] = ai.speed[i] + 1 else ai.x[i] = ai.x[i] - 1 end
- end
- end
- --if car j is left or right of car i
- if ai.y[j] > ai.y[i] - 16 and ai.y[j] < ai.y[i] + 16 then
- --if car j is to the right
- if ai.x[j] > ai.x[i] and ai.x[j] < ai.x[i] + 16 then
- --if the car i is not against the left wall
- 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
- end
- --if car j is to the left
- if ai.x[j] < ai.x[i] and ai.x[j] > ai.x[i] - 16 then
- --if car i is not against right wall
- 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
- end
- end
- end
- end
- --if car i is off the road
- 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
- --if car has passed finish line
- if ai.y[i] > race.finish then race.score[race.playersfinished] = i end
- --collisions and ai with player
- --if player is in front or behind
- if player.x > ai.x[i] - 16 and player.x < ai.x[i] + 16 then
- --if player is in front
- if player.y > ai.y[i] and player.y < ai.y[i] + 16 then
- if ai.speed[i] > 0 then ai.speed[i] = ai.speed[i] - 1 else ai.x[i] = ai.x[i] + 1 end
- end
- --if player is behind
- if player.y < ai.y[i] and player.y > ai.y[i] - 16 then
- if ai.speed[i] < ai.topspeed[i] then ai.speed[i] = ai.speed[i] + 1 else ai.x[i] = ai.x[i] - 1 end
- end
- end
- --if player is left or right of car i
- if player.y > ai.y[i] - 16 and player.y < ai.y[i] + 16 then
- --if player is to the right
- if player.x > ai.x[i] and player.x < ai.x[i] + 16 then
- --if the car i is not against the left wall
- 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
- end
- --if player is to the left
- if player.x < ai.x[i] and player.x > ai.x[i] - 16 then
- --if car i is not against right wall
- 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
- end
- end
- --collisions with player
- 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
- --display
- buffer = ai.y[i] - player.y
- spritexy ai.x[i] + 43, screenHeight - car.height - buffer, car.sprite
- ai.y[i] = ai.y[i] + ai.speed[i]
- end
- end
- function drawroad()
- line(42, 0, 42, 64, 4)
- line(84, 0, 84, 64, 4)
- end
- --[[--function obstacleCollisions()
- for i=1, obstacles.number do
- --drawObstacle(obstacles.x[i], obstacles.y[i])
- rect(obstacles.x[i] + 43, obstacles.y[i], obstacles.x[i] + 43 + 8, obstacles.y[i] + 8, 4, 4)
- 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
- end
- if random(10) == 3 then
- obstacles.y[obstacles.number] = player.y + screenHeight
- obstacles.x[obstacles.number] = random(32)
- obstacles.number = obstacles.number + 1
- end
- end
- function drawObstacle(x1, y1)
- x1 = x1 + 43
- rect(x1, y1, x1 + 8, y1 + 8, 4, 4)
- end--]]--
- function crash()
- clear nil
- graytext(1, 1, "You Died!")
- refresh2()
- wait(100)
- transition(1)
- main(1)
- end
- function transition(mode1)
- if mode1 == 1 then
- for i=1, screenHeight do
- rect(0, 0, screenWidth, i, 5, 5)
- refresh2()
- end
- elseif mode1 == 2 then
- for i=1, 5 do
- rect(0, 0, screenWidth, screenHeight, 5, 5)
- refresh2()
- wait(3)
- clear nil
- wait(3)
- end
- end
- end
- function optn()
- clear nil
- graytext(1, 1, "Options Menu")
- refresh2()
- waitKey(key_exit)
- clear nil
- end
- function help()
- clear nil
- graytext(1, 1, "How may I help you?")
- refresh2()
- waitKey(key_exit)
- clear nil
- end
- function bye()
- clear nil
- graytext(1, 1, "Bye!")
- refresh2()
- wait(100)
- end
- function waitKey(Key)
- while not key(Key) do wait(3) end
- wait(3)
- end
- function refresh2()
- --if invert == 1 then rect(0, 0, screenWidth, screenHeight) end
- if invert == 1 then fill(5) end
- refresh
- end
- main(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement