Coder Social home page Coder Social logo

pacmanblazor's Introduction

Build GitHub release GitHub license GitHub issues GitHub issues-closed

Welcome to Pac-Man written in C# and running on Blazor WebAssembly

PLAY IT NOW! https://stevedunn.github.io/PacManBlazor/

This is a project I started in order to experiment with Blazor/WebAssembly. I wanted to see if it was feasible to use Blazor to write a game in C# that runs at 60FPS.

The answer is mostly YES

Although care was needed with heap allocations in the main game-loop (which has to take less than 16 milliseconds)

The game basically manipulates the HTML 5 canvas from C# code via the interop provided by Blazor.

FAQ

How does it interact with the canvas?

It uses the .NET NuGet package Blazor.Extensions.Canvas

What are the dimensions of the canvas?

All drawing is done as on 224x314 canvas. This is upscaled by 3x3 (672/944) 672/944 is a 0.711 aspect ratio

How does sound work?

It uses Howler. sound.js is loaded and exposes SoundPlayer. In the C# code, GameSoundPlayer loads the sound effects via SoundLounder which interact with Howler via IJSRuntime.
Each sound in C# is represented by SoundEffect which again uses IJSRuntime to call methods on the JS SoundPlayer, e.g. _runtime.InvokeVoidAsync("soundPlayer.play", name]).

When a sound has stopped playing, our end event on Howler calls back into the C# code.
This is needed to loop things like the sirens and other long playing effects.

How does touch / swipe / pan work?

It uses Hammer.js.

What happens when the game first starts?

First, we override OnInitializedAsync in index.blazor (which is the Blazor class that derives from ComponentBase).

In this method, we initialise the C# world and then call setDrawCallback in the JS code. setDrawCallback does these things:

  • initialises Hammer for input
  • registers for KeyDown, KeyUp events on window; the events are fed through to the C# code
  • calls requestAnimationFrame on window; every frame it then calls the C# code

Then, back in Blazor, we override OnAfterRenderAsync and initialise the canvases. There are 3 canvases used, one is the output canvas that we draw on, and the other two are canvases for each player's maze (it's a one or two player game).

What happens when the game runs?

In JS, at startup, we called requestAnimationFrame. This means that every frame (60 per second), a JS method is called and that method calls the C# code. The C# code

pacmanblazor's People

Contributors

stevedunn avatar stevedunnman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pacmanblazor's Issues

Doesn't work on MacOS, iPad, iPod

The maze isn't drawn.
When the game runs, the player's maze, which is a canvas, is manipulated (e.g. each pill are removed) and then it is drawn onto the main output canvas.
This works on Windows/Android on all browsers, but doesn't work on Apple.

image

No license specified

There is no LICENSE file in the code repository.

Should probably consider either GPLv3 or MIT.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.