Dateien hochladen nach „“

master
Justus Jan Nico Wolff 2022-08-06 18:19:25 +02:00
parent 9999091b00
commit b03bad0bb8
2 changed files with 109 additions and 87 deletions

View File

@ -1,52 +1,35 @@
# hashengine-copyright-justus-2022
if you want to edit it and publish it give credit's to me
# how to use the module:
## the game class:
at the beginning of every hashengine game you need to initialize the engine. thats automaticlly being made if you declear a variable to the game class, it will automaticlly install all dependencies
# run the demo programm
to run the demo programm just execute the module and make sure you have these modules installed:
time
ctypes
os
keyboard
to get keys that are being pressed get the keys dictionary from the game class: game.pressedkeys
# what modules the hashengine need:
to play sounds use game.playsound as the path you give the path to the sound file, waitforfinish tells if the hashengine should wait with further code execution until the sound is done playing normal its set to False
the hashengine module needs the os module
the update function is being used for updating all objects in the game and also for updating the pressedkeys dictionary
# how to use the module
to add a object you call the addobj function in the game class
and pass the object class
how to make an box that spawns in air:
```python
import time #imports the time module
import main #imports the hashengine module
game = main.game() # makes a variable with the game instance
box = main.object("#", y=10, gravity=0.1, antixforce=1) # creates the box antixforce selects the braking gravity selects the gravity y selects the y start position x selects the x start position character selects what character the object should be in our case a hashtag just experiment with the settings and you may programm something cool!
game.addobj(box) # adds the box to the game so the game knows that it exists
while True:
game.update() # makes an update for every object in the game
time.sleep(0.05)
## the object class:
the object class is used for objects in the game
it stores data about the object but the calculating is doing the game not the object class itself
```
when you declear the object class then you need to pass this argument: character.
this tells how the object should look like in the game
example:
we give the object class the character "#" so when we add it in the game with addobj() then it shows up as "#"
if you want to control the hashtag use this code:
```python
the rest of the arguments are optional:
gravity selects the gravity for the object. normally 0.1
x selects the x starting position. normally 0
y selects the y starting position. normally 0
antixforce selects the braking when moving left or right. normally 0.1
import time #imports the time module
import main #imports the hashengine module
import keyboard #imports the keyboard module
game = main.game() # makes a variable with the game instance
box = main.object("#", y=10, gravity=0.1, antixforce=1) # creates the box antixforce selects the braking gravity selects the gravity y selects the y start position x selects the x start position character selects what character the object should be in our case a hashtag just experiment with the settings and you may programm something cool!
game.addobj(box) # adds the box to the game so the game knows that it exists
while True:
if keyboard.is_pressed("d"):
box.xvelocity -= 1 # sets it xvelocity experiment with it and you find out its for movement
elif keyboard.is_pressed("a"):
box.xvelocity += 1 # sets it xvelocity experiment with it and you find out its for movement
elif keyboard.is_pressed("w"):
box.yvelocity += 1 # sets it yvelocity experiment with it and you find out its for movement
elif keyboard.is_pressed("s"):
box.yvelocity -= 1 # sets it yvelocity experiment with it and you find out its for movement
elif keyboard.is_pressed("q"):
exit() # exits the game
game.update() # makes an update for every object in the game
time.sleep(0.05)
```
the object class also has xvelocity and yvelocity, they are used to move the object around
example:
yvelocity is being set to 1:
then the object would fly up, height is depending on gravity
the same is with xvelocity but the xvelocity "gravity" is antixforce

127
main.py
View File

@ -1,6 +1,40 @@
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):
@ -10,15 +44,32 @@ class game():
self.objects = []
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.map["{}:{}".format(round(i.y), round(i.x))] = " "
self.futuremap["{}:{}".format(round(i.y), round(i.x))] = " "
for i in self.objects:
i.x -= i.xvelocity
i.y -= i.yvelocity
if "{}:{}".format(round(i.y), round(i.x-i.xvelocity)) in self.map:
if self.map["{}:{}".format(round(i.y), round(i.x-i.xvelocity))] == " ":
i.x -= i.xvelocity
else:
i.x -= i.xvelocity
if "{}:{}".format(round(i.y-i.yvelocity), round(i.x)) in self.map:
if self.map["{}:{}".format(round(i.y-i.yvelocity), round(i.x))] == " ":
i.y -= i.yvelocity
else:
i.y -= i.yvelocity
self.map = self.futuremap.copy()
for i in self.objects:
if round(i.y) == self.size.lines or i.y > self.size.lines:
i.y += i.yvelocity
i.yvelocity = 0
@ -71,6 +122,12 @@ class game():
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
@ -82,52 +139,34 @@ class object():
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 is an ai that follows you. q will exit the demo")
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
import ctypes
import os
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: must run as administrator to run demo")
exit()
import keyboard
gameclass = game()
box = object("#", y=10, gravity=0.1, antixforce=1)
ai = object("2", y=10, x=10, gravity=0)
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(ai)
gameclass.addobj(box2)
gameclass.addobj(box3)
gameclass.addobj(box4)
gameclass.addobj(box5)
while True:
if box.x > ai.x:
ai.xvelocity = -0.3
elif box.x < ai.x:
ai.xvelocity = 0.3
if box.y > ai.y:
ai.yvelocity = -0.3
elif box.y < ai.y:
ai.yvelocity = 0.3
if keyboard.is_pressed("d"):
box.xvelocity -= 1
elif keyboard.is_pressed("a"):
box.xvelocity += 1
elif keyboard.is_pressed("w"):
box.yvelocity += 1
elif keyboard.is_pressed("s"):
box.yvelocity -= 1
elif keyboard.is_pressed("q"):
exit()
gameclass.update()
print(box.yvelocity)
print(box.xvelocity)
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)