question

Nomestack-4849 avatar image
0 Votes"
Nomestack-4849 asked JanWieldraaijer-1495 edited

Can someone fix this code to me? (Small-Basic)

It's just a player and a block, I'm trying to make the block do what a block would do

 GraphicsWindow.Width = 640
 GraphicsWindow.Height = 480
 GraphicsWindow.PenWidth = 1
 GraphicsWindow.BackgroundColor = "Gray"
 GraphicsWindow.KeyDown = KeyDown
 GraphicsWindow.KeyUp = KeyUp
    
 GraphicsWindow.BrushColor = "LightGreen"
    
 player = Shapes.AddRectangle(32,32)
 Shapes.Move(player,64,64)
 px = 64
 py = 64
 speed = 3
    
 GraphicsWindow.BrushColor = "LightGray"
    
 block = Shapes.AddRectangle(32,32)
 Shapes.Move(block,128,128)
 isBlock[4][4] = "True"
 '128/32=4
 '128/32=4
    
 While 1 = 1
   Program.Delay(10)
   If key["W"] And isBlock[rpx][rpy-1] <> "True" Then
     py = py - speed
   EndIf
   If key["A"] And isBlock[rpx-1][rpy] <> "True" Then
     px = px - speed
   EndIf
   If key["S"] And isBlock[rpx][rpy+1] <> "True" Then
     py = py + speed
   EndIf
   If key["D"] And isBlock[rpx+1][rpy] <> "True" Then
     px = px + speed
   EndIf
   Shapes.Move(player,rpx,py)
   collision()
 EndWhile
    
 Sub collision
   rpx = px / 32
   rpy = py / 32
   rpx = Math.Floor(rpx)
   rpy = Math.Floor(rpy)
 EndSub
    
 Sub KeyDown
   pressed = GraphicsWindow.LastKey
   key[pressed] = "True"
 EndSub
    
 Sub KeyUp
   pressed = GraphicsWindow.LastKey
   key[pressed] = "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.

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

I see one probably typo in your program: in line 38 you have rpx, py which should be px, py . Then the reactions on the x- and y- movement keys have equal steps (speed)

· 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.

Well, it's true I fixed it, but there's another problem, I don't know why but the green square shouldn't cross the gray square but it crosses anyway, how do I fix it?

0 Votes 0 ·
JanWieldraaijer-1495 avatar image
0 Votes"
JanWieldraaijer-1495 answered JanWieldraaijer-1495 edited

You have to realyze that the px,py of the player is the top-left corner. If your player comes from thre left you have a collision when px is less then 32px from the block. I have made some additions to your program to see what happens The px rpx ... are printed to the TextWindow Move the window to the side to click in the GraphicsWindow.

 GraphicsWindow.Width = 640
 GraphicsWindow.Height = 480
 GraphicsWindow.PenWidth = 1
 GraphicsWindow.BackgroundColor = "Gray"
    
 GraphicsWindow.KeyDown = KeyDown
 GraphicsWindow.KeyUp = KeyUp
    
 GraphicsWindow.BrushColor="black"
 for x=0 to 500 step 32
   GraphicsWindow.DrawLine(x,0,x,500)
 EndFor
 for y=0 to 500 step 32
   GraphicsWindow.DrawLine(0,y,500,y)
   GraphicsWindow.DrawText(510,y-10,y)
   GraphicsWindow.DrawText(490,y-20,y/32-1)
 EndFor
        
    
 GraphicsWindow.BrushColor = "LightGreen"
    
 player = Shapes.AddRectangle(32,32)
 Shapes.Move(player,64,64)
 px = 64
 py = 64
 speed = 4    '3>>4
    
 GraphicsWindow.BrushColor = "LightGray"
    
 block = Shapes.AddRectangle(32,32)
 Shapes.Move(block,128,160)
 isBlock[4][4] = "True"
 '128/32=4
 '128/32=4
    
 While 1 = 1
   Program.Delay(50)
   If key["W"] And isBlock[rpx][rpy-1] <> "True" Then
     py = py - speed
     TextWindow.WriteLine(px+":"+py+" W "+rpx+":"+rpy)
   EndIf
   If key["A"] And isBlock[rpx-1][rpy] <> "True" Then
     px = px - speed
     TextWindow.WriteLine(px+":"+py+" A "+rpx+":"+rpy)
   EndIf
   If key["S"] And isBlock[rpx][rpy] <> "True" Then
     py = py + speed
     TextWindow.WriteLine(px+":"+py+" S "+rpx+":"+rpy)
   EndIf
   If key["D"] And isBlock[rpx+1][rpy] <> "True" Then
     px = px + speed
     TextWindow.WriteLine(px+":"+py+" D "+rpx+":"+rpy)
   EndIf
   Shapes.Move(player,px,py)    ''rpx>>px
   collision()
 EndWhile
    
 Sub collision
   rpx = px / 32
   rpy = py / 32
   rpx = Math.Floor(rpx)
   rpy = Math.Floor(rpy)
 EndSub
    
 Sub KeyDown
   pressed = GraphicsWindow.LastKey
   key[pressed] = "True"
 EndSub
    
 Sub KeyUp
   pressed = GraphicsWindow.LastKey
   key[pressed] = "False"
 EndSub
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.