Python Curses addstr() error when keys are not pressed - python

I'm recreating this game for terminal in python using the curses library. Whenever a key is not pressed, stdscr.addstr() returns an error.
This seems to be because the cursor is off screen (in the bottom-right corner) during those frames, but I can't figure out why it moves at all. The canvas that I'm printing is always the same size (It consists entirely of spaces which are replaced when Apple or Basket objects are rendered). I tried decreasing the canvas size, but the cursor still goes to that corner. I also tried setting curses.leaveok(True), and the cursor seemed to be following the basket, but my logging proves that it is still going to that corner.
The file that uses curses:
import random
import time
import curses
from apple_season.basket import Basket
from apple_season.apple import Apple
from apple_season.coords import Canvas
def main(stdscr):
curses.curs_set(1) # so i can see where the cursor is
dims = [curses.COLS - 1, curses.LINES - 1] # pylint: disable=no-member
stdscr.nodelay(True)
stdscr.leaveok(True)
key=""
stdscr.clear()
canvas = Canvas(*dims)
basket = Basket(canvas)
apples = []
i = 0
def finished_apples():
if len(apples) <= 100:
return False
else:
for apple in apples:
if not apple.has_fallen:
return False
return True
while not finished_apples():
if len(apples) <= 100: # don't make more if there are already 100
# decide whether or not to create new apple (1/100 chance per frame)
num = random.randint(0, 100)
if num == 25:
apples.append(Apple(canvas))
try:
key = stdscr.getkey()
stdscr.clear()
# pick up keyboard inputs
# quit option
if str(key) == "q":
break
# right arrow
elif str(key) == "KEY_RIGHT":
basket.move('right')
# left arrow
elif str(key) == "KEY_LEFT":
basket.move('left')
except Exception:
pass
# render objects - alters canvas to display them
for apple in apples:
if apple.has_fallen:
apple.render()
else:
if '.0' not in str(i / 2): # check if i is even (drop every other frame)
apple.fall()
apple.render()
basket.render()
try:
stdscr.addstr(canvas.display)
except Exception:
pass
stdscr.refresh()
i += 1
time.sleep(0.01)
if __name__ == "__main__":
curses.wrapper(main)
(The code above runs fine, but it doesn't do anything when stdscr.addstr(canvas.display) doesn't work, which is whenever there are no keys pressed)
What's interesting is that this doesn't happen when it's just the basket or just the apples.
To see all of the code: https://github.com/lol-cubes/Terminal-Apple-Season/tree/soErrorCode.

I put the stdscr.clear() in the try and except block, which made it so that that code was only executed when a key was pressed, therefore overflowing the terminal because I was attempting to display multiple frames at once.

Related

Window freeze for single thread in while loop of Pygame

I am writing a short program to display cards in a round. I suspect that it is the length of the code which prevents the final 'OK' submit on P3 (the last player's submission) from executing properly: at which point the program sometimes will evaluate the winner and clear the round, but most of the time instead will freeze.
I have tried clock.tick(low fps), pygame.event.pump(), and pygame.event.clear(). Any leads would be much appreciated.
# Round loop begins. Finish until all hands are empty.
while not self.game.get_is_last_round():
player = self.game.get_player(self.game.get_player_turn())
hand = player.order_hand(player.get_hand(),
self.game.get_round_level(),
self.game.get_round_trump_suit())
ok_clicked_2 = False
pygame.event.pump()
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.deal_running = False
self.is_running = False
pygame.display.quit()
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
play = player.get_play()
click = pygame.mouse.get_pressed(num_buttons=3)
pos = pygame.mouse.get_pos()
# Used DeMorgan's law to resolve error
ok_clicked_2 = (OK1_X < pos[0] < OK1_X + B_W) and (OK1_Y < pos[1] < OK1_Y + B_H) and click[0]
b1, card = self.check_hand(pos, player)
b2, play_card = self.check_play(pos, player)
if b1:
hand.remove(card)
play.append(card)
player.set_play(
player.order_hand(play, self.game.get_round_level(),
self.game.get_round_trump_suit()))
player.set_hand(
player.order_hand(hand, self.game.get_round_level(),
self.game.get_round_trump_suit()))
if b2:
play.remove(play_card)
hand.append(play_card)
player.set_play(
player.order_hand(play, self.game.get_round_level(),
self.game.get_round_trump_suit()))
player.set_hand(player.order_hand(hand, self.game.get_round_level(),
self.game.get_round_trump_suit()))
clock.tick(100)
surface.blit(background, (0, 0))
if len(self.game.get_player(0).get_hand()) == 25:
self.game.set_is_first_round(True)
else:
self.game.set_is_first_round(False)
if len(self.game.get_player(0).get_hand()) == 0:
self.game.set_is_last_round(True)
else:
self.game.set_is_last_round(False)
if self.game.get_play_in_turn() != NUM_PLAYERS:
pygame.event.pump()
clock.tick(100)
if len(hand) <= 1:
width = 0
x = (BG_WIDTH - CARD_WIDTH) // 2
elif len(hand) >= 8:
width = (BG_WIDTH - SIDE_W - CARD_WIDTH) // (len(hand) - 1)
x = BG_WIDTH // 2 - (CARD_WIDTH + (width * (len(hand) - 1))) // 2
else:
width = CARD_WIDTH
x = (BG_WIDTH - (CARD_WIDTH * len(hand))) // 2
surface.blit(background, (0, 0))
self.blit_backs()
self.blit_round()
self.show_ok()
self.show_hand(x, ROW3h, width, hand)
self.show_hand(CARD_POSITIONS[0][0], CARD_POSITIONS[0][1], SLIM_WIDTH, play)
if ok_clicked_2:
for card in play:
hand.append(card)
player.set_hand(player.order_hand(hand, self.game.get_round_level(),
self.game.get_round_trump_suit()))
# If player is first to start a round, he/she has a different validity check.
# (Sets the pattern for the cycle)
if player.get_begins_cycle():
valid = self.game.check_validity(True) # is_first
else:
valid = self.game.check_validity(False) # Is not first to play in the round
if not valid: # Clear holding if invalid
if (play == []) or (player.get_play() == []):
print("\nYou must make a play.\n")
else:
print("Invalid play. Try again.")
if not player.get_begins_cycle():
valid_plays = player.get_valid_plays(self.game.get_pattern(),
self.game.get_round_trump_suit())
print("Valid plays: \n")
for temp_play_idx in range(len(valid_plays)):
temp_play = valid_plays[temp_play_idx]
print("[", end='')
for temp_card_idx in range(len(temp_play)):
valid_plays[temp_play_idx][temp_card_idx].show_card("", '')
if temp_card_idx != len(temp_play) - 1:
print(", ", end='')
print("]")
# Clear the current player's selection and restore hand to its original content
cycle_order = self.game.get_cycle_order()
cycle = self.game.get_cycle()
for player_order in range(len(cycle_order)):
if player == cycle_order[player_order]:
cycle_order.remove(player)
cycle.pop()
self.game.set_cycle_order(cycle_order)
self.game.set_cycle(cycle)
else: # Valid play on submit
# Special case for HIGH_SUIT play, play lowest card if another player has greater
play = self.game.check_high_suit(play)
# If friend card played, establish and print teammates
# TODO: auto-designate friends if the last round
# has been reached (friends buried in treasure case)
# TODO: determine whether friend is "dead"
self.game.check_for_friends()
cycle = self.game.get_cycle()
cycle.append(play)
self.game.set_cycle(cycle)
cycle_order = self.game.get_cycle_order()
cycle_order.append(player)
self.game.set_cycle_order(cycle_order)
# self.clear_positions()
for card in play:
hand.remove(card)
player.set_hand(
player.order_hand(hand, self.game.get_round_level(),
self.game.get_round_trump_suit()))
self.game.next_player_turn()
self.game.set_play_in_turn(self.game.get_play_in_turn() + 1)
print(self.game.get_play_in_turn())
play = []
else:
self.game.set_play_in_turn(0)
# Distribute any points in the round to round winner
self.update_cycle_points()
for p in self.game.get_players():
for card in p.get_play():
discard = self.game.get_discard()
discard.append(card)
p.set_play([])
pygame.event.clear()
clock.tick(100)
pygame.display.update()
I think it's time for a code-cleanup, then your issue will go away (or you'll find it).
Currently the main loop is a big mix-up of event handling, screen-painting and game engine. Try to separate these parts out.
Move some of the in-loop processing out to functions - like the block after if ok_clicked_2:. It may help to make a data structure in which you store the game-state, then have the events change that game state. When it comes time to draw the game to the screen, the painting code can query the state, acting accordingly.
In terms of your actual lockup, if self.game.get_play_in_turn() == NUM_PLAYERS nothing is painted to the screen. Is this intentional? Add some print()s to your code so you can know the execution flow (or learn to use the python debugger).
I think the biggest step forward would be to move all the screen painting to one section of the main loop, something like:
# Render the screen
print( "Rendering Screen" )
surface.blit(background, (0, 0))
self.blit_backs()
self.blit_round()
# etc. for all other things, score, buttons, ...
clock.tick(60)
pygame.display.update()
You seem to be handling the events OK, so it would probably be better to remove the calls to pygame.event.pump() and pygame.event.clear(). You don't need these.
Following Kingsley's advice, I organized the code by function: rendering screen, game engine, and event handling. I would provide MRE as Random Davis suggests, but that would include 5 integrated files which would take too long to pare down.
It turns out that the problem lay in a piece of code which is called separately: "update_cycle_points()". Within, there is a while loop which does not contain an event handler. The solution was to change it to a for loop, which Pygame seems to process without error (does not freeze because it does not expect event handling there).
I also removed pygame.event.clear() and pump() functions without problems.

