我正在使用curses模块 - 当我在PyCharm上运行它时,我得到一个空屏幕

问题描述 投票:0回答:1

我构建了一个蛇游戏,我使用 Windows 11 和 Python 3.12.1,当我在 PyCharm 上运行代码时,我得到一个空屏幕,我已经安装了 windows-curses 包,该程序运行几秒钟(2- 3秒)然后完成 - 屏幕上没有显示任何内容,我尝试在输出控制台中模拟终端,但我什么也没得到。

# import necessary modules :
import random
import curses

# Initialize/Start the curses :
screen = curses.initscr()

# Hide the mouse cursor :
curses.curs_set(0)

# Get max screen height and width :
screen_height, screen_width = screen.getmaxyx()

# Create a new window :
window = curses.newwin(screen_height, screen_width, 0, 0)
print("Window initialized")  # Before refresh
window.refresh()
print("Window refreshed")    # After refresh

# Allow window to receive input from the keypad :
window.keypad(True)

# Set the delay for updating the screen :
window.timeout(125)

# Set x,y coordinates of the initial position of snake's head :
snake_y = screen_height // 2
snake_x = screen_width // 4

# Define the initial position of the snake body :
snake = [
    [snake_y, snake_x],
    [snake_y, snake_x-1],
    [snake_y, snake_x-2]
]

# Create the food in the middle of the window:
food = [screen_height // 2, screen_width // 2]
# food = [600//2, 400//2] ---> food = [300, 200]

# Add the food, by using PI character, from curses module :
window.addch(food[0], food[1], curses.ACS_PI)

# Set initial movement direction to right :
key = curses.KEY_RIGHT

# Create a game loop forever, until the player loses or quits the fucking game :
while True:
    # Get the next key, that will be pressed by the player :
    next_key = window.getch()

    # if: user doesn't input anything: key ramains the same
    # else: key will be set to the new pressed key
    key = key if next_key == -1 else next_key

    # Check if snake collided with the walls or itself :
    if snake[0][0] in [0, screen_height] or snake[0][1] in [0, screen_width] or snake[0] in snake[1:]:
        curses.endwin()  # Colsing the window
        quit()  # Exiting the program

    # Set the new position of the snake head --> based on the input direction :
    new_head = [snake[0][0], snake[0][1]]

    if key == curses.KEY_DOWN:
        new_head[0] += 1

    if key == curses.KEY_UP:
        new_head[0] -= 1

    if key == curses.KEY_RIGHT:
        new_head[1] += 1

    if key == curses.KEY_LEFT:
        new_head[1] -= 1

    # Insert the new head to the first position of snake list :
    snake.insert(0, new_head)

    # Check if snake ate the food :
    if snake[0] == food:
        food = None  # Removing the food after snake eats it

        # While food is removed, generate new food in a random place on the screen :
        while food is None:
            new_food = [
                random.randint(1, screen_height-1),  # 1, 599 e.g
                random.randint(1, screen_width-1)  # 1, 399 e.g
            ]

        # دي ما محتاجة توضيح من كتر ما اني مبسوط بيها
            food = new_food if new_food not in snake else None

        # Add the new food to screen :
        window.addch(food[0], food[1], curses.ACS_PI)

    # Otherwise remove the last segmant(the last list on snake) of snake body :
    else:
        tail = snake.pop()
        window.addch(tail[0], tail[1], ' ')

    # Update the position of the snake on the screen :
    window.addch(snake[0][0], snake[0][1], curses.ACS_CKBOARD)

我希望 PyCharm 在屏幕上显示游戏。

python python-3.x windows pycharm windows-11
1个回答
0
投票

PyCharm 不支持诅咒,您必须打开终端窗口并手动运行脚本。我遇到了同样的问题,所以我打开了一个电源外壳窗口并移动到正确的目录,每次我需要查看它的运行情况时只需运行脚本即可。可能有一个插件可以工作,但我没有打扰。

你也有吗:

pip 安装 windows-curses 因为它在 Windows 中不是默认的。

© www.soinside.com 2019 - 2024. All rights reserved.