Dateien hochladen nach „“

master
Justus Jan Nico Wolff 2022-08-05 21:19:32 +02:00
parent 9739517280
commit 4bec0c8f57
1 changed files with 133 additions and 0 deletions

133
main.py 100644
View File

@ -0,0 +1,133 @@
class game():
def __init__(self):
self.osmodule = __import__("os")
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 update(self):
for i in self.objects:
self.map["{}:{}".format(round(i.y), round(i.x))] = " "
for i in self.objects:
i.x -= i.xvelocity
i.y -= i.yvelocity
if round(i.y) == self.size.lines or i.y > self.size.lines:
i.y += i.yvelocity
i.yvelocity = 0
elif round(i.y) <= 0:
i.y += i.yvelocity
i.yvelocity = 0
else:
if "{}:{}".format(round(i.y+1), round(i.x)) in self.map:
if self.map["{}:{}".format(round(i.y+1), round(i.x))] != " ":
i.y += i.yvelocity
i.yvelocity = 0
else:
i.yvelocity -= i.gravity
if round(i.x) == self.size.columns or i.x > self.size.columns:
i.x += i.xvelocity
i.xvelocity = 0
elif round(i.x) <= 0:
i.x += i.xvelocity
i.xvelocity = 0
if i.xvelocity != 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(round(i.y), round(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)
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.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 is an ai that follows you. 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)
gameclass.addobj(box)
gameclass.addobj(ai)
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)
time.sleep(0.05)