hashengine_pong/main.py

103 lines
3.7 KiB
Python

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)