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

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

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

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

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

textual
Листинг программы
  1. Public Class Form1
  2.  
  3.     Dim m_univ As New Universe
  4.     Dim bDrawCircle As Boolean
  5.     Dim m_v_max As Single = 5
  6.  
  7.     Private Sub ButtonAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click
  8.         Dim shape As New Shape
  9.         Dim r As RectangleF
  10.         r = New RectangleF(Rnd() * PictureBox1.ClientSize.Width, _
  11.                          Rnd() * PictureBox1.ClientSize.Height, 20, 20)
  12.         'r = New RectangleF(Rnd() * PictureBox1.ClientSize.Width, _
  13.         '                100, 20, 20)
  14.         shape.rect() = r
  15.         shape.velocity = New Windows.Vector(Rnd() * m_v_max, Rnd() * m_v_max)
  16.         'shape.velocity = New Windows.Vector(Rnd() * m_v_max, 0)
  17.         Dim drawObj As IDrawShape
  18.         Select Case ComboBox1.SelectedIndex
  19.             Case 0
  20.                 drawObj = New DrawShapeCircle
  21.                 shape.ShapeType = 1
  22.             Case 1
  23.                 drawObj = New DrawShapeRect
  24.                 shape.ShapeType = 2
  25.             Case 2
  26.                 drawObj = New DrawShapeSphere("ball3d.png")
  27.                 shape.ShapeType = 1
  28.         End Select
  29.         shape.DrawObj = drawObj
  30.         Dim opts As IDrawShapeOpts
  31.         opts = TryCast(drawObj, IDrawShapeOpts)
  32.         If (opts IsNot Nothing) Then
  33.             opts.pen = Pens.Brown
  34.             opts.brush = Brushes.Yellow
  35.         End If
  36.         m_univ.AddShape(shape)
  37.         Refresh()
  38.     End Sub
  39.  
  40.     Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
  41.         m_univ.Draw(e)
  42.     End Sub
  43.  
  44.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  45.         m_univ.DoTimeStep()
  46.         Refresh()
  47.     End Sub
  48.  
  49.     Private Sub PictureBox1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Resize
  50.         'Dim r As Rectangle = PictureBox1.ClientRectangle
  51.         m_univ.BoundingBox = PictureBox1.ClientRectangle 'New RectangleF(r.X, r.Y, r.Width, r.Height)
  52.     End Sub
  53.  
  54.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  55.         ComboBox1.SelectedIndex = 0
  56.         Dim testCircle As ICollisionTest = New CollisionTestCircle
  57.         m_univ.CollisionTest(1, 1) = testCircle
  58.         Dim actionDestory As ICollisionAction = New CollisionActionDestory
  59.         m_univ.CollisionAction(1, 1) = actionDestory
  60.     End Sub
  61. End Class
  62.  
  63.  
  64.  
  65.  
  66. Public Class CollisionTestCircle
  67.     Implements ICollisionTest
  68.     Dim v As New Windows.Vector
  69.     Public Function IsCollided(ByVal shape1 As Shape, ByVal shape2 As Shape) As Boolean Implements ICollisionTest.IsCollided
  70.         Dim r1 As RectangleF = shape1.rect
  71.         Dim r2 As RectangleF = shape2.rect
  72.         v.X = (r1.Left + r1.Right) / 2 - (r2.Left + r2.Right) / 2
  73.         v.Y = (r1.Top + r1.Bottom) / 2 - (r2.Top + r2.Bottom) / 2
  74.         Return (v.Length < (r1.Width / 2 + r2.Width / 2))
  75.     End Function
  76. End Class
  77.  
  78. Public Class CollisionActionDestory
  79.     Implements ICollisionAction
  80.  
  81.     Public Sub CollideShapes(ByVal shape1 As Shape, ByVal shape2 As Shape) Implements ICollisionAction.CollideShapes
  82.         shape1.Universe.DelShape(shape1)
  83.         shape2.Universe.DelShape(shape2)
  84.     End Sub
  85. End Class
  86.  
  87. Public Class DrawShapeOpts
  88.     Implements IDrawShapeOpts
  89.  
  90.     Dim m_pen As Pen = Pens.Black
  91.     Dim m_br As Brush
  92.  
  93.     Public Property brush() As System.Drawing.Brush Implements IDrawShapeOpts.brush
  94.         Get
  95.             Return m_br
  96.         End Get
  97.         Set(ByVal value As System.Drawing.Brush)
  98.             m_br = value
  99.         End Set
  100.     End Property
  101.  
  102.     Public Property pen() As System.Drawing.Pen Implements IDrawShapeOpts.pen
  103.         Get
  104.             Return m_pen
  105.         End Get
  106.         Set(ByVal value As System.Drawing.Pen)
  107.             m_pen = value
  108.         End Set
  109.     End Property
  110. End Class
  111.  
  112. Public Class DrawShapeCircle
  113.     Inherits DrawShapeOpts
  114.     Implements IDrawShape
  115.     Public Sub DrawShape(ByVal shape As Shape, ByVal e As System.Windows.Forms.PaintEventArgs) Implements IDrawShape.DrawShape
  116.         If brush() IsNot Nothing Then
  117.             e.Graphics.FillEllipse(brush(), shape.rect)
  118.         End If
  119.         If pen() IsNot Nothing Then
  120.             e.Graphics.DrawEllipse(pen(), shape.rect())
  121.         End If
  122.         'e.Graphics.DrawEllipse(Pens.Black, shape.rect())
  123.     End Sub
  124. End Class
  125.  
  126. Public Class DrawShapeRect
  127.     Inherits DrawShapeOpts
  128.     Implements IDrawShape
  129.     Public Sub DrawShape(ByVal shape As Shape, ByVal e As System.Windows.Forms.PaintEventArgs) Implements IDrawShape.DrawShape
  130.         e.Graphics.DrawRectangle(Pens.Black, shape.rect.X, shape.rect.Y, _
  131.                                  shape.rect.Width, shape.rect.Height)
  132.     End Sub
  133. End Class
  134.  
  135. Public Class DrawShapeSphere
  136.     Implements IDrawShape
  137.     Dim m_pic As System.Drawing.Bitmap
  138.     Public Sub DrawShape(ByVal shape As Shape, ByVal e As System.Windows.Forms.PaintEventArgs) Implements IDrawShape.DrawShape
  139.         e.Graphics.DrawImage(m_pic, shape.rect)
  140.     End Sub
  141.     Sub New(ByVal file As String)
  142.         m_pic = New System.Drawing.Bitmap(file)
  143.     End Sub
  144. End Class
  145.  
  146.  
  147. Public Interface IDrawShape
  148.     Sub DrawShape(ByVal shape As Shape, ByVal e As PaintEventArgs)
  149. End Interface
  150.  
  151. Public Interface IDrawShapeOpts
  152.     Property pen() As Pen
  153.     Property brush() As Brush
  154. End Interface
  155.  
  156. Public Interface IMoveShape
  157.     Sub MoveShape(ByVal shape As Shape)
  158. End Interface
  159.  
  160. Public Interface ICollisionTest
  161.     Function IsCollided(ByVal shape1 As Shape, ByVal shape2 As Shape) As Boolean
  162. End Interface
  163.  
  164. Public Interface ICollisionAction
  165.     Sub CollideShapes(ByVal shape1 As Shape, ByVal shape2 As Shape)
  166. End Interface
  167.  
  168.  
  169. Public Class MoveShapeReflect
  170.     Implements IMoveShape
  171.     Dim pt As New Windows.Vector
  172.     Public Sub MoveShape(ByVal shape As Shape) Implements IMoveShape.MoveShape
  173.         Dim r As RectangleF = shape.rect()
  174.         pt.X = shape.velocity.X
  175.         pt.Y = shape.velocity.Y
  176.         r.Offset(pt.X, pt.Y)
  177.         Dim box As RectangleF = shape.Universe.BoundingBox
  178.         If r.Left < box.Left Then
  179.             r.X = box.Left
  180.             pt.X = -pt.X
  181.         ElseIf r.Right > box.Right Then
  182.             r.X = box.Right - r.Width
  183.             pt.X = -pt.X
  184.         End If
  185.         If r.Top < box.Top Then
  186.             r.Y = box.Top
  187.             pt.Y = -pt.Y
  188.         ElseIf r.Bottom > box.Bottom Then
  189.             r.Y = box.Bottom - r.Height
  190.             pt.Y = -pt.Y
  191.         End If
  192.         shape.rect = r
  193.         shape.velocity = pt
  194.     End Sub
  195. End Class
  196.  
  197. Public Class Shape
  198.     Dim m_rect As New RectangleF
  199.     Dim m_drawObj As IDrawShape
  200.     Dim m_velocity As Windows.Vector
  201.     Dim m_moveObj As IMoveShape = New MoveShapeReflect
  202.     Dim m_univ As Universe
  203.     Dim m_bCollided As Boolean
  204.     Dim m_type As Integer
  205.     Dim m_brushOld As Brush = Brushes.Red
  206.  
  207.     Property DrawObj() As IDrawShape
  208.         Get
  209.             Return m_drawObj
  210.         End Get
  211.         Set(ByVal value As IDrawShape)
  212.             m_drawObj = value
  213.         End Set
  214.     End Property
  215.     Property rect() As RectangleF
  216.         Get
  217.             Return m_rect
  218.         End Get
  219.         Set(ByVal value As RectangleF)
  220.             m_rect = value
  221.         End Set
  222.     End Property
  223.     Public Sub DrawShape(ByVal e As PaintEventArgs)
  224.         If m_drawObj IsNot Nothing Then
  225.             m_drawObj.DrawShape(Me, e)
  226.         End If
  227.     End Sub
  228.     Public Property velocity() As Windows.Vector
  229.         Get
  230.             Return m_velocity
  231.         End Get
  232.         Set(ByVal value As Windows.Vector)
  233.             m_velocity = value
  234.         End Set
  235.     End Property
  236.     Public Sub MoveShape()
  237.         If m_moveObj IsNot Nothing Then
  238.             m_moveObj.MoveShape(Me)
  239.         End If
  240.     End Sub
  241.     Property MoveObj() As IMoveShape
  242.         Get
  243.             Return m_moveObj
  244.         End Get
  245.         Set(ByVal value As IMoveShape)
  246.             m_moveObj = value
  247.         End Set
  248.     End Property
  249.     Property Universe() As Universe
  250.         Get
  251.             Return m_univ
  252.         End Get
  253.         Set(ByVal value As Universe)
  254.             m_univ = value
  255.         End Set
  256.     End Property
  257.     Property Collided() As Boolean
  258.         Get
  259.             Return m_bCollided
  260.         End Get
  261.         Set(ByVal value As Boolean)
  262.             If m_bCollided <> value Then
  263.                 Dim opts As IDrawShapeOpts = TryCast(m_drawObj, IDrawShapeOpts)
  264.                 If opts IsNot Nothing Then
  265.                     Dim br As Brush = opts.brush
  266.                     opts.brush = m_brushOld
  267.                     m_brushOld = br
  268.                 End If
  269.             End If
  270.             m_bCollided = value
  271.         End Set
  272.     End Property
  273.     Property ShapeType() As Integer
  274.         Get
  275.             Return m_type
  276.         End Get
  277.         Set(ByVal value As Integer)
  278.             m_type = value
  279.         End Set
  280.     End Property
  281. End Class
  282.  
  283. Public Class Universe
  284.  
  285.     Dim m_shapes As New List(Of Shape)
  286.     Dim m_box As RectangleF
  287.  
  288.     Dim m_shAdd As New List(Of Shape)
  289.     Dim m_shDel As New List(Of Shape)
  290.  
  291.     Dim m_bEditEnabled As Boolean = True
  292.  
  293.     Const nShapeTypes As Integer = 10
  294.  
  295.     Dim m_clnTest(nShapeTypes, nShapeTypes) As ICollisionTest
  296.     Dim m_clnAction(nShapeTypes, nShapeTypes) As ICollisionAction
  297.  
  298.     ReadOnly Property Shapes() As List(Of Shape)
  299.         Get
  300.             Return m_shapes
  301.         End Get
  302.     End Property
  303.  
  304.     Public Sub AddShape(ByVal shape As Shape)
  305.         If m_bEditEnabled Then
  306.             shape.Universe = Me
  307.             m_shapes.Add(shape)
  308.         Else
  309.             m_shAdd.Add(shape)
  310.         End If
  311.     End Sub
  312.  
  313.     Public Sub DelShape(ByVal shape As Shape)
  314.         If m_bEditEnabled Then
  315.             m_shapes.Remove(shape)
  316.         Else
  317.             m_shDel.Add(shape)
  318.  
  319.         End If
  320.     End Sub
  321.  
  322.     Public Sub Draw(ByVal e As PaintEventArgs)
  323.         For i As Integer = 0 To m_shapes.Count - 1
  324.             'e.Graphics.DrawEllipse(Pens.Black, m_shapes(i).rect())
  325.             m_shapes(i).DrawShape(e)
  326.         Next
  327.     End Sub
  328.     Public Sub DoTimeStep()
  329.  
  330.         m_bEditEnabled = False
  331.         For i As Integer = 0 To m_shapes.Count - 1
  332.             m_shapes(i).MoveShape()
  333.             m_shapes(i).Collided = False
  334.         Next
  335.         Dim sh1 As Shape, sh2 As Shape
  336.         'Dim r1 As RectangleF, r2 As RectangleF
  337.         For i As Integer = 0 To m_shapes.Count - 1
  338.             sh1 = m_shapes(i)
  339.             For j As Integer = i + 1 To m_shapes.Count - 1
  340.                 sh2 = m_shapes(j)
  341.                 If Not sh1.rect.IntersectsWith(sh2.rect) Then
  342.                     Continue For
  343.                 End If
  344.                 If m_clnTest(sh1.ShapeType, sh2.ShapeType) Is Nothing Then
  345.                     Continue For
  346.                 End If
  347.                 If m_clnTest(sh1.ShapeType, sh2.ShapeType).IsCollided(sh1, sh2) Then
  348.                     If m_clnAction(sh1.ShapeType, sh2.ShapeType) IsNot Nothing Then
  349.                         m_clnAction(sh1.ShapeType, sh2.ShapeType).CollideShapes(sh1, sh2)
  350.                     End If
  351.                     sh1.Collided = True
  352.                     sh2.Collided = True
  353.                 End If
  354.             Next
  355.         Next
  356.         m_bEditEnabled = True
  357.         For i = 0 To m_shAdd.Count - 1
  358.             AddShape(m_shAdd(i))
  359.         Next
  360.         For i = 0 To m_shDel.Count - 1
  361.             DelShape(m_shDel(i))
  362.  
  363.         Next
  364.         m_shAdd.Clear()
  365.         m_shDel.Clear()
  366.     End Sub
  367.     Property BoundingBox() As RectangleF
  368.         Get
  369.             Return m_box
  370.         End Get
  371.         Set(ByVal value As RectangleF)
  372.             m_box = value
  373.         End Set
  374.     End Property
  375.     Property CollisionTest(ByVal i As Integer, ByVal j As Integer) As ICollisionTest
  376.         Get
  377.             Return m_clnTest(i, j)
  378.         End Get
  379.         Set(ByVal value As ICollisionTest)
  380.             m_clnTest(i, j) = value
  381.         End Set
  382.     End Property
  383.     Property CollisionAction(ByVal i As Integer, ByVal j As Integer) As ICollisionAction
  384.         Get
  385.             Return m_clnAction(i, j)
  386.         End Get
  387.         Set(ByVal value As ICollisionAction)
  388.             m_clnAction(i, j) = value
  389.         End Set
  390.     End Property
  391. End Class

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


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

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

13   голосов , оценка 3.692 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы