다음을 통해 공유


VB.Net - Totris Game


Overview

This game is a variation of Tetris with some significant differences. Instead of the familiar four block shapes used in regular Tetris this game has a single falling number, with a winning row having a sum of 42. The game uses the arrow keys to move left and right, with the DOWN arrow speeding progress of the falling number, and the UP arrow toggling the polarity of the falling number.

The game uses three extended datagridviews, the first of which is the falling numbers gameboard, the second is a sum column for the 20 datagridview rows in the gameboard, with the third being the number preview. All user input is restricted in these readonly datagridviews.

Two timers are used in the game, one for controlling the descent of the falling numbers, and the second used in assessing the datagridview rows regularly throughout the duration of the game. Any rows found to have a sum of 42 flash several times before being removed from the gameboard.

The significance of the number 42? In the Douglas Adams trilogy (of five books :D) The Hitchhikers Guide to the Galaxy, the number 42 is said to be the answer to life, the universe, and everything. When asked why he chose the number 42 in his books, Douglas Adams replied that it had to be something and that number was as good as any. Similarly, in this game the number 42 is as good as any...


The navigation commands

''' <summary>
''' WndProc
''' Avoid DGV focussing, and catch Keypresses
''' </summary>
''' <param name="m"></param>
Protected Overrides  Sub WndProc(ByRef m As System.Windows.Forms.Message)
    If m.Msg = WM_LBUTTONDBLCLK OrElse m.Msg = WM_LBUTTONDOWN Then
        Return
    ElseIf m.Msg = WM_KEYDOWN Then
        If m.WParam.ToInt32 = VK_LEFT Then
            moveLeft()
        ElseIf m.WParam.ToInt32 = VK_RIGHT Then
            moveRight()
        ElseIf m.WParam.ToInt32 = VK_DOWN Then
            moveDown()
        ElseIf m.WParam.ToInt32 = VK_UP Then
            negate()
        End If
        Return
    End If
    MyBase.WndProc(m)
End Sub

Testing for winning rows

'finds full rows in DGV
Private Function  find42() As  Integer
    For y As Integer  = 19 To  0 Step  -1
        Dim sum As Integer  = 0
        For x As Integer  = 0 To  9
            If Not  currentShape Is  Nothing Then
                If y = currentShape.CurrentPoints(0).Y And x = currentShape.CurrentPoints(0).X Then Continue For
            End If
            If gameGrid(y)(x) <> "" Then  sum += CInt(gameGrid(y)(x))
        Next
        If sum = 42 Then Return  y
    Next
    Return -1
End Function

Updating the display

'renders the numbers in the DGV
Private Sub  HasChanged(grid As  String()(), flash As Boolean, flashRow As  Integer)
    For y As Integer  = 0 To  19
        For x As Integer  = 0 To  9
            If String.IsNullOrEmpty(grid(y)(x)) Then
                Me.Rows(y).Cells(x).Value = ""
            Else
                Me.Rows(y).Cells(x).Value = grid(y)(x)
            End If
            If Not  flash OrElse  (flash And  Not flashRow = y) Then
                Me.Rows(y).Cells(x).Style.BackColor = Color.Transparent
                Me.Rows(y).Cells(x).Style.ForeColor = Color.White
            Else
                Me.Rows(y).Cells(x).Style.BackColor = Color.Silver
                Me.Rows(y).Cells(x).Style.ForeColor = Color.Black
            End If
        Next
    Next
End Sub

Conclusion

In this article only a small selection of the code used in the game is shown. The three code snippets come from the GameGrid class, which is an extended DataGridView that encapsulates the game. The falling number is an instance of the Shape class that has methods for moving left or right or down, and a negate function that reverses the polarity of the falling number. This is an example of Object Oriented Programming.

For more in depth viewing a download link has been added at the end of this article.


VB.Net - WordSearch
VB.Net - Vertex
VB.Net - Perspective
VB.Net - MasterMind
VB.Net - OOP BlackJack
VB.Net - Numbers Game
VB.Net - HangMan
Console BlackJack - VB.Net | C#
TicTacToe - VB.Net | C#
OOP Sudoku - VB.Net | C#
OctoWords VB.Net | C#
OOP Buttons Guessing Game VB.Net | C#
OOP Tangram Shapes Game VB.Net | C#
VB.Net - Three-card Monte
VB.Net - Pascal's Pyramid
VB.Net - Split Decisions
VB.Net - Random Maze Games
C# - Crack the Lock
VB.Net - Event Driven Programming - LockWords Game


Download

Download here...