Блондинке нужно сделать задание, есть программка, необходимо внести изменения) - VB

Узнай цену своей работы

Формулировка задачи:

Есть програмка (в окне бегают разные шарики и квадратики, при столкновении одинаковых исчезают) нужно внести изменения, а именно: Во вселенной должен быть один экземпляр ShapeRect и один экземпляр ShapeSphere. Последний при движении отражается от левой, верхней и правой границ окна, а также от верхней грани экземпляра ShapeRect. a. При достижении экземпляром ShapeSphere нижней границы окна должно выводиться сообщение об «окончании игры», а затем вселенная должна создаваться заново. b. Экземпляр ShapeRect находится в самом низу окна; абсцисса его центра должна совпадать с абсциссой мыши, а ордината должна быть постоянна. Полный ноль, не понимаю ничего!!! Если кто-нибудь откликнется, скину задание на мыло. Заранее большое спасибо!

Решение задачи: «Блондинке нужно сделать задание, есть программка, необходимо внести изменения)»

textual
Листинг программы
Public Class Form1
 
    Dim m_univ As New Universe
    Dim bDrawCircle As Boolean
    Dim m_v_max As Single = 5
 
    Private Sub ButtonAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click
        Dim shape As New Shape
        Dim r As RectangleF
        r = New RectangleF(Rnd() * PictureBox1.ClientSize.Width, _
                         Rnd() * PictureBox1.ClientSize.Height, 20, 20)
        'r = New RectangleF(Rnd() * PictureBox1.ClientSize.Width, _
        '                100, 20, 20)
        shape.rect() = r
        shape.velocity = New Windows.Vector(Rnd() * m_v_max, Rnd() * m_v_max)
        'shape.velocity = New Windows.Vector(Rnd() * m_v_max, 0)
        Dim drawObj As IDrawShape
        Select Case ComboBox1.SelectedIndex
            Case 0
                drawObj = New DrawShapeCircle
                shape.ShapeType = 1
            Case 1
                drawObj = New DrawShapeRect
                shape.ShapeType = 2
            Case 2
                drawObj = New DrawShapeSphere("ball3d.png")
                shape.ShapeType = 1
        End Select
        shape.DrawObj = drawObj
        Dim opts As IDrawShapeOpts
        opts = TryCast(drawObj, IDrawShapeOpts)
        If (opts IsNot Nothing) Then
            opts.pen = Pens.Brown
            opts.brush = Brushes.Yellow
        End If
        m_univ.AddShape(shape)
        Refresh()
    End Sub
 
    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        m_univ.Draw(e)
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        m_univ.DoTimeStep()
        Refresh()
    End Sub
 
    Private Sub PictureBox1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Resize
        'Dim r As Rectangle = PictureBox1.ClientRectangle
        m_univ.BoundingBox = PictureBox1.ClientRectangle 'New RectangleF(r.X, r.Y, r.Width, r.Height)
    End Sub
 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ComboBox1.SelectedIndex = 0
        Dim testCircle As ICollisionTest = New CollisionTestCircle
        m_univ.CollisionTest(1, 1) = testCircle
        Dim actionDestory As ICollisionAction = New CollisionActionDestory
        m_univ.CollisionAction(1, 1) = actionDestory
    End Sub
End Class
 
 
 
 
Public Class CollisionTestCircle
    Implements ICollisionTest
    Dim v As New Windows.Vector
    Public Function IsCollided(ByVal shape1 As Shape, ByVal shape2 As Shape) As Boolean Implements ICollisionTest.IsCollided
        Dim r1 As RectangleF = shape1.rect
        Dim r2 As RectangleF = shape2.rect
        v.X = (r1.Left + r1.Right) / 2 - (r2.Left + r2.Right) / 2
        v.Y = (r1.Top + r1.Bottom) / 2 - (r2.Top + r2.Bottom) / 2
        Return (v.Length < (r1.Width / 2 + r2.Width / 2))
    End Function
End Class
 
Public Class CollisionActionDestory
    Implements ICollisionAction
 
    Public Sub CollideShapes(ByVal shape1 As Shape, ByVal shape2 As Shape) Implements ICollisionAction.CollideShapes
        shape1.Universe.DelShape(shape1)
        shape2.Universe.DelShape(shape2)
    End Sub
End Class
 
Public Class DrawShapeOpts
    Implements IDrawShapeOpts
 
    Dim m_pen As Pen = Pens.Black
    Dim m_br As Brush
 
    Public Property brush() As System.Drawing.Brush Implements IDrawShapeOpts.brush
        Get
            Return m_br
        End Get
        Set(ByVal value As System.Drawing.Brush)
            m_br = value
        End Set
    End Property
 
    Public Property pen() As System.Drawing.Pen Implements IDrawShapeOpts.pen
        Get
            Return m_pen
        End Get
        Set(ByVal value As System.Drawing.Pen)
            m_pen = value
        End Set
    End Property
