# Walls for a game?

• ### Question

• I have been working with Small Basic for a few weeks now and I started making a sort of "Maze Game" and to do that I need to make walls, I tried;

If (x>250 and y>250) Then
x = 250
y = 250
EndIf

And that didn't work, I have a good comprehension of SB and am learning new items each day, the code I have for the movement of the ball is:

EDIT: Changed the code for original, you can see it in the replies.

And that's the code I have for movement(With the WASD controls and a small menu screen) So how would I add walls in, would I have to in some way "bind" the wall to a shape so that the ball(Which in there is named "Man") wouldn't go through it?

Thanks!

• Edited by Sunday, December 18, 2011 4:35 AM Changed the code I used at first
Sunday, December 18, 2011 3:11 AM

• I have changed one of your four 'Move' subroutines to achieve what you are attempting and am attaching the code below.   Note that because I have changed the MoveDown I have only protected it from above. Note also that I have allowed the y movement to the left and the right.  There may be more elegent solutions but similar code with the same logic would achieve what you are attempting.

```Sub MoveDown
GraphicsWindow.Title = "x= " + x + ", y= " + y
x = x
y = y+10
If y > 236 And y < 350 And x > 236 And x < 350 Then
y = 236
EndIf
Shapes.Move(Man,x,y)
EndSub```

• Edited by Sunday, December 18, 2011 6:47 AM
• Marked as answer by Sunday, December 18, 2011 8:06 PM
Sunday, December 18, 2011 5:45 AM

### All replies

• Without seeing the rest of the code I am not sure of this, but it appears to me that the reason the If fails may be that two seperate tests and grouped together.   Your code requires BOTH x and y to be > 250.  I suggest that you want to limit x and y independently. If x > 250 then x = 250.  If y > 250 then y = 250. Theses two tests, one after another should work if either or both are true.

Sunday, December 18, 2011 3:56 AM
• Ok I tried that and it didn't seem to work, I don't think I explained myself correctly, I need walls like a square or rectangle that the ball can't go into, I simplified the code as much as I could to get it in here, while doing quiet a few changes just so it's easier to read.

Start()

Sub Start
GraphicsWindow.Show()
GraphicsWindow.Width = "1024"
GraphicsWindow.Height = "576"
GraphicsWindow.Left=0.5*(Desktop.Width-GraphicsWindow.Width)
GraphicsWindow.top=0.5*(Desktop.Height-GraphicsWindow.Height)
GraphicsWindow.KeyDown = Buttons
GraphicsWindow.BrushColor = "Red"
GraphicsWindow.FillRectangle(250,250,100,100)
GraphicsWindow.DrawRectangle(250,250,100,100)

x = 0
y = 0

Shapes.Move(Actor,x,y)

EndSub

Sub Buttons
If (GraphicsWindow.LastKey = "W") Then
MoveUp()
ElseIf (GraphicsWindow.LastKey = "A") Then
MoveLeft()
ElseIf (GraphicsWindow.LastKey = "S") Then
MoveDown()
ElseIf (GraphicsWindow.LastKey = "D") Then
MoveRight()
Elseif (GraphicsWindow.LastKey = "P") Then
EndGameButton()
ElseIf (GraphicsWindow.LastKey = "O") Then
Reset()
EndIf
EndSub

Sub MoveUp
GraphicsWindow.Title = "x= " + x + ", y= " + y
x = x
y = y-10
Shapes.Move(Man,x,y)
EndSub

Sub MoveLeft
GraphicsWindow.Title = "x= " + x + ", y= " + y
x = x-10
y = y
Shapes.Move(Man,x,y)
EndSub

Sub MoveRight
GraphicsWindow.Title = "x= " + x + ", y= " + y
x = x+10
y = y
Shapes.Move(Man,x,y)
EndSub

Sub MoveDown
GraphicsWindow.Title = "x= " + x + ", y= " + y
x = x
y = y+10
Shapes.Move(Man,x,y)
EndSub

Sub EndGameButton
Program.End()
EndSub

Sub Reset
x = 0
y = 0
Shapes.Move(Man,x,y)
GraphicsWindow.Title = "x= " + x + ", y= " + y
EndSub

So for instance, I want the rectangle that's drawn at the start to be so the ball can't run into it, but can go everywhere else on the screen. I had something like

If y<0 Then
y = 0
EndIf

So the ball can't move up and out of the screen. I hope this clarifies what I'm asking?

Sunday, December 18, 2011 4:31 AM
• I have changed one of your four 'Move' subroutines to achieve what you are attempting and am attaching the code below.   Note that because I have changed the MoveDown I have only protected it from above. Note also that I have allowed the y movement to the left and the right.  There may be more elegent solutions but similar code with the same logic would achieve what you are attempting.

```Sub MoveDown
GraphicsWindow.Title = "x= " + x + ", y= " + y
x = x
y = y+10
If y > 236 And y < 350 And x > 236 And x < 350 Then
y = 236
EndIf
Shapes.Move(Man,x,y)
EndSub```

• Edited by Sunday, December 18, 2011 6:47 AM
• Marked as answer by Sunday, December 18, 2011 8:06 PM
Sunday, December 18, 2011 5:45 AM
• Thanks! This is exactly what I was looking for! Thanks Kiwi Bert!
Sunday, December 18, 2011 8:07 PM