Increase just by one when a key is pressed

I want to increase the variable "shot_pressed" just by one when the key "s" is pressed no matter how long I pressed. But the result is that the variable keeps on increasing. The longer I pressed, the bigger the value of the variable. Below is a part of my code.
import keyboard
shot_pressed = 0
if keyboard.is_pressed('s'):
shot_pressed += 1
First of all looks like you use https://pypi.python.org/pypi/keyboard
Second, I assume your code is not like you wrote above but like
import keyboard
shot_pressed = 0
while True:
if keyboard.is_pressed('s'):
shot_pressed += 1
print("shot_pressed %d times"%shot_pressed)
If yes, here is the core of the problem: is_pressed will be always True, while key is pressed. So if condition will be True and while will repeat it many times.
There are two ways of dealing with that.
1) Use the same method, but check if this is the first is_pressed moment, so inroduce was_pressed variable:
import keyboard
shot_pressed = 0
was_pressed = False
while True:
if keyboard.is_pressed('s'):
if not was_pressed:
shot_pressed += 1
print("shot_pressed %d times"%shot_pressed)
was_pressed = True
else:
was_pressed = False
2) Better use the library. You can set a hook, so on key pressed your function will be called (only once for one press). So the code will look like this:
import keyboard
shot_pressed = 0
def on_press_reaction(event):
global shot_pressed
if event.name == 's':
shot_pressed += 1
print("shot_pressed %d times"%shot_pressed)
keyboard.on_press(on_press_reaction)
while True:
pass
I do not know that keyboard module but the problem with your code is that the program takes input once. Your program should wait next input from keyboard. Try to use while loop to take inputs from user.
import keyboard
import time
shot_pressed = 0
try:
while True:
if keyboard.is_pressed("S"):
shot_pressed += 1
time.sleep(0.1)
print(sh)
except Exception as er:
pass
Or can use read key
try:
shot_pressed = 0
while True:
key.read_key()
if key.is_pressed("s"):
sh += 1
print(shot_pressed)
except Exception as er:
pass
I haven't used that module, but you probably want the same thing that you would want in javascript. keyboard.KEY_DOWN instead of is_pressed.
https://github.com/boppreh/keyboard#keyboard.KEY_DOWN
You probably need to handle things asynchronously as well.

Progress bar in python curses

I have created a progress bar which updates itself after getting a percentage from another function but I'm having issues getting it to trail like this ############. Instead, it just move the '#' to the right until 100% is reached. Below is my code. The reason why it's this way is because I need the percentage to come externally so that the code can be reusable. please help me.
import curses
import time
curses.initscr()
def percentage():
loading = 0
while loading < 100:
loading += 1
time.sleep(0.03)
update_progress(loading)
def update_progress(progress):
win = curses.newwin(3, 32, 3, 30)
win.border(0)
rangex = (30 / float(100)) * progress
pos = int(rangex)
display = '#'
if pos != 0:
win.addstr(1, pos, "{}".format(display))
win.refresh()
percentage()
The problem is that you call newwin() every time, discarding the old win and replacing it with a new one in the same place. That new window only gets one character added to it, with the background being blank, so you see an advancing cursor instead of a bar.
One possible solution:
import curses
import time
curses.initscr()
def percentage():
win = curses.newwin(3, 32, 3, 30)
win.border(0)
loading = 0
while loading < 100:
loading += 1
time.sleep(0.03)
update_progress(win, loading)
def update_progress(win, progress):
rangex = (30 / float(100)) * progress
pos = int(rangex)
display = '#'
if pos != 0:
win.addstr(1, pos, "{}".format(display))
win.refresh()
percentage()
curses.endwin()
(Note the addition of a call to endwin() to restore the terminal to its normal mode.)
As far as leaving it onscreen after the program finishes, that's kind of outside the scope of curses. You can't really depend on any interaction between curses and stdio, sorry.
you can just switch the pos to multiply the display #:
if pos != 0:
win.addstr(1, 1, "{}".format(display*pos))
win.refresh()

How to make a menu in Python navigable with arrow keys

