Demo entry 3927797

ayy

   

Submitted by anonymous on Mar 07, 2016 at 20:25
Language: Python. Code size: 19.8 kB.

import pygame, time, pywapi, dropbox, socket

pygame.init() 
BACKGROUNDC = (236,236,232)

#Display Colours:
FONTC = (64,64,64)
NOTEC = (203,204,199)
DROPC = (108,108,108)

#Tag Colours:
RED = (210,115,115)
GREEN = (131,192,131)
BLUE = (113,160,211)

YELLOW = (223,178,61)
PURPLE = (102,75,120)
DROPRED = (184,60,60)
DROPGREEN = (100,172,100)
DROPBLUE = (59,122,191)
DROPYELLOW = (185,142,29)
DROPPURPLE = (63,57,67)

#A 2D list that holds the current notes according to what page they are on:
currentNotes = []
#A list that holds all the current notes:
theNotes = []

size = (990,640)
screen = pygame.display.set_mode(size)
clockfont = pygame.font.Font("Fonts\KozGoPro-ExtraLight.otf", 70)
dayfont = pygame.font.Font("Fonts\KozGoPro-ExtraLight.otf", 35)
smallnotefont = pygame.font.Font("Fonts\KozGoPro-ExtraLight.otf", 30)
nonotesfont = pygame.font.Font("Fonts\KozGoPro-ExtraLight.otf", 55)
smallnonotesfont = pygame.font.Font("Fonts\KozGoPro-ExtraLight.otf", 30)
littlefont = pygame.font.Font("Fonts\KozGoPro-ExtraLight.otf", 15)
reminderfont = pygame.font.Font("Fonts\KozGoPro-ExtraLight.otf", 20)
degreessymbol = pygame.image.load("pngs\degrees.png").convert_alpha()

#Weather Symbols:
problems = pygame.image.load("pngs\problems.png").convert_alpha()
cloudy = pygame.image.load("pngs\cloudy.png").convert_alpha()
sunshine = pygame.image.load("pngs\sunshine.png").convert_alpha()
heavyrain = pygame.image.load("pngs\heavyrain.png").convert_alpha()
lightning = pygame.image.load("pngs\lightning.png").convert_alpha()
lightningwithrain = pygame.image.load("pngs\lightningwithrain.png").convert_alpha()
partlycloudy = pygame.image.load("pngs\partlycloudy.png").convert_alpha()
partlycloudywithrain = pygame.image.load("pngs\partlycloudywithrain.png").convert_alpha()
rain = pygame.image.load("pngs\\rain.png").convert_alpha()
sleet = pygame.image.load("pngs\sleet.png").convert_alpha()
snow = pygame.image.load("pngs\snow.png").convert_alpha()

#Dropbox API code:
access_token = "Ox8-4pTCwgkAAAAAAAB8vehjPwsUnmdrJ7SJpZoDjh8wBMM0kKz90DLSbgpwIUxr"
client = dropbox.client.DropboxClient(access_token)

#Will change the given value for month into the correct form for the program:
def typeMonth(mon):
    monthlist = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"]
    month = monthlist[mon]
    return month

#Will change the given value for days into the correct form for the program:
def typeDay(d):
    daylist = ["MON","TUE","WED","THU","FRI","SAT","SUN"]
    day = daylist[d]
    return day

#Will change the given time value into the correct form for the program:
def timeCorrect(t):
    timelist = ["00","01","02","03","04","05","06","07","08","09"]
    if t < 10:
        time = timelist[t]
    else:
        time = t
    return time

#Will change the given value of temperature from Pywapi into the correct form for the program:
def tempCorrect(t):
    templist = ["  0","  1","  2","  3","  4","  5","  6","  7","  8","  9"]
    if t < 10:
        temp = templist[t]
    else:
        temp = t
    return temp

