Replies: 4 comments
-
There is this small package that puts some wrappers in place for pico-tic apis. Works as a first step if you're looking to port without really changing the code. Works sometimes, othertimes not so much..... https://github.com/musurca/pico2tic |
Beta Was this translation helpful? Give feedback.
0 replies
-
Try this code in_progress = 0
start_end_game = 1
game_over = 2
left=0 right=1 up=2 down=3
valid_moves = {left,right,up,down}
rnd=math.random
flr=math.floor
sqrt=math.sqrt
function _init()
player = {}
player.x = flr(rnd(120))
player.y = flr(rnd(114)+8)
player.startsprite = 0
player.endsprite = 1
player.sprite = 0
player.speed = 2
player.stuck = 0
enemy = {}
enemy.x = flr(rnd(120))
enemy.y = flr(rnd(114)+8)
enemy.startsprite = 4
enemy.endsprite = 5
enemy.sprite = 4
enemy.speed = 1
enemy.stuck = 0
state = in_progress
score = 0
end
function move(unit)
unit.sprite =unit.sprite+ 1
if unit.sprite > unit.endsprite then
unit.sprite = unit.startsprite
end
end
function draw_unit(unit) spr(unit.sprite, unit.x, unit.y) end
function get_map_cell(unit) return mget(flr((unit.x+4)/8), flr((unit.y-4)/8)) end
function hit_house(unit) return get_map_cell(unit) == 16 end
function move_unit(unit, direction)
unit.moving = false
if hit_house(unit) then
unit.stuck =unit.stuck+ 1
if unit.stuck > 4 then
unit.stuck = 0
else
return
end
end
if direction == left and
unit.x - unit.speed > 0 then
unit.x =unit.x- unit.speed
unit.moving = true
end
if direction == right and
unit.x + unit.speed < 120 then
unit.x =unit.x+ unit.speed
unit.moving = true
end
if direction == up and
unit.y - unit.speed > 8 then
unit.y =unit.y- unit.speed
unit.moving = true
end
if direction == down and
unit.y + unit.speed < 120 then
unit.y =unit.y+ unit.speed
unit.moving = true
end
if not unit.moving then
unit.sprite = unit.startsprite
else
move(unit)
end
end
function move_player()
for i=1,#valid_moves do
if btn(valid_moves[i]) then
move_unit(player, valid_moves[i])
end
end
end
function move_enemy()
if enemy.x > player.x then
move_unit(enemy, left)
end
if enemy.x < player.x then
move_unit(enemy, right)
end
if enemy.y > player.y then
move_unit(enemy, up)
end
if enemy.y < player.y then
move_unit(enemy, down)
end
enemy.speed =enemy.speed+ 0.0005
end
function distance(p0, p1)
dx=p0.x-p1.x dy=p0.y-p1.y
return sqrt(dx*dx+dy*dy)
end
function check_game_over()
if
distance(enemy,player) < 7
and state ~= game_over
then
state = start_end_game
end
end
function _update()
move_player()
move_enemy()
check_game_over()
end
_init()
function TIC()
_update()
cls()
if state == in_progress then
map(0,0,0,8,16,15)
draw_unit(player)
draw_unit(enemy)
score =score+ 1
print("score: "..score)
elseif state == start_end_game then
sfx(0)
state = game_over
elseif state == game_over then
print("\135 game over \135")
print("your final score was: "..score)
print("press action to try again")
if btn(4) then
_init()
end
end
end |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thank you so much!
William Mangum
(He. Him. His.)
Career and Technical Education
512-414-3217 (Office)
Campus Innovation Connector & Yearbook Advisor
512-414-0412 (Classroom 112)
Lamar Middle School & Fine Arts Academy
512-414-4511 (Fax)
The Austin Independent School District offers career and technical education sequences of coursework based on the sixteen federally defined Career Clusters. Admission to these courses is based on interest and aptitude, age appropriateness, and class space availability. It is the policy of the Austin Independent School District not to discriminate on the basis of race, color, national origin, sex, or handicap in its vocational programs, services or activities as required by Title VI of the Civil Rights Act of 1964, as amended; Title IX of the Education Amendment of 1972; the Age Discrimination Act of 1975, as amended; and Section 504 of the Rehabilitation Act of 1973, as amended.
CONFIDENTIALITY NOTICE: This email & attached documents, if any, may contain confidential information. All information is intended only for the use of the named recipient(s). If you are not the named recipient, you are not authorized to read, disclose, copy, distribute or take any action in reliance on the information and any action other than immediate delivery to the named recipient is strictly prohibited. If you have received this email in error, please immediately notify sender by telephone or priority email to arrange for a return of the original documents. If you are the named recipient, you are not authorized to reveal any of this information to any other unauthorized person.
…________________________________
From: Vadim Grigoruk ***@***.***>
Sent: Tuesday, April 27, 2021 6:12 AM
To: nesbox/TIC-80 ***@***.***>
Cc: William Mangum ***@***.***>; Author ***@***.***>
Subject: Re: [nesbox/TIC-80] Help newbie go from Pico-8 to TIC-80 (#1429)
Warning: EXTERNAL SENDER, use caution when opening links or attachments.
Try this code
in_progress = 0
start_end_game = 1
game_over = 2
left=0 right=1 up=2 down=3
valid_moves = {left,right,up,down}
rnd=math.random
flr=math.floor
sqrt=math.sqrt
function _init()
player = {}
player.x = flr(rnd(120))
player.y = flr(rnd(114)+8)
player.startsprite = 0
player.endsprite = 1
player.sprite = 0
player.speed = 2
player.stuck = 0
enemy = {}
enemy.x = flr(rnd(120))
enemy.y = flr(rnd(114)+8)
enemy.startsprite = 4
enemy.endsprite = 5
enemy.sprite = 4
enemy.speed = 1
enemy.stuck = 0
state = in_progress
score = 0
end
function move(unit)
unit.sprite =unit.sprite+ 1
if unit.sprite > unit.endsprite then
unit.sprite = unit.startsprite
end
end
function draw_unit(unit) spr(unit.sprite, unit.x, unit.y) end
function get_map_cell(unit) return mget(flr((unit.x+4)/8), flr((unit.y-4)/8)) end
function hit_house(unit) return get_map_cell(unit) == 16 end
function move_unit(unit, direction)
unit.moving = false
if hit_house(unit) then
unit.stuck =unit.stuck+ 1
if unit.stuck > 4 then
unit.stuck = 0
else
return
end
end
if direction == left and
unit.x - unit.speed > 0 then
unit.x =unit.x- unit.speed
unit.moving = true
end
if direction == right and
unit.x + unit.speed < 120 then
unit.x =unit.x+ unit.speed
unit.moving = true
end
if direction == up and
unit.y - unit.speed > 8 then
unit.y =unit.y- unit.speed
unit.moving = true
end
if direction == down and
unit.y + unit.speed < 120 then
unit.y =unit.y+ unit.speed
unit.moving = true
end
if not unit.moving then
unit.sprite = unit.startsprite
else
move(unit)
end
end
function move_player()
for i=1,#valid_moves do
if btn(valid_moves[i]) then
move_unit(player, valid_moves[i])
end
end
end
function move_enemy()
if enemy.x > player.x then
move_unit(enemy, left)
end
if enemy.x < player.x then
move_unit(enemy, right)
end
if enemy.y > player.y then
move_unit(enemy, up)
end
if enemy.y < player.y then
move_unit(enemy, down)
end
enemy.speed =enemy.speed+ 0.0005
end
function distance(p0, p1)
dx=p0.x-p1.x dy=p0.y-p1.y
return sqrt(dx*dx+dy*dy)
end
function check_game_over()
if
distance(enemy,player) < 7
and state ~= game_over
then
state = start_end_game
end
end
function _update()
move_player()
move_enemy()
check_game_over()
end
_init()
function TIC()
_update()
cls()
if state == in_progress then
map(0,0,0,8,16,15)
draw_unit(player)
draw_unit(enemy)
score =score+ 1
print("score: "..score)
elseif state == start_end_game then
sfx(0)
state = game_over
elseif state == game_over then
print("\135 game over \135")
print("your final score was: "..score)
print("press action to try again")
if btn(4) then
_init()
end
end
end
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<#1429 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AIFU3LEQSMBQLOK6KTVZPLLTK2LYVANCNFSM43TKYCHA>.
Confidentiality Notice: This email message, including all attachments, is for the sole use of the intended recipient(s) and may contain confidential student and/or employee information. Unauthorized use of disclosure is prohibited under the federal Family Educational Rights & Privacy Act (20 U.S.C. §1232g, 34 CFR Part 99, 19 TAC 247.2, Gov’t Code 552.023, Educ. Code 21.355, 29 CFR 1630.14(b)(c)). If you are not the intended recipient, you may not use, disclose, copy or disseminate this information. Please call the sender immediately or reply by email and destroy all copies of the original message, including attachments.
|
Beta Was this translation helpful? Give feedback.
0 replies
-
Cartridge is published here -- title: Basic Game
-- author: nesbox & stevenfelix505
-- desc: Basic PICO-8 Game ported into TIC-80
-- script: lua
developer_mode = 0
in_progress = 0
start_end_game = 1
game_over = 2
menustate = 0
game_over_menustate = 0
optionsstate = 0
pause_menustate = 0
soundval = 0
invincibleval = 0
playerspeedval = 2
enemycountval = 1
enemyspeedval = 1
enemyspeedincrval = 0.0005
left=2 right=3 up=0 down=1
valid_moves = {left,right,up,down}
rnd=math.random
flr=math.floor
sqrt=math.sqrt
function _init()
player = {}
player.x = flr(rnd(216)+8)
player.y = flr(rnd(114)+8)
player.startsprite = 0
player.endsprite = 1
player.sprite = 0
player.speed = playerspeedval
player.stuck = 0
enemy = {}
enemy.x = flr(rnd(216)+8)
enemy.y = flr(rnd(114)+8)
enemy.startsprite = 4
enemy.endsprite = 5
enemy.sprite = 4
enemy.speed = enemyspeedval
enemy.stuck = 0
enemy2 = {}
enemy2.x = flr(rnd(216)+8)
enemy2.y = flr(rnd(114)+8)
enemy2.startsprite = 8
enemy2.endsprite = 9
enemy2.sprite = 8
enemy2.speed = enemyspeedval
enemy2.stuck = 0
enemy3 = {}
enemy3.x = flr(rnd(216)+8)
enemy3.y = flr(rnd(114)+8)
enemy3.startsprite = 12
enemy3.endsprite = 13
enemy3.sprite = 12
enemy3.speed = enemyspeedval
enemy3.stuck = 0
state = menu
score = 0
end
function menu()
print("Select Exit",164,8,12)
spr(254,154,7)
spr(255,201,7)
print("Basic PICO-8 Game",8,8,12)
print(menustate+1,226,16,12)
print("Main Menu",8,16,12)
print("ported by nesbox fixed by stevenfelix505",7,124,12)
print("Play",8,26,12)
print("Options",8,34,12)
print("Exit",8,42,12)
if menustate == 0 then
print("Play",8,26,2)
if btnp(0) then
menustate = 2
elseif btnp(1) then
menustate = 1
elseif btnp(4) then
state = in_progress
end
elseif menustate == 1 then
print("Options",8,34,2)
if btnp(0) then
menustate = 0
elseif btnp(1) then
menustate = 2
elseif btnp(4) then
state = options
end
elseif menustate == 2 then
print("Exit",8,42,2)
if btnp(0) then
menustate = 1
elseif btnp(1) then
menustate = 0
elseif btnp(4) then
exit()
end
end
if btnp(5) then
exit()
end
end
function game_over_menu()
print("Select Main Menu",136,8,12)
spr(254,126,7)
spr(255,173,7)
print("Final score = "..score,8,8,12)
print(game_over_menustate+1,226,16,12)
print("Game Over",8,16,12)
print("ported by nesbox fixed by stevenfelix505",7,124,12)
print("Retry",8,26,12)
print("Main Menu",8,34,12)
print("Exit",8,42,12)
if game_over_menustate == 0 then
print("Retry",8,26,2)
if btnp(0) then
game_over_menustate = 2
elseif btnp(1) then
game_over_menustate = 1
elseif btnp(4) then
_init()
state = in_progress
end
elseif game_over_menustate == 1 then
print("Main Menu",8,34,2)
if btnp(0) then
game_over_menustate = 0
elseif btnp(1) then
game_over_menustate = 2
elseif btnp(4) then
_init()
end
elseif game_over_menustate == 2 then
print("Exit",8,42,2)
if btnp(0) then
game_over_menustate = 1
elseif btnp(1) then
game_over_menustate = 0
elseif btnp(4) then
exit()
end
end
if btnp(5) then
_init()
end
end
function options()
print("Select Back",161,8,12)
spr(254,151,7)
spr(255,198,7)
print("Basic PICO-8 Game",8,8,12)
print(optionsstate+1,226,16,12)
print("Options",8,16,12)
print("ported by nesbox fixed by stevenfelix505",7,124,12)
print("Sound Volume = "..soundval,8,26,12)
print("Invincible = "..invincibleval,8,34,12)
print("Player Speed = "..playerspeedval,8,42,12)
print("Enemy Count = "..enemycountval,8,50,12)
print("Enemy Speed = "..enemyspeedval,8,58,12)
print("Enemy Speed Increase = "..enemyspeedincrval,8,66,12)
print("Back",8,74,12)
if optionsstate == 0 then
print("Sound Volume = "..soundval,8,26,2)
if btnp(0) then
optionsstate = 6
elseif btnp(1) then
optionsstate = 1
elseif btnp(2) then
soundval = soundval-1
elseif btnp(3) then
soundval = soundval+1
end
elseif optionsstate == 1 then
print("Invincible = "..invincibleval,8,34,2)
if btnp(0) then
optionsstate = 0
elseif btnp(1) then
optionsstate = 2
elseif btnp(2) then
invincibleval = invincibleval-1
elseif btnp(3) then
invincibleval = invincibleval+1
end
elseif optionsstate == 2 then
print("Player Speed = "..playerspeedval,8,42,2)
if btnp(0) then
optionsstate = 1
elseif btnp(1) then
optionsstate = 3
elseif btnp(2) then
playerspeedval = playerspeedval-1
elseif btnp(3) then
playerspeedval = playerspeedval+1
end
elseif optionsstate == 3 then
print("Enemy Count = "..enemycountval,8,50,2)
if btnp(0) then
optionsstate = 2
elseif btnp(1) then
optionsstate = 4
elseif btnp(2) then
enemycountval = enemycountval-1
elseif btnp(3) then
enemycountval = enemycountval+1
end
elseif optionsstate == 4 then
print("Enemy Speed = "..enemyspeedval,8,58,2)
if btnp(0) then
optionsstate = 3
elseif btnp(1) then
optionsstate = 5
elseif btnp(2) then
enemyspeedval = enemyspeedval-1
elseif btnp(3) then
enemyspeedval = enemyspeedval+1
end
elseif optionsstate == 5 then
print("Enemy Speed Increase = "..enemyspeedincrval,8,66,2)
if btnp(0) then
optionsstate = 4
elseif btnp(1) then
optionsstate = 6
elseif btnp(2) then
enemyspeedincrval = enemyspeedincrval-0.0001
elseif btnp(3) then
enemyspeedincrval = enemyspeedincrval+0.0001
end
elseif optionsstate == 6 then
print("Back",8,74,2)
if btnp(0) then
optionsstate = 5
elseif btnp(1) then
optionsstate = 0
elseif btnp(4) then
_init()
end
end
if btnp(5) then
_init()
end
end
function pause_menu()
print("Select Resume",148,8,12)
spr(254,138,7)
spr(255,185,7)
print("Basic PICO-8 Game",8,8,12)
print(pause_menustate+1,224,16,12)
print("Paused",8,16,12)
print("ported by nesbox fixed by stevenfelix505",7,124,12)
print("Resume",8,26,12)
print("Retry",8,34,12)
print("Main Menu",8,42,12)
print("Exit",8,50,12)
if pause_menustate == 0 then
print("Resume",8,26,2)
if btnp(0) then
pause_menustate = 3
elseif btnp(1) then
pause_menustate = 1
elseif btnp(4) then
state = in_progress
end
elseif pause_menustate == 1 then
print("Retry",8,34,2)
if btnp(0) then
pause_menustate = 0
elseif btnp(1) then
pause_menustate = 2
elseif btnp(4) then
_init()
state = in_progress
end
elseif pause_menustate == 2 then
print("Main Menu",8,42,2)
if btnp(0) then
pause_menustate = 1
elseif btnp(1) then
pause_menustate = 3
elseif btnp(4) then
_init()
end
elseif pause_menustate == 3 then
print("Exit",8,50,2)
if btnp(0) then
pause_menustate = 2
elseif btnp(1) then
pause_menustate = 0
elseif btnp(4) then
exit()
end
end
if btnp(5) then
state = in_progress
end
end
function move(unit)
unit.sprite =unit.sprite+ 1
if unit.sprite > unit.endsprite then
unit.sprite = unit.startsprite
end
end
function draw_unit(unit) spr(unit.sprite, unit.x, unit.y) end
function get_map_cell(unit) return mget(flr((unit.x+4)/8), flr((unit.y-4)/8)) end
function hit_house(unit) return get_map_cell(unit) == 16 end
function move_unit(unit, direction)
unit.moving = false
if hit_house(unit) then
unit.stuck =unit.stuck+ 1
if unit.stuck > 4 then
unit.stuck = 0
else
return
end
end
if direction == left and
unit.x - unit.speed > 8 then
unit.x =unit.x- unit.speed
unit.moving = true
end
if direction == right and
unit.x + unit.speed < 224 then
unit.x =unit.x+ unit.speed
unit.moving = true
end
if direction == up and
unit.y - unit.speed > 12 then
unit.y =unit.y- unit.speed
unit.moving = true
end
if direction == down and
unit.y + unit.speed < 116 then
unit.y =unit.y+ unit.speed
unit.moving = true
end
if not unit.moving then
unit.sprite = unit.startsprite
else
move(unit)
end
end
function move_player()
for i=1,#valid_moves do
if btn(valid_moves[i]) then
move_unit(player, valid_moves[i])
end
end
end
function move_enemy()
if enemy.x > player.x then
move_unit(enemy, left)
end
if enemy.x < player.x then
move_unit(enemy, right)
end
if enemy.y > player.y then
move_unit(enemy, up)
end
if enemy.y < player.y then
move_unit(enemy, down)
end
enemy.speed =enemy.speed+ enemyspeedincrval
end
function move_enemy2()
if enemy2.x > player.x then
move_unit(enemy2, left)
end
if enemy2.x < player.x then
move_unit(enemy2, right)
end
if enemy2.y > player.y then
move_unit(enemy2, up)
end
if enemy2.y < player.y then
move_unit(enemy2, down)
end
enemy2.speed =enemy2.speed+ enemyspeedincrval
end
function move_enemy3()
if enemy3.x > player.x then
move_unit(enemy3, left)
end
if enemy3.x < player.x then
move_unit(enemy3, right)
end
if enemy3.y > player.y then
move_unit(enemy3, up)
end
if enemy3.y < player.y then
move_unit(enemy3, down)
end
enemy3.speed =enemy3.speed+ enemyspeedincrval
end
function distance(p0, p1)
dx=p0.x-p1.x dy=p0.y-p1.y
return sqrt(dx*dx+dy*dy)
end
function check_game_over()
if invincibleval == 0 then
if enemycountval == 1 then
if distance(enemy,player) < 7
and state ~= game_over
then
state = start_end_game
end
elseif enemycountval == 2 then
if distance(enemy,player) < 7
and state ~= game_over
then
state = start_end_game
elseif distance(enemy2,player) < 7
and state ~= game_over
then
state = start_end_game
end
elseif enemycountval == 3 then
if distance(enemy,player) < 7
and state ~= game_over
then
state = start_end_game
elseif distance(enemy2,player) < 7
and state ~= game_over
then
state = start_end_game
elseif distance(enemy3,player) < 7
and state ~= game_over
then
state = start_end_game
end
end
elseif invincibleval == 1 then
if enemycountval == 1 then
if distance(enemy,player) < 0
and state ~= game_over
then
state = start_end_game
end
elseif enemycountval == 2 then
if distance(enemy,player) < 0
and state ~= game_over
then
state = start_end_game
elseif distance(enemy2,player) < 0
and state ~= game_over
then
state = start_end_game
end
elseif enemycountval == 3 then
if distance(enemy,player) < 0
and state ~= game_over
then
state = start_end_game
elseif distance(enemy2,player) < 0
and state ~= game_over
then
state = start_end_game
elseif distance(enemy3,player) < 0
and state ~= game_over
then
state = start_end_game
end
end
end
end
function _update()
if enemycountval == 1 then
move_player()
move_enemy()
elseif enemycountval == 2 then
move_player()
move_enemy()
move_enemy2()
elseif enemycountval == 3 then
move_player()
move_enemy()
move_enemy2()
move_enemy3()
end
check_game_over()
end
_init()
function TIC()
cls()
if state == menu then
menu()
elseif state == in_progress then
_update()
map(0,0,0,8,16,15)
if enemycountval == 1 then
draw_unit(player)
draw_unit(enemy)
elseif enemycountval == 2 then
draw_unit(player)
draw_unit(enemy)
draw_unit(enemy2)
elseif enemycountval == 3 then
draw_unit(player)
draw_unit(enemy)
draw_unit(enemy2)
draw_unit(enemy3)
end
score =score+ 1
spr(255,191,7)
print("Pause",201,8,12)
print("Score = "..score,8,8,12)
print("ported by nesbox fixed by stevenfelix505",7,124,12)
if btnp(5) then
state = pause
end
elseif state == start_end_game then
sfx(0,40,4,0,soundval)
print("ported by nesbox fixed by stevenfelix505",7,124,12)
state = game_over
elseif state == game_over then
game_over_menu()
elseif state == options then
options()
elseif state == pause then
pause_menu()
end
if soundval == -1 then
soundval = 0
elseif soundval == 16 then
soundval = 15
end
if invincibleval == -1 then
invincibleval = 0
elseif invincibleval == 2 then
invincibleval = 1
end
if enemycountval == 0 then
enemycountval = 1
elseif enemycountval == 4 then
enemycountval = 3
end
if btnp(0) or btnp(1) or btnp(2) or btnp(3) or btnp(4) or btnp(5) or btnp(6) or btnp(7) or btnp(8) then
sfx(0,40,4,0,soundval)
end
end |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Thank you in advance for any help you offer.
Ok, first, I am a newbie programmer trying to go from Pico-8 to TIC-80.
I am trying to take code from one to the other, with no success.
Here is the original code (courtesy of [http://lukemerrett.com/getting-started-with-pico-8/]):
`in_progress = 0
start_end_game = 1
game_over = 2
left=0 right=1 up=2 down=3
valid_moves = {left,right,up,down}
function _init()
player = {}
player.x = flr(rnd(120))
player.y = flr(rnd(114)+8)
player.startsprite = 0
player.endsprite = 1
player.sprite = 0
player.speed = 2
player.stuck = 0
enemy = {}
enemy.x = flr(rnd(120))
enemy.y = flr(rnd(114)+8)
enemy.startsprite = 4
enemy.endsprite = 5
enemy.sprite = 4
enemy.speed = 1
enemy.stuck = 0
state = in_progress
score = 0
end
function move(unit)
unit.sprite += 1
if unit.sprite > unit.endsprite then
unit.sprite = unit.startsprite
end
end
function draw_unit(unit) spr(unit.sprite, unit.x, unit.y) end
function get_map_cell(unit) return mget(flr((unit.x+4)/8), flr((unit.y-4)/8)) end
function hit_house(unit) return get_map_cell(unit) == 16 end
function move_unit(unit, direction)
unit.moving = false
if hit_house(unit) then
unit.stuck += 1
if unit.stuck > 4 then
unit.stuck = 0
else
return
end
end
if direction == left and
unit.x - unit.speed > 0 then
unit.x -= unit.speed
unit.moving = true
end
if direction == right and
unit.x + unit.speed < 120 then
unit.x += unit.speed
unit.moving = true
end
if direction == up and
unit.y - unit.speed > 8 then
unit.y -= unit.speed
unit.moving = true
end
if direction == down and
unit.y + unit.speed < 120 then
unit.y += unit.speed
unit.moving = true
end
if not unit.moving then
unit.sprite = unit.startsprite
else
move(unit)
end
end
function move_player()
for i=1,#valid_moves do
if btn(valid_moves[i]) then
move_unit(player, valid_moves[i])
end
end
end
function move_enemy()
if enemy.x > player.x then
move_unit(enemy, left)
end
if enemy.x < player.x then
move_unit(enemy, right)
end
if enemy.y > player.y then
move_unit(enemy, up)
end
if enemy.y < player.y then
move_unit(enemy, down)
end
enemy.speed += 0.0005
end
function distance(p0, p1)
dx=p0.x-p1.x dy=p0.y-p1.y
return sqrt(dxdx+dydy)
end
function check_game_over()
if
distance(enemy,player) < 7
and state != game_over
then
state = start_end_game
end
end
function _update()
move_player()
move_enemy()
check_game_over()
end
function _draw()
cls()
if state == in_progress then
map(0,0,0,8,16,15)
draw_unit(player)
draw_unit(enemy)
score += 1
print("score: "..score)
elseif state == start_end_game then
sfx(0)
state = game_over
elseif state == game_over then
print("\135 game over \135")
print("your final score was: "..score)
print("press action to try again")
if btn(4) then
_init()
end
end
end`
and here is what I have tried using:
`in_progress = 0
start_end_game = 1
game_over = 2
left=0 right=1 up=2 down=3
valid_moves = {left,right,up,down}
function _init()
player = {}
player.x = flr(rnd(120))
player.y = flr(rnd(114)+8)
player.startsprite = 0
player.endsprite = 1
player.sprite = 0
player.speed = 2
player.stuck = 0
enemy = {}
enemy.x = flr(rnd(120))
enemy.y = flr(rnd(114)+8)
enemy.startsprite = 4
enemy.endsprite = 5
enemy.sprite = 4
enemy.speed = 1
enemy.stuck = 0
state = in_progress
score = 0
end
function move(unit)
unit.sprite = unit.sprite+1
if unit.sprite > unit.endsprite then
unit.sprite = unit.startsprite
end
end
function draw_unit(unit) spr(unit.sprite, unit.x, unit.y) end
function get_map_cell(unit) return mget(flr((unit.x+4)/8), flr((unit.y-4)/8)) end
function hit_house(unit) return get_map_cell(unit) == 16 end
function move_unit(unit, direction)
unit.moving = false
if hit_house(unit) then
unit.stuck = unit.stuck+1
if unit.stuck > 4 then
unit.stuck = 0
else
return
end
end
if direction == left and
unit.x - unit.speed > 0 then
unit.x = unit.x-unit.speed
unit.moving = true
end
if direction == right and
unit.x + unit.speed < 120 then
unit.x = unit.x+unit.speed
unit.moving = true
end
if direction == up and
unit.y - unit.speed > 8 then
unit.y = unit.y-unit.speed
unit.moving = true
end
if direction == down and
unit.y + unit.speed < 120 then
unit.y = unit.y+unit.speed
unit.moving = true
end
if not unit.moving then
unit.sprite = unit.startsprite
else
move(unit)
end
end
function move_player()
for i=1,#valid_moves do
if btn(valid_moves[i]) then
move_unit(player, valid_moves[i])
end
end
end
function move_enemy()
if enemy.x > player.x then
move_unit(enemy, left)
end
if enemy.x < player.x then
move_unit(enemy, right)
end
if enemy.y > player.y then
move_unit(enemy, up)
end
if enemy.y < player.y then
move_unit(enemy, down)
end
enemy.speed = enemy.speed+0.0005
end
function distance(p0, p1)
dx=p0.x-p1.x dy=p0.y-p1.y
return sqrt(dxdx+dydy)
end
function check_game_over()
if
distance(enemy,player) < 7
and state == game_over
then
state = start_end_game
end
end
_init()
function TIC()
move_player()
move_enemy()
check_game_over()
end
function _draw()
cls()
if state == in_progress then
map(0,0,0,8,16,15)
draw_unit(player)
draw_unit(enemy)
score = score+1
print("score: "..score)
elseif state == start_end_game then
sfx(0)
state = game_over
elseif state == game_over then
print("\135 game over \135")
print("your final score was: "..score)
print("press action to try again")
if btn(4) then
_init()
end
end
end`
Beta Was this translation helpful? Give feedback.
All reactions