MyException - 我的异常网
当前位置:我的异常网» VB Dotnet » 哟嗬.这不是159行的俄罗斯方块么.该如何处理

哟嗬.这不是159行的俄罗斯方块么.该如何处理

www.MyException.Cn  网友分享于:2013-03-25  浏览:7次
哟嗬...这不是159行的俄罗斯方块么....

VB.NET code

Public Class Form1
    Private ShowBitMap As New Bitmap(20, 20), BackBitMap As New Bitmap(20, 20), BlockType As Integer, BlockState As Integer, NextType As Integer = 3, Blocks(,) As Integer '0 隐藏,1显示,2 静止  
    Private DrawRectangle As Rectangle = New Rectangle(2, 2, 15, 25), DrawLocation As Point = New Point(5, 0), Score(1) As Long
    Private WithEvents MyTimer As New Timer
    Private Function GetPreviewPoints(ByVal nType As Integer, ByVal nState As Integer) As Point()
        If nType = 1 Then
            If nState Mod 4 = 0 Then Return New Point() {New Point(0, 1), New Point(1, 1), New Point(2, 1), New Point(2, 0)}
            If nState Mod 4 = 1 Then Return New Point() {New Point(0, 0), New Point(0, 1), New Point(0, 2), New Point(1, 2)}
            If nState Mod 4 = 2 Then Return New Point() {New Point(0, 0), New Point(0, 1), New Point(1, 0), New Point(2, 0)}
            If nState Mod 4 = 3 Then Return New Point() {New Point(0, 0), New Point(1, 0), New Point(1, 1), New Point(1, 2)}
        ElseIf nType = 2 Then
            If nState Mod 4 = 0 Then Return New Point() {New Point(0, 0), New Point(1, 0), New Point(2, 0), New Point(2, 1)}
            If nState Mod 4 = 1 Then Return New Point() {New Point(0, 0), New Point(0, 1), New Point(0, 2), New Point(1, 0)}
            If nState Mod 4 = 2 Then Return New Point() {New Point(0, 0), New Point(0, 1), New Point(1, 1), New Point(2, 1)}
            If nState Mod 4 = 3 Then Return New Point() {New Point(1, 0), New Point(1, 1), New Point(1, 2), New Point(0, 2)}
        ElseIf nType = 3 Then
            If nState Mod 2 = 0 Then Return New Point() {New Point(0, 0), New Point(1, 0), New Point(2, 0), New Point(3, 0)}
            If nState Mod 2 = 1 Then Return New Point() {New Point(0, 0), New Point(0, 1), New Point(0, 2), New Point(0, 3)}
        ElseIf nType = 4 Then
            If nState Mod 2 = 0 Then Return New Point() {New Point(0, 0), New Point(0, 1), New Point(1, 1), New Point(1, 2)}
            If nState Mod 2 = 1 Then Return New Point() {New Point(0, 1), New Point(1, 0), New Point(1, 1), New Point(2, 0)}
        ElseIf nType = 5 Then
            If nState Mod 2 = 0 Then Return New Point() {New Point(0, 1), New Point(0, 2), New Point(1, 0), New Point(1, 1)}
            If nState Mod 2 = 1 Then Return New Point() {New Point(0, 0), New Point(1, 0), New Point(1, 1), New Point(2, 1)}
        ElseIf nType = 6 Then
            If nState Mod 4 = 0 Then Return New Point() {New Point(0, 1), New Point(1, 0), New Point(1, 1), New Point(1, 2)}
            If nState Mod 4 = 1 Then Return New Point() {New Point(0, 0), New Point(1, 0), New Point(1, 1), New Point(2, 0)}
            If nState Mod 4 = 2 Then Return New Point() {New Point(0, 0), New Point(0, 1), New Point(0, 2), New Point(1, 1)}
            If nState Mod 4 = 3 Then Return New Point() {New Point(0, 1), New Point(1, 1), New Point(2, 1), New Point(1, 0)}
        Else
            Return New Point() {New Point(0, 0), New Point(0, 1), New Point(1, 0), New Point(1, 1)}
        End If
    End Function
    Private Function NewBlock(ByVal nLocation As Point) As Boolean
        Dim Left As Integer = 100, Right As Integer = -1, Bottom As Integer = -1, Top As Integer = 100, nPoints As Point() = GetPreviewPoints(BlockType, BlockState)
        For Each n As Point In nPoints
            If n.X < Left Then Left = n.X
            If n.X > Right Then Right = n.X
            If n.Y < Top Then Top = n.Y
            If n.Y > Bottom Then Bottom = n.Y
        Next
        If nLocation.X + Left < 0 Then
            nLocation.X = -Left
        ElseIf nLocation.X + Right - Left > DrawRectangle.Width Then
            nLocation.X = DrawRectangle.Width - (Right - Left)
        End If
        If nLocation.Y + Top < 0 Then
            nLocation.Y = -Top
        ElseIf nLocation.Y + (Bottom - Top) > DrawRectangle.Height Then
            nLocation.Y = DrawRectangle.Height - (Bottom - Top)
            Return True
        End If
        For Each p As Point In nPoints
            If Blocks(p.X + nLocation.X, p.Y + nLocation.Y) > 1 Then Return True
        Next
        For y As Integer = 0 To DrawRectangle.Height
            For x As Integer = 0 To DrawRectangle.Width
                If Blocks(x, y) = 1 OrElse Blocks(x, y) > 5 Then Blocks(x, y) = 0
            Next
        Next
        For Each p As Point In nPoints
            Blocks(p.X + nLocation.X, p.Y + nLocation.Y) = 1
        Next
        DrawLocation = nLocation
    End Function
    Private Sub Key_Up(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
        If e.KeyCode = Keys.W OrElse e.KeyCode = Keys.Up Then
            BlockState += 1
            If NewBlock(DrawLocation) = False Then DrawBlock()
        ElseIf e.KeyCode = Keys.D OrElse e.KeyCode = Keys.Right Then
            If NewBlock(New Point(DrawLocation.X + 1, DrawLocation.Y)) = False Then DrawBlock()
        ElseIf e.KeyCode = Keys.A OrElse e.KeyCode = Keys.Left Then
            If NewBlock(New Point(DrawLocation.X - 1, DrawLocation.Y)) = False Then DrawBlock()
        ElseIf e.KeyCode = Keys.S OrElse e.KeyCode = Keys.Down OrElse e.KeyCode = Keys.Space Then
            For y As Integer = 0 To DrawRectangle.Height
                If NewBlock(New Point(DrawLocation.X, DrawLocation.Y + 1)) Then Exit For
            Next
            DrawBlock()
        ElseIf e.KeyCode = Keys.Enter OrElse e.KeyCode = Keys.Escape Then
            MyTimer.Enabled = Not MyTimer.Enabled
            If MyTimer.Enabled Then
                ReDim Blocks(DrawRectangle.Width, DrawRectangle.Height)
                NewBlock(New Point(5, 0))
                Score(1) = 0
                Me.Text = "分数:" & Score(1)
            End If
        End If
    End Sub
    Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyTimer.Tick
        If NewBlock(New Point(DrawLocation.X, DrawLocation.Y + 1)) = False Then
            DrawBlock()
        Else
            For y As Integer = 0 To DrawRectangle.Height
                For x As Integer = 0 To DrawRectangle.Width
                    If Blocks(x, y) = 1 Then Blocks(x, y) = 2
                Next
            Next
            Score(0) = ClearLine(0)
            If Score(0) Then
                Score(1) += (Score(0) ^ 2) * 10
                Me.Text = "分数:" & Score(1)
            Else
                BlockType = NextType
                NextType = Rnd() * 6
                If NewBlock(New Point(5, 0)) Then
                    MyTimer.Enabled = False
                    MsgBox("游戏结束,按下 Enter 键重新开始。")
                End If
            End If
        End If
    End Sub
    Private Function ClearLine(ByVal StartIndex As Integer) As Integer '消行
        If StartIndex > DrawRectangle.Height Then Return 0
        For x As Integer = 0 To DrawRectangle.Width
            If Blocks(x, StartIndex) <> 2 Then Return ClearLine(StartIndex + 1)
        Next
        For x As Integer = 0 To DrawRectangle.Width
            For y = StartIndex To 0 Step -1
                If y = 0 Then
                    Blocks(x, y) = 0
                Else
                    Blocks(x, y) = Blocks(x, y - 1)
                End If
            Next
        Next
        Return ClearLine(StartIndex + 1) + 1
    End Function
    Private Sub DrawBlock()
        Dim i(4, 4) As Integer
        For Each p As Point In GetPreviewPoints(NextType, 0)
            i(p.X + 1, p.Y + 1) = 1
        Next
        DrawPicture(Blocks, DrawRectangle.Location, Me.CreateGraphics)
        DrawPicture(i, New Point(DrawRectangle.Right + 2, DrawRectangle.Y), Me.CreateGraphics)
    End Sub
    Private Sub DrawPicture(ByVal Picture(,) As Integer, ByVal nDrawPoint As Point, ByVal DrawGraphics As Graphics)
        For x As Integer = 0 To Picture.GetUpperBound(0)
            For y As Integer = 0 To Picture.GetUpperBound(1)
                If Picture(x, y) = 0 Then
                    DrawGraphics.DrawImage(BackBitMap, New Point(nDrawPoint.X * 20 + x * 20, nDrawPoint.Y * 20 + y * 20))
                ElseIf Picture(x, y) = 1 OrElse Picture(x, y) = 2 Then
                    DrawGraphics.DrawImage(ShowBitMap, New Point(nDrawPoint.X * 20 + x * 20, nDrawPoint.Y * 20 + y * 20))
                End If
            Next
        Next
    End Sub
    Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Randomize()
        Graphics.FromImage(ShowBitMap).FillRectangle(New System.Drawing.Drawing2D.HatchBrush(Rnd() * 52, Color.FromArgb(&HFF000000 Or &HFFFFFF * Rnd()), Color.FromArgb(&HFF000000 Or &HFFFFFF * Rnd())), New Rectangle(0, 0, 20, 20))
        Graphics.FromImage(ShowBitMap).DrawRectangle(Pens.Black, New Rectangle(0, 0, 19, 19))
        Graphics.FromImage(BackBitMap).FillRectangle(New System.Drawing.Drawing2D.HatchBrush(Rnd() * 52, Color.FromArgb(&HFF000000 Or &HFFFFFF * Rnd()), Color.FromArgb(&HFF000000 Or &HFFFFFF * Rnd())), New Rectangle(0, 0, 20, 20))
        Graphics.FromImage(BackBitMap).DrawRectangle(Pens.Black, New Rectangle(0, 0, 19, 19))
        MyTimer.Interval = 500
        Me.Text = "按下 Enter 开始新游戏"
        Me.SetBounds(Screen.PrimaryScreen.Bounds.X + (Screen.PrimaryScreen.Bounds.Width - (DrawRectangle.Right + 10) * 20) / 2, Screen.PrimaryScreen.Bounds.Y + (Screen.PrimaryScreen.Bounds.Height - (DrawRectangle.Bottom + 5) * 20) / 2, (DrawRectangle.Right + 10) * 20, (DrawRectangle.Bottom + 5) * 20)
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
        Me.MaximizeBox = False
    End Sub
End Class

文章评论

亲爱的项目经理,我恨你
亲爱的项目经理,我恨你
10个帮程序员减压放松的网站
10个帮程序员减压放松的网站
程序员周末都喜欢做什么?
程序员周末都喜欢做什么?
什么才是优秀的用户界面设计
什么才是优秀的用户界面设计
程序猿的崛起——Growth Hacker
程序猿的崛起——Growth Hacker
要嫁就嫁程序猿—钱多话少死的早
要嫁就嫁程序猿—钱多话少死的早
程序员的一天:一寸光阴一寸金
程序员的一天:一寸光阴一寸金
Web开发者需具备的8个好习惯
Web开发者需具备的8个好习惯
初级 vs 高级开发者 哪个性价比更高?
初级 vs 高级开发者 哪个性价比更高?
那些争议最大的编程观点
那些争议最大的编程观点
我是如何打败拖延症的
我是如何打败拖延症的
每天工作4小时的程序员
每天工作4小时的程序员
程序员应该关注的一些事儿
程序员应该关注的一些事儿
一个程序员的时间管理
一个程序员的时间管理
如何成为一名黑客
如何成为一名黑客
鲜为人知的编程真相
鲜为人知的编程真相
10个调试和排错的小建议
10个调试和排错的小建议
老美怎么看待阿里赴美上市
老美怎么看待阿里赴美上市
程序员的鄙视链
程序员的鄙视链
科技史上最臭名昭著的13大罪犯
科技史上最臭名昭著的13大罪犯
十大编程算法助程序员走上高手之路
十大编程算法助程序员走上高手之路
“肮脏的”IT工作排行榜
“肮脏的”IT工作排行榜
 程序员的样子
程序员的样子
漫画:程序员的工作
漫画:程序员的工作
旅行,写作,编程
旅行,写作,编程
如何区分一个程序员是“老手“还是“新手“?
如何区分一个程序员是“老手“还是“新手“?
程序员都该阅读的书
程序员都该阅读的书
程序员必看的十大电影
程序员必看的十大电影
我跳槽是因为他们的显示器更大
我跳槽是因为他们的显示器更大
编程语言是女人
编程语言是女人
团队中“技术大拿”并非越多越好
团队中“技术大拿”并非越多越好
老程序员的下场
老程序员的下场
中美印日四国程序员比较
中美印日四国程序员比较
程序员眼里IE浏览器是什么样的
程序员眼里IE浏览器是什么样的
总结2014中国互联网十大段子
总结2014中国互联网十大段子
“懒”出效率是程序员的美德
“懒”出效率是程序员的美德
Java 与 .NET 的平台发展之争
Java 与 .NET 的平台发展之争
代码女神横空出世
代码女神横空出世
写给自己也写给你 自己到底该何去何从
写给自己也写给你 自己到底该何去何从
5款最佳正则表达式编辑调试器
5款最佳正则表达式编辑调试器
做程序猿的老婆应该注意的一些事情
做程序猿的老婆应该注意的一些事情
看13位CEO、创始人和高管如何提高工作效率
看13位CEO、创始人和高管如何提高工作效率
程序员和编码员之间的区别
程序员和编码员之间的区别
为什么程序员都是夜猫子
为什么程序员都是夜猫子
为啥Android手机总会越用越慢?
为啥Android手机总会越用越慢?
软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有