Coffeehouse Thread

8 posts

How to built a C# of the player dodging/avoiding falling objects

Back to Forum: Coffeehouse
  • User profile image
    Qobacha

    Hi,

    I have an assignment to develop a game that consists of a player and a raindrops. The objective of the game is to move the player so that he does not get wet that is the player must dodge the raindrops. If a raindrop touches the player then the game is over.

    Your help is highly appreciated.

  • User profile image
    Sven Groot

    What have you tried, and where are you getting stuck? We aren't here to do your work for you.

  • User profile image
    Maddus Mattus

    Player player = new Player();
    List<RainDrops> drops = new List<RaindDrops>();
    
    while(true)
    {
    if(player.BoundingBox.CollidesWith(drops))
    {
    GameOver();
    }
    else
    {
    RenderNextScene();
    }
    Thread.Sleep(500);
    }

  • User profile image
    ManipUni

    class Program
    {
    struct Rain
    {
    public int x;
    public int y;
    public char[] c;
    }
    static void Main(string[] args)
    {
    char[,] board = new char[10, 50];
    int playerY = 25;
    List<Rain> rain = new List<Rain>();
    Random rnd = new Random(DateTime.Now.Millisecond);
    bool continueGame = true;
    do
    {
    // 1/2 chance of making rain this key-press 
    if (rnd.Next(1, 10) > 5)
    rain.Add(new Rain() { x = -1, y = rnd.Next(0, board.GetLength(1)), c = new char[0x3EFFFFF] });
    // Move the rain down, destroy the rain, and check for player collision. 
    for (int n = 0; n < rain.Count; n++)
    {
    Rain temp = rain[n];
    if (temp.x < board.GetLength(0) - 1)
    temp.x++;
    else if (temp.x == board.GetLength(0) - 1)
    {
    if (temp.y == playerY)
    {
    Console.Clear();
    Console.WriteLine("You died!");
    Console.ReadKey();
    return;
    }
    rain.RemoveAt(n);
    continue;
    }
    rain[n] = temp;
    }
    // Draw the board & player to a buffer 
    char[,] buffer = new char[board.GetLength(0), board.GetLength(1)];
    for (int x = 0; x < board.GetLength(0); x++)
    {
    for (int y = 0; y < board.GetLength(1); y++)
    {
    if (x == board.GetLength(0) - 1 && y == playerY)
    buffer[x, y] = '@';
    else
    buffer[x, y] = ' ';
    }
    }
    // Add enemies to the board 
    foreach (Rain rt in rain)
    {
    buffer[rt.x, rt.y] = '*';
    }
    // Convert our char board to a string (StringBuilder is faster due to pre-allocation) 
    StringBuilder output = new StringBuilder();
    for (int x = 0; x < board.GetLength(0); x++)
    {
    for (int y = 0; y < board.GetLength(1); y++)
    {
    output.Append(buffer[x, y]);
    }
    output.Append(Environment.NewLine);
    }
    // Add "ground" under the player for pretty 
    for (int y = 0; y < board.GetLength(1); y++)
    {
    output.Append('-');
    }
    output.Append(Environment.NewLine);
    output.Append("Press Q to quit. A or Left and D or Right to avoid the 'rain'.");
    output.Append(Environment.NewLine);
    Console.Clear();
    Console.Write(output);
    switch (Console.ReadKey().Key)
    {
    case ConsoleKey.Q:
    continueGame = false;
    break;
    case ConsoleKey.LeftArrow:
    case ConsoleKey.A:
    if (playerY > 1)
    playerY--;
    break;
    case ConsoleKey.RightArrow:
    case ConsoleKey.D:
    if (playerY < board.GetLength(1) - 1)
    playerY++;
    break;
    }
    }
    while (continueGame);
    }
    }
    

    PS - Random.Next(x, y) is bugged. Technically it should be rnd.Next(0, board.GetLength(1) - 1) but since Random.Next() never returns MaxValue it is safe to call it in this way. It is Microsoft's bug, not mine...   

  • User profile image
    evildictait​or

    , ManipUni wrote

    Random.Next(x, y) is bugged. Technically it should be rnd.Next(0, board.GetLength(1) - 1) but since Random.Next() never returns MaxValue it is safe to call it in this way. It is Microsoft's bug, not mine...   

    It's by design. Random.Next(i,j) returns a value between i inclusive and j exclusive. It's more common that you want a [i, j) bound than an [i, j] one.

  • User profile image
    ManipUni

    @evildictaitor: Two issues with that theory:    
     - First off if that was true then the arg names make no sense. They are "minValue" and "maxValue" clearly implying that the values provided can be included (otherwise they would call them "lowerBound" and "upperBound").   
     - Secondly Microsoft acknowledged it was a bug:
    http://connect.microsoft.com/VisualStudio/feedback/details/95744/random-next-minvalue-maxvalue-never-returns-maxvalue 

  • User profile image
    Sven Groot

    But that report was closed as "by design".

  • User profile image
    Blue Ink

    @Sven Groot: "as designed with a bad hangover" would be more fitting. I am perfectly happy with the way the method works, but... what would you expect this code to do?

    var a = random.Next (10, 10);

    I already have misgivings about random.Next (10, 11) not throwing: a random method having a constant output is an oximoron, but I understand how it may be convenient to have it that way. The above, instead, is just evil.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.