I'm making a text-based game with an option to select a class for their character. At present, the player enters their option, either typing in a number or the name of the class. It works well enough.
However, I would like to have the player navigate the menu with the arrow keys and select an option using the "enter" key. In order to make it clear which option they are about to select, I would also like to have the text of the selected option highlighted. If you've ever played an ASCII roguelike, you know what it looks like.
Here is the code that I currently have for classes:
def character():
print "What is your class?"
print "1. The sneaky thief."
print "2. The smarty wizard."
print "3. The proletariat."
charclass = raw_input("> ")
if charclass == "1" or "thief":
charclass = thief
print "You are a thief!"
elif charclass == "2" or "wizard":
charclass = wizard
print "You are a wizard!"
elif charclass == "3" or "prole":
charclass = prole
print "You are a prole!"
else:
print "I'm sorry, I didn't get that"
Thanks!
As it was already mentioned in a comment, you may use curses. Here is a small working menu to achieve what you want
import curses
classes = ["The sneaky thief", "The smarty wizard", "The proletariat"]
def character(stdscr):
attributes = {}
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
attributes['normal'] = curses.color_pair(1)
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_WHITE)
attributes['highlighted'] = curses.color_pair(2)
c = 0 # last character read
option = 0 # the current option that is marked
while c != 10: # Enter in ascii
stdscr.erase()
stdscr.addstr("What is your class?\n", curses.A_UNDERLINE)
for i in range(len(classes)):
if i == option:
attr = attributes['highlighted']
else:
attr = attributes['normal']
stdscr.addstr("{0}. ".format(i + 1))
stdscr.addstr(classes[i] + '\n', attr)
c = stdscr.getch()
if c == curses.KEY_UP and option > 0:
option -= 1
elif c == curses.KEY_DOWN and option < len(classes) - 1:
option += 1
stdscr.addstr("You chose {0}".format(classes[option]))
stdscr.getch()
curses.wrapper(character)
The last call to getch is just so you can see the result before the program terminates
I would consider using simple-term-menu : https://github.com/IngoMeyer441/simple-term-menu
This project allows you to search through the menu using '/'
Like Jona's answer but actually runs :P
Also have a longer version here that does vertical and horizontal menu: https://pastebin.com/raw/zv9EXdgH
#!/usr/bin/env python3
# SOURCE: https://docs.python.org/2/library/curses.html
# SOURCE: https://docs.python.org/3/howto/curses.html
# For Windows: pip install windows-curses
import curses
window = curses.initscr() # Initialize the library. Returns a WindowObject which represents the whole screen.
window.keypad(True) # Escape sequences generated by some keys (keypad, function keys) will be interpreted by curses.
curses.cbreak() # Keys are read one by one. Also safer than curses.raw() because you can still interrupt a running script with hotkeys.
curses.noecho() # Prevent getch() keys from being visible when pressed. Echoing of input characters is turned off.
# Initialize colors.
curses.start_color() # Must be called if the programmer wants to use colors.
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
black = curses.color_pair(1)
white = curses.color_pair(2)
def display_menu(window):
selectedIndex = 0
while True:
window.clear()
window.addstr('Pick an option:\n', curses.A_UNDERLINE)
for i in range(len(MENU_OPTIONS)):
# Uncolored line number.
window.addstr('{}. '.format(i + 1))
# Colored menu option.
window.addstr(MENU_OPTIONS[i] + '\n', black if i == selectedIndex else white)
c = window.getch()
if c == curses.KEY_UP or c == curses.KEY_LEFT:
# Loop around backwards.
selectedIndex = (selectedIndex - 1 + len(MENU_OPTIONS)) % len(MENU_OPTIONS)
elif c == curses.KEY_DOWN or c == curses.KEY_RIGHT:
# Loop around forwards.
selectedIndex = (selectedIndex + 1) % len(MENU_OPTIONS)
# If curses.nonl() is called, Enter key = \r else \n.
elif c == curses.KEY_ENTER or chr(c) in '\r\n':
# If the last option, exit, is selected.
if selectedIndex == len(MENU_OPTIONS) - 1:
curses.endwin() # De-initialize the library, and return terminal to normal status. <-- Works without this on Windows, however in Linux you can't type in the terminal after exiting without this :P
break
window.addstr('\nYou choose {}\n'.format(MENU_OPTIONS[selectedIndex]))
window.getch()
else:
window.addstr("\nThe pressed key '{}' {} is not associated with a menu function.\n".format(chr(c), c))
window.getch()
MENU_OPTIONS = [
'Option 1',
'Option 2',
'Option 3',
'Exit',
]
if __name__ == '__main__':
display_menu(window)

Individual keypress bug in python

