Question

I've learned how to make square symbol with for loop today, like below:

import curses

stdscr = curses.initscr()
for y in range(1,10):
    for x in range(1,10):
        stdscr.addch(y,x,'#')
stdscr.getch()

But yet I didn't figure out how to add other graphs like triangle, round in a smart way. Do you have any ideas?

Was it helpful?

Solution

You may find more resources with the name "ASCII art". For a triangle, I would use slashes /\ and underline _ for the base. Just make sure each slash is one character away from the previous' character column. Like this:

import curses
stdscr = curses.initscr()

for i in range(10):
    stdscr.addch(i, 10-i, '/')
    stdscr.addch(i, 11 + i, '\\')

for i in range(2, 20):
    stdscr.addch(9, i, '_')

stdscr.getch()

Result:

          /\
         /  \
        /    \
       /      \
      /        \
     /          \
    /            \
   /              \
  /                \
 /__________________\

For a circle, you'll need sin and cos, like this:

import math
import curses
stdscr = curses.initscr()

radius = 10
for part in range(0, 100):
    angle = (part / 100) * math.pi * 2
    x = math.cos(angle) * radius + radius
    y = math.sin(angle) * radius + radius
    stdscr.addch(int(y * .7), int(x), 'o')

stdscr.getch()

Doesn't look as good, but it's a start:

     oooooooooo
   oo          oo
 oo              oo
 o                o
o                  o
o                  o
o                  o
o                  oo
o                  o
o                  o
 o                o
 oo              oo
   oo          oo
     oooooooooo
          o
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top