Untitled

Anonymous
plain_text
04/04/2024 9:56 PM
16.0 KB
42
Indexable
import curses
from curses import wrapper
from curses.textpad import Textbox, rectangle
from fractions import Fraction

import sys
import time
import random
import cursor

cursor.hide()
color = {}
player_cords = {
    "x": 0,
    "y": 0
                }
grid_map = {}

term = "DIM "

WIDTH = 660
HEIGHT = 180

player_stats = {
    "health": 10,
    "hunger": 0,
    "level": 1,
    
    "defense": 0,
    "attack": 0,
    "speed": 0
}

player_inv = {
    "armor": [],
    "weapons": [],
    "scrolls": [],
    "potions": []
}


def resize(ro, co):
    sys.stdout.write("\x1b[8;{rows};{cols}t".format(rows=ro, cols=co))


def set_colors():
    global color
    
    b = 1
    color = {}
    colors = [
        "DIM RED", "DIM GREEN", "DIM YELLOW", "DIM BLUE", "DIM MAGENTA",
        "DIM TUR", "DIM WHITE", "GREY", "RED", "GREEN", "YELLOW", "BLUE",
        "MAGENTA", "TUR", "WHITE"
    ]
    for i in range(1, 16):
        curses.init_pair(b, i, curses.COLOR_BLACK)
        color[colors[b - 1]] = curses.color_pair(b)
        b += 1
        
    curses.init_pair(b, curses.COLOR_BLACK, curses.COLOR_BLACK)
    color["BLACK"] = curses.color_pair(b)
    
    #curses.init_pair(15, 88, curses.COLOR_BLACK)
    #color["VERY DIM RED"] = curses.color_pair(b)
    #curses.init_pair(16, 22, curses.COLOR_BLACK)
    #color["VERY DIM GREEN"] = curses.color_pair(b)


def new_map(WIDTH, HEIGHT):
    global grid_map
    
    for y in range(HEIGHT):
        for x in range(WIDTH):
            grid_map[(x, y)] = random.choice(["'", "\"",  ".", ".",  ".", ".", "S", "S", "S"])
            
    #newgrid = {}
    for i in range(2):
        for y in range(HEIGHT):
            for x in range(WIDTH):
                n = 0
                s =0
                for i in [(-1, 0), (1, 0), (-1, 1), (-1, -1), (1, -1), (1, 1), (0, 1), (0 -1)]:
                    try:
                        if grid_map[(x+i[0], y+i[1])] in ["\"", "'"]:
                            n += 1
                        if grid_map[(x+i[0], y+i[1])] in ["S"]:
                            s += 1
                    except:
                        pass
     
                if s > 3:
                    grid_map[(x, y)] = "S"
                elif n > 2:
                    grid_map[(x, y)] = random.choice(["\"", "'"])
                else:
                    grid_map[(x, y)] = "."



    #grid_map = newgrid.copy()


def main(stdscr):
    resize(30, 100)
    header_win = curses.newwin(3, 100, 0, 0)
    game_map = curses.newpad(HEIGHT + 1, WIDTH + 1)
    stats_win = curses.newwin(25, 30, 4, 68)
    des_win = curses.newpad(1000, 66) 
    
    set_colors()
    new_map(WIDTH, HEIGHT)
    
    first = True
    ticks = 0
    
    if False:
        b = 1

        for i in range(1, 99):
            curses.init_pair(b, i, curses.COLOR_BLACK)
            stdscr.addstr(int(b / 10), (i*2) - (int(b/10) * 20), str(b) if len(str(b)) != 1 else "0" + str(b), curses.color_pair(b))
            b += 1
        stdscr.getkey()
        
    while True:
        
        if first:
            stdscr.clear()
            header_win.clear()
            stats_win.clear()
            
            rectangle(header_win, 0, 0, 2, 98)
            rectangle(stdscr, 3, 67, 29, 98)
            rectangle(stdscr, 3, 0, 20, 66)
            rectangle(stdscr, 21, 0, 29, 66)

            header_win.addstr(1, int(50 - len(f"Health: {player_stats['health']}  |  Level {player_stats['level']}  |  Hunger: {player_stats['hunger']}")/2), "Health: ")
            header_win.addstr(f"{player_stats['health']}", color['RED'] | curses.A_BOLD)
            header_win.addstr("  |  Level ", )
            header_win.addstr(f"{player_stats['level']}", color['DIM GREEN'] | curses.A_BOLD)
            header_win.addstr("  |  Hunger: ")
            header_win.addstr(f"{player_stats['hunger']}", color['DIM RED'] | curses.A_BOLD)
            

            stats_win.addstr("  Stats\n\n")
            
            stats_win.addstr("  You have ")
            stats_win.addstr(f"{player_stats['defense']}", color["GREY"])
            stats_win.addstr(" defense\n")
            
            stats_win.addstr("  You have ")
            stats_win.addstr(f"{player_stats['attack']}", color["DIM MAGENTA"])
            stats_win.addstr(" attack\n")
            
            stats_win.addstr("  You have ")
            stats_win.addstr(f"{player_stats['speed']}", color["TUR"])
            stats_win.addstr(" speed\n")
            
            
            stdscr.refresh()
            header_win.refresh()
            stats_win.refresh()
            
            first = False
            
        #des_win.clear()
        
        
            
        toY = player_cords["y"] - 9
        fromY = player_cords["y"] + 9
        toX = player_cords["x"] - 33
        fromX = player_cords["x"] + 33
        
        if player_cords["y"] - 9 < 0:
            toY = 0
            fromY = 18
        if player_cords["x"] - 33 < 0:
            toX = 0
            fromX = 66
        if fromY-1 >= HEIGHT:
            toY = HEIGHT - 18
            fromY = HEIGHT
        if fromX-1 >= WIDTH:
            toX = WIDTH - 66
            fromX = WIDTH
            
        des_win.addstr("\n")
        
        for y in range(toY, fromY):
            for x in range(toX, fromX):
                i = (x, y)
            
                if i == (player_cords["x"], player_cords["y"]):
                    game_map.addstr(i[1], i[0], "@", curses.A_BOLD | color["YELLOW"])
                    
                    des_win.addstr(" You are on ")
                    if grid_map[i] in ["\"", "'"]:
                        des_win.addstr("some grass", color["GREEN"])
                    elif grid_map[i] in ["S"]:
                        des_win.addstr("some stone", color["GREY"])
                    elif grid_map[i] == ".":
                        des_win.addstr("the ground", color["DIM RED"])
                else:
                    dis_x = player_cords['x'] - x
                    dis_y = player_cords['y'] - y
                    #print(dis_x, dis_y)

                    no_light = ""

                    if abs(dis_x) > abs(dis_y):
                        if dis_y != 0:
                            fra = abs(dis_x) / abs(dis_y)
                            #print(fra)
                            
                            spe = fra - int(fra)
                            re = int(fra)
                            #print(spe, re)
                            
                            fra2 = Fraction(spe).limit_denominator(100)
                            #print(fra2)
                            
                            offset = [fra2.numerator, fra2.denominator]
                        else:
                            re = 1
                            offset = [0, 1]
                        
                        move_tick = 1
                        fake_cords = list(i)
                        #input("READY? ")
                        while True:
                            #input("GU")
                            for p in range(re):
                                fake_cords[0] += int((dis_x/abs(dis_x)) * 1)
                                if grid_map[tuple(fake_cords)] == "S":
                                    no_light = term
                            
                            if grid_map[tuple(fake_cords)] == "S":
                                no_light = term
                            try:
                                fake_cords[1] += int((dis_y/abs(dis_y)) * 1)
                            except:
                                fake_cords[1] += 0
                            
                            
                            if move_tick == offset[1]:
                                move_tick = 0
                                for p in range(offset[0]):
                                    
                                    fake_cords[0] += 1 * (dis_x/abs(dis_x))
                                    if grid_map[tuple(fake_cords)] == "S":
                                        no_light = term
                            
                            if grid_map[tuple(fake_cords)] == "S":
                                no_light = term
                            
                            if tuple(fake_cords) == (player_cords["x"], player_cords["y"]):
                                break
                            
                            move_tick += 1
                    else:
                        if dis_x != 0:
                            fra = abs(dis_y) / abs(dis_x)
                            #print(fra)
                            
                            spe = fra - int(fra)
                            re = int(fra)
                            #print(spe, re)
                            
                            fra2 = Fraction(spe).limit_denominator(100)
                            #print(fra2)
                            
                            offset = [fra2.numerator, fra2.denominator]
                        else:
                            re = 1
                            offset = [0, 1]
                        
                        move_tick = 1
                        fake_cords = list(i)
                        #input("READY? ")
                        while True:
                            #input("GU")
                            for p in range(re):
                                fake_cords[1] += int((dis_y/abs(dis_y)) * 1)
                                if grid_map[tuple(fake_cords)] == "S":
                                    no_light = term
                            
                            if grid_map[tuple(fake_cords)] == "S":
                                no_light = term
                            try:
                                fake_cords[0] += int((dis_x/abs(dis_x)) * 1)
                            except:
                                fake_cords[0] += 0
                            
                            
                            if move_tick == offset[1]:
                                move_tick = 0
                                for p in range(offset[0]):
                                    
                                    fake_cords[1] += 1 * (dis_y/abs(dis_y))
                                    if grid_map[tuple(fake_cords)] == "S":
                                        no_light = term
                            
                            if grid_map[tuple(fake_cords)] == "S":
                                no_light = term
                            
                            if tuple(fake_cords) == (player_cords["x"], player_cords["y"]):
                                break
                            
                            move_tick += 1
                            
                        
                    if grid_map[i] == "\"" or grid_map[i] == "'":
                        if no_light != "":
                            game_map.addstr(i[1], i[0], grid_map[i], color[no_light +  "GREEN"] | curses.A_DIM)
                        else:
                            game_map.addstr(i[1], i[0], grid_map[i], color[no_light +  "GREEN"])
                    elif grid_map[i] == "S":
                        if no_light != "":
                            game_map.addstr(i[1], i[0], grid_map[i], color["GREY"] )
                        else:
                            game_map.addstr(i[1], i[0], grid_map[i], color["GREY"])
                    else:
                        if no_light != "":
                            game_map.addstr(i[1], i[0], grid_map[i], color[no_light +  "WHITE"] | curses.A_DIM)
                        else:
                            game_map.addstr(i[1], i[0], grid_map[i], color[no_light +  "WHITE"])


        game_map.refresh(toY, toX, 4, 1, 19, 65)
        des_win.refresh(max(ticks - 5, 0), 0, 22, 1, 28, 65)
        
        
        key = stdscr.getkey()
        if key == "q":
            break
        if key == "KEY_LEFT":
            player_cords["x"] -= 1
        if key == "KEY_RIGHT":
            player_cords["x"] += 1
        if key == "KEY_UP":
            player_cords["y"] -= 1
        if key == "KEY_DOWN":
            player_cords["y"] += 1
        ticks += 1
        


wrapper(main)
Editor is loading...
Leave a Comment