Capacitación básica en Computación

En general, la detección de colisiones trata de cómo determinar la intersección de dos objetos en movimiento.
La detección de colisiones en los juegos necesita de los siguientes pasos:
- Selección de dos objetos para la prueba de colisión.
- Comprobación de si se produjo una colisión entre los objetos identificados o no.
- Se dice que dos objetos chocan cuando entran en contacto el uno con el otro. Los objetos pueden moverse después del choque o pueden detenerse, dependiendo del tipo de colisión.
El siguiente juego contiene dos objetos: la tortuga y el objetivo. El jugador tiene que hacer que la tortuga golpee el objetivo, introduciento para ello el ángulo correcto y la distancia.
Código:
- ‘ Copyright (c) Microsoft Corporation. Todos los derechos reservados.
- GraphicsWindow.Hide()
- gw = 620
- gh = 450
- GraphicsWindow.BackgroundColor = “LightBlue”
- GraphicsWindow.CanResize = “False”
- GraphicsWindow.Width = gw
- GraphicsWindow.Height = gh
- GraphicsWindow.Top = ( Desktop.Height – gh ) / 2
- GraphicsWindow.Left = ( Desktop.Width – gw ) / 2
- GraphicsWindow.Title = “Dar en el blanco”
- GraphicsWindow.Show()
- Turtle.Hide()
- rx = 40
- ry = 20
- tortugaX = 315
- tortugaY = 300
- ancho = 550
- alto = 390
- CuadroPuntuaciónX = 400
- TeclaEntrar = “Return”
- diana = 10
- vidas = 3
- puntuación = 0
- GraphicsWindow.FontSize = 30
- GraphicsWindow.DrawText(10, 200, “¡Pulse ENTRAR para comenzar el juego!”)
- GraphicsWindow.KeyDown = AlPresionar
- Sub AlPresionar
- entrar = entrar + 1
- If GraphicsWindow.LastKey = TeclaEntrar And entrar = 1 Then
- GraphicsWindow.Clear()
- GraphicsWindow.FontSize = 12
- Interfaz()
- MoverFigura()
- MostrarPuntuación()
- Controls.ButtonClicked
= Disparar - EndIf
- EndSub
- Sub Disparar
- ángulo = Math.Round(Controls.GetTextBoxText(ángulotexto))
- distancia = Math.Round(Controls.GetTextBoxText(distanciatexto))
- presionado = Controls.GetButtonCaption(Controls.LastClickedButton)
- If presionado = “Disparar” Then
- GraphicsWindow.PenWidth = 0
- Turtle.Angle = ángulo
- Turtle.Move(distancia)
- If Turtle.x > x and Turtle.x < x + 93 and Turtle.y > y and Turtle.y < y + 96 Then
- Shapes.HideShape(diana)
- puntuación = puntuación + 10
- MostrarPuntuación()
- Program.Delay(500)
- If diana = puntuación Then
- TerminarJuego()
- EndIf
- Controls.SetTextBoxText(ángulotexto, “”)
- Controls.SetTextBoxText(distanciatexto, “”)
- MoverFigura()
- Else
- Program.Delay(1000)
- If vidas > 1 Then
- GraphicsWindow.ShowMessage(“Haga clic en Aceptar para continuar el juego”,”Reintentar”)
- presionado = “Reintentar”
- Else
- TerminarJuego()
- EndIf
- EndIf
- EndIf
- If presionado = “Reintentar” Then
- vidas = vidas – 1
- If puntuación >= 10 Then
- puntuación = puntuación – 5
- EndIf
- MostrarPuntuación()
- Controls.SetTextBoxText(ángulotexto, “”)
- Controls.SetTextBoxText(distanciatexto, “”)
- MoverFigura()
- If vidas < 1 Then
- TerminarJuego()
- EndIf
- EndIf
- EndSub
- Sub Interfaz
- imagen = Program.Directory + “\dibujo.png”
- GraphicsWindow.DrawRectangle(rx, ry, ancho, alto)
- diana = Shapes.AddImage(imagen)
- GraphicsWindow.BrushColor = “Black”
- Shapes.Move(diana, 100, 50)
- Turtle.Show()
- Turtle.X = tortugaX
- Turtle.y = tortugaY
- Turtle.Angle = 0
- GraphicsWindow.DrawText(rx + 5, alto – 10, “Ángulo:”)
- ángulotexto = Controls.AddTextBox(tortugaX - 205, tortugaY + 70)
- Controls.SetSize(ángulotexto, 40, 30)
- GraphicsWindow.DrawText(rx + 140, alto – 10, “Distancia:”)
- distanciatexto = Controls.AddTextBox(tortugaX - 60, tortugaY + 70)
- Controls.SetSize(distanciatexto, 40, 30)
- firebutton = Controls.AddButton(“Disparar”, tortugaX + 20, tortugaY + 70)
- Controls.SetSize(firebutton, 80, 30)
- Endsub
- Sub MoverFigura
- Shapes.ShowShape(diana)
- Turtle.x = tortugaX
- Turtle.Y = tortugaY
- Turtle.Angle = 0
- x = Math.GetRandomNumber(450)
- y = Math.GetRandomNumber(80)
- If x <= 40 Then
- x = 40
- Shapes.Animate(diana, x, y, 1000)
- ElseIf y <= 20 Then
- y = 20
- Shapes.Animate(diana, x, y, 1000)
- Else
- Shapes.Animate(diana, x, y, 1000)
- EndIf
- EndSub
- Sub TerminarJuego
- GraphicsWindow.ShowMessage(“Puntuación: ” + puntuación, “FIN”)
- Program.End()
- EndSub
- Sub MostrarPuntuación
- GraphicsWindow.FontSize = 14
- GraphicsWindow.BrushColor = “Gray”
- GraphicsWindow.FillRectangle(CuadroPuntuaciónX + 50, CuadroPuntuaciónX – 65, 135, 70)
- GraphicsWindow.BrushColor = “White”
- GraphicsWindow.DrawText(CuadroPuntuaciónX + 60, CuadroPuntuaciónX – 55, “Puntuación: “)
- GraphicsWindow.DrawText(CuadroPuntuaciónX + 150, CuadroPuntuaciónX – 55, puntuación)
- GraphicsWindow.DrawText(CuadroPuntuaciónX + 60, CuadroPuntuaciónX – 20, “Vidas: “)
- GraphicsWindow.DrawText(CuadroPuntuaciónX + 110, CuadroPuntuaciónX – 20, vidas)
- EndSub
%d bloggers like this:

No comments:
Post a Comment