End Class
 
Public Class DrawShapeCircle
    Inherits DrawShapeOpts
    Implements IDrawShape
    Public Sub DrawShape(ByVal shape As Shape, ByVal e As System.Windows.Forms.PaintEventArgs) Implements IDrawShape.DrawShape
        If brush() IsNot Nothing Then
            e.Graphics.FillEllipse(brush(), shape.rect)
        End If
        If pen() IsNot Nothing Then
            e.Graphics.DrawEllipse(pen(), shape.rect())
        End If
        'e.Graphics.DrawEllipse(Pens.Black, shape.rect())
    End Sub
End Class
 
Public Class DrawShapeRect
    Inherits DrawShapeOpts
    Implements IDrawShape
    Public Sub DrawShape(ByVal shape As Shape, ByVal e As System.Windows.Forms.PaintEventArgs) Implements IDrawShape.DrawShape
        e.Graphics.DrawRectangle(Pens.Black, shape.rect.X, shape.rect.Y, _
                                 shape.rect.Width, shape.rect.Height)
    End Sub
End Class
 
Public Class DrawShapeSphere
    Implements IDrawShape
    Dim m_pic As System.Drawing.Bitmap
    Public Sub DrawShape(ByVal shape As Shape, ByVal e As System.Windows.Forms.PaintEventArgs) Implements IDrawShape.DrawShape
        e.Graphics.DrawImage(m_pic, shape.rect)
    End Sub
    Sub New(ByVal file As String)
        m_pic = New System.Drawing.Bitmap(file)
    End Sub
End Class
 
 
Public Interface IDrawShape
    Sub DrawShape(ByVal shape As Shape, ByVal e As PaintEventArgs)
End Interface
 
Public Interface IDrawShapeOpts
    Property pen() As Pen
    Property brush() As Brush
End Interface
 
Public Interface IMoveShape
    Sub MoveShape(ByVal shape As Shape)
End Interface
 
Public Interface ICollisionTest
    Function IsCollided(ByVal shape1 As Shape, ByVal shape2 As Shape) As Boolean
End Interface
 
Public Interface ICollisionAction
    Sub CollideShapes(ByVal shape1 As Shape, ByVal shape2 As Shape)
End Interface
 
 
Public Class MoveShapeReflect
    Implements IMoveShape
    Dim pt As New Windows.Vector
    Public Sub MoveShape(ByVal shape As Shape) Implements IMoveShape.MoveShape
        Dim r As RectangleF = shape.rect()
        pt.X = shape.velocity.X
        pt.Y = shape.velocity.Y
        r.Offset(pt.X, pt.Y)
        Dim box As RectangleF = shape.Universe.BoundingBox
        If r.Left < box.Left Then
            r.X = box.Left
            pt.X = -pt.X
        ElseIf r.Right > box.Right Then
            r.X = box.Right - r.Width
            pt.X = -pt.X
        End If
        If r.Top < box.Top Then
            r.Y = box.Top
            pt.Y = -pt.Y
        ElseIf r.Bottom > box.Bottom Then
            r.Y = box.Bottom - r.Height
            pt.Y = -pt.Y
        End If
        shape.rect = r
        shape.velocity = pt
    End Sub
End Class
 
Public Class Shape
    Dim m_rect As New RectangleF
    Dim m_drawObj As IDrawShape
    Dim m_velocity As Windows.Vector
    Dim m_moveObj As IMoveShape = New MoveShapeReflect
    Dim m_univ As Universe
    Dim m_bCollided As Boolean
    Dim m_type As Integer
    Dim m_brushOld As Brush = Brushes.Red
 
    Property DrawObj() As IDrawShape
        Get
            Return m_drawObj
        End Get
        Set(ByVal value As IDrawShape)
            m_drawObj = value
        End Set
    End Property
    Property rect() As RectangleF
        Get
            Return m_rect
        End Get
        Set(ByVal value As RectangleF)
            m_rect = value
        End Set
    End Property
    Public Sub DrawShape(ByVal e As PaintEventArgs)
        If m_drawObj IsNot Nothing Then
            m_drawObj.DrawShape(Me, e)
        End If
    End Sub
    Public Property velocity() As Windows.Vector
        Get
            Return m_velocity
        End Get
        Set(ByVal value As Windows.Vector)
            m_velocity = value
        End Set
    End Property
    Public Sub MoveShape()
        If m_moveObj IsNot Nothing Then
            m_moveObj.MoveShape(Me)
        End If
    End Sub
    Property MoveObj() As IMoveShape
        Get
            Return m_moveObj
        End Get
        Set(ByVal value As IMoveShape)
            m_moveObj = value
        End Set
    End Property
    Property Universe() As Universe
        Get
            Return m_univ
        End Get
        Set(ByVal value As Universe)
            m_univ = value
        End Set
    End Property
    Property Collided() As Boolean
        Get
            Return m_bCollided
        End Get
        Set(ByVal value As Boolean)
            If m_bCollided <> value Then
                Dim opts As IDrawShapeOpts = TryCast(m_drawObj, IDrawShapeOpts)
                If opts IsNot Nothing Then
                    Dim br As Brush = opts.brush
                    opts.brush = m_brushOld
                    m_brushOld = br
                End If
            End If
            m_bCollided = value
        End Set
    End Property
    Property ShapeType() As Integer
        Get
            Return m_type
        End Get
        Set(ByVal value As Integer)
            m_type = value
        End Set
    End Property
End Class
 
Public Class Universe
 
    Dim m_shapes As New List(Of Shape)
    Dim m_box As RectangleF
 
    Dim m_shAdd As New List(Of Shape)
    Dim m_shDel As New List(Of Shape)
 
    Dim m_bEditEnabled As Boolean = True
 
    Const nShapeTypes As Integer = 10
 
    Dim m_clnTest(nShapeTypes, nShapeTypes) As ICollisionTest
    Dim m_clnAction(nShapeTypes, nShapeTypes) As ICollisionAction
 
    ReadOnly Property Shapes() As List(Of Shape)
        Get
            Return m_shapes
        End Get
    End Property
 
    Public Sub AddShape(ByVal shape As Shape)
        If m_bEditEnabled Then
            shape.Universe = Me
            m_shapes.Add(shape)
        Else
            m_shAdd.Add(shape)
        End If
    End Sub
 
    Public Sub DelShape(ByVal shape As Shape)
        If m_bEditEnabled Then
            m_shapes.Remove(shape)
        Else
            m_shDel.Add(shape)
 
        End If
    End Sub
 
    Public Sub Draw(ByVal e As PaintEventArgs)
        For i As Integer = 0 To m_shapes.Count - 1
            'e.Graphics.DrawEllipse(Pens.Black, m_shapes(i).rect())
            m_shapes(i).DrawShape(e)
        Next
    End Sub
    Public Sub DoTimeStep()
 
        m_bEditEnabled = False
        For i As Integer = 0 To m_shapes.Count - 1
            m_shapes(i).MoveShape()
            m_shapes(i).Collided = False
        Next
        Dim sh1 As Shape, sh2 As Shape
        'Dim r1 As RectangleF, r2 As RectangleF
        For i As Integer = 0 To m_shapes.Count - 1
            sh1 = m_shapes(i)
            For j As Integer = i + 1 To m_shapes.Count - 1
                sh2 = m_shapes(j)
                If Not sh1.rect.IntersectsWith(sh2.rect) Then
                    Continue For
                End If
                If m_clnTest(sh1.ShapeType, sh2.ShapeType) Is Nothing Then
                    Continue For
                End If
                If m_clnTest(sh1.ShapeType, sh2.ShapeType).IsCollided(sh1, sh2) Then
                    If m_clnAction(sh1.ShapeType, sh2.ShapeType) IsNot Nothing Then
                        m_clnAction(sh1.ShapeType, sh2.ShapeType).CollideShapes(sh1, sh2)
                    End If
                    sh1.Collided = True
                    sh2.Collided = True
                End If
            Next
        Next
        m_bEditEnabled = True
        For i = 0 To m_shAdd.Count - 1
            AddShape(m_shAdd(i))
        Next
        For i = 0 To m_shDel.Count - 1
            DelShape(m_shDel(i))
 
        Next
        m_shAdd.Clear()
        m_shDel.Clear()
    End Sub
    Property BoundingBox() As RectangleF
        Get
            Return m_box
        End Get
        Set(ByVal value As RectangleF)
            m_box = value
        End Set
    End Property
    Property CollisionTest(ByVal i As Integer, ByVal j As Integer) As ICollisionTest
        Get
            Return m_clnTest(i, j)
        End Get
        Set(ByVal value As ICollisionTest)
            m_clnTest(i, j) = value
        End Set
    End Property
    Property CollisionAction(ByVal i As Integer, ByVal j As Integer) As ICollisionAction
        Get
            Return m_clnAction(i, j)
        End Get
        Set(ByVal value As ICollisionAction)
            m_clnAction(i, j) = value
        End Set
    End Property
End Class

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

13   голосов , оценка 3.692 из 5
Похожие ответы