Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Tom Pillot
Thymio Game
Commits
bed25ff1
Commit
bed25ff1
authored
Jun 30, 2020
by
Tom Pillot
Browse files
Sort teams by points automatically, add points when a tag is detected.
parent
0a8784e2
Changes
4
Show whitespace changes
Inline
Side-by-side
game/detector.py
View file @
bed25ff1
...
...
@@ -49,6 +49,7 @@ class Detector(apriltag.Detector):
ident
=
str
(
det
.
tag_id
)
pos
=
det
.
center
.
astype
(
int
)
+
(
-
10
,
10
)
cv2
.
putText
(
img
,
ident
,
tuple
(
pos
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
1
,
(
0
,
0
,
255
),
2
)
return
dets
def
stop_webcam
(
self
):
self
.
cam
.
release
()
game/scoreboard.py
View file @
bed25ff1
...
...
@@ -13,12 +13,14 @@ class ScoreWindow(ScoreWindowUi):
super
().
__init__
()
self
.
teams
=
teams
self
.
initial_time
=
0
self
.
time_is_red
=
False
self
.
detector
=
Detector
()
def
start_scoreboard
(
self
,
time
):
# Start and display the clock
self
.
time_edit
.
setTime
(
time
)
self
.
initial_time
=
-
time
.
secsTo
(
qtc
.
QTime
(
0
,
0
,
0
,
0
))
# Time set initially in seconds
self
.
timer
.
timeout
.
connect
(
self
.
timer_timeout
)
self
.
timer
.
start
(
1000
)
self
.
update_gui
()
...
...
@@ -37,8 +39,8 @@ class ScoreWindow(ScoreWindowUi):
self
.
time_is_red
=
True
else
:
self
.
update_gui
()
#
Sort
the table
self
.
sort_by_points
()
#
Refresh
the table
self
.
refresh_table
()
def
update_gui
(
self
):
self
.
time_edit
.
setTime
(
self
.
time_edit
.
time
().
addSecs
(
-
1
))
...
...
@@ -63,12 +65,14 @@ class ScoreWindow(ScoreWindowUi):
points_item
.
setTextAlignment
(
qtc
.
Qt
.
AlignCenter
)
self
.
table_widget
.
setItem
(
row
,
self
.
POINTS
,
points_item
)
team
.
points_item
=
points_item
self
.
table_widget
.
resizeRowsToContents
()
self
.
table_widget
.
resizeColumnsToContents
()
def
refresh_webcam
(
self
):
# Read an image from the webcam and detect the tags it contains
ret
,
frame
=
self
.
detector
.
cam
.
read
()
self
.
detector
.
show_tags_on_image
(
frame
)
dets
=
self
.
detector
.
show_tags_on_image
(
frame
)
if
ret
:
# Convert frame to QImage
cv2
.
cvtColor
(
frame
,
cv2
.
COLOR_RGB2BGR
,
frame
)
...
...
@@ -79,11 +83,35 @@ class ScoreWindow(ScoreWindowUi):
self
.
webcam_label
.
setPixmap
(
pixmap
)
else
:
print
(
"Failed to grab frame."
)
# Update scores
for
det
in
dets
:
if
det
.
decision_margin
>
self
.
detector
.
DECISION_MARGIN
:
self
.
add_points
(
det
.
tag_id
)
self
.
webcam_timer
.
start
(
self
.
WEBCAM_REFRESH_TIME
)
def
sort_by_points
(
self
):
def
refresh_table
(
self
):
# Update points
for
team
in
self
.
teams
.
values
():
team
.
points_item
.
setData
(
qtc
.
Qt
.
EditRole
,
team
.
points
)
# Sort the table by points
self
.
table_widget
.
sortByColumn
(
self
.
POINTS
,
qtc
.
Qt
.
DescendingOrder
)
# Update ranks
for
row
in
range
(
self
.
table_widget
.
rowCount
()):
item
=
self
.
table_widget
.
item
(
row
,
self
.
RANK
)
item
.
setText
(
"#"
+
str
(
row
+
1
))
def
add_points
(
self
,
tag_id
):
"""
Add points to the team with the corresponding tag id
"""
team
=
self
.
teams
[
tag_id
]
remaining_time
=
-
self
.
time_edit
.
time
().
secsTo
(
qtc
.
QTime
(
0
,
0
,
0
,
0
))
if
remaining_time
>
0
:
# Teams can't win any points after the timer has ended
max_points
=
1000
min_points
=
100
points_added
=
min_points
+
(
max_points
-
min_points
)
/
self
.
initial_time
*
remaining_time
team
.
add_points
(
points_added
)
game/team.py
View file @
bed25ff1
...
...
@@ -5,7 +5,8 @@ class Team:
self
.
color
=
color
self
.
points
=
points
self
.
rank
=
0
self
.
can_win_points
=
True
self
.
points_item
=
None
# Points item in the scoreboard table
self
.
can_win_points
=
True
# Prevent a team from winning multiple times
def
add_points
(
self
,
points_added
):
if
self
.
can_win_points
:
...
...
game/team_chooser.py
View file @
bed25ff1
...
...
@@ -113,10 +113,12 @@ class TeamWindow(TeamWindowUi):
def
can_start_activity
(
self
):
"""
Check if the activity can be started
Check if the activity can be started
(at least one team registered, time and activity set)
"""
if
len
(
self
.
teams
)
>
0
:
return
True
else
:
if
len
(
self
.
teams
)
==
0
:
self
.
status_bar
.
showMessage
(
"Veuillez ajouter au moins une équipe."
,
self
.
STATUS_TIME
)
return
False
elif
self
.
time_edit
.
time
()
==
qtc
.
QTime
(
0
,
0
,
0
,
0
):
self
.
status_bar
.
showMessage
(
"Veuillez choisir le temps de l'activité."
,
self
.
STATUS_TIME
)
return
False
return
True
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment