js+css实现小行星太空街机游戏

代码语言:html

所属分类:游戏

代码描述:js+css实现小行星太空街机游戏,基于浏览器的街机太空游戏。玩家操控飞船,击毁来袭小行星,在限时波次攻势中求生,拾取生命补给与强化道具,不断冲击更高分数。

代码标签: js css 小行星 太空 街机 游戏

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />

<style>
    @import url("https://fonts.googleapis.com/css2?family=Orbitron:wght@600;700;800&family=Oxanium:wght@500;700;800&display=swap");

:root {
  --bg: #050514;
  --panel: rgba(12, 18, 40, 0.82);
  --panel-soft: rgba(14, 24, 52, 0.7);
  --stroke: rgba(138, 173, 255, 0.28);
  --text: #eef4ff;
  --muted: #b9c8e8;
  --accent: #7ec7e6;
  --danger: #ff8f8f;
  --ok: #7ff3b0;
  --warn: #ffd58a;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: "Oxanium", "Segoe UI", Arial, sans-serif;
}

#app {
  position: relative;
  width: 100%;
  height: 100%;
}

#gameCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  background: var(--bg);
  touch-action: none;
  user-select: none;
}

.hud {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  width: min(1020px, calc(100% - 24px));
  display: grid;
  grid-template-columns: repeat(4, minmax(120px, 1fr));
  gap: 8px;
  z-index: 5;
  pointer-events: none;
}

.stat {
  background: var(--panel);
  border: 1px solid var(--stroke);
  border-radius: 12px;
  padding: 8px 10px;
  box-shadow: 0 8px 20px rgba(4, 8, 24, 0.45);
}

.label {
  font-family: "Orbitron", "Oxanium", sans-serif;
  font-size: 12px;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  color: var(--muted);
  line-height: 1;
}

.value {
  font-family: "Orbitron", "Oxanium", sans-serif;
  font-size: 24px;
  font-weight: 800;
  line-height: 1.05;
  margin-top: 4px;
}

.controls {
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
  z-index: 8;
}

button {
  font-family: inherit;
  border: 1px solid rgba(116, 158, 255, 0.45);
  background: linear-gradient(180deg, #223266, #18264f);
  color: var(--text);
  border-radius: 12px;
  padding: 10px 16px;
  font-size: 20px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 8px 20px rgba(2, 5, 18, 0.4);
  transition: transform 120ms ease, filter 120ms ease;
}

.controls button,
.actions button {
  font-family: "Orbitron", "Oxanium", sans-serif;
  letter-spacing: 0.4px;
}

button:hover {
  transform: translateY(-1px);
  filter: brightness(1.08);
}

button:active {
  transform: translateY(0);
}

.btn-accent {
  border-color: rgba(126, 199, 230, 0.65);
  background: linear-gradient(180deg, #5ca7d8, #3a7daf);
  color: #e9f8ff;
}

.btn-danger {
  border-color: rgba(255, 120, 120, 0.6);
  background: linear-gradient(180deg, #cb5252, #9c3636);
  color: #fff1f1;
}

.panel {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(3, 7, 20, 0.55);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 220ms ease, visibility 220ms ease;
  z-index: 12;
  padding: 18px;
}

.panel.show {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.card {
  width: min(720px, 100%);
  background: linear-gradient(
    180deg,
    rgba(18, 31, 70, 0.98),
    rgba(12, 22, 52, 0.98)
  );
  border: 1px solid rgba(133, 173, 255, 0.35);
  border-radius: 18px;
  padding: 28px;
  box-shadow: 0 24px 46px rgba(1, 6, 19, 0.55);
  transform: translateY(14px) scale(0.985);
  opacity: 0.88;
  transition: transform 260ms cubic-bezier(0.2, 0.7, 0.15, 1), opacity 220ms ease;
}

.panel.show .card {
  transform: translateY(0) scale(1);
  opacity: 1;
}

h1,
h2,
p {
  margin: 0;
}

.title {
  font-family: "Orbitron", "Oxanium", sans-serif;
  font-size: clamp(52px, 10vw, 120px);
  text-align: center;
  line-height: 0.95;
  letter-spacing: 1px;
  color: #f0f6ff;
  text-shadow: 0 4px 0 rgba(80, 117, 201, 0.35);
}

.subtitle {
  margin-top: 14px;
  text-align: center;
  font-size: 22px;
  color: var(--muted);
}

.help {
  margin-top: 22px;
  background: var(--panel-soft);
  border: 1px solid rgba(132, 169, 255, 0.28);
  border-radius: 12px;
  padding: 12px;
  font-size: 18px;
  color: #dde9ff;
  text-align: center;
}

.actions {
  margin-top: 18px;
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

#gameOverPanel.show .card {
  animation: game-over-slide-in 520ms ease-in-out;
}

@keyframes game-over-slide-in {
  0% {
    transform: translateY(-90px) scale(0.98);
    opacity: 0;
  }

  100% {
    transform: translateY(0) scale(1);
    opacity: 1;
  }
}

.over-row {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(162, 192, 255, 0.24);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 24px;
}

.over-row strong {
  font-size: 30px;
}

#toast {
  position: absolute;
  top: 88px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(18, 30, 64, 0.92);
  border: 1px solid rgba(132, 171, 255, 0.5);
  border-radius: 12px;
  padding: 8px 14px;
  color: #e9f4ff;
  z-index: 20;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
}

#toast.show {
  opacity: 1;
}

@media (max-width: 820px) {
  .hud {
    grid-template-columns: repeat(2, minmax(120px, 1fr));
  }

  button {
    font-size: 18px;
  }

  .value {
    font-size: 20px;
  }

  .subtitle {
    font-size: 18px;
  }
}


/* Home-screen audio controls */
.home-audio-controls {
  margin-top: 18px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.home-audio-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 13px 14px;
  border: 1px solid rgba(132, 169, 255, 0.28);
  border-radius: 12px;
  background: var(--panel-soft);
  cursor: pointer;
}

.home-audio-copy {
  display: grid;
  gap: 2px;
}

.home-audio-copy strong {
  font-family: "Orbitron", "Oxanium", sans-serif;
  font-size: 15px;
}

.home-audio-copy small {
  color: var(--muted);
  font-size: 14px;
}

.switch-control {
  position: relative;
  flex: 0 0 auto;
  width: 52px;
  height: 28px;
}

.switch-control input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.switch-track {
  position: absolute;
  inset: 0;
  border: 1px solid rgba(143, 173, 235, 0.42);
  border-radius: 999px;
  background: #152044;
  transition: background 160ms ease, border-color 160ms ease;
}

.switch-track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #d9e6ff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
  transition: transform 160ms ease, background 160ms ease;
}

.switch-control input:checked + .switch-track {
  border-color: rgba(126, 199, 230, 0.78);
  background: #357fae;
}

.switch-control input:checked + .switch-track::after {
  transform: translateX(24px);
  background: #ffffff;
}

.switch-control input:focus-visible + .switch-track {
  outline: 3px solid rgba(126, 199, 230, 0.42);
  outline-offset: 2px;
}

/* Sound settings */
.settings-card {
  width: min(570px, 100%);
}

.settings-title {
  font-family: "Orbitron", "Oxanium", sans-serif;
  font-size: 34px;
  text-align: center;
}

.settings-description {
  margin-top: 8px;
  color: var(--muted);
  text-align: center;
  line-height: 1.45;
}

.audio-setting {
  margin-top: 16px;
  padding: 15px;
  border: 1px solid rgba(132, 169, 255, 0.26);
  border-radius: 13px;
  background: rgba(14, 24, 52, 0.72);
}

.audio-setting-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  font-size: 18px;
  font-weight: 700;
}

.audio-setting-label output {
  min-width: 48px;
  color: var(--accent);
  text-align: right;
}

.audio-range {
  width: 100%;
  margin-top: 12px;
  accent-color: #5ca7d8;
}

.audio-setting-note {
  margin-top: 7px;
  color: var(--muted);
  font-size: 14px;
}

.settings-actions {
  justify-content: flex-end;
}

@media (max-width: 640px) {
  .home-audio-controls {
    grid-template-columns: 1fr;
  }
}


/* Pause screen */
.pause-card {
  width: min(520px, 100%);
  text-align: center;
}

.pause-title {
  font-family: "Orbitron", "Oxanium", sans-serif;
  font-size: clamp(36px, 7vw, 56px);
}

.pause-description {
  margin-top: 10px;
  color: var(--muted);
  font-size: 18px;
  line-height: 1.45;
}

.controls button {
  white-space: nowrap;
}

@media (max-width: 700px) {
  .controls {
    width: calc(100% - 20px);
    gap: 7px;
    flex-wrap: wrap;
    justify-content: center;
  }

  .controls button {
    padding: 8px 11px;
    font-size: 15px;
  }
}


/* CodePen-safe fullscreen mode */
#app:fullscreen,
#app:-webkit-full-screen {
  width: 100vw;
  height: 100vh;
  background: var(--bg);
}

#app:fullscreen #gameCanvas,
#app:-webkit-full-screen #gameCanvas {
  width: 100%;.........完整代码请登录后点击上方下载按钮下载查看

网友评论0