Random Movement
Blitz3D Forums/Blitz3D Beginners Area/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 in "BlitzPlus?" 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 |
| ||
What I would do is give the character a random destination x,y,z to get to and then once it has arrived change it again. you can then control how long between direction changes it has... otherwise it would look crazy Don't forget to allow for some slight deviation from the destination location when checking it has arrived... I believe 1/2 movement rate is generally assumed. |
| ||
Looking at what you have, it seems to be 2D rather than 3D, the word "EATUM" suggests something PacMan-like, leading me to assume that perhaps your random movement only needs to be fixed to UP, DOWN, LEFT or RIGHT. Is this correct? Either way, there are other consideratioons, such as - what happens if EATUM hits the edge of the screen? Wraparound or does it block them? Im only asking so much so that I can get a better grasp of what you want, so I can write something that will be of more use for you. |
| ||
Looking at what you have, it seems to be 2D rather than 3D, the word "EATUM" suggests something PacMan-like, leading me to assume that perhaps your random movement only needs to be fixed to UP, DOWN, LEFT or RIGHT. Is this correct? Either way, there are other consideratioons, such as - what happens if EATUM hits the edge of the screen? Wraparound or does it block them? Im only asking so much so that I can get a better grasp of what you want, so I can write something that will be of more use for you. Malice yes, you are correct. EATUM is a pac-man-like game. But for now, EATUM is a ball. I shortened my code so that I can learn how to make EATUM do random movements. I shortened it so that there won't be as much to sift through if one could help me. Also yes, I 'm working in BlitzPlus (it seems like not many people respond in the BlitzPlus forum so thus, I posted here). I was trying to get random movement for when EATUM bounces off of the walls or edge of the screen like a ball. Thus, I was wondering what do I put into my code to make that happen? I know for the wall it's probablly ImageRectCollide, but I don't know what to do after that (skully had an idea, and I will try that too). But, I've seen it done with one of the Random commands (RND or RAND I think). By the way, if one could show me how to get random movement with just one wall and one edge of the screen, I'll figure out the rest. I redeveloped my code below to include the walls and I included a screen shot to show what I'm trying to do. I hope this all makes sense. If not tell me. Thanks Here is the screenshot: http://authorhenryemphrey.tripod.com/gallery/ Graphics 900, 900 SetBuffer BackBuffer() ;Loads image EATUM = LoadImage ("Eatum.png") ;Sets image type Type EATUM Field x,y End Type ; Type Rectangle Field x,y,w,h End Type Type Rectangle2 Field x,y,w,h End Type ;Changing EATUM into a variable name player.EATUM = New EATUM player\x = 70 player\y = 200 ;here is the top wall that EATUM will bounce off of Topwall.rectangle = New rectangle Topwall\x = 0 Topwall\y = 0 Topwall\w = 1000 Topwall\h = 50 ;here is the bottom wall that EATUM will bounce off of Bottomwall.rectangle2 = New rectangle2 Bottomwall\x = 0 Bottomwall\y = 780 Bottomwall\w = 1000 Bottomwall\h = 500 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? Rect Topwall\x,Topwall\y,Topwall\w,Topwall\h,1 Rect Bottomwall\x,Bottomwall\y,Bottomwall\w,Bottomwall\h,1 ;what do I put here to get random movement if EATUM bounces off of the 1 wall If ImageRectCollide (EATUM,player\x,player\y,0,Topwall\x,Topwall\y,Topwall\w,Topwall\h) Then EndIf ;same here, what do I put here to get random movement If EATUM bounces off of the 2 wall If ImageRectCollide (EATUM,player\x,player\y,0,Bottomwall\x,Bottomwall\y,Bottomwall\w,Bottomwall\h) Then EndIf ;Draws Eatum onto screen DrawImage (EATUM,player\x,player\y) Flip Wend |
| ||
Add some more Fields to the TYPE EATUM:Type EATUM Field x,y Field DirX, DirY Field TargetX, TargetY End Type Now you can give hime a "job", e.g. walk to a certain point. Choose a point TargetX, TargetY by Random and let him move to this point. When he reaches this point, find again a random new point. You could also choose a players or enemy position instead of a random point. Now the figure seems to have "ai". You can calculate the Direction from Player\X towards Player\TargetX and store this in DirX, DirY. Now follow this plan until he cannot move any more. In this moment you give him a new direction. Function PlayerMoves() If ImageRectCollide (EATUM, Player\x+Player\DirX, Player\y....)=0 Then Player\X=Player\X + Player\DirX .... Else Player\DirX=Rand(-1,1) Player\DirY=Rand(-1,1) ... In my games the figures always get light changes in DirX, DirY (calculated by sin/cos in a range of -5/+5 degrees.) This makes them move not so straight and look a bit individual. |