[Solved]Help With Simple AI code
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- LÖVEYoungGunnaTeam
- Prole
- Posts: 12
- Joined: Sun Jul 14, 2013 3:22 am
- Location: Mars (The Darker Side)
- Contact:
[Solved]Help With Simple AI code
I need help to get the Hamster in the .love file to freely move from right to left and back. I have been trying for a few days now and thought the forums could help me out. I don't need anything fancy, but if its needed, please do explain. (All of us have a different coding style.) Thanks guys!! ~YoungGunna
- Attachments
-
- Game.love
- Here is da love!
- (11.73 KiB) Downloaded 104 times
Last edited by LÖVEYoungGunnaTeam on Sun Sep 29, 2013 2:45 am, edited 1 time in total.
Re: Help With Simple AI code
I'm trying to decide if you're trying to get it to move back once it hits the side or if you're trying to get it to be randomized. I decided to do both. I have comments in the code as well as various other small changes.
- Attachments
-
- Game_2.love
- Random
- (25.68 KiB) Downloaded 110 times
-
- Game_1.love
- Go to the screen
- (25.69 KiB) Downloaded 105 times
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: Help With Simple AI code
Well, I am not sure about your intent, but from your previous code, it looks like you are trying to make the ball move left for 5 seconds, then right, and so on.
Try this. I am using a variable named "dir", which tells in which direction the ball should move. "-1" for left, "1" for right.
At start, direction is right (dir = 1).
In love.update, timer is updated using dt.
Each 5 seconds, direction is inverted (multiplying dir by -1) and timer is reset to 0, to be ready to count for 5 seconds again.
Position is updated adding each frame speed * dt * dir. Note that, when dir is positive, x will increase (as speed * dt * dir is positive), then the ball goes right. When dir is negative, x will decrease (provided that speed * dt * dir is negative), then the ball goes left.
PS:love.draw remains the same.
EDIT: davidsdude was faster.
Try this. I am using a variable named "dir", which tells in which direction the ball should move. "-1" for left, "1" for right.
At start, direction is right (dir = 1).
In love.update, timer is updated using dt.
Each 5 seconds, direction is inverted (multiplying dir by -1) and timer is reset to 0, to be ready to count for 5 seconds again.
Position is updated adding each frame speed * dt * dir. Note that, when dir is positive, x will increase (as speed * dt * dir is positive), then the ball goes right. When dir is negative, x will decrease (provided that speed * dt * dir is negative), then the ball goes left.
PS:love.draw remains the same.
Code: Select all
function love.load()
cpu = love.graphics.newImage("hamster.png")
x, y, timer, speed, dir = 0, 300, 0, 100, 1
end
function love.update(dt)
timer = timer + dt
if timer >= 5 then
dir = dir * -1
timer = 0
end
x = x + (speed * dt * dir)
end
Re: Help With Simple AI code
I may have been faster, but I believe you were more correct. I think that was their original intent, I just sort of forgot...
So how 'bout it's a draw.
So how 'bout it's a draw.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
- LÖVEYoungGunnaTeam
- Prole
- Posts: 12
- Joined: Sun Jul 14, 2013 3:22 am
- Location: Mars (The Darker Side)
- Contact:
Re: Help With Simple AI code
Both of you helped out, and i wasn't very clear my intent. Thank you both! I like the option of the randomized movement, but the whole timed movement was what i was trying to go for (and failed.) This was for a game i'm going to make, but needed help. (One man coding isn't fun.) Anyway, thanks guys!!
Last edited by LÖVEYoungGunnaTeam on Sat Sep 28, 2013 4:09 am, edited 1 time in total.
Re: Help With Simple AI code
No problem! Glad we could help!
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests