Python诅咒如何在终端中间打印

时间:2017-07-20 12:57:18

标签: python python-3.x ncurses curses python-curses

我正试图用诅咒制作一个小小的街机游戏,这在过去40分钟里一直困扰着我。你如何在屏幕中间打印一些东西,以及我在第一个屏幕上的语法有什么问题.addstr(游戏结束)

我正在运行linux并使用xterm。

import curses
import time
import random

color_pref = int(input('Choose color mode (0-WHITE, 1-GREEN, 2-RED, 3-BLUE): '))
screen = curses.initscr()
curses.start_color()
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.noecho()
screen.keypad(1)
curses.curs_set(0)
dims = screen.getmaxyx()
screen.refresh()

def game():
        x, y = 3, 2
        points = 5
        q, Vertical, Horizontal = -1, 1, 1
        gameover = False
        screen.border(0)

        if q == ord('q'):
            gameover = True

        if gameover == True:
            screen.clear()
            screen.border()
            screen.refresh()
            screen.nodelay(0)
            score = '1'
            message2 = 'Press Space to Play Again.'
            message3 = 'Press q to Quit.'
            message4 = 'You killed ' + score+ 'enemies'
            screen.addstr(int(int(dims[0])/2-1), int(int(dims[1])) - int(len(message))), 'Game Over!', curses.color_pair(color_pref)|curses.A_BOLD)
            screen.addstr(int(int(dims[0])/2-1), int(int(dims[1])) - int(len(message4))), message4)
            screen.addstr(int(int(dims[0])/2+3), int(int(dims[1])) - int(len(message2))), message2)
            screen.addstr(int(int(dims[0])/2+4), int(int(dims[1])) - int(len(message3))), message3)
            screen.getch()

game()
curses.endwin()

1 个答案:

答案 0 :(得分:0)

作为我的第一个答案,我修复了你的语法。你错过了4个开口括号。

import curses
import time
import random

color_pref = int(input('Choose color mode (0-WHITE, 1-GREEN, 2-RED, 3-BLUE): '))
screen = curses.initscr()
curses.start_color()
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.noecho()
screen.keypad(1)
curses.curs_set(0)
dims = screen.getmaxyx()
screen.refresh()

def game():
        x, y = 3, 2
        points = 5
        q, Vertical, Horizontal = -1, 1, 1
        gameover = False
        screen.border(0)

        if q == ord('q'):
            gameover = True

        if gameover == True:
            screen.clear()
            screen.border()
            screen.refresh()
            screen.nodelay(0)
            score = '1'
            message2 = 'Press Space to Play Again.'
            message3 = 'Press q to Quit.'
            message4 = 'You killed ' + score+ 'enemies'
            screen.addstr(int(int(dims[0])/2-1), int(int(dims[1])) - int(len(message))), ('Game Over!', curses.color_pair(color_pref)|curses.A_BOLD)
            screen.addstr(int(int(dims[0])/2-1), int(int(dims[1])) - int(len(message4))), ( message4)
            screen.addstr(int(int(dims[0])/2+3), int(int(dims[1])) - int(len(message2))), ( message2)
            screen.addstr(int(int(dims[0])/2+4), int(int(dims[1])) - int(len(message3))), ( message3)
            screen.getch()

game()
curses.endwin()