npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

game-greedy-snake

v0.0.2

Published

抽象游戏逻辑,把渲染和玩法扩展交给其他开发者,毕竟核心逻辑是不变的,UI 是千变万化的。

Downloads

4

Readme

经典贪吃蛇

抽象游戏逻辑,把渲染和玩法扩展交给其他开发者,毕竟核心逻辑是不变的,UI 是千变万化的。

Usage

<script setup lang="ts">
import { onMounted, reactive } from "vue";
import hotkeys from "hotkeys-js";

import {
  Game,
  Direction,
  Obstacle,
  GameObstacle,
  GameSnakeNode,
} from "@game/greedy-snake";
const game = new Game({
  map: {
    width: 30,
    height: 25,
  },
  snake: {
    bodyLength: 3,
  },
});
/**
 * 炸弹
 */
class Bomb extends Obstacle {
  public type = "bomb";

  public produce() {
    const point = this.getRandomPoint();
    this.x = point.x;
    this.y = point.y;
  }

  public consume() {
    // 炸弹爆炸,蛇身长度减1
    this.game.snake.reduce();
    // 计数减1
    this.game.count--;
    return true;
  }
}

// game.addObstacle(new Bomb(game));
game.init();

(window as any).game = game;

const { width, height } = game.map;

const map = new Array(width * height);

const g = reactive<{
  body: GameSnakeNode[];
  obstacles: GameObstacle[];
}>({
  body: [],
  obstacles: [],
});

let started = true;

const onStart = () => {
  game.init();
  started = true;
};

console.log(game);

onMounted(() => {
  setInterval(() => {
    if (!started) {
      return;
    }
    const success = game.update();
    if (!success) {
      started = false;
    }

    g.body = game.body;
    g.obstacles = game.obstacles;
  }, 300);

  hotkeys("w", function () {
    game.setDirection(Direction.UP);
  });
  hotkeys("s", function () {
    game.setDirection(Direction.DOWN);
  });
  hotkeys("a", function () {
    game.setDirection(Direction.LEFT);
  });
  hotkeys("d", function () {
    game.setDirection(Direction.RIGHT);
  });
});
</script>

<template>
  <div>
    <h1>{{ game.count }}</h1>
    <button @click="onStart">start</button>
    <div
      :style="{
        position: 'relative',
        display: 'flex',
        flexWrap: 'wrap',
        width: `${width * 20}px`,
        height: `${height * 20}px`,
      }"
    >
      <div
        :key="index"
        v-for="(_, index) in map"
        style="width: 20px; border: 1px solid #ccc; box-sizing: border-box"
      ></div>
      <div
        :key="index"
        v-for="(item, index) in g.body"
        :style="{
          position: 'absolute',
          width: '20px',
          height: '20px',
          left: `${item.x * 20}px`,
          top: `${item.y * 20}px`,
          backgroundColor: index === 0 ? 'black' : 'blue',
        }"
      ></div>
      <div
        :key="index"
        v-for="(item, index) in game.obstacles"
        :style="{
          position: 'absolute',
          width: '20px',
          height: ' 20px',
          left: `${item.x * 20}px`,
          top: `${item.y * 20}px`,
          backgroundColor: item.type === 'food' ? 'green' : 'yellow',
        }"
      ></div>
    </div>
  </div>
</template>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}
</style>