首页 > 解决方案 > 背景图片移动 vb.net

问题描述

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

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

有什么建议吗?谢谢

标签: vb.netimagebackground

解决方案


推荐阅读