Dateien hochladen nach „“

master
Justus Jan Nico Wolff 2022-08-17 15:41:39 +02:00
parent a95d73fd15
commit 4d817c8d94
2 changed files with 289 additions and 0 deletions

98
asteroids.py 100644
View File

@ -0,0 +1,98 @@
import hashengine as he
import time
import random
game = he.game()
player = []
for i in range(3):
player.append(he.object("#", gravity=0, x=int(game.size.columns/2)+i, y=game.size.lines-1, antixforce=1))
player.append(he.object("#", gravity=0, x=int(game.size.columns/2)+1, y=game.size.lines-2, antixforce=0))
for i in player:
game.addobj(i)
def lose():
print("you lose!")
exit()
asteroids = []
bullets = []
class asteroid():
def __init__(self):
self.object = he.object("", gravity=0, x=random.randint(1, game.size.columns-2), y=1, antixforce=0)
game.addobj(self.object)
def update(self):
self.object.targety = self.object.y + 1
try:
if self.object.y == game.size.lines-1:
self.temp = True
elif game.map["{}:{}".format(self.object.y+1, self.object.x)] == "#":
lose()
elif game.map["{}:{}".format(self.object.y+1, self.object.x)] == "":
self.temp = True
game.update()
ammo += 2
if self.temp:
self.object.targetx = 100000
except:
pass
class bullet():
def __init__(self):
self.object = he.object("", gravity=0, x=player[0].x+1, y=game.size.lines-3, antixforce=0)
game.addobj(self.object)
def update(self):
try:
if game.map["{}:{}".format(self.object.y-1, self.object.x)] == "":
self.temp = True
game.update()
else:
self.object.targety = self.object.y - 1
if self.temp:
self.object.targetx = 10000
except:
pass
asteroidcount = 0
count = 0
ammocount = 0
acm = 10
ammo = 10
while True:
game.update()
if game.pressedkeys["d"] == True:
if player[-1].x != game.size.columns-2:
for i in player:
i.targetx = i.x + 1
if game.pressedkeys["a"] == True:
if player[1].x != 1:
for i in player:
i.targetx = i.x - 1
if game.pressedkeys["q"] == True:
if ammo != 0:
bullets.append(bullet())
ammo -= 1
asteroidcount += 1
count += 1
if ammo == 0:
ammocount += 1
if asteroidcount == acm:
asteroidcount = 0
asteroids.append(asteroid())
if count == 100:
acm = 1
print("is it easy?")
time.sleep(3)
print("well if its so easy try this out:")
time.sleep(3)
if ammocount == 30:
ammocount = 0
ammo += 1
for i in asteroids:
i.update()
for i in bullets:
i.update()
print("ammo: {}".format(ammo))
time.sleep(0.05)

191
hashengine.py 100644
View File

@ -0,0 +1,191 @@
class game():
def __init__(self):
import sys
import subprocess
import pkg_resources
print("initializing hashengine and installing dependencies if missing")
required = {'keyboard', '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 os
import ctypes
import string
def isAdmin():
try:
is_admin = (os.getuid() == 0)
except AttributeError:
is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
return is_admin
if not isAdmin():
print("error: hashengine needs to be run as an admin")
input("enter to continue...")
exit()
self.osmodule = __import__("os")
self.keyboard = __import__("keyboard")
self.sounddevice = __import__("sounddevice")
self.soundfile = __import__("soundfile")
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):
for i in self.pressedkeys:
if self.keyboard.is_pressed(i):
self.pressedkeys[i] = True
else:
self.pressedkeys[i] = False
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):
data, fs = self.soundfile.read(path, dtype='float32')
self.sounddevice.play(data, fs)
if waitforfinish:
self.sounddevice.wait()
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)