The following code is a python sprinting game. It was posted as an answer to my previous post, by #mango You have to tap 'a' and 'd' as fast as you can to run 100 meters. However, there are a few bugs...
1) If you hold down 'a' and 'd' at the same time, the game is completed as fast as possible and defeats the point of the game.
2) Like in my previous post, I would like to incorporate a scoring system into this code, however, due to my level of skill and experience with python, unfortunately, I am unable to do so, and would appreciate and suggestions.
Many Thanks
Previous code:
import msvcrt
import time
high_score = 50
name = "no-one"
while True:
distance = int(0)
print("\n--------------------------------------------------------------")
print('\n\nWelcome to the 100m sprint, tap a and d rapidly to move!')
print('* = 10m')
print("\n**Current record: " + str(high_score) + "s, by: " + name)
print('\nPress enter to start')
input()
print('Ready...')
time.sleep(1)
print('GO!')
start_time = time.time()
while distance < 100:
k1 = msvcrt.getch().decode('ASCII')
if k1 == 'a':
k2 = msvcrt.getch().decode('ASCII')
if k2 == 'd':
distance += 1
if distance == 50:
print("* You're halfway there!")
elif distance % 10 == 0:
print('*')
fin_time = time.time() - start_time
fin_time = round(fin_time,2)
print('Well done you did it in...'+str(fin_time))
if fin_time < high_score:
print("Well done you've got a new high score ")
name = input("Please enter your name : ")
This is the code that I recieved as feedback, I have made a few changes, but I am struggling witht the bugs that I listed before.
1) If you hold down 'a' and 'd' at the same time, the game is completed as fast as possible and defeats the point of the game.
2) Like in my previous post, I would like to incorporate a scoring system into this code, however, due to my level of skill and experience with python, unfortunately, I am unable to do so, and would appreciate and suggestions.
Many Thanks
# these are the modules we'll need
import sys
import tkinter as tk
class SprintGame(object):
# this represents the distance we'll allow our player to run
# NOTE: the distance is in meters
distance = 100
# this represents the stride length of the players legs
# NOTE: basically how far the player can travel in one footstep
stride = 1.5
# this represents the last key the user has pressed
lastKey = ""
# this represents wether or not the race has been completed
completed = False
# this function initiates as soon as the "sprint" variable is defined
def __init__(self):
# create the tk window
self.root = tk.Tk()
# set the tk window size
self.root.geometry('600x400')
# set the tk window title
self.root.title("Sprinting Game")
# bind the keypress event to the self.keypress handler
self.root.bind('<KeyPress>', self.keypress)
# center the window
self.centerWindow(self.root)
# insert the components
self.insertComponents()
# initial distance notice
self.output("{0}m left to go!".format(self.distance))
# start out wonderful game
self.start()
# this function centers the window
def centerWindow(self, window):
window.update_idletasks()
# get the screen width
width = window.winfo_screenwidth()
# get the screen height
height = window.winfo_screenheight()
# get the screen size
size = tuple(int(_) for _ in window.geometry().split('+') [0].split('x'))
# get the screen's dimensions
x = (width / 2) - (size[0] / 2)
y = (height / 2) - (size[1] / 2)
# set the geometry
window.geometry("%dx%d+%d+%d" % (size + (x, y)))
# this function replaces the old text in the textbox with new text
def output(self, text = ""):
self.text.delete('1.0', tk.END)
self.text.insert("end", text)
# this function handles key presses inside the tkinter window
def keypress(self, event):
# get the key and pass it over to self.logic
self.logic(event.char)
# this function handles game logic
def logic(self, key):
# convert key to a lower case string
key = str(key).lower()
# let us know how far we've got left
if key == "l":
self.output("{0}m left to go!".format(self.distance))
# restart the race
if key == "r":
# clear the output box
self.text.delete('1.0', tk.END)
# reset the distance
self.distance = 100
# reset the stride
self.stride = 1.5
# reset the last key
self.lastKey = ""
# set race completed to false
self.completed = False
# output restart notice
self.output("The Race Has Been Restarted.")
# don't bother with logic if race is completed
if self.completed == True:
return False
# check if distance is less than or equal to zero (meaning the race is over)
if self.distance <= 0:
# set the "self.completed" variable to True
self.completed = True
# let us know we've completed the race
self.output("Well done, you've completed the race!")
# return true to stop the rest of the logic
return True
# convert the key to lower case
key = key.lower()
# this is the quit function
if key == "q":
# lights out...
sys.exit(0)
# check if the key is a
if key == "a":
# set the last key to a so that we can decrement the "distance"
# variable if it is pressed next
self.lastKey = "a"
# only bother with "d" keypresses if the last key was "a"
if self.lastKey == "a":
# capture the "d" keypress
if key == "d":
# decrement the "distance" variable
self.distance -= self.stride
# let us know how far into the game we are
self.output("{0}m left to go!".format(self.distance))
# this function inserts the components into the window
def insertComponents(self):
# this component contains all of our output
self.text = tk.Text(self.root, background='#d6d167', foreground='#222222', font=('Comic Sans MS', 12))
# lets insert out text component
self.text.pack()
# this function opens the window and starts the game
def start(self):
self.root.mainloop()
# create a new instance of the game
Game = SprintGame()

Categories