我创建了大小为map
的位图(mapWidth, mapHeight)
。我用正方形填充它,大小为:cellSize
。
然后先前创建了用正方形填充的位图map
。
map = New Bitmap(mapWidth, mapHeight)
Dim g As Graphics = Graphics.FromImage(map)
Dim pen As Pen = New Pen(Color.FromArgb(50, 255, 255, 255))
This is the grid inside picturebox
现在我将网格存储在位图中,一切正常。但我想要实现的是,如果我按下箭头键,就像一些2D自上而下的游戏一样穿过那个网格。
所以,如果我想在网格中移动我的视图,我必须通过用新的起始x,y坐标重新绘制它来改变地图位置。
如果我想向上移动,当我按下KEY_UP时,我将y坐标降低moveSpeed
,然后在该y位置重绘图像。但是如果我想在2秒内重绘大小为5000x5000 10倍的图像,那么性能就非常糟糕。
我做的是,我试图只绘制map
的区域,其中包含我的viewx的大小。因此,不是绘制整个地图,而是仅绘制地图区域(800,640),这是我的表格大小。这只是我为修复性能所做的事情。
但是,如果我按住KEY_UP和800,只要我按住KEY_UP就会重新绘制640像素,并且它仍需占处理器的50 +%。
我的代码:
Private Sub draw_map()
Dim xx As Integer = 0
Dim yy As Integer = 0
x1 = 200
x2 = 200
y1 = 200
y2 = 200
map = New Bitmap(mapWidth, mapHeight)
Dim g As Graphics = Graphics.FromImage(map)
Dim pen As Pen = New Pen(Color.FromArgb(50, 255, 255, 255))
cellSize = 32
Do Until viewx Mod cellSize = 0
cellSize -= 1
Loop
For i As Integer = 0 To mapWidth / cellSize Step 1
For j As Integer = 0 To mapHeight / cellSize Step 1
g.DrawRectangle(pen, xx, yy, cellSize, cellSize)
xx += cellSize
Next
yy += cellSize
xx = 0
Next
'mapScreen.Image = map
g.Dispose()
pen.Dispose()
mapPosition = New Bitmap(viewx, viewy)
LoadPartOfMap((map.Width / 2) - (viewx / 2), (map.Height / 2) - (viewy / 2), viewx, viewy)
End Sub
Private Sub LoadPartOfMap(ByVal xx As Integer, ByVal yy As Integer, ByVal width As Integer, ByVal height As Integer)
curX = xx
curY = yy
Dim ds_rect As Rectangle = New Rectangle(xx, yy, width, height)
'mapPosition = New Bitmap(width, height)
grNew = Graphics.FromImage(mapPosition)
grNew.Clear(Color.Black)
grNew.DrawImage(map, 0, 0, ds_rect, GraphicsUnit.Pixel)
mapScreen.Image = mapPosition
grNew.Dispose()
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.Up
LoadPartOfMap(curX, curY - 5, viewx, viewy)
Case Keys.Left
Case Keys.Right
Case Keys.Down
End Select
End Sub
有没有办法改善性能?
我真的只想移动相机而不是图像。但我不认为这是可能的。