Small Basic > Curriculum > Online > Lesson 4.4: Advanced Games
In this lesson, you will learn how to:
Congratulations! You are now well acquainted with programming fundamentals and advanced concepts of Small Basic.
You have learned to use basic programming concepts in Small Basic. You have also been introduced to Small Basic objects and advanced concepts.
Let’s see how we can use all these concepts to create advanced games.
You are probably familiar with the popular Tic-Tac-Toe game. Let’s see how we can create our own version of this game.
The user and the computer try to win the game by placing Xs or Os in a horizontal, vertical, or diagonal row before the other player does.
Notice how you use the Shapes object to draw various game elements. You use mouse events to enable the user to place Xs in the graphics window.
So how do you play this game?
Steps to play the game:
Now let’s understand the code for the game in detail…
' Copyright (c) Microsoft Corporation. All rights reserved. GraphicsWindow.Hide() gw = 350 gh = 350 gridX = 25 gridY = 25 cellSize = 100 GraphicsWindow.CanResize = "False" GraphicsWindow.Width = gw GraphicsWindow.Height = gh GraphicsWindow.Top = (Desktop.Height - gh) / 2 GraphicsWindow.Left = (Desktop.Width - gw) / 2 GraphicsWindow.Title = "Tic-Tac-Toe" GraphicsWindow.Show() playerTurn = "True" GraphicsWindow.MouseMove = OnMouseMove GraphicsWindow.MouseDown = OnMouseDown SetupBoard() gameLoop: CheckGameOver() If gameOver = "True" Then Goto endGame EndIf If playerTurn = "True" Then Program.Delay(1000) Goto gameLoop EndIf TryWin() If compRow <> -1 And compCol <> -1 Then PlayComputerTurn() Goto gameCheck EndIf TryBlock() If compRow <> -1 And compCol <> -1 Then PlayComputerTurn() Goto gameCheck EndIf ' Play Center If cells[1][1] = "" Then compRow = 1 compCol = 1 PlayComputerTurn() Goto gameCheck EndIf ' Pick a corner PickCorner() If compRow <> -1 And compCol <> -1 Then PlayComputerTurn() Goto gameCheck EndIf ' Pick any empty cell PickAny() If compRow <> -1 And compCol <> -1 Then PlayComputerTurn() Goto gameCheck EndIf gameCheck: CheckGameOver() If gameOver <> "True" Then Goto gameLoop EndIf endGame: If playerWins = "True" Then GraphicsWindow.ShowMessage("You win! Nice work.", "Game Over") Program.End() ElseIf computerWins = "True" Then GraphicsWindow.ShowMessage("Computer wins! Better luck next time.", "Game Over") Program.End() Else GraphicsWindow.ShowMessage("Tie game!", "Game Over") Program.End() EndIf Sub TryWin compRow = -1 compCol = -1 For i = 0 to 2 If cells[i][0] = "O" And cells[i][1] = "O" And cells[i][2] = "" Then compRow = i compCol = 2 ElseIf cells[i][0] = "O" And cells[i][1] = "" And cells[i][2] = "O" Then compRow = i compCol = 1 ElseIf cells[i][0] = "" And cells[i][1] = "O" And cells[i][2] = "O" Then compRow = i compCol = 0 ElseIf cells[0][i] = "O" And cells[1][i] = "O" And cells[2][i] = "" Then compCol = i compRow = 2 ElseIf cells[0][i] = "O" And cells[1][i] = "" And cells[2][i] = "O" Then compCol = i compRow = 1 ElseIf cells[0][i] = "" And cells[1][i] = "O" And cells[2][i] = "O" Then compCol = i compRow = 0 EndIf EndFor If cells[0][0] = "O" And cells[1][1] = "O" And cells[2][2] = "" Then compCol = 2 compRow = 2 ElseIf cells[0][0] = "O" And cells[1][1] = "" And cells[2][2] = "O" Then compCol = 1 compRow = 1 ElseIf cells[0][0] = "" And cells[1][1] = "O" And cells[2][2] = "O" Then compCol = 0 compRow = 0 EndIf EndSub Sub TryBlock compRow = -1 compCol = -1 For i = 0 to 2 If cells[i][0] = "X" And cells[i][1] = "X" And cells[i][2] = "" Then compRow = i compCol = 2 ElseIf cells[i][0] = "X" And cells[i][1] = "" And cells[i][2] = "X" Then compRow = i compCol = 1 ElseIf cells[i][0] = "" And cells[i][1] = "X" And cells[i][2] = "X" Then compRow = i compCol = 0 ElseIf cells[0][i] = "X" And cells[1][i] = "X" And cells[2][i] = "" Then compCol = i compRow = 2 ElseIf cells[0][i] = "X" And cells[1][i] = "" And cells[2][i] = "X" Then compCol = i compRow = 1 ElseIf cells[0][i] = "" And cells[1][i] = "X" And cells[2][i] = "X" Then compCol = i compRow = 0 EndIf EndFor If cells[0][0] = "X" And cells[1][1] = "X" And cells[2][2] = "" Then compCol = 2 compRow = 2 ElseIf cells[0][0] = "X" And cells[1][1] = "" And cells[2][2] = "X" Then compCol = 1 compRow = 1 ElseIf cells[0][0] = "" And cells[1][1] = "X" And cells[2][2] = "X" Then compCol = 0 compRow = 0 EndIf EndSub Sub CheckGameOver For i = 0 to 2 If cells[i][0] = "X" And cells[i][1] = "X" And cells[i][2] = "X" Then playerWins = "True" gameOver = "True" ElseIf cells[i][0] = "O" And cells[i][1] = "O" And cells[i][2] = "O" Then computerWins = "True" gameOver = "True" ElseIf cells[0][i] = "X" And cells[1][i] = "X" And cells[2][i] = "X" Then playerWins = "True" gameOver = "True" ElseIf cells[0][i] = "O" And cells[1][i] = "O" And cells[2][i] = "O" Then computerWins = "True" gameOver = "True" EndIf EndFor If cells[0][0] = "X" And cells[1][1] = "X" And cells[2][2] = "X" Then playerWins = "True" gameOver = "True" ElseIf cells[0][0] = "O" And cells[1][1] = "O" And cells[2][2] = "O" Then computerWins = "True" gameOver = "True" ElseIf cells[0][2] = "O" And cells[1][1] = "O" And cells[2][0] = "O" Then computerWins = "True" gameOver = "True" ElseIf cells[0][2] = "X" And cells[1][1] = "X" And cells[2][0] = "X" Then playerWins = "True" gameOver = "True" EndIf If gameOver <> "True" Then gameOver = "True" For i = 0 to 2 For j = 0 To 2 If cells[i][j] = "" Then gameOver = "False" EndIf EndFor EndFor EndIf EndSub Sub PickCorner If cells[0][0] = "X" And cells[2][2] = "" Then compRow = 2 compCol = 2 ElseIf cells[0][2] = "X" And cells[2][0] = "" Then compRow = 2 compCol = 0 ElseIf cells[2][0] = "X" And cells[0][2] = "" Then compRow = 0 compCol = 2 ElseIf cells[2][2] = "X" And cells[0][0] = "" Then compRow = 2 compCol = 2 ElseIf cells[0][0] = "" Then compRow = 0 compCol = 0 ElseIf cells[0][2] = "" Then compRow = 0 compCol = 2 ElseIf cells[2][0] = "" Then compRow = 2 compCol = 0 ElseIf cells[2][2] = "" Then compRow = 2 compCol = 2 EndIf EndSub Sub PickAny For i = 0 to 2 For j = 0 To 2 If cells[i][j] = "" Then compRow = i compCol = j Goto endPickAny EndIf EndFor EndFor endPickAny: EndSub Sub PlayComputerTurn r = compRow c = compCol cells[r][c] = "O" GraphicsWindow.BrushColor = "Black" GraphicsWindow.FontSize = 40 GraphicsWindow.FontBold = "False" cellText = Shapes.AddText("O") Shapes.Move(cellText, c * cellSize + gridX + 40, r * cellSize + gridY + 26) playerTurn = "True" EndSub Sub OnMouseDown If playerTurn = "True" Then mx = GraphicsWindow.MouseX my = GraphicsWindow.MouseY If mx > gridX And my > gridY And mx < (gridX + 3 * cellSize) And my < (gridY + 3 * cellSize) Then c = Math.Floor((mx - gridX) / cellSize) r = Math.Floor((my - gridY) / cellSize) If cells[r][c] = "" Then cells[r][c] = "X" GraphicsWindow.BrushColor = "Black" GraphicsWindow.FontSize = 40 GraphicsWindow.FontBold = "False" cellText = Shapes.AddText("X") Shapes.Move(cellText, c * cellSize + gridX + 40, r * cellSize + gridY + 26) playerTurn = "False" EndIf EndIf EndIf EndSub Sub OnMouseMove If playerTurn = "True" Then hover = "False" DrawCell() mx = GraphicsWindow.MouseX my = GraphicsWindow.MouseY If mx > gridX And my > gridY And mx < (gridX + 3 * cellSize) And my < (gridY + 3 * cellSize) Then col = Math.Floor((mx - gridX) / cellSize) row = Math.Floor((my - gridY) / cellSize) hover = "True" DrawCell() EndIf EndIf EndSub Sub SetupBoard hover = "False" For col = 0 to 2 For row = 0 To 2 DrawCell() EndFor EndFor row = -1 col = -1 EndSub Sub DrawCell If row >= 0 And col >= 0 And row <= 2 And col <=2 Then x = col * cellSize + gridX y = row * cellSize + gridY If hover = "True" Then GraphicsWindow.BrushColor = "Azure" Else GraphicsWindow.BrushColor = "White" EndIf GraphicsWindow.FillRectangle(x, y, cellSize, cellSize) GraphicsWindow.PenColor = "Black" GraphicsWindow.DrawRectangle(x, y, cellSize, cellSize) EndIf EndSub
This is the output you will see:
Congratulations!
Now you know how to:
Create a game that involves the following objects: a wall of colored and grey blocks, a ball, and a paddle. The wall slowly moves down toward the paddle. Using the paddle, you must hit all the colored blocks with the ball before the wall hits the paddle.
You use the mouse to move the paddle; the paddle controls the movement of the ball. The ball will bounce off grey blocks. After you hit all the colored blocks, you win the game. You lose the game if the paddle misses the ball or if the blocks hit the paddle before you can remove all the colored blocks.
To see the answers to these questions, go to the Answer Key page.
Next Lesson
Fernando Lugão Veltem edited Revision 1. Comment: added toc