; Revise collision detection to reflect across Y-axis ; Project: Project C - Modified Textanoid ; Written by: Ian Stait-Gardner ; Initialize Graphics Graphics 800,600,0,2 ; CONST Const ESCKEY = 1 Const LEFTKEY = 203 Const RIGHTKEY = 205 Const PADDLEHEIGHT = 10 Const PADDLEWIDTH = 60 Const PADDLEY = 510 Const SCOLEVX = 20 Const LEVELY = 540 Const SCOREY = 560 Const NUMROWS = 10 Const NUMCOLS = 16 Const BLOCKWIDTH = 50 Const BLOCKHEIGHT = 15 Const BALLWIDTH = 15 ; TYPE Type Ball Field X Field Y Field DirX Field DirY End Type ; GLOBAL Global PBall.ball = New Ball Global PaddleX = 370 Global Score = 0 Global Level = 0 Global BlockCount = 0 ; Create Images Global imgBlocks = CreateImage(BLOCKWIDTH, BLOCKHEIGHT, 3) Global imgBall = CreateImage(BALLWIDTH,BALLWIDTH) Global imgPaddle = CreateImage(PADDLEWIDTH, PADDLEHEIGHT) SetBuffer ImageBuffer(imgBlocks, 0) Color 255, 0, 0 Rect 0, 0, BLOCKWIDTH, BLOCKHEIGHT, 0 SetBuffer ImageBuffer(imgBlocks, 1) Color 0, 255, 0 Rect 0, 0, BLOCKWIDTH, BLOCKHEIGHT, 0 SetBuffer ImageBuffer(imgBlocks, 2) Color 0, 0, 255 Rect 0, 0, BLOCKWIDTH, BLOCKHEIGHT, 0 SetBuffer ImageBuffer(imgBall) Oval 0, 0, BALLWIDTH, BALLWIDTH, 0 SetBuffer ImageBuffer(imgPaddle) Rect 0, 0, PADDLEWIDTH, PADDLEHEIGHT, 0 ; Initialize Variables Dim Blocks(NUMROWS, NUMCOLS) NewLevel() ; Main Game Loop While Not KeyDown(ESCKEY) Cls If KeyDown(LEFTKEY) Then PaddleX = PaddleX - 8 If PaddleX <= 0 Then PaddleX = 0 Else If KeyDown(RIGHTKEY) Then PaddleX = PaddleX + 8 If PaddleX >= 800 - PADDLEWIDTH Then PaddleX = 800 - PADDLEWIDTH End If If PBall\DirX = 1 Then PBall\x = PBall\x + 5 Else PBall\x = PBall\x - 5 If PBall\DirY = 1 Then PBall\y = PBall\y + 5 Else PBall\y = PBall\y - 5 SetBuffer BackBuffer() CheckCollide() DrawBlocks() DrawPaddle() DrawHUD() Flip Wend End ; Check Ball/Object Collision Function CheckCollide() ; Ball-Paddle Collision If PBall\x >= PaddleX And PBall\x <= PaddleX + PADDLEWIDTH And PBall\y >= PADDLEY - BALLWIDTH Then PBall\DirY = 0 ; Ball-Block Collision For I = 0 To (NUMROWS - 1) For Q = 0 To (NUMCOLS - 1) If Blocks(I,Q) > 0 Then ; ; Section due for revision based on inproper function ; There is now horizontal deflection from block collision ; If ImagesCollide(imgBall, PBall\x, PBall\y, 0, imgBlocks, (Q * BLOCKWIDTH), (I * BLOCKHEIGHT) + 30, Blocks(I,Q) - 1) Then Blocks(I,Q) = Blocks(I,Q) - 1 If Blocks(I,Q) < 1 Then Score = Score + 50 BlockCount = BlockCount - 1 End If If BlockCount <= 0 Then EndGame(1) BlockX = Q * BLOCKWIDTH BlockY = I * BLOCKHEIGHT If PBall\DirY = 0 Then PBall\DirY = 1 Else PBall\DirY = 0 If PBall\x + BALLWIDTH >= BlockX And PBall\x <= BlockX + BLOCKWIDTH And PBall\y + BALLWIDTH >= BlockY And PBall\y <= BlockY + BLOCKHEIGHT Then If PBall\DirX = 1 Then If PBall\x + BALLWIDTH >= BlockX And PBall\x + BALLWIDTH <= BlockX + 10 Then PBall\DirX = 0 Else If PBall\x <= BlockX + BLOCKWIDTH And PBall\x >= BlockX + BLOCKWIDTH - 10 Then PBall\DirX = 1 End If End If Return End If End If Next Next ; Ball-Ege Collision If PBall\x <= 0 Then PBall\x = 0 PBall\DirX = 1 Else If PBall\x >= 800 - BALLWIDTH PBall\x = 800 - BALLWIDTH PBall\DirX = 0 End If If PBall\y <= 0 Then PBall\y = 0 PBall\DirY = 1 Else If PBall\y >= PADDLEY + PADDLEHEIGHT Then ;EndGame(0) PBall\DirY = 0 End If End Function ; Draw HUD Function DrawHUD() Text SCOLEVX, LEVELY, "Level: " + Level Text SCOLEVX, SCOREY, "Score: " + Score End Function ; Draw Blocks Function DrawBlocks() For I = 0 To (NUMROWS - 1) For Q = 0 To (NUMCOLS - 1) If Blocks(I,Q) > 0 Then DrawImage imgBlocks, (Q * BLOCKWIDTH), (I * BLOCKHEIGHT) + 30, Blocks(I,Q) - 1 End If Next Next End Function ; Draw Ball and Paddle Function DrawPaddle() DrawImage imgBall, PBall\x, PBall\y Rect PaddleX, PADDLEY, PADDLEWIDTH, PADDLEHEIGHT, 0 End Function Function EndGame(WinLose) Cls If WinLose = 0 Then Text 300,300, "You Lose! Hit ESC to quit" Flip Repeat Until KeyDown(ESCKEY) Else Text 300, 300, "Congratulations! New Level in 5 seconds... Flip Delay 5000 NewLevel() Return End If End Function Function NewLevel() Level = Level + 1 For I = 0 To (NUMROWS - 1) For Q = 0 To (NUMCOLS - 1) Blocks(I,Q) = Rand(1,3) BlockCount = BlockCount + 1 Next Next PBall\x = 385 PBall\Y = 450 PBall\DirX = Rnd(0,1) PBall\DirY = 0 End Function