Can someone send me a "Block behaviour" code

Nomestack 61 Reputation points
2021-04-07T15:32:45.867+00:00

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
Small BASIC
A programming language created by Microsoft that serves a stepping stone for beginners from block-based coding languages to more complex text-based languages.
277 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Kristian Virtanen 126 Reputation points
    2021-04-07T16:22:40.887+00:00

    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.

    0 comments No comments

  2. Kristian Virtanen 126 Reputation points
    2021-04-07T16:35:25.463+00:00

    Hi again,

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


  3. WhTurner 1,611 Reputation points
    2021-04-08T11:21:02.04+00:00

    Did you see my solution on the problem in tour posting: https://learn.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