Skip to content

Commit

Permalink
Fixed controller
Browse files Browse the repository at this point in the history
  • Loading branch information
stlucasgarcia committed Nov 12, 2020
1 parent 1c206da commit f1ccb11
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 58 deletions.
92 changes: 43 additions & 49 deletions controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def __init__(self):
axes = self.joysticks[i].get_numaxes()
print(axes)

print(self.type)
print(self.joysticks)

def checkController(self):
Expand All @@ -38,20 +37,20 @@ def isControllerEvent(self, event):
)

def isControllerDropEvent(self, event):
if self.type[0] == "PS4 Controller":
if event.type == JOYBUTTONDOWN:
return (event.button == 12 or event.button == 0)
if event.type == JOYAXISMOTION:
return (abs(event.axis) == 5 and event.value == 1)

else:
if event.type == JOYBUTTONDOWN:
return event.button == 0
if event.type == JOYHATMOTION:
return event.value[1] == -1
if event.type == JOYAXISMOTION:
return (abs(event.axis) == 5 and event.value == 1)

if len(self.type) > 0:
if self.type[0] == "PS4 Controller":
if event.type == JOYBUTTONDOWN:
return event.button == 12 or event.button == 0
if event.type == JOYAXISMOTION:
return abs(event.axis) == 5 and event.value == 1

else:
if event.type == JOYBUTTONDOWN:
return event.button == 0
if event.type == JOYHATMOTION:
return event.value[1] == -1
if event.type == JOYAXISMOTION:
return abs(event.axis) == 5 and event.value == 1

def get_x_pos(self, event):
px_diff_hd = 109 # how much the chip will move each time
Expand Down Expand Up @@ -140,54 +139,53 @@ def menu_action(self, event):
if event.value == 1:
self.Esc()

def check_event(self, event, menu:object,screen, clock):
def check_event(self, event, menu: object, screen, clock):
px_diff_hd = 109 # how much the chip will move each time
max_px_hd = 931 # max x position of the chip
min_px_hd = 269 # min x position of the chip

if self.type[0] == "PS4 Controller":

# Press Buttons
if event.type == JOYBUTTONDOWN:
if event.button == 1 or event.button == 6:
play_again = menu.run(screen,clock)
if event.button == 1 or event.button == 6:
play_again = menu.run(screen, clock)
if not play_again:
return play_again

# if event.button == 12 or event.button == 0:
# self.DropChip()
# if event.button == 12 or event.button == 0:
# self.DropChip()

if event.button == 13 or event.button == 9:
print("Move left")
self.x_hd -= px_diff_hd if self.x_hd - px_diff_hd >= min_px_hd else 0
self.x_hd -= (
px_diff_hd if self.x_hd - px_diff_hd >= min_px_hd else 0
)
return self.x_hd


if event.button == 14 or event.button == 10:
print("Move right")
self.x_hd += px_diff_hd if self.x_hd + px_diff_hd < max_px_hd else 0
return self.x_hd


# Sticks PS4
if event.type == JOYAXISMOTION:
if abs(event.axis) == 0:
if event.value > .7:
if event.value > 0.7:
print("Move Right")

self.x_hd += 5 if self.x_hd + 5 < max_px_hd else 0
return self.x_hd


if event.value < -.7:

if event.value < -0.7:
print("Move Left")

self.x_hd -= 5 if self.x_hd - 5 >= min_px_hd else 0
return self.x_hd

if abs(event.axis) == 4:
if event.value == 1:
play_again = menu.run(screen,clock)
play_again = menu.run(screen, clock)
if not play_again:
return play_again

Expand All @@ -197,55 +195,51 @@ def check_event(self, event, menu:object,screen, clock):
# Press buttons
if event.type == JOYBUTTONDOWN:

if event.button == 1 or event.button == 7:
play_again = menu.run(screen,clock)
if event.button == 1 or event.button == 7:
play_again = menu.run(screen, clock)
if not play_again:
return play_again

if event.button == 4:
print("Move left")
self.x_hd -= px_diff_hd if self.x_hd - px_diff_hd >= min_px_hd else 0
self.x_hd -= (
px_diff_hd if self.x_hd - px_diff_hd >= min_px_hd else 0
)
return self.x_hd


if event.button == 5:
print("Move right")
self.x_hd += px_diff_hd if self.x_hd + px_diff_hd < max_px_hd else 0
return self.x_hd



# D-pad Xbox
if event.type == JOYHATMOTION:
if event.value[0] == 1:
print("Move right")
self.x_hd += px_diff_hd if self.x_hd + px_diff_hd < max_px_hd else 0
return self.x_hd



if event.value[0] == -1:
print("Move left")
self.x_hd -= px_diff_hd if self.x_hd - px_diff_hd >= min_px_hd else 0
self.x_hd -= (
px_diff_hd if self.x_hd - px_diff_hd >= min_px_hd else 0
)
return self.x_hd



if event.type == JOYAXISMOTION:
if abs(event.axis) == 0:
if event.value > .7:
if event.value > 0.7:
print("Move Right")
self.x_hd += 5 if self.x_hd + 5 < max_px_hd else 0
return self.x_hd



if event.value < -.7:

if event.value < -0.7:
print("Move Left")
self.x_hd -= 5 if self.x_hd - 5 >= min_px_hd else 0
return self.x_hd

if abs(event.axis) == 4:
if event.value == 1:
play_again = menu.run(screen,clock)
play_again = menu.run(screen, clock)
if not play_again:
return play_again

7 changes: 0 additions & 7 deletions main_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ def main_screen(self, usernames: list, option="AI"):
option,
)

print(player_turn)

if play_again != None:
return play_again

Expand All @@ -124,8 +122,6 @@ def main_screen(self, usernames: list, option="AI"):
option,
)

print(player_turn)

if play_again != None:
return play_again

Expand All @@ -143,8 +139,6 @@ def main_screen(self, usernames: list, option="AI"):
control.check_event(event, menu, screen, clock)

if option == "AI" and player_turn % 2 == 1:
print(player_turn)

column, minimax_score = utilities.minimaxTree(
matrix, 5, -math.inf, math.inf, True
)
Expand All @@ -153,7 +147,6 @@ def main_screen(self, usernames: list, option="AI"):
row = utilities.get_open_row(matrix, column)
utilities.drop_piece(matrix, row, column, self.ai_chip)

print(matrix)
if utilities.is_victory(matrix, self.ai_chip):
utilities.draw_board(matrix, start_time, clock)

Expand Down
4 changes: 2 additions & 2 deletions utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def is_valid(
turn += 1

if turn % 2 == 0:
chip = self.chip_1
chip = self.chip_2

turn = 2

Expand All @@ -205,7 +205,7 @@ def is_valid(
return turn, chip

elif turn % 2 == 1:
chip = self.chip_2
chip = self.chip_1

turn = 1

Expand Down

0 comments on commit f1ccb11

Please sign in to comment.