![]() |
Tipp 0138
|
Mauszeiger folgen
|
 |
|
Autor/Einsender: Datum: |
|
Max Herrmann 26.09.2006 |
|
Entwicklungsumgebung: |
|
VB.Net 2005 |
Framework: |
|
2.0 |
|
|
Was für manche Spiele unentbehrlich ist, wird hier gezeigt, ein Ball (Icon) der auf direkten Weg dem Mauszeiger folgt, und von ihm angezogen wird,
wie ein Nagel von einem Magnet. Es gibt also kein Entkommen.
|
Dabei ist neben einer Kollisionserkennung mit den Bildschirmrändern mit einem entsprechenden Sound auch die notwendige Reibung berücksichtigt.
Durch die so entstehende realistische Bewegung des Balls ist der Phantasie zur Verwirklichung in eigenen Projekte keine Grenze gesetzt.
|
|
|
Dim TSpeed As Integer = 1
Dim Speed() As Double = {0, 0}
Dim RB As Double = 0.2
Dim BS As Double = 0.5
Dim MaxSpeed As Integer = 10
Dim MPos As Point
Dim Bound As Point = New Point(My.Computer.Screen.Bounds.Width, _
My.Computer.Screen.Bounds.Height)
Dim UP As String = _
My.Computer.FileSystem.CurrentDirectory & "\data\"
Dim IP As String = UP & "gfx\"
Dim SP As String = UP & "sound\"
Private Sub Main_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
LoadImages()
Timer.Interval = TSpeed
End Sub
Sub LoadImages()
BackgroundImage = Image.FromFile(IP & "Blackball.bmp")
End Sub
Private Sub Main_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Click
Application.Exit()
End Sub
Private Sub Timer_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer.Tick
GetMPos()
If MPos.X < Location.X Then
Speed(0) -= BS
End If
If MPos.X > Location.X Then
Speed(0) += BS
End If
If MPos.Y < Location.Y Then
Speed(1) -= BS
End If
If MPos.Y > Location.Y Then
Speed(1) += BS
End If
BoundingSpeed()
Collision()
SpeedDown(RB, 0)
SpeedDown(RB, 1)
Location += New Point(Speed(0), Speed(1))
End Sub
Sub Collision()
If Location.X <= 0 Then
Location = New Point(1, Location.Y)
Speed(0) *= -1
My.Computer.Audio.Play(SP & "ballcollision.wav", _
AudioPlayMode.Background)
End If
If Location.X >= Bound.X - 20 Then
Location = New Point(Bound.X - 21, Location.Y)
Speed(0) *= -1
My.Computer.Audio.Play(SP & "ballcollision.wav", _
AudioPlayMode.Background)
End If
If Location.Y <= 0 Then
Location = New Point(Location.X, 1)
Speed(1) *= -1
My.Computer.Audio.Play(SP & "ballcollision.wav", _
AudioPlayMode.Background)
End If
If Location.Y >= Bound.Y - 20 Then
Location = New Point(Location.X, Bound.Y - 21)
Speed(1) *= -1
My.Computer.Audio.Play(SP & "ballcollision.wav", _
AudioPlayMode.Background)
End If
End Sub
Sub BoundingSpeed()
If Speed(0) < -MaxSpeed Then
Speed(0) = -MaxSpeed
End If
If Speed(0) > MaxSpeed Then
Speed(0) = MaxSpeed
End If
If Speed(1) < -MaxSpeed Then
Speed(1) = -MaxSpeed
End If
If Speed(1) > MaxSpeed Then
Speed(1) = MaxSpeed
End If
End Sub
Sub SpeedDown(ByVal Value As Double, ByVal ax As Integer)
If Speed(ax) < 0 Then
Speed(ax) += Value
ElseIf Speed(ax) > 0 Then
Speed(ax) -= Value
End If
End Sub
Sub GetMPos()
MPos = New Point(Control.MousePosition.X - 10, _
Control.MousePosition.Y - 10)
End Sub
|
|
|
|
|
Windows-Version |
98/SE |
 |
|
ME |
 |
|
NT |
 |
|
2000 |
 |
|
XP |
 |
|
Vista |
 |
|
Win
7 |
 |
|
|
|
Download (100 kB)
|
Downloads bisher: [ 402 ]
|
|
|