Skip to content

Commit c6a5bca

Browse files
committed
merged set_bgpic and del_bgpic in bgpic function
1 parent 210cb3b commit c6a5bca

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

adafruit_turtle.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ def __init__(self, display=None, scale=1):
227227
self._pencolor = 1
228228
self.pencolor(Color.WHITE)
229229
self._bg_pic = None
230+
self._bg_pic_filename = ""
230231
self._turtle_pic = None
231232
self._turtle_odb = None
232233
self._turtle_alt_sprite = None
@@ -915,33 +916,34 @@ def bgcolor(self, c=None):
915916
self._fg_bitmap[w, h] = self._bg_color
916917
return Color.colors[self._bg_color]
917918

918-
def set_bgpic(self, file):
919-
"""
920-
Set a picture as background.
921-
922-
set_bgpic(filename)
923-
Set backgroud picture using OnDiskBitmap.
924-
"""
925-
self._bg_pic = open(file, 'rb')
926-
odb = displayio.OnDiskBitmap(self._bg_pic)
927-
self._odb_tilegrid = displayio.TileGrid(odb, pixel_shader=displayio.ColorConverter())
928-
self._bg_addon_group.append(self._odb_tilegrid)
929-
# centered
930-
self._odb_tilegrid.y = ((self._h*self._fg_scale)//2) - (odb.height//2)
931-
self._odb_tilegrid.x = ((self._w*self._fg_scale)//2) - (odb.width//2)
932-
933-
def del_bgpic(self):
934-
"""
935-
Remove the background picture, if any
936-
937-
del_bgpic()
938-
Remove the picture and close the file
919+
# pylint:disable=inconsistent-return-statements
920+
def bgpic(self, picname=None):
921+
"""Set background image or return name of current backgroundimage.
922+
Optional argument:
923+
picname -- a string, name of an image file or "nopic".
924+
If picname is a filename, set the corresponding image as background.
925+
If picname is "nopic", delete backgroundimage, if present.
926+
If picname is None, return the filename of the current backgroundimage.
939927
"""
940-
if self._bg_pic is not None:
941-
self._bg_addon_group.remove(self._odb_tilegrid)
942-
self._odb_tilegrid = None
943-
self._bg_pic.close()
944-
self._bg_pic = None
928+
if picname is None:
929+
return self._bg_pic_filename
930+
if picname == "nopic":
931+
if self._bg_pic is not None:
932+
self._bg_addon_group.remove(self._odb_tilegrid)
933+
self._odb_tilegrid = None
934+
self._bg_pic.close()
935+
self._bg_pic = None
936+
self._bg_pic_filename = ""
937+
else:
938+
self._bg_pic = open(picname, 'rb')
939+
odb = displayio.OnDiskBitmap(self._bg_pic)
940+
self._odb_tilegrid = displayio.TileGrid(odb, pixel_shader=displayio.ColorConverter())
941+
self._bg_addon_group.append(self._odb_tilegrid)
942+
self._bg_pic_filename = picname
943+
# centered
944+
self._odb_tilegrid.y = ((self._h*self._fg_scale)//2) - (odb.height//2)
945+
self._odb_tilegrid.x = ((self._w*self._fg_scale)//2) - (odb.width//2)
946+
# pylint:enable=inconsistent-return-statements
945947

946948
###########################################################################
947949
# More drawing control

0 commit comments

Comments
 (0)