Dateien hochladen nach „“

master
Justus Jan Nico Wolff 2022-08-10 17:09:14 +02:00
parent 2429756959
commit 9f715125dc
3 changed files with 298 additions and 2 deletions

View File

@ -1,2 +1,4 @@
# hashengine_pong
# pong in hashengine
## controls:
w and s is for the left player, w is up and s is down
i and k is for the right player, i is up and k is down

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)

103
main.py 100644
View File

@ -0,0 +1,103 @@
import hashengine as he
import time
game = he.game()
player1 = []
player2 = []
player2points = 0
player1points = 0
ball = he.object("o", gravity=0, y=game.size.lines/2, x=game.size.columns/2, antixforce=0)
for i in range(1, 6):
player1.append(he.object("#", gravity=0, y=(game.size.lines)-i))
for i in range(1, 6):
player2.append(he.object("#", gravity=0, y=(game.size.lines)-i, x=game.size.columns-1))
for i in player1:
game.addobj(i)
for i in player2:
game.addobj(i)
class pointshandler():
def __init__(self):
pass
def update(self):
self.text = "{}:{}".format(player1points, player2points)
for i in range(len(self.text), 0, -1):
game.map["{}:{}".format(1, int(game.size.columns/2)-i)] = self.text[len(self.text)-i]
game.addobj(ball)
pointsystem = pointshandler()
ball.xvelocity = 1
balldirection = 1
# 1 = left, 2 = right
while True:
pointsystem.update()
game.update()
if game.round2(ball.xvelocity) == 0:
ball.y = game.size.lines/2
ball.x = game.size.columns/2
if game.round2(ball.y) == game.size.lines-1:
ball.yvelocity = abs(ball.yvelocity)
elif game.round2(ball.y) == 1:
ball.yvelocity = -abs(ball.yvelocity)
if game.pressedkeys["w"] == True:
if player1[-1].y != 1:
for i in player1:
i.targety = i.y - 1
elif game.pressedkeys["s"] == True:
if player1[0].y != game.size.lines-1:
for i in player1:
i.targety = i.y + 1
if game.pressedkeys["i"] == True:
if player2[-1].y != 1:
for i in player2:
i.targety = i.y - 1
elif game.pressedkeys["k"] == True:
if player2[0].y != game.size.lines-1:
for i in player2:
i.targety = i.y + 1
if balldirection == 1:
if "{}:{}".format(game.round2(ball.y), 0) in game.map:
if game.map["{}:{}".format(game.round2(ball.y), 0)] == "#" and 0 <= game.round2(ball.x) <= 2:
balldirection = 2
ball.xvelocity = -abs(ball.xvelocity)
if player1[0].targety != None:
if player1[0].targety < player1[0].y:
ball.yvelocity += 0.3
elif player1[0].targety > player1[0].y:
ball.yvelocity -= 0.3
elif game.round2((ball.x-ball.xvelocity)-1) < 0:
player2points += 1
print("spieler 2 hat gewonnen!")
time.sleep(1)
ball.xvelocity += 0.1
game.map["{}:{}".format(game.round2(ball.y), game.round2(ball.x))] = " "
ball.y = game.size.lines/2
ball.x = game.size.columns/2
else:
if "{}:{}".format(game.round2(ball.y), game.size.columns-1) in game.map:
if game.map["{}:{}".format(game.round2(ball.y), game.size.columns-1)] == "#" and game.size.columns-2.5 <= game.round2(ball.x) < game.size.columns-1.5:
balldirection = 1
ball.xvelocity = abs(ball.xvelocity)
if player2[0].targety != None:
if player2[0].targety < player2[0].y:
ball.yvelocity += 0.3
elif player2[0].targety > player2[0].y:
ball.yvelocity -= 0.3
elif game.round2(ball.x+1) == game.size.columns:
player1points += 1
print("spieler 1 hat gewonnen!")
time.sleep(1)
ball.xvelocity -= 0.1
game.map["{}:{}".format(game.round2(ball.y), game.round2(ball.x))] = " "
ball.y = game.size.lines/2
ball.x = game.size.columns/2
time.sleep(0.05)