#Will create a cross clickable for each note so that they can be deleted:
def crossCreate(x,y):
    global crosshoverlist
    x += 430 #:)
    y += 24
    crosslist = [x,y]
    if len(crosshoverlist) != 0:
        xc = crosshoverlist[0]
        yc = crosshoverlist[1]
        pygame.draw.line(screen, FONTC, [xc-2,yc-2], [xc+12,yc+12], 5)
        pygame.draw.line(screen, FONTC, [xc+12,yc-2], [xc-2,yc+12], 5)
    pygame.draw.line(screen, FONTC, [x,y], [x+10,y+10], 3)
    pygame.draw.line(screen, FONTC, [x+10,y], [x,y+10], 3)
    if crosslist not in deletelist:
        deletelist.append(crosslist)

#Will create a coloured tag based on the user's input when they entered a note:        
def tagCreate(x,y,colour):
    if colour == RED:
        dropcolour = DROPRED
    if colour == GREEN:
        dropcolour = DROPGREEN
    if colour == BLUE:
        dropcolour = DROPBLUE
    if colour == YELLOW:
        dropcolour = DROPYELLOW
    if colour == PURPLE:
        dropcolour = DROPPURPLE
    
    pygame.draw.polygon(screen, dropcolour, [[x,y+12], [x,y+17], [x-10,y+17]], 0)
    pygame.draw.polygon(screen, colour,[[x-10,y+18], [x-10,y+38], [x+25,y+38], [x+40,y+28],[x+25,y+18]], 0)

#Makes text fit to the notes by checking the length of
#the notes and moving the text into the next line when
#the current line length is greater than thirty:
def noteText(x,y,message):
    notetext = message
    notestore = []
    noteline = ""
    word = ""
    wordadd = ""
    for i in range(len(notetext)):
        wordadd = notetext[i]
        word += wordadd
        if wordadd == " ":
            if len(noteline+word) > 30:
                notestore.append(noteline)
                noteline = ""
            noteline = noteline + word
            word = ""
    if len(noteline+word) > 30:
        notestore.append(noteline)
        noteline = ""
    noteline = noteline + word
    notestore.append(noteline)

    for i in range(len(notestore)):
        text = smallnotefont.render((notestore[i]), True, FONTC)
        screen.blit(text,[x+8,y+10])
        y += 35
        
#size: 1 = Top Left (default)| 2 = Top Right | 3 = Bottom Left
#4 = Bottom Right | 5 = Large Left | 6 - Large Right
def noteCreate(size,colour,note):
    xs = 22
    ys = 115
    x = 460
    y = 240

    if size == 2:
        xs += 485
    if size == 3:
        ys += 260
    if size == 4:
        ys += 260
        xs += 485
    if size == 5:
        y += 260
    if size == 6:
        xs += 485
        y += 260

    if colour == 1:
        colour = RED
    if colour == 2:
        colour = GREEN
    if colour == 3:
        colour = BLUE
    if colour == 4:
        colour = YELLOW
    if colour == 5:
        colour = PURPLE
        
    pygame.draw.rect(screen, NOTEC, [xs,ys,x,y], 0)
    pygame.draw.rect(screen, DROPC, [xs+8,ys+y,x-16,4], 0)
    tagCreate(xs-1,ys,colour)
    crossCreate(xs,ys)
    noteText(xs+20,ys+45,note)

def alertNoteCreate(note):
    notetext = theNotes[note][2]
    s = pygame.Surface((990,640), pygame.SRCALPHA)
    s.fill((50,50,50,80))
    screen.blit(s, (0,0))

    xs = 260
    ys = 210
    x = 460
    y = 240
    
    pygame.draw.rect(screen, DROPC, [xs-3,ys-3,x+6,y+6], 0)
    pygame.draw.rect(screen, NOTEC, [xs,ys,x,y], 0)

    noteText(xs+20,ys+45,str(notetext))
    reminder = reminderfont.render(("Reminder:"), True, FONTC)
    screen.blit(reminder, [xs+28,ys+15])


    xs += 430
    ys += 206

    if tickhover == True:
        pygame.draw.line(screen, FONTC, [xs-7,ys+5], [xs,ys+12], 5)
        pygame.draw.line(screen, FONTC, [xs+13,ys-2], [xs,ys+12], 5)
    pygame.draw.line(screen, FONTC, [xs-5,ys+5], [xs,ys+10], 3)
    pygame.draw.line(screen, FONTC, [xs+10,ys], [xs,ys+10], 3)

