Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- title: game title
- -- author: game developer
- -- desc: short description
- -- script: lua
- -- input: keyboard
- local key_repeat = true
- local key_cooldown = 0.10
- local key_1st_cooldown = 0.25
- local key_timer = 0
- local max_key_timer = key_1st_cooldown
- local k_left = false -- key flags
- local k_right = false
- local k_up = false
- local k_down = false
- local moving_left = false -- movement flags for no-key-repeat
- local moving_right = false
- local moving_up = false
- local moving_down = false
- local moving = false
- local UP, DOWN, LEFT, RIGHT = 58, 59, 60, 61
- local SS = 8 -- sprite size
- local t_old = 0
- local x, y = 8*14, 8*8
- function TIC()
- local t_now = time()/1000
- local dt = t_now-t_old
- t_old = t_now
- took_turn, k_left, k_right, k_up, k_down = false, false, false, false, false
- cls(2)
- if key(UP) then k_up = true end -- also works with "keyp(UP, 0, 0)"
- if key(DOWN) then k_down = true end -- also works with "keyp(DOWN, 0, 0)"
- if key(LEFT) then k_left = true end -- also works with "keyp(LEFT, 0, 0)"
- if key(RIGHT) then k_right = true end -- also works with "keyp(RIGHT, 0, 0)"
- if key_repeat then move_repeat(dt)
- else move_once() end
- spr(64, x, y, 14)
- print("Took turn", 8, 8, took_turn and 14 or 7)
- end
- function move_once()
- if k_left and not moving_left and not k_right then
- took_turn = move( -1, 0 )
- moving_left = true
- elseif not k_left and moving_left then
- moving_left = false
- end
- if k_right and not moving_right and not k_left then
- took_turn = move( 1, 0 )
- moving_right = true
- elseif not k_right and moving_right then
- moving_right = false
- end
- if k_up and not moving_up and not k_down then
- took_turn = move( 0, -1 )
- moving_up = true
- elseif not k_up and moving_up then
- moving_up = false
- end
- if k_down and not moving_down and not k_up then
- took_turn = move( 0, 1 )
- moving_down = true
- elseif not k_down and moving_down then
- moving_down = false
- end
- end
- function move_repeat(dt)
- -- if already moving since last frame, make the cooldown shorter
- -- else, make it longer for the first key press
- if moving then max_key_timer = key_cooldown
- else max_key_timer = key_1st_cooldown end
- -- check if any movement key is being pressed
- moving = k_left or k_right or k_up or k_down
- -- if none is pressed, reset the timer so the player is free to move
- if not moving then key_timer = 0 end
- -- if no cooldown and a key was pressed
- if key_timer <= 0 and moving then
- if k_left and not k_right then
- if move(-1, 0) then took_turn = true end end
- if k_right and not k_left then
- if move( 1, 0) then took_turn = true end end
- if k_up and not k_down then
- if move( 0, -1) then took_turn = true end end
- if k_down and not k_up then
- if move( 0, 1) then took_turn = true end end
- if took_turn then
- key_timer = max_key_timer end -- set the cooldown
- -- else if still cooling down, just reduce timer by dt
- else
- key_timer = key_timer-dt
- end
- end
- function move(dx, dy)
- -- check against obstacles/mobs if player can_move()
- x = x + dx*SS
- y = y + dy*SS
- return true
- end
Advertisement
Add Comment
Please, Sign In to add comment