背景图片移动vb.net

时间:2018-11-03 07:38:23

标签: vb.net image background

我正在尝试创建一个小游戏,但是我遇到了麻烦。 我希望背景图像在玩家在地图上移动时以循​​环的方式移动。以下代码是我想要的示例,但相反。

Public Class Form1
Private WithEvents Tmr As New Timer With {.Interval = 40}
Private water As New Bitmap("C:\TestFolder\Water.png")
Private waterposition As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    PictureBox1.Size = New Size(240, 74)
    Tmr.Start()
End Sub

Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
    waterposition += 1
    If waterposition = water.Width Then waterposition = 0
    PictureBox1.Invalidate()
End Sub

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    e.Graphics.DrawImage(water, waterposition, 0)
    If waterposition > 0 Then e.Graphics.DrawImage(water, 0 - (water.Width - waterposition), 0)
End Sub End class

现在,当我尝试将其反转时(从右到左),图像将不会重新绘制。

Private Sub Tmr_Tick(sender As Object, e As EventArgs) Handles Tmr.Tick
    waterposition -= 1
    If waterposition = 0 Then waterposition = water.Width
    PictureBox1.Invalidate()
End Sub

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    e.Graphics.DrawImage(water, waterposition, 0)
    If waterposition < 0 Then e.Graphics.DrawImage(water, (water.Width + waterposition), 0)
End Sub

有什么建议吗?谢谢

0 个答案:

没有答案