gpt4 book ai didi

python - 诅咒蛇游戏不删除 linux 操作系统中的单元格

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:12:58 26 4
gpt4 key购买 nike

这是我用来掌握它的 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], ' ')

关于python - 诅咒蛇游戏不删除 linux 操作系统中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34421309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com