我正在为我的A-level计算机科学开发课程。在我的课程中,我已经设法在Visual Basic中设计Shogi(日本象棋)。我编写了每个碎片动作,加载每个碎片位图,如何移动碎片和其他一些东西。
游戏有效,但我注意到了一个问题。每次我移动一块,无论是棋子,国王还是其他什么,内存使用量都会增加大约7MB。 7MB是围绕我正在使用的所有位图的大小,加在一起。 我可以理解这种内存使用量增加的位置。为了理解,我提供了一些代码片段:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = "Shogi"
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Size = New Point(1280, 750)
pb = New PictureBox() With {
.Parent = Me,
.Dock = DockStyle.Fill,
.BackColor = Color.Green
}
Shogi_SetUp()
board_refresh()
End Sub
Private Sub board_refresh()
surface = New Bitmap(Me.Size.Width, Me.Size.Height)
device = Graphics.FromImage(surface)
Dim Board_Design As New Drawing.Bitmap(1280, 720)
Board_Design = New Bitmap("BackGround_WIP.bmp")
device.DrawImage(Board_Design, 0, 0, 1280, 720)
Dim move_btn As New Drawing.Bitmap(128, 32)
Dim move_btn_TF As Boolean = False
If move_btn_TF = False Then
move_btn = New Bitmap("Movement_Button.bmp")
ElseIf move_btn_TF = True Then
move_btn = New Bitmap("Movement_Button.bmp")
End If
device.DrawImage(move_btn, 980, 564, 128, 32)
Doupdate()
pb.Image = surface
End Sub
以下几行将重复,但对于每一件,将它们单独放在电路板上。
digital_board
和expanded_board
是二维数组。 digital_board
填充了数字,每个数字都分配给某个部分。例如,1将是国王,2将是女王。 expanded_board
填充了数字,但这一次,数字都是负数和正数,只分配给一个部分而不是相同类型的部分。例如,他们对于1号车来说是41岁,对于2号车来说是42岁。
Private Sub Doupdate()
Dim doupdate_code As Integer = 0
For y = 0 To 9
For x = 0 To 9
If Digital_board(x, y) = 1 Or Digital_board(x, y) = -1 Then
If Expanded_board(x, y) = 10 Then
Try
N_Gyoku = New Bitmap("Gyoku_Bitmap.bmp")
N_Gyoku.RotateFlip(RotateFlipType.Rotate180FlipNone)
device.DrawImage(N_Gyoku, Default_X + (x * 68), Default_Y + (y * 68), 64, 64)
Catch ex As Exception
MessageBox.Show("Error code: N_Gyoku - DoUpdate(001)")
End Try
ElseIf Expanded_board(x, y) = -10 Then
Try
S_Gyoku = New Bitmap("Gyoku_Bitmap.bmp")
device.DrawImage(S_Gyoku, Default_X + (x * 68), Default_Y + (y * 68), 64, 64)
Catch ex As Exception
MessageBox.Show("Error code: S_Gyoku - DoUpdate(001)")
End Try
End If
Next
Next
Private Sub Piece_Selected(sender As Object, e As MouseEventArgs) Handles pb.Click, pb.MouseMove
If e.Button = MouseButtons.Left Then
If piece_Selected_int = 0 Or piece_Selected_TF = True Then
For y = 0 To 9
For x = 0 To 9
If (Default_X + (68 * x)) < e.X And e.X < ((Default_X + 608) - (68 * (8 - x))) And (Default_Y + (68 * y)) < e.Y And e.Y < ((Default_Y + 608) - (68 * (8 - y))) Then
If piece_Selected_TF = False Then
Dim value_digital_board As Integer = Digital_board(x, y)
Select Case value_digital_board
Case = 1, -1
MessageBox.Show("Gyoku Selected")
piece_Selected_int = 1
ox = x
oy = y
Case = 2, -2
MessageBox.Show("Kin-Sho Selected")
piece_Selected_int = 2
ox = x
oy = y
Case = 3, -3
MessageBox.Show("Gin-Sho Selected")
piece_Selected_int = 3
ox = x
oy = y
Case = 4, -4
MessageBox.Show("Kei-Ma Selected")
piece_Selected_int = 4
ox = x
oy = y
Case = 5, -5
piece_Selected_int = 5
ox = x
oy = y
Case = 6, -6
piece_Selected_int = 6
ox = x
oy = y
Case = 7, -7
piece_Selected_int = 7
ox = x
oy = y
Case = 8, -8
piece_Selected_int = 8
ox = x
oy = y
Case = 0
End Select
ElseIf piece_Selected_TF = True Then
Select Case piece_Selected_int
Case = 1
piece_Selected_int = 0
piece_Selected_TF = False
MessageBox.Show("Gyoku moved")
Gyoku_Movement(ox, oy, x, y)
dispose_update()
board_refresh()
Case = 2
piece_Selected_int = 0
piece_Selected_TF = False
MessageBox.Show("Kin-Sho moved")
KinSho_Movement(ox, oy, x, y)
dispose_update()
board_refresh()
Case = 3
piece_Selected_int = 0
piece_Selected_TF = False
MessageBox.Show("Gin-Sho moved")
GinSho_Movement(ox, oy, x, y)
dispose_update()
board_refresh()
Case = 4
piece_Selected_int = 0
piece_Selected_TF = False
MessageBox.Show("Kei-Ma moved")
KeiMa_Movement(ox, oy, x, y)
dispose_update()
board_refresh()
Case = 5
piece_Selected_int = 0
piece_Selected_TF = False
MessageBox.Show("Kyo-Sha moved")
KyoSha_Movement(ox, oy, x, y)
dispose_update()
board_refresh()
Case = 6
piece_Selected_int = 0
piece_Selected_TF = False
MessageBox.Show("Kaku moved")
Kaku_Movement(ox, oy, x, y)
dispose_update()
board_refresh()
Case = 7
piece_Selected_int = 0
piece_Selected_TF = False
MessageBox.Show("Hisha moved")
Hisha_Movement(ox, oy, x, y)
dispose_update()
board_refresh()
Case = 8
piece_Selected_int = 0
piece_Selected_TF = False
MessageBox.Show("Fuhyo moved")
Fuhyo_Movement(ox, oy, x, y)
dispose_update()
board_refresh()
End Select
End If
End If
Next
Next
ElseIf piece_Selected_int > 0 Then
If 980 < e.X And e.X < 1108 And 564 < e.Y And e.Y < 596 Then
MessageBox.Show("Button clicked")
piece_Selected_TF = True
End If
End If
End If
End Sub
任何人都可以帮我找到一种方法来阻止我的游戏每次移动时都使用更多内存吗?