2 # Experimental implementation of a Python-based Tennix AI Bot
9 (INPUT_AXIS_X
, INPUT_AXIS_Y
) = range(2)
10 (INPUT_KEY_HIT
, INPUT_KEY_TOPSPIN
, INPUT_KEY_SMASH
) = range(3)
12 __name__
= 'John Python'
14 class DumbPythonBot(object):
15 SCREEN_MID_Y
= 480./2.
16 DESIRED_POWER
= { INPUT_KEY_HIT
: 90, INPUT_KEY_TOPSPIN
: 70, INPUT_KEY_SMASH
: 70 }
20 self
.cancelled
= False
26 self
.power_locked
= False
27 self
.current_tactic
= INPUT_KEY_HIT
28 self
.desired_power
= self
.DESIRED_POWER
[self
.current_tactic
]
32 while not self
.cancelled
:
34 self
.current_tactic
= random
.randint(INPUT_KEY_HIT
, INPUT_KEY_SMASH
)
35 self
.desired_power
= self
.DESIRED_POWER
[self
.current_tactic
]*(1.2-0.4*random
.random())
36 print 'determined new tactic:', self
.current_tactic
38 print 'thread cancelled'
41 print __name__
, 'has been loaded'
42 self
.t
= threading
.Thread(target
=self
.blubb
)
46 print __name__
, 'has been unloaded'
48 if self
.t
is not None:
51 def get_key(self
, key_id
):
52 if math
.sqrt((self
.player_x
- self
.ball_x
)**2 + (self
.player_y
- self
.ball_y
)**2) > 150:
55 if self
.power
> self
.desired_power
and random
.randint(0, 3)==0:
56 self
.power_locked
= True
58 if key_id
== self
.current_tactic
and not self
.power_locked
:
59 #if abs(self.player_x - self.ball_x) < 10 and self.power > self.desired_power:
60 return abs(self
.player_x
- self
.ball_x
) > 5 and self
.power
< self
.desired_power
+ 0.3*random
.random()
64 def get_axis(self
, axis_id
):
65 desired_position
= self
.ball_y
- 20. * (self
.ball_y
- self
.SCREEN_MID_Y
) / self
.SCREEN_MID_Y
66 if axis_id
== INPUT_AXIS_Y
and abs(desired_position
- self
.player_y
) > 5:
67 return max(-1.0, min(1.0, desired_position
- self
.player_y
))
71 def ball_moved(self
, x
, y
):
75 def player_moved(self
, x
, y
):
79 def power_changed(self
, power
):
80 if power
< max(0.05, self
.desired_power
- 0.8):
81 self
.power_locked
= False
88 unloaded
= bot
.unloaded
90 get_axis
= bot
.get_axis
91 set_ball_coords
= bot
.ball_moved
92 set_player_coords
= bot
.player_moved
93 set_player_power
= bot
.power_changed