209 lines
7.4 KiB
Python
209 lines
7.4 KiB
Python
|
|
class game():
|
|
def __init__(self):
|
|
import sys
|
|
import subprocess
|
|
import pkg_resources
|
|
import os
|
|
print("initializing hashengine and installing dependencies if missing")
|
|
self.linecount = 0
|
|
if os.name == 'nt':
|
|
required = {'sounddevice', 'soundfile', 'windows-curses'}
|
|
else:
|
|
required = {'sounddevice', 'soundfile'}
|
|
installed = {pkg.key for pkg in pkg_resources.working_set}
|
|
missing = required - installed
|
|
|
|
if missing:
|
|
print("dependencies missing, installing, info:this may take some time")
|
|
python = sys.executable
|
|
for i in missing:
|
|
print(i)
|
|
subprocess.check_call([python, '-m', 'pip', 'install', i], stdout=sys.stdout)
|
|
print("done")
|
|
import string
|
|
self.osmodule = __import__("os")
|
|
import curses
|
|
self.screen = curses.initscr()
|
|
self.screen.nodelay(1)
|
|
curses.noecho()
|
|
curses.endwin()
|
|
try:
|
|
self.sounddevice = __import__("sounddevice")
|
|
except OSError:
|
|
print("ERROR: Could not find port audio library. so that means that no audio can be played.")
|
|
input("enter to continue")
|
|
self.sounddevice = False
|
|
try:
|
|
self.soundfile = __import__("soundfile")
|
|
|
|
except OSError:
|
|
print("ERROR: Could not find sndfile library. so that means that no audio can be played.")
|
|
input("enter to continue")
|
|
self.sounddevice = False
|
|
self.pressedkeys = {}
|
|
for i in string.ascii_letters:
|
|
self.pressedkeys[i] = False
|
|
self.size = self.osmodule.get_terminal_size()
|
|
self.map = {}
|
|
for i in range(self.size.lines):
|
|
for f in range(self.size.columns):
|
|
self.map["{}:{}".format(i, f)] = " "
|
|
|
|
self.objects = []
|
|
|
|
def round2(self, target):
|
|
from decimal import localcontext, Decimal, ROUND_HALF_UP
|
|
with localcontext() as ctx:
|
|
ctx.rounding = ROUND_HALF_UP
|
|
return Decimal(target).to_integral_value()
|
|
|
|
def update(self):
|
|
import curses
|
|
curses.noecho()
|
|
curses.endwin()
|
|
temp = self.screen.getch()
|
|
#temp = ""
|
|
for i in self.pressedkeys:
|
|
if temp == ord(i):
|
|
self.pressedkeys[i] = True
|
|
else:
|
|
self.pressedkeys[i] = False
|
|
curses.flushinp()
|
|
|
|
self.futuremap = self.map.copy()
|
|
for i in self.objects:
|
|
self.futuremap["{}:{}".format(self.round2(i.y), self.round2(i.x))] = " "
|
|
|
|
for i in self.objects:
|
|
if i.targetx == None:
|
|
if "{}:{}".format(self.round2(i.y), self.round2(i.x-i.xvelocity)) in self.map:
|
|
if self.map["{}:{}".format(self.round2(i.y), self.round2(i.x-i.xvelocity))] == " ":
|
|
i.x -= i.xvelocity
|
|
else:
|
|
i.x -= i.xvelocity
|
|
else:
|
|
i.x = i.targetx
|
|
i.targetx = None
|
|
|
|
if i.targety == None:
|
|
if "{}:{}".format(self.round2(i.y-i.yvelocity), self.round2(i.x)) in self.map:
|
|
if self.map["{}:{}".format(self.round2(i.y-i.yvelocity), self.round2(i.x))] == " ":
|
|
i.y -= i.yvelocity
|
|
else:
|
|
i.y -= i.yvelocity
|
|
else:
|
|
i.y = i.targety
|
|
i.targety = None
|
|
|
|
self.map = self.futuremap.copy()
|
|
|
|
for i in self.objects:
|
|
if self.round2(i.y) == self.size.lines or i.y > self.size.lines:
|
|
i.y += i.yvelocity
|
|
i.yvelocity = 0
|
|
elif self.round2(i.y) <= 0:
|
|
i.y += i.yvelocity
|
|
i.yvelocity = 0
|
|
else:
|
|
if "{}:{}".format(self.round2(i.y+1), self.round2(i.x)) in self.map:
|
|
if self.map["{}:{}".format(self.round2(i.y+1), self.round2(i.x))] != " ":
|
|
i.y += i.yvelocity
|
|
i.yvelocity = 0
|
|
else:
|
|
i.yvelocity -= i.gravity
|
|
|
|
if self.round2(i.x) == self.size.columns or i.x > self.size.columns:
|
|
i.x += i.xvelocity
|
|
i.xvelocity = 0
|
|
elif self.round2(i.x) <= 0:
|
|
i.x += i.xvelocity
|
|
i.xvelocity = 0
|
|
|
|
if i.xvelocity != 0 and i.antixforce != 0:
|
|
if i.xvelocity >= i.antixforce:
|
|
if i.xvelocity > 0:
|
|
i.xvelocity -= i.antixforce
|
|
elif i.xvelocity < 0:
|
|
i.xvelocity += i.antixforce
|
|
else:
|
|
i.xvelocity = 0
|
|
|
|
|
|
for i in self.objects:
|
|
self.map["{}:{}".format(self.round2(i.y), self.round2(i.x))] = i.character
|
|
|
|
self.osmodule.system('cls' if self.osmodule.name == 'nt' else 'clear')
|
|
for i in range(self.size.lines):
|
|
self.temp = ""
|
|
for f in range(self.size.columns):
|
|
self.temp = self.temp + self.map["{}:{}".format(i, f)]
|
|
print(self.temp)
|
|
|
|
def addobj(self, obj):
|
|
try:
|
|
obj.checkifobj
|
|
except AttributeError:
|
|
raise TypeError("given object is not an object")
|
|
|
|
if not 0 <= obj.x <= self.size.columns:
|
|
raise ValueError("given object is out of bounds")
|
|
if not 0 <= obj.y <= self.size.lines:
|
|
raise ValueError("given object is out of bounds")
|
|
|
|
self.objects.append(obj)
|
|
|
|
def playsound(self, path, waitforfinish=False):
|
|
if self.sounddevice != False:
|
|
data, fs = self.soundfile.read(path, dtype='float32')
|
|
self.sounddevice.play(data, fs)
|
|
if waitforfinish:
|
|
self.sounddevice.wait()
|
|
else:
|
|
return
|
|
|
|
class object():
|
|
def __init__(self, character, gravity=0.1, x=0, y=0, antixforce=0.1):
|
|
self.character = character
|
|
self.gravity = gravity
|
|
self.x = x
|
|
self.y = y
|
|
self.targetx = None
|
|
self.targety = None
|
|
self.antixforce = antixforce
|
|
self.xvelocity = 0
|
|
self.yvelocity = 0
|
|
self.checkifobj = None
|
|
|
|
if __name__ == "__main__":
|
|
print("this is a demo program for the hashengine")
|
|
print("use w, a, s, d to control the hashtag. the 2's are for collision testing. q will exit the demo")
|
|
input("enter to continue")
|
|
import time
|
|
gameclass = game()
|
|
box = object("#", antixforce=1, y=10, gravity=0.1)
|
|
box2 = object("2", antixforce=1, y=10, x=10, gravity=0.1)
|
|
box3 = object("2", antixforce=1, y=11, x=10, gravity=0.1)
|
|
box4 = object("2", antixforce=1, y=12, x=10, gravity=0.1)
|
|
box5 = object("2", antixforce=1, y=13, x=10, gravity=0.1)
|
|
keys = gameclass.pressedkeys
|
|
gameclass.addobj(box)
|
|
gameclass.addobj(box2)
|
|
gameclass.addobj(box3)
|
|
gameclass.addobj(box4)
|
|
gameclass.addobj(box5)
|
|
while True:
|
|
gameclass.update()
|
|
if "w" in keys and keys["w"] == True:
|
|
box.yvelocity = 1
|
|
elif "s" in keys and keys["s"] == True:
|
|
box.yvelocity = -1
|
|
elif "a" in keys and keys["a"] == True:
|
|
box.xvelocity = 1
|
|
elif "d" in keys and keys["d"] == True:
|
|
box.xvelocity = -1
|
|
elif "q" in keys and keys["q"] == True:
|
|
exit()
|
|
time.sleep(0.05)
|
|
|