giovedì 9 aprile 2015

Codice Visual Studio per la Rappresentazione grafica con Disegna()

Imports System.Math


Public Class Disegna
    Public X As Integer
    Public Y As Integer
    Public collisionX As Boolean
    Public passo As Integer
    Public asseX As Integer
    Public asseY As Integer
    Public collisionY As Boolean
    Public colore As New Color

    Sub New(ByVal X As Integer, ByVal Y As Integer, ByVal asseX As Integer, ByVal asseY As Integer, ByVal colore As Color, ByVal passo As Integer, ByVal collisionX As Boolean, ByVal collisionY As Boolean)
        Me.X = X
        Me.Y = Y
        Me.asseX = asseX
        Me.asseY = asseY
        Me.colore = colore
        Me.passo = passo
        Me.collisionX = collisionX
        Me.collisionY = collisionY
    End Sub
    Public Sub ricalcolo(ByVal g As Graphics, ByVal b As Bitmap)
        If X + asseX > b.Width Then
            collisionX = True
        End If
        If Y + asseY > b.Height Then

            collisionY = True
        End If
        If X <= 0 Then
            collisionX = False
        End If
        If Y <= 0 Then
            collisionY = False
        End If


        If collisionX = True And collisionY = True Then
            X = X - passo
            Y = Y - passo
        End If
        If collisionX = False And collisionY = False Then
            X = X + passo
            Y = Y + passo
        End If

        If collisionX = True And collisionY = False Then
            X = X - passo
            Y = Y + passo
        End If
        If collisionX = False And collisionY = True Then
            X = X + passo
            Y = Y - passo
        End If


        g.FillEllipse(New SolidBrush(colore), X, Y, asseX, asseY)
    End Sub

End Class

Public Class Form1

    'Dim b As New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)
    Public b As New Bitmap(1000, 500)
    Public g As Graphics = Graphics.FromImage(b)
    Dim lista As New List(Of Disegna)

    Private Sub button1_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim random As Random = New Random
        Dim posizioneX1 As Integer = random.Next(0, 1000)
        Dim posizioneY1 As Integer = random.Next(0, 500)
        Dim posizioneX2 As Integer = random.Next(0, 1000)
        Dim posizioneY2 As Integer = random.Next(0, 500)
        Dim posizioneX3 As Integer = random.Next(0, 1000)
        Dim posizioneY3 As Integer = random.Next(0, 500)

        Dim p1 As New Disegna(posizioneX1, posizioneY1, 30, 30, Color.NavajoWhite, 5, True, True)
        Dim p2 As New Disegna(posizioneX2, posizioneY2, 30, 30, Color.Aqua, 5, True, True)
        Dim p3 As New Disegna(posizioneX3, posizioneY3, 30, 30, Color.Black, 5, True, False)


        Me.lista.Add(p1)

        Me.lista.Add(p2)

        Me.lista.Add(p3)

        Me.Timer1.Interval = 100
        Me.Timer1.Start()

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        g.Clear(Color.Green)
        For Each p As Disegna In Me.lista
            p.ricalcolo(g, b)

            ' If Math.Abs(p1.X) = Math.Abs(p2.X) OrElse Math.Abs(p1.Y) = Math.Abs(p2.Y) Then
            '     p1.X = p1.X + 3
            '     p1.Y = p1.Y - 3
            '     p2.X = p2.X - 10
            '     p2.Y = p2.Y + 10
            '     p2.collisionX = False
            '     p2.collisionY = False
            ' End If


        Next

        PictureBox1.Image = b

    End Sub


End Class

Nessun commento:

Posta un commento