web-devkits / webinizer

Webinizer is a collection of tools to convert native applications, such as these programmed by C/C++/Rust, to Web applications which are constructed by HTML/CSS/JavaScript and WebAssembly.
Apache License 2.0
20 stars 7 forks source link

The migration of Snake Game project #57

Closed NingW101 closed 9 months ago

NingW101 commented 10 months ago

The Snake Game is classic game using SDL2, we are trying to successfully build and add it into the demos pool. Moreover, we can enhance the current advisors' implementation during porting. This issue will track the problems encountered and the corresponding solutions during the migration

The source code link of Snake Game: https://github.com/udacity/CppND-Capstone-Snake-Game

NingW101 commented 10 months ago

After adding Snake project(upload with zip or pull with Github repo link), click Step to enter the build steps configuration page, then APPLY recommended builder steps then click Build.

During the Building process, webinizer will give following recipes.

NingW101 commented 10 months ago

In the original code of Snake game, the score will be displayed when user quit or end up the game, which could not be normally realized in the browser and we can make some modification to solve that.

We can insert the score prompt logic when Snake died as followings,

//game.cpp
void Game::Update() {
  if (!snake.alive){
    EM_ASM({
        var message = 'Game Over...\nYour Score: ' +  $0 + '\nSize: '+ $1;
        alert(message);
      }, score, snake.size);
    emscripten_cancel_main_loop();
    return;
  }

  snake.Update();

  int new_x = static_cast<int>(snake.head_x);
  int new_y = static_cast<int>(snake.head_y);

  // Check if there's food over here
  if (food.x == new_x && food.y == new_y) {
    score++;
    PlaceFood();
    // Grow snake and increase speed.
    snake.GrowBody();
    snake.speed += 0.02;
  }
}

image

NingW101 commented 9 months ago

close as the demo has been added