#Creates scroll buttons at the top and bottom of the page
#based on how many notes/pages there are:
def scrollButton():
    global scrollhover
    for i in range(len(scrollhover)):
        scrollaction = scrollhover[i]
        if scrollaction == 0:
            pygame.draw.polygon(screen, FONTC, [[490,630], [500,630], [495,635]], 0)
        if scrollaction == 1:
            pygame.draw.rect(screen, NOTEC, [483,624,25,16], 0)
            pygame.draw.polygon(screen, FONTC, [[488,629], [502,629], [495,636]], 0)
        if scrollaction == 2:
            pygame.draw.polygon(screen, FONTC, [[490,106], [500,106], [495,101]], 0)
        if scrollaction == 3:
            pygame.draw.rect(screen, NOTEC, [483,95,25,16], 0)
            pygame.draw.polygon(screen, FONTC, [[488,107], [502,107], [495,100]], 0)
    scrollhover = []

#Performs a check to see if the new notes have been updated
#since the last time saved:
def readNotes():
    global theNotes
    global timecheck
    notelist = []
    try:
        notes = open("notes.txt", "r")
        timecheck2 = notes.readline()
        if timecheck2 != timecheck:
            theNotes = []
            timecheck = timecheck2
            notes.readline()
            remainingnotes = True
            while remainingnotes == True:
                try:
                    colour = int(notes.readline()[:-1])
                except:
                    remainingnotes = False
                    break
                reminder = notes.readline()[:-1]
                note = notes.readline()[:-1]
                notelist.append(colour)
                notelist.append(reminder)
                notelist.append(note)
                theNotes.append(notelist)
                notelist = []
                if notes.readline()[:-1] == "END":
                    remainingnotes = False
        notes.close()
    except:
        pass

#Formats the notes into a list where the notes in the 2D formatted
#list will correspond to a page and positon on that page. i.e
#"currentNotes[n][i]" will be the "n"th pages and the "i"th note on
#that page.
def orderNotes():
    global currentNotes
    currentNotes = []
    notepage = []
    for i in range(len(theNotes)):
        notepage.append(theNotes[i])
        if len(notepage) == 4:
            currentNotes.append(notepage)
            notepage = []
    if len(notepage) > 0:
        currentNotes.append(notepage)

#Will find which note has been highlighted based on the current
#cross coordinates are being highlighted by the mouse:
def noteFinder(notecoor):
    if notecoor == [452,139]:
        note = 0
    if notecoor == [937,139]:
        note = 1
    if notecoor == [452,399]:
        note = 2
    if notecoor == [937,399]:
        note = 3
    note += 4 * (page-1)
    return note

#Using Pywapi the program will try to sync the latest weather
#depending on if there is an internet connection:
def weatherSync():
    global weatherlist
    if networkCheck() == True:
        weather_com_result = pywapi.get_weather_from_weather_com('UKXX1773')
        temperature = int(weather_com_result["current_conditions"]["temperature"])
        temperature = tempCorrect(temperature)
        weatherlist.append(temperature)
        forecast = str(weather_com_result["current_conditions"]["text"]).lower()
        forecast = weatherCorrect(forecast)
        weatherlist.append(forecast)
    else:
        weatherlist = []
    return weatherlist

#Will take the output from Pywapi and transtlate the output
#to an pre-defiened weather symbol:
def weatherCorrect(weather):
    if weather == "cloudy":
        weather = cloudy
    elif weather == "partly cloudy" or weather == "scattered clouds" or weather == "mostly sunny" or weather == "partly sunny":
        weather = partlycloudy
    elif weather == "sunny" or weather == "clear":
        weather = sunshine
    elif weather == "rain" or "showers in the vicinity":
        weather = rain
    elif weather == "thunderstorm" or weather == "thunderstorms":
        weather = lightningwithrain
    elif weather == "snow":
        weather = snow
    elif weather == "sleet":
        weather = sleet
    else:
        print(weather)
        weather = problems
    return weather

