俄罗斯方块代码(vb。net)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Public Class Form1
Private ShowBitMap As New Bitmap(20, 20), BackBitMap As New Bitmap(20, 20), PreviewBitmap As Bitmap, PreviewGraphics As Graphics
Private BlockType As Integer, BlockState As Integer, NextType As Integer = 3, Blocks(,) As Integer'0 隐藏U,C1显示|,C2 静A止~
Private DrawRectangle As Rectangle= New Rectangle(2, 2, 15, 25), DrawLocation As Point= New Point(5, 0), Score As Long
Private WithEvents MyTimer As New Timer
Private Function GetPreviewPoints(ByVal nType As Integer, ByVal nState As Integer) As Point() Dim nPoints As Point(), i As Integer, m As Integer, n As Integer
If nType = 1 Then
nPoints = New Point() {New Point(0, 0), New Point(1, 0), New Point(2, 0), New Point(0, 1)} '左 ÷L
ElseIf nType = 2 Then
nPoints = New Point() {New Point(0, 0), New Point(1, 0), New Point(2, 0), New Point(2, 1)} '右EL
ElseIf nType = 3 Then
nPoints = New Point() {New Point(0, 0), New Point(1, 0), New Point(2, 0), New Point(3, 0)} '长条d
ElseIf nType = 4 Then
nPoints = New Point() {New Point(0, 0), New Point(0, 1), New Point(1, 1), New Point(1, 2)} '左 ÷Z
ElseIf nType = 5 Then
nPoints = New Point() {New Point(1, 0), New Point(2, 0), New Point(0, 1), New Point(1, 1)} '右EZ
ElseIf nType = 6 Then
nPoints = New Point() {New Point(0, 0), New Point(1, 0), New Point(2, 0), New Point(1, 1)} 'T型^
Else
nPoints = New Point() {New Point(0, 0), New Point(0, 1), New Point(1, 0), New Point(1, 1)} '方u块
End If
For i = 1 To nState Mod 4 '旋u转
m = nPoints(1).Y - 1
n = nPoints(1).X - 1
nPoints(2) = New Point(nPoints(2).Y - m + n, 2 - (nPoints(2).X - n) + m)
nPoints(0) = New Point(nPoints(0).Y - m + n, 2 - (nPoints(0).X - n) + m)
nPoints(3) = New Point(nPoints(3).Y - m + n, 2 - (nPoints(3).X - n) + m) Next
Return nPoints
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
If nLocation.X + Right > DrawRectangle.Width Then nLocation.X = DrawRectangle.Width - Right
If nLocation.Y + Bottom > DrawRectangle.Height Then Return True
For Each p As Point In nPoints
If p.Y + nLocation.Y >= 0 AndAlso 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 Then Blocks(x, y) = 0
Next
Next
For Each p As Point In nPoints
If p.Y + nLocation.Y >= 0 Then 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 MyTimer.Enabled AndAlso (e.KeyCode = Keys.W OrElse e.KeyCode = Keys.Up) Then'向u
上a键
BlockState += 1
If NewBlock(DrawLocation) = False Then DrawBlock()
ElseIf MyTimer.Enabled AndAlso (e.KeyCode = Keys.D OrElse e.KeyCode = Keys.Right) Then '向u右E键
If NewBlock(New Point(DrawLocation.X + 1, DrawLocation.Y)) = False Then DrawBlock() ElseIf MyTimer.Enabled AndAlso (e.KeyCode = Keys.A OrElse e.KeyCode = Keys.Left) Then'向u左 ÷键
If NewBlock(New Point(DrawLocation.X - 1, DrawLocation.Y)) = False Then DrawBlock() ElseIf MyTimer.Enabled AndAlso (e.KeyCode = Keys.S OrElse e.KeyCode = Keys.Down OrElse e.KeyCode = Keys.Space) Then'向u下o键
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'回n车键
MyTimer.Enabled = Not MyTimer.Enabled
If MyTimer.Enabled Then
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,