<gh_stars>0
import curses
import random
import sys

#260 left
#261 right
#259 up
#258 down
def main(screen):

    width = 40 
    height = 20 

    screen.nodelay(True)
    curses.curs_set(0)
    window = curses.newwin(height, width, 0, 0)
    x = 4
    y = 4
    direction = 261 
    old_coordinates = [(x, y),(x-1, y),(x-2, y)]
    initial_delay = 100 
    score = 0 

    def get_direction(c):
        if  c == 260 or c == 261 or c == 259 or c == 258:
            if direction == 261 and c == 260:
                pass
            elif direction == 260 and c == 261:
                pass
            elif direction == 259 and c == 258:
                pass
            elif direction == 258 and c == 259:
                pass
            else:
                return c 
        return direction

    def get_coordinates(x, y):

        if direction == 260:
            x -= 1
            return x, y 
        if direction == 261:
            x += 1
            return x, y 
        if direction == 259:
            y -= 1
            return x, y 
        if direction == 258:
            y += 1
            return x, y 

    def create_apple():
        
        while True:
            applex = random.randint(1, width - 2)
            appley = random.randint(3, height - 3)
            for coordx, coordy in old_coordinates:
                if applex == coordx and appley == coordy:
                    continue
            break
        return applex, appley

    def center_text(text):
        text_center = int(len(text) / 2)
        return text_center

    def new_game():
        lastb = 260
        while True:
            window.clear()
            screen.clear()
            draw_walls(new_game=True)
            centerx = int((width - 1) / 2)
            thirdy = int((height -2) / 3)
            forthy = int((height -2) / 4)
            offset_game_over = center_text("@SNAKE v.1.0")
            window.addstr(forthy, centerx - offset_game_over, "@SNAKE v.1.0")
            b = screen.getch()
            if b != 260 and b != 261:
                if b == 10:
                    if lastb == 260:
                        return 
                    if lastb == 261:
                        sys.exit()
                b = lastb
            if b == 260:
                window.addstr(thirdy * 2, 5, "START GAME", curses.A_STANDOUT)
                window.addstr(thirdy * 2, width -11, "QUIT")
                lastb = 260
            if b == 261:
                window.addstr(thirdy * 2, 5, "START GAME") 
                window.addstr(thirdy * 2, width -11, "QUIT",curses.A_STANDOUT)
                lastb = 261

            window.refresh()
            screen.refresh()
            curses.napms(100)

    def game_over():
        lastb = 260 
        while True:
            window.clear()
            screen.clear()
            draw_walls()
            update_score(score)
            create_title() 
            centerx = int((width - 1) / 2)
            thirdy = int((height -2) / 3)
            forthy = int((height -2) / 4)
            offset_game_over = center_text("GAME OVER")
            window.addstr(forthy, centerx - offset_game_over, "GAME OVER")
            b = screen.getch()
            if b != 260 and b != 261:
                if b == 10:
                    if lastb == 260:
                        return False 
                    if lastb == 261:
                        sys.exit()
                b = lastb
            if b == 260:
                window.addstr(thirdy * 2, 5, "PLAY AGAIN", curses.A_STANDOUT)
                window.addstr(thirdy * 2, width -11, "QUIT")
                lastb = 260
            if b == 261:
                window.addstr(thirdy * 2, 5, "PLAY AGAIN") 
                window.addstr(thirdy * 2, width -11, "QUIT",curses.A_STANDOUT)
                lastb = 261

            window.refresh()
            screen.refresh()
            curses.napms(100)

    def draw_walls(new_game=False):
        for n in range(width):
            window.addch(0, n, "-")
            if not new_game:
                window.addch(2, n, "-")
            window.addch(height - 2, n, "-")
        for n in range(height - 1):
            window.addch(n, 0, "|")
            window.addch(n, width - 1, "|")
        
    def collision_check(x, y):
        for coordx, coordy in old_coordinates:
            if x == coordx and y == coordy:
                return True
        
        if x < 1 or x > width - 2 or y < 3 or y > height - 3:
            return True

    def update_score(score): 
        window.addstr(1, 2, f"SCORE: {score}")

    def create_title():
        window.addstr(1, width - 14, "@SNAKE v.1.0")
        
    new_game()
    applex, appley = create_apple()
    while True:

        screen.clear()
        window.clear()
        draw_walls()
        update_score(score)
        create_title()
        c = screen.getch()
        direction = get_direction(c)
        x, y = get_coordinates(x, y)
        if collision_check(x, y):
            if not game_over():
                x = 4
                y = 4
                direction = 261 
                score = 0
                initial_delay = 100
                old_coordinates = [(x, y),(x-1, y),(x-2, y)]
                continue

        old_coordinates.insert(0,(x, y)) 

        if x == applex and y == appley: 
            applex, appley = create_apple()
            score += 10 
            update_score(score)
            if initial_delay > 49:
                initial_delay = initial_delay - 2 
        else:
            old_coordinates.pop()
            pass
     
        for coordx, coordy in old_coordinates:
            window.addch(coordy, coordx, "@")
            
        window.addch(appley, applex, "O")
        window.refresh()
        screen.refresh()
        curses.napms(initial_delay)

curses.wrapper(main)

import curses
import random
import sys

#260 left
#261 right
#259 up
#258 down
def main(screen):

    width = 40 
    height = 20 

    screen.nodelay(True)
    curses.curs_set(0)
    window = curses.newwin(height, width, 0, 0)
    x = 4
    y = 4
    direction = 261 
    old_coordinates = [(x, y),(x-1, y),(x-2, y)]
    initial_delay = 100 
    score = 0 

    def get_direction(c):
        if  c == 260 or c == 261 or c == 259 or c == 258:
            if direction == 261 and c == 260:
                pass
            elif direction == 260 and c == 261:
                pass
            elif direction == 259 and c == 258:
                pass
            elif direction == 258 and c == 259:
                pass
            else:
                return c 
        return direction

    def get_coordinates(x, y):

        if direction == 260:
            x -= 1
            return x, y 
        if direction == 261:
            x += 1
            return x, y 
        if direction == 259:
            y -= 1
            return x, y 
        if direction == 258:
            y += 1
            return x, y 

    def create_apple():
        
        while True:
            applex = random.randint(1, width - 2)
            appley = random.randint(3, height - 3)
            for coordx, coordy in old_coordinates:
                if applex == coordx and appley == coordy:
                    continue
            break
        return applex, appley

    def center_text(text):
        text_center = int(len(text) / 2)
        return text_center

    def new_game():
        lastb = 260
        while True:
            window.clear()
            screen.clear()
            draw_walls(new_game=True)
            centerx = int((width - 1) / 2)
            thirdy = int((height -2) / 3)
            forthy = int((height -2) / 4)
            offset_game_over = center_text("@SNAKE v.1.0")
            window.addstr(forthy, centerx - offset_game_over, "@SNAKE v.1.0")
            b = screen.getch()
            if b != 260 and b != 261:
                if b == 10:
                    if lastb == 260:
                        return 
                    if lastb == 261:
                        sys.exit()
                b = lastb
            if b == 260:
                window.addstr(thirdy * 2, 5, "START GAME", curses.A_STANDOUT)
                window.addstr(thirdy * 2, width -11, "QUIT")
                lastb = 260
            if b == 261:
                window.addstr(thirdy * 2, 5, "START GAME") 
                window.addstr(thirdy * 2, width -11, "QUIT",curses.A_STANDOUT)
                lastb = 261

            window.refresh()
            screen.refresh()
            curses.napms(100)

    def game_over():
        lastb = 260 
        while True:
            window.clear()
            screen.clear()
            draw_walls()
            update_score(score)
            create_title() 
            centerx = int((width - 1) / 2)
            thirdy = int((height -2) / 3)
            forthy = int((height -2) / 4)
            offset_game_over = center_text("GAME OVER")
            window.addstr(forthy, centerx - offset_game_over, "GAME OVER")
            b = screen.getch()
            if b != 260 and b != 261:
                if b == 10:
                    if lastb == 260:
                        return False 
                    if lastb == 261:
                        sys.exit()
                b = lastb
            if b == 260:
                window.addstr(thirdy * 2, 5, "PLAY AGAIN", curses.A_STANDOUT)
                window.addstr(thirdy * 2, width -11, "QUIT")
                lastb = 260
            if b == 261:
                window.addstr(thirdy * 2, 5, "PLAY AGAIN") 
                window.addstr(thirdy * 2, width -11, "QUIT",curses.A_STANDOUT)
                lastb = 261

            window.refresh()
            screen.refresh()
            curses.napms(100)

    def draw_walls(new_game=False):
        for n in range(width):
            window.addch(0, n, "-")
            if not new_game:
                window.addch(2, n, "-")
            window.addch(height - 2, n, "-")
        for n in range(height - 1):
            window.addch(n, 0, "|")
            window.addch(n, width - 1, "|")
        
    def collision_check(x, y):
        for coordx, coordy in old_coordinates:
            if x == coordx and y == coordy:
                return True
        
        if x < 1 or x > width - 2 or y < 3 or y > height - 3:
            return True

    def update_score(score): 
        window.addstr(1, 2, f"SCORE: {score}")

    def create_title():
        window.addstr(1, width - 14, "@SNAKE v.1.0")
        
    new_game()
    applex, appley = create_apple()
    while True:

        screen.clear()
        window.clear()
        draw_walls()
        update_score(score)
        create_title()
        c = screen.getch()
        direction = get_direction(c)
        x, y = get_coordinates(x, y)
        if collision_check(x, y):
            if not game_over():
                x = 4
                y = 4
                direction = 261 
                score = 0
                initial_delay = 100
                old_coordinates = [(x, y),(x-1, y),(x-2, y)]
                continue

        old_coordinates.insert(0,(x, y)) 

        if x == applex and y == appley: 
            applex, appley = create_apple()
            score += 10 
            update_score(score)
            if initial_delay > 49:
                initial_delay = initial_delay - 2 
        else:
            old_coordinates.pop()
            pass
     
        for coordx, coordy in old_coordinates:
            window.addch(coordy, coordx, "@")
            
        window.addch(appley, applex, "O")
        window.refresh()
        screen.refresh()
        curses.napms(initial_delay)

curses.wrapper(main)
#260 left #261 right #259 up #258 down