Small Basic Sokoban

lookdatturtle 1 Reputation point
2021-01-13T08:52:18.107+00:00

Hi, I have to program a game in Small Basic for a school project, I decided to play Sokoban. I get so far that I can move my "player", but I don't know how to program that when I run into a box, it is then moved forward by 1. Would be very happy with a quick response and / or suggestions.

LG Lookdatturtle

GraphicsWindow.Show()
GraphicsWindow.Width=600
GraphicsWindow.Height=600

GraphicsWindow.KeyDown=KeyPressed
PlayerX=200
PlayerY=200

Reihe[1]="222222222222"
Reihe[2]="222222222222"
Reihe[3]="222222222222"
Reihe[4]="221111222222"
Reihe[5]="211001112222"
Reihe[6]="210303011222"
Reihe[7]="210010301222"
Reihe[8]="210020221222"
Reihe[9]="211111111222"
Reihe[10]="222222222222"
Reihe[11]="222222222222"
Reihe[12]="222222222222"

GraphicsWindow.BrushColor="#CD5C5C"

For r=1 to 12
For c=1 to 12
If Text.GetSubText(Reihe[r],c,1)="1" Then
GraphicsWindow.FillRectangle((c*50)-50,(r*50)-50,50,50)
EndIf
EndFor
EndFor

GraphicsWindow.BrushColor="Green"

For r=1 to 12
For c=1 to 12
If Text.GetSubText(Reihe[r],c,1)="2" Then
GraphicsWindow.FillRectangle((c*50)-50,(r*50)-50,50,50)
EndIf
EndFor
EndFor

GraphicsWindow.BrushColor="blue"

For r=1 to 12
For c=1 to 12
If Text.GetSubText(Reihe[r],c,1)="3" Then
GraphicsWindow.FillRectangle((c*50)-50,(r*50)-50,50,50)
EndIf
EndFor
EndFor

DrawPlayer()

Sub DrawPlayer
GraphicsWindow.BrushColor="Red"
GraphicsWindow.FillRectangle(PlayerX,PlayerY,50,50)
EndSub

Sub RemovePlayer
GraphicsWindow.BrushColor="White"
GraphicsWindow.FillRectangle(PlayerX,PlayerY,50,50)
EndSub

Sub KeyPressed
If GraphicsWindow.LastKey="Up" And PlayerY>0 And GraphicsWindow.GetPixel(PlayerX+25,PlayerY-25)<>"#CD5C5C" Then
RemovePlayer()
PlayerY=PlayerY-50
DrawPlayer()
ElseIf GraphicsWindow.LastKey="Down" And PlayerY<550 And GraphicsWindow.GetPixel(PlayerX+25,PlayerY+75)<>"#CD5C5C" Then
RemovePlayer()
PlayerY=PlayerY+50
DrawPlayer()
ElseIf GraphicsWindow.LastKey="Right" And PlayerX<550 And GraphicsWindow.GetPixel(PlayerX+75,PlayerY+25)<>"#CD5C5C" Then
RemovePlayer()
PlayerX=PlayerX+50
DrawPlayer()
ElseIf GraphicsWindow.LastKey="Left" And PlayerX>0 And GraphicsWindow.GetPixel(PlayerX-25,PlayerY+25)<>"#CD5C5C" Then
RemovePlayer()
PlayerX=PlayerX-50
DrawPlayer()
EndIf
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

7 answers

Sort by: Most helpful
  1. WhTurner 1,611 Reputation points
    2021-01-13T12:09:10.127+00:00

    If you move the player, you check for runnng into the wall (#CD5C5C) , but you should after that check if the player moves into a box (blue) and then move the box and the player.
    You then have to check if the box can move onto a free cell. (or do this before moving the player) ;-)


  2. WhTurner 1,611 Reputation points
    2021-01-13T13:21:11.163+00:00

    I did some testing for the keyleft, with the following changed last part of your program:

    .............
    ElseIf GraphicsWindow.LastKey="Left" And PlayerX>0 Then
    aaa=GraphicsWindow.GetPixel(PlayerX-25,PlayerY+25)
    bbb=GraphicsWindow.GetPixel(PlayerX-75,PlayerY+25)
    If GraphicsWindow.GetPixel(PlayerX-25,PlayerY+25)="#000000" Then
    RemovePlayer()
    PlayerX=PlayerX-50
    DrawPlayer()
    EndIf
    If GraphicsWindow.GetPixel(PlayerX-25,PlayerY+25)="#0000FF" and GraphicsWindow.GetPixel(PlayerX-75,PlayerY+25)="#000000" Then
    boxX=PlayerX-50
    boxleft()
    RemovePlayer()
    PlayerX=PlayerX-50
    DrawPlayer()
    ''move box and player
    EndIf
    EndIf
    EndSub
    Sub boxleft
    GraphicsWindow.BrushColor="White"
    GraphicsWindow.FillRectangle(boxX,PlayerY,50,50)
    GraphicsWindow.BrushColor="blue"
    GraphicsWindow.FillRectangle(boxX-50,PlayerY,50,50)
    EndSub

    This works on the first two moves left after one keydown.

    This doesn't cover all of your problem but is a start.

    EDIT" the two lines aaa= ... and bbb=.... are only for debugging purposes


  3. WhTurner 1,611 Reputation points
    2021-01-13T14:18:02.323+00:00

    I pubnlished my version as SBK107 partly working for pushing a box to the left.


  4. WhTurner 1,611 Reputation points
    2021-01-13T14:39:45.897+00:00

    THeree is a problem with the getpixel as you try to move the second block to the left.
    Getpixel retuns #FFFFFF (black) in stead of #000000 (white). I don't know why


  5. WhTurner 1,611 Reputation points
    2021-01-13T16:23:07.24+00:00

    You have to have a test : if cell-to-the-left = white Then move player