<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Snake Game using python(simple code)]]></title><description><![CDATA[<p dir="auto">import random<br />
import curses</p>
<p dir="auto">s = curses.initscr()<br />
curses.curs_set(0)<br />
sh, sw = s.getmaxyx()<br />
w = curses.newwin(sh, sw, 0, 0)<br />
w.keypad(1)<br />
w.timeout(100)</p>
<p dir="auto">snk_x = sw/4<br />
snk_y = sh/2<br />
snake = [<br />
[snk_y, snk_x],<br />
[snk_y, snk_x-1],<br />
[snk_y, snk_x-2]<br />
]</p>
<p dir="auto">food = [sh/2, sw/2]<br />
w.addch(food[0], food[1], curses.ACS_PI)</p>
<p dir="auto">key = curses.KEY_RIGHT</p>
<p dir="auto">while True:<br />
next_key = w.getch()<br />
key = key if next_key == -1 else next_key</p>
<pre><code>if snake[0][0] in [0, sh] or snake[0][1]  in [0, sw] or snake[0] in snake[1:]:
    curses.endwin()
    quit()

new_head = [snake[0][0], snake[0][1&rsqb;&rsqb;

if key == curses.KEY_DOWN:
    new_head[0] += 1
if key == curses.KEY_UP:
    new_head[0] -= 1
if key == curses.KEY_LEFT:
    new_head[1] -= 1
if key == curses.KEY_RIGHT:
    new_head[1] += 1

snake.insert(0, new_head)

if snake[0] == food:
    food = None
    while food is None:
        nf = [
            random.randint(1, sh-1),
            random.randint(1, sw-1)
        ]
        food = nf if nf not in snake else None
    w.addch(food[0], food[1], curses.ACS_PI)
else:
    tail = snake.pop()
    w.addch(tail[0], tail[1], ' ')

w.addch(snake[0][0], snake[0][1], curses.ACS_CKBOARD)
</code></pre>
<p dir="auto">Windows walata curses eka support karan nethi nisa ModuleError ekk enava.<br />
Ekata pip install windows-curses karala balanna.</p>
<p dir="auto">Linux vala nan ehema prashnayak naha.</p>
]]></description><link>https://lankadevelopers.lk/topic/240/snake-game-using-python-simple-code</link><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Apr 2026 04:05:19 GMT</lastBuildDate><atom:link href="https://lankadevelopers.lk/topic/240.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 14 May 2019 06:19:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Snake Game using python(simple code) on Thu, 16 May 2019 17:30:20 GMT]]></title><description><![CDATA[<p dir="auto">eka patta mchan thanks</p>
]]></description><link>https://lankadevelopers.lk/post/1530</link><guid isPermaLink="true">https://lankadevelopers.lk/post/1530</guid><dc:creator><![CDATA[Nubelle]]></dc:creator><pubDate>Thu, 16 May 2019 17:30:20 GMT</pubDate></item><item><title><![CDATA[Reply to Snake Game using python(simple code) on Wed, 15 May 2019 11:09:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://lankadevelopers.lk/uid/935">@thilina-dilshan</a> niyamai machan... 😍</p>
]]></description><link>https://lankadevelopers.lk/post/1527</link><guid isPermaLink="true">https://lankadevelopers.lk/post/1527</guid><dc:creator><![CDATA[dev_lak]]></dc:creator><pubDate>Wed, 15 May 2019 11:09:43 GMT</pubDate></item></channel></rss>