#Will try to download the latest notes from the Dropbox
#cloud based on whether there is an internet connection:
def downloadNotes():  
    if networkCheck() == True:
        try:
            note=open("notes.txt", "wb")
            with client.get_file("/" + "notes.txt") as f:
                note.write(f.read())
            note.close()
        except:
            pass

#Will try to upload the latest notes from the Dropbox
#cloud based on whether there is an internet connection:
def uploadNotes():
    global timecheck
    notes = open("notes.txt", "w")
    notes.write(str(timecheck))
    notes.write("\n")
    for i in range(len(theNotes)):
        for j in range(3):
            notes.write(str(theNotes[i][j]))
            notes.write("\n")
        if i+1 != len(theNotes):
            notes.write("\n")
    notes.write("END")
    notes.write("\n")
    notes.close()
    
    if networkCheck() == True:
        upload = open("notes.txt", "rb")
        client.put_file("notes.txt", upload, True)

#Will check if there is currently an internet connection:
def networkCheck():
    global lastUpdate
    REMOTE_SERVER = "www.google.com"
    try:
        host = socket.gethostbyname(REMOTE_SERVER)
        s = socket.create_connection((host, 80), 2)
        localtime = time.localtime(time.time())
        year = (localtime[0])
        month = timeCorrect(localtime[1])
        numday = timeCorrect(localtime[2])
        h = timeCorrect(localtime[3])
        m = timeCorrect(localtime[4])
        day = typeDay(localtime[6])
        lastUpdate = (str(h) + ":" + str(m) + ":" + "00 " + str(numday) + "/" + str(month) + "/" + str(year))
        return True
    except:
        pass
        print("No Internet! :c")
        return False

def currentTimeFind():
    global currentTime
    localtime = time.localtime(time.time())
    year = timeCorrect(localtime[0])
    month = timeCorrect(localtime[1])
    numday = timeCorrect(localtime[2])
    h = timeCorrect(localtime[3])
    m = timeCorrect(localtime[4])
    day = typeDay(localtime[6])
    currentTime = str(str(h)+str(m)+str(numday)+str(month)+str(year))


#init the check to see if the new notes = the old notes:
timecheck = 0
#init the note page counter:
page = 1
#init the list where the weather is stored (will be an empty list if
#it cannot sync weather):
weatherlist = []
crosshoverlist = []
tickhover = False
deletelist = []
scrollhover = []
scroll = False
bottomscroll = False
topscroll = False
done = False
last_time1 = time.time()
last_time2 = time.time()
clock = pygame.time.Clock()
weatherlist = weatherSync()
currentTime = 0
alertNote = False
alertNoteNum = 0
remindedNotes = []

while not done:
        
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if scroll == True:
            if x in range(483,508) and y in range(624,640):
                bottomscroll = True
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        page += 1
            else:
                bottomscroll = False
                
            if x in range(483,508) and y in range(95,120):
                topscroll = True
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        page -= 1
            else:
                topscroll = False
                
        for i in range(len(deletelist)):
            if x in range(deletelist[i][0] - 5,deletelist[i][0] + 15) and y in range(deletelist[i][1] - 5,deletelist[i][1] + 15):
                crosshoverlist = [deletelist[i][0],deletelist[i][1]]
                hoverednote = deletelist.pop(i)
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        deletednote = noteFinder(hoverednote)
                        theNotes.pop(deletednote)
                        uploadNotes()
                break
            if x in range(680,705) and y in range(411,431) and alertNote == True:
                tickhover = True
                if event.type == pygame.MOUSEBUTTONDOWN:
                    alertNote = False
            else:
                crosshoverlist = []
                tickhover = False

    currentTimeFind()
    for i in range(len(theNotes)):
        if currentTime == theNotes[i][1] and theNotes[i][1] not in (remindedNotes):
            remindedNotes.append(theNotes[i][1])
            alertNote = True
            alertNoteNum = i
        
    pos = pygame.mouse.get_pos()
    x = pos[0]
    y = pos[1]

    localtime = time.localtime(time.time())
    month = typeMonth(localtime[1] - 1)
    numday = localtime[2]
    h = timeCorrect(localtime[3])
    m = timeCorrect(localtime[4])
    day = typeDay(localtime[6])
        
    screen.fill(BACKGROUNDC)


    new_time1 = time.time()
    if new_time1 > last_time1 + 180:
        weatherlist = weatherSync()
        uploadNotes()
        last_time1 = new_time1

    new_time2 = time.time()
    if new_time2 > last_time2 + 15:
        downloadNotes()
        last_time2 = new_time2

    printtime = clockfont.render((str(h)+":"+str(m)), True, FONTC)
    printdate = dayfont.render((str(day)+" "+str(numday)+" "+str(month)), True, FONTC)
    screen.blit(printtime, [15,15])
    screen.blit(printdate, [182,46])

    if networkCheck() == True:
        try:
            printtemp = dayfont.render(weatherlist[0], True, FONTC)
        except:
            weatherlist = weatherSync()
            printtemp = dayfont.render(weatherlist[0], True, FONTC)
        screen.blit(printtemp, [835,46])
        screen.blit(weatherlist[1],[900,15])
        screen.blit(degreessymbol, [873,51])
    else:
        nointernet = littlefont.render("No connection. Last update: " + str(lastUpdate), True, FONTC)
        screen.blit(nointernet, [656,65])

    readNotes()
    orderNotes()
    
    pages = len(currentNotes)

    if pages == 0:
        nonoteshere = nonotesfont.render(("no notes here. . ."), True, FONTC)
        toaddnotes = smallnonotesfont.render(("to add notes, use your phone!"), True, FONTC)
        screen.blit(nonoteshere, [303,300])
        screen.blit(toaddnotes, [305,355])

    else:
        try:
            if len(currentNotes[page - 1]) == 4:
                for i in range(len(currentNotes[page - 1])):
                    noteCreate(i+1,currentNotes[page - 1][i][0],currentNotes[page - 1][i][2])
            if len(currentNotes[page - 1]) == 3:
                for i in range(len(currentNotes[page - 1])):
                    if i == 0:
                        noteCreate(1,currentNotes[page - 1][i][0],currentNotes[page - 1][i][2])
                    if i == 1:
                        noteCreate(3,currentNotes[page - 1][i][0],currentNotes[page - 1][i][2])
                    if i == 2:
                        noteCreate(6,currentNotes[page - 1][i][0],currentNotes[page - 1][i][2])
            if len(currentNotes[page - 1])== 2:
                for i in range(len(currentNotes[page - 1])):
                   noteCreate(5+i,currentNotes[page - 1][i][0],currentNotes[page - 1][i][2])
            if len(currentNotes[page - 1]) == 1:
                   noteCreate(5,currentNotes[page - 1][i][0],currentNotes[page - 1][i][2])
        except:
            page -= 1
        if pages > 1:
            scroll = True
            if page == 1:
                if bottomscroll == True:
                    scrollhover.append(1)
                else:
                    scrollhover.append(0)
            elif page != pages:
                if bottomscroll == True:
                    scrollhover.append(1)
                else:
                    scrollhover.append(0)
                if topscroll == True:
                    scrollhover.append(3)
                else:
                    scrollhover.append(2)
            elif page == pages:
                if topscroll == True:
                    scrollhover.append(3)
                else:
                    scrollhover.append(2)
            scrollButton()
        else:
            scroll = False

    pygame.draw.rect(screen, FONTC, [12,95,968,2], 0)

    if alertNote == True:
        alertNoteCreate(alertNoteNum)

    pygame.display.flip()
    clock.tick(60)
pygame.quit()

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).