summaryrefslogtreecommitdiff
path: root/pong.py
blob: 574253ad5bb1f29793bc4488c28673e7a33dbe64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# zustand.Ball
# Ball.xKoordinate()
# Ball.yKoordinate()
#
# zustand.listeBall()
# zustand.Schlaeger
# Schlaeger.identifikation()
# Schlaeger.yKoordinate()
# Schlaeger.punktzahl()
# zustand.listeSchlaeger()
#
# zug.ausgabe(text)
# zug.nachOben()
# zug.nachUnten()

import math

def zug(id, zustand, zug):
 if not "round" in globals():
  # in der ersten Runde nur initialisieren
  globals()["t_ballX"] = globals()["letztes_ballX"] = getBall(zustand).xKoordinate()
  globals()["t_ballY"] = globals()["letztes_ballY"] = getBall(zustand).yKoordinate()
  globals()["deltaX"] = globals()["deltaY"] = globals()["round"] = globals()["count"] = 0
  return

 globals()["round"] += 1

# zug.ausgabe("Runde " + str(globals()["round"]))

 globals()["t_ballX"] = getBall(zustand).xKoordinate()
 globals()["t_ballY"] = getBall(zustand).yKoordinate()

 if (globals()["t_ballX"] < 15 and globals()["deltaX"] <= 0) or (globals()["t_ballX"] > 50 and globals()["deltaX"] >= 0) \
   or (globals()["t_ballY"] < 5) or (globals()["t_ballY"] > 55):
  globals()["letztes_ballX"], globals()["letztes_ballY"] = globals()["t_ballX"], globals()["t_ballY"]
  globals()["count"] = 0
 
 if globals()["count"] != 0:
  globals()["deltaX"] = float((globals()["t_ballX"] - globals()["letztes_ballX"]) / globals()["count"])
  globals()["deltaY"] = float((globals()["t_ballY"] - globals()["letztes_ballY"]) / globals()["count"])
 globals()["count"] += 1

 bounces = 0
 if globals()["deltaX"] != 0:
  schritte = abs(0 + globals()["t_ballX"] / globals()["deltaX"])
  #nach `schritte` Runden ist der Ball beim anderen Spieler
  bounces = math.floor((globals()["t_ballY"] +  abs(globals()["deltaY"] * schritte)) / 60)
#  zug.ausgabe("Achtung: ")
#  zug.ausgabe(bounces)
#  zug.ausgabe(schritte)

 if globals()["deltaX"] < 0:
  schritte = abs(0 - globals()["t_ballX"]) / globals()["deltaX"]
  target_ballY = ((globals()["t_ballY"] + abs(globals()["deltaY"] * schritte)) % 60)
 else:
  target_ballY = 30
 
 if globals()["t_ballX"] > 55 or globals()["t_ballX"] < 10:
  target_ballY = globals()["t_ballY"]
  
 if getMe(zustand, id).yKoordinate() > target_ballY - 3:
  zug.nachOben()
 else:
  if getMe(zustand, id).yKoordinate() < target_ballY - 3:
   zug.nachUnten()

###
def getMe(zustand, id):
 if zustand.listeSchlaeger()[0].identifikation() == id:
  return zustand.listeSchlaeger()[0]
 return zustand.listeSchlaeger()[1]

def getEnemy(zustand, id):
 if zustand.listeSchlaeger()[0].identifikation() == id:
  return zustand.listeSchlaeger()[1]
 return zustand.listeSchlaeger()[0]

def getBall(zustand):
 return zustand.listeBall()[0]