Tuesday, August 3, 2010

latest tetris -- more functionality, but more bugs

Public Class Form1
    Dim board(9, 9) As Boolean
    Dim drawnBoard(9, 9) As PictureBox
    Dim sq As Square
    Sub undraw()
        For I = 0 To sq.data.GetUpperBound(0)
            For J = 0 To sq.data.GetUpperBound(1)
                If sq.data(I, J) = True Then
                    drawnBoard(sq.Y + I, sq.X + J).Visible = False
                End If
            Next
        Next
    End Sub
    Sub moveDown()
        If isOverLap(0, +1) Then Return
        undraw()
        sq.Y = sq.Y + 1
    End Sub
    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.Down Then
            moveDown()
        ElseIf e.KeyCode = Keys.Left Then
            If isOverLap(-1, 0) Then Return
            undraw()
            If sq.X <> 0 Then
                sq.X = sq.X - 1
            End If
        ElseIf e.KeyCode = Keys.Right Then
            If isOverLap(+1, 0) Then Return
            undraw()
            If sq.X + sq.data.GetUpperBound(1) <> 10 Then
                sq.X = sq.X + 1
            End If
        ElseIf e.KeyCode = Keys.Up Then
            undraw()
            sq.Rotate()
        End If
    End Sub

    Function isOverLap(ByVal XOffset As Integer, ByVal YOffset As Integer) As Boolean
        For I = 0 To sq.data.GetUpperBound(0)
            For J = 0 To sq.data.GetUpperBound(1)
                If sq.data(I, J) = True And board(I + sq.Y + YOffset, J + sq.X + XOffset) = True Then
                    Return True
                End If
            Next
        Next
        Return False
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim pb As PictureBox
        For I = 0 To 9
            For J = 0 To 9
                pb = New PictureBox
                pb.ImageLocation = "C:\Users\joshwaxman.CSDEPT.000\Pictures\box.png"
                pb.Location = New Point(J * 16, I * 16)
                pb.Size = New Point(16, 16)
                pb.SizeMode = PictureBoxSizeMode.StretchImage
                pb.Visible = False
                Controls.Add(pb)
                drawnBoard(I, J) = pb
            Next J
        Next I

        board(6, 3) = True
        drawnBoard(6, 3).Visible = True

        sq = New Square()

        Timer1.Interval = 100
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        ' draw the piece
        For I = 0 To sq.data.GetUpperBound(0)
            For J = 0 To sq.data.GetUpperBound(1)
                If sq.data(I, J) = True Then
                    drawnBoard(sq.Y + I, sq.X + J).Visible = True
                Else
                    drawnBoard(sq.Y + I, sq.X + J).Visible = False
                End If
            Next
        Next
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        moveDown()
    End Sub
End Class

No comments:

Post a Comment