诅咒贪吃蛇游戏不移除 linux os 中的单元格
Curses snake game not removing cells in linux os
这是我用来掌握它的 curses 教程中的代码,但即使我已经多次检查代码,它仍然没有删除旧单元格。编写代码的人在 mac 上,而我正在使用 linux,那会不会有问题?
import curses
import time
import random
screen = curses.initscr()
dims = screen.getmaxyx()
def game():
screen.nodelay(1)
head = [1, 1]
body = [head[:]]*5
screen.border()
direction = 0 # 0:right, 1:down, 2:left, 3:up
gameover = False
while not gameover:
deadcell = body[-1][:]
if deadcell not in body:
screen.addch(deadcell[0], deadcell[1], ' ')
screen.addch(head[0], head[1], 'X')
if direction == 0:
head[1] += 1
elif direction == 2:
head[1] -= 1
elif direction == 1:
head[0] += 1
elif direction == 3:
head[0] -= 1
deadcell = body[-1][:]
for z in range(len(body)-1, 0, -1):
body[z] = body[z-1][:]
body[0] = head[:]
if screen.inch(head[0], head[1]) != ord(' '):
gameover = True
screen.move(dims[0]-1, dims[1]-1)
screen.refresh()
time.sleep(0.1)
game()
curses.endwin()
问题似乎出在以下几行附近:
while not gameover:
deadcell = body[-1][:]
if deadcell not in body:
screen.addch(deadcell[0], deadcell[1], ' ')
deadcell
总是在 body
中,因此不会清除任何单元格。
试试这个:
deadcell = body[-1][:]
while not gameover:
if deadcell not in body:
screen.addch(deadcell[0], deadcell[1], ' ')
这是我用来掌握它的 curses 教程中的代码,但即使我已经多次检查代码,它仍然没有删除旧单元格。编写代码的人在 mac 上,而我正在使用 linux,那会不会有问题?
import curses
import time
import random
screen = curses.initscr()
dims = screen.getmaxyx()
def game():
screen.nodelay(1)
head = [1, 1]
body = [head[:]]*5
screen.border()
direction = 0 # 0:right, 1:down, 2:left, 3:up
gameover = False
while not gameover:
deadcell = body[-1][:]
if deadcell not in body:
screen.addch(deadcell[0], deadcell[1], ' ')
screen.addch(head[0], head[1], 'X')
if direction == 0:
head[1] += 1
elif direction == 2:
head[1] -= 1
elif direction == 1:
head[0] += 1
elif direction == 3:
head[0] -= 1
deadcell = body[-1][:]
for z in range(len(body)-1, 0, -1):
body[z] = body[z-1][:]
body[0] = head[:]
if screen.inch(head[0], head[1]) != ord(' '):
gameover = True
screen.move(dims[0]-1, dims[1]-1)
screen.refresh()
time.sleep(0.1)
game()
curses.endwin()
问题似乎出在以下几行附近:
while not gameover:
deadcell = body[-1][:]
if deadcell not in body:
screen.addch(deadcell[0], deadcell[1], ' ')
deadcell
总是在 body
中,因此不会清除任何单元格。
试试这个:
deadcell = body[-1][:]
while not gameover:
if deadcell not in body:
screen.addch(deadcell[0], deadcell[1], ' ')