You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python文本类躲避游戏:实现多物体同时下落及代码优化需求

文本类躲避游戏功能扩展与代码优化请求

我是Python新手,正在通过Grok学习编程,制作了一款文本类躲避游戏。当前游戏仅能实现单个标记为c的物体下落,希望对游戏进行以下修改并优化现有代码:

  • 支持多个物体同时下落:每次玩家按a或d左右移动时,所有物体下移一行
  • 物体触底后消失,并在顶部随机列生成新物体
  • 若玩家被物体击中则游戏重启
  • 物体触底时得分加1

当前代码

import random
# Sets up Board
grid = []
x = 1
cx = random.randrange(3)
cy = 0
for i in range(4):
  row = []
  for j in range(3):
    row.append('.')
  grid.append(row)
for k in range(3):
  grid[3][k] = '='
grid[2][x] = 'x'
grid[cy][cx] = 'c'
# Draws Board
for row in grid:
  print(''.join(row))
# Movement (Left or Right)
d = input('a or d? ')
while d:
  dx = x
  cdy = cy
  if d == 'a':
    x -= 1
    if x < 0:
      x += 1
  if d == 'd':
    x += 1
    if x > 2:
      x -= 1
  cy += 1 # Makes objects come closer
# Checks if objects are touching floor and if so creates new object at random column
  if cy > 2:
    cy = 0
    grid[cdy][cx] = '.'
    cx = random.randrange(3)
# Ends game if player gets hit by object
  if cx == x and cy == 2:
    if x != dx:
      grid[2][dx] = '.'
    grid[cdy][cx] = '.'
    grid[2][x] = 'x'
    grid[cy][cx] = 'c'
    for row in grid:
      print(''.join(row))
    print('You got hit by a car :(')
    break
  if x != dx:
    grid[2][dx] = '.'
  grid[cdy][cx] = '.'
  grid[2][x] = 'x'
  grid[cy][cx] = 'c'
  for row in grid:
    print(''.join(row))
  d = input('a or d? ')

现有棋盘样式

.c.
...
.x.
===

其中x代表玩家,c代表下落物体。

期望的棋盘样式

..c
...
.c.
...
c..
...
.c.
.x.
===

内容的提问来源于stack exchange,提问作者Gwotchya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 18:43:20