Skip to content

Instantly share code, notes, and snippets.

@linus-jansson
Created November 15, 2021 14:02
Show Gist options
  • Save linus-jansson/df35b6295c81e5948603f256e34d0fc0 to your computer and use it in GitHub Desktop.
Save linus-jansson/df35b6295c81e5948603f256e34d0fc0 to your computer and use it in GitHub Desktop.
Phone Apps
app.background = gradient(rgb(120, 120, 120), rgb(30, 30, 30))
app.stepsSinceNotification = 0
app.stepsForNotification = 30
class Phone(object):
def __init__(self, appDict, owner, backgroundColor):
self.appDict = appDict
self.owner = owner
self.apps = []
self.draw(backgroundColor)
def selectApp(self, appName):
# This method is called when a PhoneApp instance is clicked on. This
# displays the app's name at the bottom of the screen.
self.selectedApp.value = appName
def draw(self, backgroundColor):
self.selectedApp = Label('', 200, 360, fill='whiteSmoke', size=24,
bold=True)
self.drawing = Group(
Label(self.owner + ' phone', 200, 30, fill='whiteSmoke', size=18),
Rect(125, 50, 150, 275, fill='gainsboro'),
Rect(130, 55, 140, 250, fill=backgroundColor),
Circle(200, 315, 8, fill='darkGrey'),
self.selectedApp
)
x = 150
y = 80
# Creates all of the PhoneApp instances and stores them in self.apps.
for name in self.appDict:
self.apps.append(PhoneApp(x, y, self.appDict[name], name))
x += 32
if (x > 250):
x = 150
y += 48
# There are 6 methods that you need to write (including .__init__()). Look at
# the rest of the code and the test cases to figure out how to code them.
### (HINT: There's also a Phone class above this!)
### Fix Your Code Here ###
class PhoneApp(object):
def __init__(self, cx, cy, color, name):
self.draw(cx, cy, color)
self.name = name
self.numNotifications = 0
def draw(self, cx, cy, color):
self.notifLabel = Label('', cx + 12, cy - 12, fill='white', size=10,
bold=True)
self.notifications = Group(
Circle(cx + 12, cy - 12, 6, fill='red'),
self.notifLabel
)
self.drawing = Group(
Circle(cx - 10, cy - 10, 3, fill=color),
Circle(cx + 10, cy - 10, 3, fill=color),
Circle(cx - 10, cy + 10, 3, fill=color),
Circle(cx + 10, cy + 10, 3, fill=color),
Rect(cx, cy, 21, 25, fill=color, align='center'),
Rect(cx, cy, 25, 21, fill=color, align='center'),
self.notifications
)
self.notifications.visible = False
def isClicked(self, x, y):
return self.drawing.hits(x, y)
def handleClick(self):
app.phone.selectApp(self.name)
self.removeNotification()
def removeNotification(self):
self.numNotifications -= 1
self.updateNotificationDisplay()
def addNotification(self):
self.numNotifications += 1
self.updateNotificationDisplay()
def updateNotificationDisplay(self):
if self.numNotifications < 1:
self.notifications.visible = False
else:
self.notifications.visible = True
self.notifLabel.value = self.numNotifications
def onMousePress(mouseX, mouseY):
# This checks if any of the phone's apps were clicked on. If an app was hit,
# it calls the PhoneApp .handleClick() method.
for phoneApp in app.phone.apps:
if (phoneApp.isClicked(mouseX, mouseY) == True):
phoneApp.handleClick()
def onStep():
# Picks a random app to add a notification to.
if (app.stepsSinceNotification >= app.stepsForNotification):
randomApp = choice(app.phone.apps)
randomApp.addNotification()
app.stepsSinceNotification = 0
app.stepsSinceNotification += 1
##### Place your code above this line, code below is for testing purposes #####
# test case:
apps = {
'weather': 'lightSkyBlue',
'notes': 'gold',
'reminders': 'white',
'calendar': 'royalBlue',
'music': 'plum',
'messages': 'lime',
'camera': 'darkGrey',
'settings': 'grey',
'calculator': 'orange',
'contacts': 'gainsboro',
'photos': 'tomato',
'maps': 'mediumSeaGreen',
'clock': 'dimGrey'
}
app.phone = Phone(apps, 'Alice', gradient(rgb(60, 60, 60), 'darkGrey', start='top'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment