LINUX.ORG.RU

решено

 ,


0

1

Привет всем, нужна помощь с Python. Сам на нём всего пару мелких скриптов писал, поэтому не совсем понимаю что да как. Есть значит скрипт:

import time
import curses

stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(1)


x = y = 10
np = 'r'
while True:
	stdscr.addstr(0, 0, str(np))


	keycode = stdscr.getch()
	if keycode == 261 : np = 'r'
	elif keycode == 260 : np = 'l'
	elif keycode == 259 : np = 'u'
	elif keycode == 258 : np = 'd'

	if np == 'r' : x = x + 1
	elif np == 'l': x = x - 1
	elif np == 'u': y = y - 1
	elif np == 'd': y = y + 1

	stdscr.addstr(y, x, str("#"))
	stdscr.refresh()
	time.sleep(0.2)
Вот это:
keycode = stdscr.getch()
Запрашивает ввод символа, как сделать в фоне? А то скрипт останавливается...

Решил проблему: stdscr.nodelay(1)



Последнее исправление: WiseAlex (всего исправлений: 2)

Ответ на: комментарий от buddhist

Они уже осилили curses под виндой, кстати?

Я осилил, написал простенький биндинг к PDCurses. Может, помочь девелоперам?

pacify ★★★★★
()
Ответ на: комментарий от WiseAlex

Кому интересно: я писал простенькую змейку =) Пока без уровней и прочей фигни, самое простое:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Snake for terminal v0.1 by WiseAlex aka AleKi
import time
import curses
import random
stdscr = curses.initscr()
stdscr.nodelay(1)
stdscr.keypad(1)
curses.noecho()
curses.cbreak()
curses.curs_set(0)
curses.start_color()
curses.init_pair(1, curses.COLOR_BLUE, curses.COLOR_BLUE)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_RED)
term_max = stdscr.getmaxyx()
x = y = 0
np = 'r'
game_over = False
ch = [ [0, 2], [0, 1], [0, 0] ]
pt = [random.randint(5, term_max[0]-5), random.randint(5, term_max[1])-5]
add_ch = False
def GameOver():
	game_over_text = 'Game Over.'
	stdscr.addstr(term_max[0]/2, (term_max[1]/2)-(len(game_over_text)/2), game_over_text)
	for i in xrange(term_max[0]/2+1, term_max[0]):
		stdscr.addstr(i, 0, " ")
	stdscr.addstr(pt[0], pt[1], str(" "))
	stdscr.refresh()
def StepUp():
	for i in range(len(ch)):
		stdscr.addstr(ch[i][0], ch[i][1], str(" "))
	if np == 'r' :  ch_ins = [ch[0][0], ch[0][1] + 1]
	elif np == 'l': ch_ins = [ch[0][0], ch[0][1] - 1]
	elif np == 'u': ch_ins = [ch[0][0] - 1, ch[0][1]]
	elif np == 'd': ch_ins = [ch[0][0] + 1, ch[0][1]]
	ch.insert(0, ch_ins)
	if (ch[0][0] < 0) or (ch[0][0] >= term_max[0]) or (ch[0][1] < 0) or (ch[0][1] >= term_max[1]) :
			return False
	for i in range(len(ch)):
		for j in range(len(ch)):
			if (ch[i][0] == ch[j][0]) and (ch[i][1] == ch[j][1]) and (i != j):
				return False
	global add_ch
	global pt
	if (ch[0][0] == pt[0]) and (ch[0][1] == pt[1]):
		add_ch = True
		pt = [random.randint(5, term_max[0]-5), random.randint(5, term_max[1])-5]
	if add_ch == False:
		del(ch[-1])
	add_ch = False
	for i in range(len(ch)):
		stdscr.addstr(ch[i][0], ch[i][1], str("#"), curses.color_pair(1) | curses.A_BOLD)
	return True
while True:
	keycode = stdscr.getch()
	if keycode == 261 : np = 'r'
	elif keycode == 260 : np = 'l'
	elif keycode == 259 : np = 'u'
	elif keycode == 258 : np = 'd'
	stdscr.addstr(pt[0], pt[1], str("#"), curses.color_pair(2) | curses.A_BOLD)

	if StepUp() != True : 
		GameOver()
		break
	stdscr.refresh()
	time.sleep(0.05)

WiseAlex
() автор топика
Ответ на: комментарий от WiseAlex

Помогите оптимизацией, пожалуйста! Кто может на классах научить писать?

WiseAlex
() автор топика

решено

Хорошее название. Предлагаешь при проблемах искать это слово?

J ★★★★
()
Ответ на: комментарий от J

Название в любом случае было совсем не по теме. Всё оказалось намного проще, да и хз теперь уже как назвать.

WiseAlex
() автор топика
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.