Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --The width and height of the screen
- local w,h = term.getSize()
- --Whether or not the game is running
- local running = true
- --The interval between each game update
- local refreshRate = 0.15
- --The game update timer
- local gtID = os.startTimer(refreshRate)
- --Header for updateBullets function
- local updateBullets = nil
- --[[ Our player:
- x: his X position
- y: his Y position
- icon: the icon drawn at his position
- rotation: the player facing
- ]]--
- local player = {
- x = math.floor(w/2),
- y = math.floor(h/2),
- icon = "&",
- rotation = 0.0
- }
- --[[ The list of bullets:
- x: the x position
- y: the y position
- xstep: the movement each update on x
- ystep: the movement each update on y
- type: the colour of the bullet
- ]]--
- local bullets = { }
- --The list of all possible bullet types
- local bullTypes = { colours.orange, colours.yellow, colours.lime }
- --The currently selected bullet type
- local selBull = 1
- --Draws the player, bullets and header info
- local function display()
- -- Player draw code
- term.setBackgroundColour(colours.black)
- term.clear()
- term.setTextColour(colours.lime)
- term.setCursorPos(player.x, player.y)
- term.write(player.icon)
- -- Footer draw code
- term.setCursorPos(1,h)
- term.setBackgroundColour(bullTypes[selBull])
- term.write(string.rep(" ", w))
- -- Bullet draw code
- for _,v in pairs(bullets) do
- term.setCursorPos(v.x, v.y)
- term.setBackgroundColour(v.type)
- term.write(" ")
- end
- -- Reticle code
- term.setCursorPos(player.x + math.cos(player.rotation) * 8,
- player.y - math.sin(player.rotation) * 6)
- term.setBackgroundColour(colours.black)
- term.setTextColour(colours.green)
- term.write("+")
- end
- local function createBullet(x,y,xvel,yvel)
- table.insert(bullets, {
- x = x,
- y = y,
- xstep = xvel,
- ystep = yvel,
- type = bullTypes[selBull]
- })
- end
- --Accepts and acts upon input (and timers)
- local function handleInput()
- local id, p1, p2, p3 = os.pullEvent()
- if id == "key" then
- elseif id == "mouse_click" or id == "mouse_drag" then
- elseif id == "mouse_scroll" then
- end
- if id == "timer" and p1 == gtID then
- updateBullets()
- gtID = os.startTimer(refreshRate)
- end
- end
- --Moves each bullet according to their velocity,
- --and removes those that have left the screen
- function updateBullets()
- for _,v in pairs(bullets) do
- v.x = v.x + v.xstep
- v.y = v.y + v.ystep
- end
- local blistlength = #bullets
- for i=1,blistlength do
- local v = bullets[i]
- if v and (v.x < 1 or v.x > w or
- v.y < 1 or v.y > h-1) then
- table.remove(bullets, i)
- blistlength = blistlength - 1
- i = i - 1
- end
- end
- end
- --The main game loop
- local function mainLoop()
- while running do
- display()
- handleInput()
- end
- end
- mainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement