[ prog / sol / mona ]

prog


Lets write some games

1 2021-09-10 15:57

Game engines don't need to be big, I'll start with a tiny entity framework+tty graphics

 cat stuff/ef.py
import tty
import sys
import fcntl
import os
class drawable:
    def __init__(self):
        tty.setraw(sys.stdin.fileno())
        orig_fl = fcntl.fcntl(sys.stdin, fcntl.F_GETFL)
        fcntl.fcntl(sys.stdin, fcntl.F_SETFL, orig_fl | os.O_NONBLOCK)
        print("init")
    def drawchar(self,x,y,c):
        print("\033["+str(y)+";"+str(x)+"H"+c,end='')
        print(" ")
    def clear(self):
        print("\033[2J",end="")
        print("")
class entity:
    def __init__(self,x,y,c,attr,name):
        self.x=x
        self.y=y
        self.attr=attr
        self.c=c
        self.name=name
    def render(self,d):
        d.drawchar(self.x,self.y,self.c)
    def update(self,world,dt):
        for a in self.attr:
            a(self,world,dt)
class world:
    def __init__(self,entities):
            self.entities=entities
            self.screen=drawable()
    def render(self):
        self.screen.clear()
        for e in self.entities:
            e.render(self.screen)
    def update(self,dt):
        for e in self.entities:
            e.update(self,dt)
    def getobxy(self,x,y):
        r=[]
        for e in self.entities:
            if e.x == x and e.y == y:
                r.append(e)
        return r
    def getobbytype(self,t):
        r=[]
        for e in self.entities:
            if e.name == t:
                r.append(e)
        return r
    def getinput(self):
        return sys.stdin.read(1)
2


VIP:

do not edit these