Random Movement
BlitzPlus Forums/BlitzPlus Programming/Random Movement
| ||
I haven't posted here in a while. That's because I gotten better (ever since I discovered how to make the rectangle and make it do various things, I've been doing pretty good). Anyway, I was wondering how do I get random movement with my EATUM character? The simple code is below. I would like to know what do I add to the main loop to get EATUM to move randomly. My second question relates to the first. I was wondering, what do I do if I also wanted EATUM to move randomly, stop, and change directions to a random spot, stop and move again to a random spot (like the soldiers do in the game "Metal Gear.)" Thanks Graphics 900, 900 SetBuffer BackBuffer() ;Loads image EATUM = LoadImage ("Eatum.png") ;Sets image type Type EATUM Field x,y End Type ;Changing EATUM into a variable name e.EATUM = New EATUM e\x = 70 e\y = 200 While Not KeyDown (1) Cls ;here is where I'm trying to get EATUM to move around the screen randomly. Thus, what do I do? What do I add here? ;Draws Eatum onto screen DrawImage (EATUM,e\x,e\y) Flip Wend |
| ||
QED e\x=e\x+rand(1,10)-5 ;change x value by -5 to 5 each time e\y=e\y |
| ||
When you say move randomly, are you talking about just buzzing around the screen or with some sort of path? The method TAS suggested will have the guy just buzz around. You could make them walk left for a certain number of fames, then up, then down, in a line. To do that, you give your character a direction variable, and a counter for how many frames they've moved in that direction. Something like this: If dir=1 x=x-1 movecount=movecount-1 ElseIf dir=2 x=x+1 movecount=movecount-1 ElseIf dir=3 y=y-1 movecount=movecount-1 ElseIf dir=4 y=y+1 movecount=movecount-1 EndIf If movecount<=0 dir=rand(1,4) movecount=30 ;reset the move counter to whatever EndIf |
| ||
Not bad, but why not use a random angle and Sin/Cos to have more possible directions? |
| ||
the code would look like this:type eatum field x,y field aim# field xspeed#=[speed]*cos(aim) field yspeed#=[speed]*sin(aim) field movesleft end type img=loadimage("eatum.png") e.eatum=new eatum e\x=[any value] e\y=[any value] e\aim=rnd(0,360) setbuffer backbuffer while not keydown(1) cls drawimage img,e\x,e\y flip e\x=e\x + int(xspeed) e\y=e\y + int(yspeed] if movesleft=0 then movesleft=[maxmoves] e\aim=rnd(0,360) else movesleft=movesleft-1 wend not absolutely sure if it works, but try it, it should =) |