question

Nomestack-4849 avatar image
0 Votes"
Nomestack-4849 asked Nomestack-4849 commented

Can someone send me a "Block behaviour" code

Can someone send me a code for a block / shape that you cannot pass throught but the movement of the player needs to be like this :

 While 1 = 1
 Program.Delay(10)
 playerBehaviour()
 EndWhile
    
 Sub playerBehaviour
   If key[input_upKey] Then
     player_y = player_y - player_speed
     Shapes.Move(gamePlayer,player_x,player_y)
   EndIf
   If key[input_downKey] Then
     player_y = player_y + player_speed
     Shapes.Move(gamePlayer,player_x,player_y)
   EndIf
   If key[input_leftkey] And canMoveLeft Then
     player_x = player_x - player_speed
     Shapes.Move(gamePlayer,player_x,player_y)
   EndIf
   If key[input_rightkey] And canMoveRight Then
     player_x = player_x + player_speed
     Shapes.Move(gamePlayer,player_x,player_y)
   EndIf
 EndSub
    
 Sub keyDown
   k = GraphicsWindow.LastKey
   key[k] = "True"
 EndSub
    
 Sub keyUp
   k = GraphicsWindow.LastKey
   key[k] = "False"
 EndSub


small-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

KristianVirtanen-7032 avatar image
0 Votes"
KristianVirtanen-7032 answered

Hi Nomestack.

First of all, you need to keep a track for each shape location. Then you can detect possible collisions. Here is main principle.

object1X = 100
object1Y = 100
Object2X = 200
Object2Y = 200

Of course, you can turn those as arrays, but lets go like this for example.

Formula itself is easy, you may study it here https://wumbo.net/formula/distance-between-two-points-2d/

Calculate distance between your player and each object (shape) you have. If distance gets too small, dont allow movement.
You propably keep track of x & y like PlayerX & PlayerY, dont change them before you have checked the collision.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

KristianVirtanen-7032 avatar image
0 Votes"
KristianVirtanen-7032 answered Nomestack-4849 commented

Hi again,

if struglin with maths, then this LitDev function comes handy ;)
http://litdev.co.uk/LitDev.html#LDMathConvert2Radial

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ok, but you can send me an example code (one that has an impassable block using this method)

0 Votes 0 ·
JanWieldraaijer-1495 avatar image
0 Votes"
JanWieldraaijer-1495 answered Nomestack-4849 commented

Did you see my solution on the problem in tour posting: https://docs.microsoft.com/answers/questions/334628/a-problem-in-my-game.html ?
See the image, the player can move around the obstacle or through the gap but not over the obstracle.85784-maze.png



maze.png (1.7 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

yes i managed to create a collision in that game, but if i want another type of movement, like in the game 'sidescroller', if i try to use the movement of the sidescroller game with this collision it will not work, how can i solve this?

0 Votes 0 ·