 | Distinguished Member with 3,606 posts. | | Join Date: Oct 2008 Location: Near Washington, D.C. Experience: Advanced in Networking | | Solved: Brickwall Game [Help] Hi all,
Last week I followed a guide online to write a Brickwall game. The problem is, the ball will not knock out the bricks, but it should. I did not import the program, but wrote out each line as I saw it, with what I think the only changes being the actual message when the game is over, whether the user wins or not. The game starts off with red bricks, and that is where the problem is, although I don't know if the problem is with yellow/and/or green bricks, since I can't get to that point in the game itself. I have attached the code for it to be looked over to see any things with the code that looks out of place for this problem to occur.
Thanks, Jason Code: 'paddle = GraphicsWindow.AddRectangle(120, 12) 'v0.2
paddle = Shapes.AddRectangle(120, 12) 'v0.3.1
'ball = GraphicsWindow.AddEllipse(16, 16) 'v0.2
ball = Shapes.AddEllipse(16, 16) 'v.0.3.1
bricksLeft = 48
brickStartY = 35
hitcount = 0
GraphicsWindow.FontSize = 14
GraphicsWindow.MouseMove = OnMouseMove
GraphicsWindow.Title = "Brickwall Game"
For idx = 0 To 15
Array.SetValue("Green Bricks", idx, 1)
Array.SetValue("Yellow Bricks", idx, 1)
Array.SetValue("Red Bricks", idx, 1)
EndFor
DrawBricks()
score = 0
PrintScore()
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
y = gh - 28
'GraphicsWindow.MoveShape(ball, x, y) 'v0.2
Shapes.Move(ball,x,y) 'v0.3.1
deltaX = 1
deltaY = -2
Sound.PlayBellRingAndWait()
RunLoop:
x = x + deltaX
y = y + deltaY
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
If (x >= gw - 16 Or x <= 0) Then
deltaX = -deltaX
EndIf
If (y <= 0) Then
deltaY = -deltaY
EndIf
'padX = GraphicsWindow.GetLeftOfShape(paddle) 'v0.2
padX = Shapes.GetLeft(paddle) 'v.0.3.1
If ((y >= gh - 28 + 2) And x >= padX And x <= padX + 120) Then
y = gh - 28 + 2
'Sound.PlayClick()
hitcount = hitcount + 1
If Math.Remainder(hitcount, 3) = 0 Then 'Move brick downwards
For idx = 0 To 15
RemoveGreenBrick()
RemoveYellowBrick()
RemoveRedBrick()
Endfor
brickStartY = brickStartY + 20
DrawBricks()
EndIf
TestRed:
For idx = 0 To 15
If Array.GetValue("RedBricks", idx) = 1 Then
If brickStartY > gh - 160 Then
Goto EndGame
EndIf
EndIf
EndFor
TestYellow:
For idx = 0 To 15
If Array.GetValue("YellowBricks", idx) = 1 Then
If brickStartY > gh - 100 Then
Goto EndGame
EndIf
EndIf
EndFor
TestGreen:
For idx = 0 To 15
If Array.GetValue("GreenBricks", idx) = 1 Then
If brickStartY > gh - 40 Then
Goto EndGame
EndIf
EndIf
EndFor
EndTest:
deltaX = deltaX - 2 + (x -padX) / 30 ' Add some skill
If score = oldScore Then 'No bricks hit
score = score - 1
EndIf
oldScore = score
PrintScore()
deltaY = -deltaY 'Change the ball direction
EndIf
'GraphicsWindow.MoveShape(ball, x, y) 'v0.2
Shapes.Move(ball,x,y) 'v0.3.1
Program.Delay(5)
'Green Bricks
If y > brickStartY - 16 And y < brickstartY + 20 Then 'y position of brick - diameter of ball
idx = (x+8) / 40 ' Radius of ball / length of brick
idx = Math.Floor(idx) 'take integer part
If Array.GetValue("GreenBricks", idx) = 1 Then
Array.SetValue("GreenBricks", idx, 0)
RemoveGreenBrick()
Sound.PlayChime()
bricksLeft = bricksLeft - 1
deltaY = -deltaY 'Change ball direction
score = score + 15
PrintScore()
CheckEnd()
EndIf
EndIf
'Yellow Bricks
If y > brickStartY + 44 And y < brickStartY + 80 Then ' y position of brick - diameter of ball = 19
idx = (x+8) / 40 'Radius of ball / length of brick
idx = Math.Floor(idx) 'take integer part
If Array.GetValue("YellowBricks", idx) = 1 Then
Array.SetValue("YellowBricks", idx, 0)
RemoveYellowBrick()
Sound.PlayChime()
bricksLeft = bricksLeft - 1
deltaY = -deltaY 'Change ball direction
score = score + 10
PrintScore()
CheckEnd()
EndIf
EndIf
'Red Bricks
If y > brickStartY + 104 And y < brickStartY + 140 Then 'y position of brick - diameter of ball = 19
idx = (x+8) / 40 'Radius of ball / length of brick
idx = Math.Floor(idx) 'take integer part
If Array.GetValue("RedBricks", idx) = 1 Then
Array.SetValue("RedBricks", idx, 0)
RemoveRedBrick()
Sound.PlayChime()
bricksLeft = bricksLeft - 1
deltaY = -deltaY ' Change ball direction
score = score + 5
PrintScore()
CheckEnd()
EndIf
EndIf
If (y < gh) Then 'Ball not reached bottom of window
Goto RunLoop
EndIf
EndGame:
GraphicsWindow.ShowMessage("Thanks, for playing Brickwall, and your score is : " + score, "Brickwall Game")
Program.End()
Sub OnMouseMove
paddleX = GraphicsWindow.MouseX
'GraphicsWindow.MoveShape(paddle, paddleX - 60, GraphicsWindow.Height - 12) 'v0.2
Shapes.Move(paddle, paddleX - 60, GraphicsWindow.Height - 12) 'v0.3.1
EndSub
Sub PrintScore
' Clear the score first and then draw the real score text
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(10, 10, 200, 20)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(10, 10, "Your score is: " + score)
EndSub
Sub DrawBricks
For idx = 0 To 15 'Draw bricks
'Program.Delay(100)
If Array.GetValue("GreenBricks", idx) = 1 Then
GraphicsWindow.PenColor = "Black"
GraphicsWindow.BrushColor = "Green"
Else
GraphicsWindow.PenColor = "White"
GraphicsWindow.BrushColor = "White"
EndIf
GraphicsWindow.FillRectangle(idx * 40, brickStartY, 40, 20)
GraphicsWindow.DrawRectangle(idx * 40, brickStartY, 40, 20)
GraphicsWindow.BrushColor = "Yellow"
If Array.GetValue("YellowBricks", idx) = 1 Then
GraphicsWindow.PenColor = "Black"
GraphicsWindow.BrushColor = "Yellow"
Else
GraphicsWindow.PenColor = "White"
GraphicsWindow.BrushColor = "White"
EndIf
GraphicsWindow.FillRectangle(idx * 40, brickStartY + 60, 40, 20)
GraphicsWindow.DrawRectangle(idx * 40, brickStartY + 60, 40, 20)
GraphicsWindow.BrushColor = "Red"
If Array.GetValue("RedBricks", idx) = 1 Then
GraphicsWindow.PenColor = "Black"
GraphicsWindow.BrushColor = "Red"
Else
GraphicsWindow.PenColor = "White"
GraphicsWindow.PenColor = "White"
EndIf
GraphicsWindow.FillRectangle(idx * 40, brickStartY + 120, 40, 20)
GraphicsWindow.DrawRectangle(idx * 40, brickStartY + 120, 40, 20)
EndFor
EndSub
Sub RemoveGreenBrick
__________________ The Lord is my shepherd; I shall not want. …He restores my soul; He leads me in the paths of righteousness. -Psalm 23:1,3 | | Distinguished Member with 3,606 posts. | | Join Date: Oct 2008 Location: Near Washington, D.C. Experience: Advanced in Networking | | | | | Senior Member with 1,497 posts. | | Join Date: Jul 2008 Location: Earth Experience: Advanced - Einstein | | Looking through the code, you do not appear to have a sub for removing the bricks, apart from the declaration of RemoveGreenBrick, are you sure you added all of the code from the tutorial ? | | Distinguished Member with 3,606 posts. | | Join Date: Oct 2008 Location: Near Washington, D.C. Experience: Advanced in Networking | | I'm pretty sure I did, but I think I forgot to attach all of the code. I accidentally chopped off some the last, and I reattached the whole thing. I do have the subs for removing the bricks (near the end), but the bricks still aren't getting taken out. Code: 'paddle = GraphicsWindow.AddRectangle(120, 12) 'v0.2
paddle = Shapes.AddRectangle(120, 12) 'v0.3.1
'ball = GraphicsWindow.AddEllipse(16, 16) 'v0.2
ball = Shapes.AddEllipse(16, 16) 'v.0.3.1
bricksLeft = 48
brickStartY = 35
hitcount = 0
GraphicsWindow.FontSize = 14
GraphicsWindow.MouseMove = OnMouseMove
GraphicsWindow.Title = "Brickwall Game"
For idx = 0 To 15
Array.SetValue("Green Bricks", idx, 1)
Array.SetValue("Yellow Bricks", idx, 1)
Array.SetValue("Red Bricks", idx, 1)
EndFor
DrawBricks()
score = 0
PrintScore()
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
y = gh - 28
'GraphicsWindow.MoveShape(ball, x, y) 'v0.2
Shapes.Move(ball,x,y) 'v0.3.1
deltaX = 1
deltaY = -2
Sound.PlayBellRingAndWait()
RunLoop:
x = x + deltaX
y = y + deltaY
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
If (x >= gw - 16 Or x <= 0) Then
deltaX = -deltaX
EndIf
If (y <= 0) Then
deltaY = -deltaY
EndIf
'padX = GraphicsWindow.GetLeftOfShape(paddle) 'v0.2
padX = Shapes.GetLeft(paddle) 'v.0.3.1
If ((y >= gh - 28 + 2) And x >= padX And x <= padX + 120) Then
y = gh - 28 + 2
'Sound.PlayClick()
hitcount = hitcount + 1
If Math.Remainder(hitcount, 3) = 0 Then 'Move brick downwards
For idx = 0 To 15
RemoveGreenBrick()
RemoveYellowBrick()
RemoveRedBrick()
Endfor
brickStartY = brickStartY + 20
DrawBricks()
EndIf
TestRed:
For idx = 0 To 15
If Array.GetValue("RedBricks", idx) = 1 Then
If brickStartY > gh - 160 Then
Goto EndGame
EndIf
EndIf
EndFor
TestYellow:
For idx = 0 To 15
If Array.GetValue("YellowBricks", idx) = 1 Then
If brickStartY > gh - 100 Then
Goto EndGame
EndIf
EndIf
EndFor
TestGreen:
For idx = 0 To 15
If Array.GetValue("GreenBricks", idx) = 1 Then
If brickStartY > gh - 40 Then
Goto EndGame
EndIf
EndIf
EndFor
EndTest:
deltaX = deltaX - 2 + (x -padX) / 30 ' Add some skill
If score = oldScore Then 'No bricks hit
score = score - 1
EndIf
oldScore = score
PrintScore()
deltaY = -deltaY 'Change the ball direction
EndIf
'GraphicsWindow.MoveShape(ball, x, y) 'v0.2
Shapes.Move(ball,x,y) 'v0.3.1
Program.Delay(5)
'Green Bricks
If y > brickStartY - 16 And y < brickstartY + 20 Then 'y position of brick - diameter of ball
idx = (x+8) / 40 ' Radius of ball / length of brick
idx = Math.Floor(idx) 'take integer part
If Array.GetValue("GreenBricks", idx) = 1 Then
Array.SetValue("GreenBricks", idx, 0)
RemoveGreenBrick()
Sound.PlayChime()
bricksLeft = bricksLeft - 1
deltaY = -deltaY 'Change ball direction
score = score + 15
PrintScore()
CheckEnd()
EndIf
EndIf
'Yellow Bricks
If y > brickStartY + 44 And y < brickStartY + 80 Then ' y position of brick - diameter of ball = 19
idx = (x+8) / 40 'Radius of ball / length of brick
idx = Math.Floor(idx) 'take integer part
If Array.GetValue("YellowBricks", idx) = 1 Then
Array.SetValue("YellowBricks", idx, 0)
RemoveYellowBrick()
Sound.PlayChime()
bricksLeft = bricksLeft - 1
deltaY = -deltaY 'Change ball direction
score = score + 10
PrintScore()
CheckEnd()
EndIf
EndIf
'Red Bricks
If y > brickStartY + 104 And y < brickStartY + 140 Then 'y position of brick - diameter of ball = 19
idx = (x+8) / 40 'Radius of ball / length of brick
idx = Math.Floor(idx) 'take integer part
If Array.GetValue("RedBricks", idx) = 1 Then
Array.SetValue("RedBricks", idx, 0)
RemoveRedBrick()
Sound.PlayChime()
bricksLeft = bricksLeft - 1
deltaY = -deltaY ' Change ball direction
score = score + 5
PrintScore()
CheckEnd()
EndIf
EndIf
If (y < gh) Then 'Ball not reached bottom of window
Goto RunLoop
EndIf
EndGame:
GraphicsWindow.ShowMessage("Thanks, for playing Brickwall, and your score is : " + score, "Brickwall Game")
Program.End()
Sub OnMouseMove
paddleX = GraphicsWindow.MouseX
'GraphicsWindow.MoveShape(paddle, paddleX - 60, GraphicsWindow.Height - 12) 'v0.2
Shapes.Move(paddle, paddleX - 60, GraphicsWindow.Height - 12) 'v0.3.1
EndSub
Sub PrintScore
' Clear the score first and then draw the real score text
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(10, 10, 200, 20)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(10, 10, "Your score is: " + score)
EndSub
Sub DrawBricks
For idx = 0 To 15 'Draw bricks
'Program.Delay(100)
If Array.GetValue("GreenBricks", idx) = 1 Then
GraphicsWindow.PenColor = "Black"
GraphicsWindow.BrushColor = "Green"
Else
GraphicsWindow.PenColor = "White"
GraphicsWindow.BrushColor = "White"
EndIf
GraphicsWindow.FillRectangle(idx * 40, brickStartY, 40, 20)
GraphicsWindow.DrawRectangle(idx * 40, brickStartY, 40, 20)
GraphicsWindow.BrushColor = "Yellow"
If Array.GetValue("YellowBricks", idx) = 1 Then
GraphicsWindow.PenColor = "Black"
GraphicsWindow.BrushColor = "Yellow"
Else
GraphicsWindow.PenColor = "White"
GraphicsWindow.BrushColor = "White"
EndIf
GraphicsWindow.FillRectangle(idx * 40, brickStartY + 60, 40, 20)
GraphicsWindow.DrawRectangle(idx * 40, brickStartY + 60, 40, 20)
GraphicsWindow.BrushColor = "Red"
If Array.GetValue("RedBricks", idx) = 1 Then
GraphicsWindow.PenColor = "Black"
GraphicsWindow.BrushColor = "Red"
Else
GraphicsWindow.PenColor = "White"
GraphicsWindow.PenColor = "White"
EndIf
GraphicsWindow.FillRectangle(idx * 40, brickStartY + 120, 40, 20)
GraphicsWindow.DrawRectangle(idx * 40, brickStartY + 120, 40, 20)
EndFor
EndSub
Sub RemoveGreenBrick
GraphicsWindow.PenColor = "White"
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(idx * 40, brickStartY, 40, 20)
GraphicsWindow.DrawRectangle(idx * 40, brickStartY, 40, 20)
EndSub
Sub RemoveYellowBrick
GraphicsWindow.PenColor = "White"
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(idx * 40, brickStartY + 60, 40, 20)
GraphicsWindow.DrawRectangle(idx * 40, brickStartY + 60, 40, 20)
EndSub
Sub RemoveRedBrick
GraphicsWindow.PenColor = "White"
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(idx * 40, brickStartY + 120, 40, 20)
GraphicsWindow.DrawRectangle(idx * 40, brickStartY + 120, 40, 20)
EndSub
Sub CheckEnd
If bricksLeft = 0 Then
GraphicsWindow.ShowMessage("Congrats, you finished the game! Your score is: " + score, "Brickwall Game")
'Goto GameStart
Program.End()
'Goto EndGame
EndIf
EndSub
__________________ The Lord is my shepherd; I shall not want. …He restores my soul; He leads me in the paths of righteousness. -Psalm 23:1,3 | | Distinguished Member with 3,606 posts. | | Join Date: Oct 2008 Location: Near Washington, D.C. Experience: Advanced in Networking | | | | | Senior Member with 305 posts. | | Join Date: Aug 2007 Location: London, UK Experience: Intermediate | | I could be wrong, but I noted this bit:
For idx = 0 To 15
RemoveGreenBrick()
RemoveYellowBrick()
RemoveRedBrick()
Endfor
but I think they should call 'Green Bricks etc.
The GreenBricks bit does a hit test (i think) and sets the array value to zero (block hit) and then calls removeGreenBrick()
RemoveGreenBrick() itself only paints the block area white at that moment. The window painting stuff relies on the array value being set to zero.So it will be painted white and then repainted red/green/yellow on the next drawBlocks call
what language is that written in by the way? I'm looking at scripting languages atm - seems like a cool one | | Distinguished Member with 3,606 posts. | | Join Date: Oct 2008 Location: Near Washington, D.C. Experience: Advanced in Networking | | Ok, like this? I'm using Small Basic, and it says an unreconized statement encountered.
For idx = 0 To 15
Remove 'Green Bricks()
Remove 'Yellow Bricks()
Remove 'Red Bricks()
Endfor | | Senior Member with 305 posts. | | Join Date: Aug 2007 Location: London, UK Experience: Intermediate | | Sorry, had a look over the syntax for small basic and I thought the comments might be in some way function declarations. in essence: The code above is fine.
The critical section for removing bricks is this bit:
'Green Bricks
If y > brickStartY - 16 And y < brickstartY + 20 Then 'y position of brick - diameter of ball
idx = (x+8) / 40 ' Radius of ball / length of brick
idx = Math.Floor(idx) 'take integer part
If Array.GetValue("GreenBricks", idx) = 1 Then
Array.SetValue("GreenBricks", idx, 0)
RemoveGreenBrick()
Sound.PlayChime()
bricksLeft = bricksLeft - 1
deltaY = -deltaY 'Change ball direction
score = score + 15
PrintScore()
CheckEnd()
EndIf
I've checked it with http://smallbasic.com/program/?QRQ360 and you've coded it the same as there. Looks like a strange bug that'd probably be best to ignore and move on to another project
Found this tutorial on the web though for small basic - looks quite cool - fractals! http://download.microsoft.com/downlo...ll%20Basic.pdf | | Distinguished Member with 3,606 posts. | | Join Date: Oct 2008 Location: Near Washington, D.C. Experience: Advanced in Networking | | | | | Distinguished Member with 3,606 posts. | | Join Date: Oct 2008 Location: Near Washington, D.C. Experience: Advanced in Networking |
09-Jul-2009, 09:16 PM
#10 | Someone on another forum saw a mistake, in the code for setting the value of the bricks. It turned out it was a sequence of things that caused the problem. 
First, for drawing the bricks, Code: Else
GraphicsWindow.PenColor = "White"
GraphicsWindow.PenColor = "White"
EndIf
instead of for the second line GraphicsWindow. BrushColor = "White". Second, upon fixing that mistake, the red bricks were removed completely. Then he noticed that near the beginning, Code: Array.SetValue("Green Bricks", idx, 1)
Array.SetValue("Yellow Bricks", idx, 1)
Array.SetValue("Red Bricks", idx, 1)
Instead of Array.SetValue("Gree nBricks"…
Array.SetValue(Yello wBricks…
Array.SetValue(Re dBricks)…
It was the spacing there that was the problem.
I don't blame you for not finding the mistakes, those were not obvious, and thanks for sticking with me.
__________________ The Lord is my shepherd; I shall not want. …He restores my soul; He leads me in the paths of righteousness. -Psalm 23:1,3
Last edited by Jason08 : 09-Jul-2009 09:20 PM.
Reason: Fixed incorrect grammar and a couple of spelling errors.
|  THIS THREAD HAS EXPIRED.
Are you having the same problem?
We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.
| | |
Smart Search
| Find your solution! | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |  WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
| You Are Using: |
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 04:25 AM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd. | |
|