How to make two different objects at different speeds "bounce back", only when they reach a certain point

时间:2018-10-27 13:37:54

标签: vb.net

I think from the title itself you can tell I'm an absolute noob at this. My code causes the two blocks to move and loop themselves, but since "speedone" is going at 1 integer per 50 milliseconds, and "speedtwo" is going at 3 integers, speedone never bounces to the end before it bounces back.

How do I make it so the blocks only "bounce" back once they hit the end (<80)? Sorry for any bothers^^

    log_speeds:
    'one and two are pre determined
    Randomize()

    one = 1

    two = 3

    three = random_int(1, 3)

    four = random_int(1, 3)

    five = random_int(1, 3)

    six = random_int(1, 3)

    seven = random_int(1, 3)

    eight = random_int(1, 3)

    nine = random_int(1, 3)

    ten = random_int(1, 3)

    GoTo enemies

    Console.Clear()

enemies:

    Do
        Do
            speedone = speedone + one
            Console.SetCursorPosition(speedone, 2)
            Console.BackgroundColor = ConsoleColor.White
            Console.Write(Space(3))
            Console.SetCursorPosition(speedone + 3, 2)
            Console.BackgroundColor = ConsoleColor.Black
            Console.Write(Space(5))
            Console.SetCursorPosition(speedone + 8, 2)
            Console.BackgroundColor = ConsoleColor.White
            Console.Write(Space(3))

            speedtwo = speedtwo + two
            Console.SetCursorPosition(speedtwo, 4)
            Console.BackgroundColor = ConsoleColor.White
            Console.Write(Space(3))
            Console.SetCursorPosition(speedtwo + 3, 4)
            Console.BackgroundColor = ConsoleColor.Black
            Console.Write(Space(5))
            Console.SetCursorPosition(speedtwo + 8, 4)
            Console.BackgroundColor = ConsoleColor.White
            Console.Write(Space(3))

            Sleep(50)

            Console.BackgroundColor = ConsoleColor.White
        Loop Until speedone And speedtwo > 80
        Do
            speedone = speedone - one
            Console.SetCursorPosition(speedone, 2)
            Console.BackgroundColor = ConsoleColor.White
            Console.Write(Space(3))
            Console.SetCursorPosition(speedone + 3, 2)
            Console.BackgroundColor = ConsoleColor.Black
            Console.Write(Space(5))
            Console.SetCursorPosition(speedone + 8, 2)
            Console.BackgroundColor = ConsoleColor.White
            Console.Write(Space(3))

            speedtwo = speedtwo - two
            Console.SetCursorPosition(speedtwo, 4)
            Console.BackgroundColor = ConsoleColor.White
            Console.Write(Space(3))
            Console.SetCursorPosition(speedtwo + 3, 4)
            Console.BackgroundColor = ConsoleColor.Black
            Console.Write(Space(5))
            Console.SetCursorPosition(speedtwo + 8, 4)
            Console.BackgroundColor = ConsoleColor.White
            Console.Write(Space(3))

            Sleep(50)

            Console.BackgroundColor = ConsoleColor.White
        Loop Until speedone And speedtwo < 5
    Loop

1 个答案:

答案 0 :(得分:0)

您必须跟踪每个对象分别执行的操作,并在每个游戏循环中仅对每个对象执行很小的步骤

这样做

Do
    For Each object in the list of objects
        Perform next step
    Loop
Loop

也就是说,您必须跟踪每个对象的当前状态,才能知道每个循环的下一步操作。

使用看起来像这样的类创建对象

Public Class GameObject
    Public Property Speed As Integer
    Public Property Position AS Integer
End Class

将所有对象添加到List(Of GameObject)(可以超过2个)中。

以这种方式执行下一步(请记住,这是在“对于对象列表中的每个对象”循环中)

If obj.Speed > 0 Then
    Calculate next Speed and Position for this object going forward.
    If obj reaches the end Then
        'Bounce off the end
        obj.Speed = -obj.Speed
    End If
Else
    Calculate next Speed and Position for this object going backward.
    If obj reaches the start Then
        'Bounce off the start
        obj.Speed = - obj.Speed
    End If
End If

现在,所有对象都独立反弹。

计算下一个位置可以像obj.Position = obj.Position + obj.Speed一样简单。