Skip to content

Commit

Permalink
text and border fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
namannarula committed May 25, 2021
1 parent e8705ca commit 2d699f3
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

# Defining class map for each label
CLASS_MAP = {
0: "rock",
1: "paper",
2: "scissors",
0: "Rock",
1: "Paper",
2: "Scissors",
}


Expand All @@ -23,22 +23,22 @@ def calculate_winner(move1, move2):
if move1 == move2:
return "Tie"

if move1 == "rock":
if move2 == "scissors":
if move1 == "Rock":
if move2 == "Scissors":
return "Naman"
if move2 == "paper":
if move2 == "Paper":
return "Computer"

if move1 == "paper":
if move2 == "rock":
if move1 == "Paper":
if move2 == "Rock":
return "Naman"
if move2 == "scissors":
if move2 == "Scissors":
return "Computer"

if move1 == "scissors":
if move2 == "paper":
if move1 == "Scissors":
if move2 == "Paper":
return "Naman"
if move2 == "rock":
if move2 == "Rock":
return "Computer"


Expand All @@ -55,12 +55,12 @@ def calculate_winner(move1, move2):
if not ret:
continue
# rectangle for computer to play
cv2.rectangle(frame, (400, 50), (600, 250), (255, 255, 255), 2)
cv2.rectangle(frame, (370, 85), (620, 335), (255, 255, 255), 2)
# rectangle for user to play
cv2.rectangle(frame, (50, 50), (350, 350), (255, 255, 255), 2)
cv2.rectangle(frame, (30, 70), (350, 350), (255, 255, 255), 2)

# specifying to only detect the hand inside the rectangle
roi = frame[50:350, 50:350]
roi = frame[70:350, 30:350]
img = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (155, 155))

Expand All @@ -73,25 +73,34 @@ def calculate_winner(move1, move2):
# deciding the winner
if prev_move != user_move_name:
if user_move_name != "none":
computer_move_name = choice(['rock', 'paper', 'scissors'])
computer_move_name = choice(['Rock', 'Paper', 'Scissors'])
winner = calculate_winner(user_move_name, computer_move_name)
prev_move = user_move_name

# Text & Formatting
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, "Naman's Move: " + user_move_name,
(25, 25), font, 0.6, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "Computer's Move: " + computer_move_name,
(325, 25), font, 0.6, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "Winner: " + winner,
(150, 410), font, 1.5, (0, 0, 255), 4, cv2.LINE_AA)
font_winner = cv2.FONT_HERSHEY_COMPLEX_SMALL
font_move = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, "NAMAN'S MOVE:",
(87, 35), font_move, 0.8, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, user_move_name,
(157, 60), font_move, 0.7, (0, 155, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "COMPUTER'S MOVE:",
(365, 35), font_move, 0.8, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, computer_move_name,
(450, 60), font_move, 0.7, (0, 155, 255), 2, cv2.LINE_AA)
if winner != "Tie":
cv2.putText(frame, winner + " won the round!",
(30, 420), font_winner, 1.8, (0, 255, 0), 4, cv2.LINE_AA)
else:
cv2.putText(frame, "It's a " + winner + "!",
(210, 420), font_winner, 2, (0, 255, 0), 4, cv2.LINE_AA)

# displaying the computer's move
if computer_move_name != "none":
icon = cv2.imread(
"computerImages/{}.png".format(computer_move_name))
icon = cv2.resize(icon, (200, 200))
frame[50:250, 400:600] = icon
icon = cv2.resize(icon, (250, 250))
frame[85:335, 370:620] = icon

cv2.imshow("Rock Paper Scissors", frame)

Expand Down

0 comments on commit 2d699f3

Please sign in to comment.