How do I do a collision to be dead by an enemy in the run tutorial
I have been doing this for hours, and nothing worked. does anyone how to do that. please help me :(
I have been doing this for hours, and nothing worked. does anyone how to do that. please help me :(
here is the game that is based on the run tutorial and I added a monster, and I am trying to give a collision for the player, does any one know how? https://microstudio.io/i/Tiberius/babyhollowrun/
collide_AABB = function(r1,r2)
return r1.x - r1.width/2 < r2.x + r2.width/2 and
r1.x + r1.width/2 > r2.x - r2.width/2 and
r1.y- r1.height/2 < r2.y + r2.height/2 and
r1.y+ r1.height/2 > r2.y - r2.height/2
end
init = function()
gameover = 0
running = 0
blades = [spawn_blade(200), spawn_blade(400), spawn_blade(600), spawn_blade(800), spawn_blade(1000)]
passed = [0,0,0,0,0]
score = 0
position = 0
speed = 2
dead = false
camera = object
x = 0
y = 0
update = function( target )
this.x = target.x
this.y = target.y
end
end
monster = object
draw_x = 250
draw_y = 0
x = 250
y = 0
speed = 5
width = 10
height = 10
spawn = function( pos )
this.x = pos
this.start_pos = pos
this.y = randRange(0, -25)
end
draw = function( camera )
screen.drawSprite("monster", this.x - camera.x, this.y - camera.y, 32)
screen.drawRect( this.x - camera.x, this.y - camera.y, this.width, this.height, "red" )
end
update = function()
this.x -= this.speed
this.draw_x -= this.speed
if this.x - this.start_pos < -500 then
this.spawn( hero.x + 250 )
end
end
end
hero = object
x = -80
y = -50
ground_y = -50
vy = 0
dead = false
width = 20
height = 20
speed = 2
jumping_now = false
jump = function()
if not this.jumping_now then
this.jumping_now = true
this.vy = 7
end
end
draw = function( camera )
if this.dead == true then
screen.drawSprite("hero_dead", this.x - camera.x, this.y - camera.y, 20)
else
screen.drawSprite("hero", this.x - camera.x, this.y - camera.y, 20)
screen.drawRect( this.x - camera.x, this.y - camera.y, this.width, this.height, "red" )
end
end
update = function()
if ( touch.touching or gamepad.A or keyboard.SPACE ) and ( not this.jumping_now ) then
local music = audio.playMusic("retro8bithappyvideogamemusic24", 1, 1)
this.jump()
audio.beep("square tempo 20000 volume 10 span 100 C4 to C6")
end
this.x += this.speed
this.vy -= 0.3
this.y = max(this.ground_y, this.y+this.vy)
if this.ground_y == floor( this.y ) then
this.jumping_now = false
end
this.speed += 0.001
end
end
end
randRange = function(low, high)
return (high - low) * random.next() + low
end
test_passed = function( target, blade )
if target.x > blade.x + 20 and (not blade.passed ) then
score += 1
blade.passed = true
audio.beep("saw tempo 960 volume 50 span 20 C6")
end
end
spawn_blade = function( position )
return object
x = position+random.next()*200
y = -50
width = 5
height = 15
draw_width = 20
draw_height = 20
passed = false
end
end
update = function()
if running == 1 then
monster.update()
if collide_AABB( hero, monster ) then
gameover = 1
running = 0
print("colide")
end
end
if gameover>0 then
gameover = gameover+1
if gameover>300 then init() end
elsif running then
hero.update()
camera.x = hero.x + 80
position = hero.x
for i=0 to blades.length-1
if blades[i].x<camera.x-120 then
blades[i] = spawn_blade( camera.x + 80)
end
if collide_AABB( hero, blades[i] ) then
running = 0
gameover = 1
hero.dead = true
audio.beep("saw tempo 10000 volume 50 span 50 C2 to C4 to C2 to C4")
else
test_passed( hero, blades[i] )
end
end
else
if touch.touching then running = 1 end
if keyboard.SPACE then running = 1 end
if gamepad.A then running = 1 end
end
end
draw = function()
screen.fillRect(0,0,screen.width,screen.height,"rgb(57,0,57)")
screen.drawMap( "runmap", 0, 0, 520, 200 )
screen.drawSprite( "sun", 119.5, 80, 32, 32 )
screen.drawText(score,120,80,20,"rgb(98,0,147)")
for i=-6 to 6 by 1
screen.drawSprite("wall",i*40-position%40,-80,40)
end
for i=0 to blades.length-1
screen.drawSprite("blade",blades[i].x - camera.x, blades[i].y - camera.y, blades[i].draw_width, blades[i].draw_height,20)
screen.drawRect( blades[i].x - camera.x, blades[i].y - camera.y, blades[i].width, blades[i].height, "green" )
end
hero.draw(camera)
monster.draw(camera)
screen.drawText(score,120,80,20,"#FFF")
if gameover then
screen.fillRect(0,0,screen.width,screen.height,"rgba(255,0,0,.5)")
screen.drawText("GAME OVER",0,0,50,"#FFF")
elsif not running then
screen.drawText("READY?",0,30,50,"#FFF")
end
end
MicroScript has a cool feature called screen.setTranslation
. Try using it - it will make writing a game easier.
Try to organize your code into classes
and objects
.
WOW! thank you. :)
but there is a problem, my character is infinitely jumps if you press the buttons to jump, can you fix please.
Oh ever mind, thank you for fixing it. you man. :)
I am sorry to say but you have made some more problems, the score does not show when you pass the enemy or the spikes. And the speed does not change when pass, and it starts too slow. I am sorry, I know you are trying your very best. :) I hope I can cheer you up.
Please, I love this game. π₯Ίππ
THANK YOU, THANK YOU!!! Loginus, thank you doing this, I hope It was okay for you to do this. :)
You rock, you have teached me many things. And made my first game even cooler and better, I wish I could return the favor, just tell me if you want to. π