Skip to content

Instantly share code, notes, and snippets.

@jacob-herr
Created March 28, 2023 02:31
Show Gist options
  • Save jacob-herr/0fc770a2c61c32ce34cfa2c5dfbcd01e to your computer and use it in GitHub Desktop.
Save jacob-herr/0fc770a2c61c32ce34cfa2c5dfbcd01e to your computer and use it in GitHub Desktop.
import random
import curses
from curses import wrapper
from time import sleep
import math
#this class will establish and draw a board
class Board:
#2d array from rows and columns
def __init__(self, rows, columns):
self.board = [[' ' for column in range(columns)] for row in range(rows)]
self.rows = rows
self.columns = columns
#inside this for loop, is where the error occurs, whenever i loop through this more than eight times,
#i get the error specified in the post, at the \n.
def draw_board(self, stdscr):
stdscr.addstr('-----' * self.columns)
for i in range(self.rows):
stdscr.addstr('|')
stdscr.addstr('\n')
#right ^^ there
stdscr.addstr('-----' * self.columns)
#this class isnt relevant to this problem at all.
class Snake():
def __init__(self, board, starty, startx):
self.head = '#'
self.board = board
self.board.board[starty][startx] = self.head
self.heady = starty
self.headx = startx
def movement(self, ydir, xdir, stdscr):
self.board.board[self.heady][self.headx] = ' '
self.heady = self.heady + ydir
self.headx = self.headx + xdir
try:
self.board.board[self.heady][self.headx] = self.head
self.board.draw_board(stdscr)
except:
exit()
sleep(0.3)
#rows are set at eight, if thats helps
b = Board(8 ,10)
s = Snake(b, math.floor(b.rows/2), math.floor(b.columns/2))
#while loop set here to test if it works (which it doesnt),
#im going to want to constantly be updating and drawing the board during the game.
def main(stdscr):
stdscr.clear()
while True:
b.draw_board(stdscr)
stdscr.refresh()
wrapper(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment