gPiOBox icon

Further - Tell the Truth! - RPi - Python

gPiOBox | PRO | 10/16/17 11:49:09 AM UTC | 0 ⭐ | 830 👁️ | Never ⏰ | []
Python |

22.23 KB

|

None

|

0 👍

/

0 👎

"""
filename:gpio_rpi_python_truth.py
Tell the Truth! game code
written by Trevor Olsson & Matthew Wells
© GPIO Support Services LTD MMXVII
more info at www.gpio.co.uk/truth
"""
from gpiozero import LED
from time import sleep
import random
import pygame
import time
import sys
import random
import vlc
import os
 
pygame.init()
pygame.font.init()
font = pygame.font.SysFont("monospace",15)
#font_large = pygame.font.SysFont("monospace",)
 
displayInfo = pygame.display.Info()
screen = pygame.display.set_mode((displayInfo.current_w,displayInfo.current_h),pygame.FULLSCREEN)
#screen = pygame.display.set_mode((displayInfo.current_w,displayInfo.current_h))
display_width,display_height = screen.get_size()
font_large = pygame.font.SysFont("monospace",displayInfo.current_h/10)
 
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
 
 
crashed = False
 
OP_old = 0
OP1 = LED(17) # A=0,B=0 in Truth Table
OP2 = LED(18) # A=0,B=1 in Truth Table
OP3 = LED(27) # A=1,B=0 in Truth Table
OP4 = LED(22) # A=1,B=1 in Truth Table
OP5 = LED(23)
OP6 = LED(24)
 
INA = 0
INB = 0
INC = 0
IND = 0
INE = 0
INF = 0
ING = 0
 
# tables used to ensure Gibberish is not a real gate
ANDg = [0,0,0,1]
ORg = [0,1,1,1]
NANDg = [1,1,1,0]
NORg = [1,0,0,0]
XORg = [0,1,1,0]
XNORg = [1,0,0,1]
 
# tables used to turn on lights
AND = [OP4] # Lights required for AND list
OR = [OP2, OP3, OP4] # Lights required for OR list
NAND = [OP1, OP2, OP3] # Lights required for NAND list
NOR = [OP1] # Lights required for NOR list
XOR = [OP2,OP3]
XNOR = [OP1,OP4]
ALL= [OP1, OP2, OP3, OP4, OP5, OP6] # All lights list - used to turn all off
 
score=0
chances=10 # number of goes
def getFinalScore(a,b,c):
    score = 0
    if b == 1:
        score = a/c+4
    if b == 2:
        score = a/c+2
    if b == 3:
        scpre = a/c+4
    scoreDisplay(score) 
    writeScore(score)
def writeScore(score):
    fHandler = open("Scores.txt", "r")
    fData = fHandler.readlines()
    fHandler.close()
    fHandler = open("Scores.txt", "w")
    fData.append(str(score)+"\n")
    fHandler.writelines(fData)
    fHandler.close()
def quitF():
    pygame.quit()
    sys.exit()
def AND_on():
    for light in AND:
        light.on()
def OR_on():
    for light in OR:
        light.on()
def NAND_on():
    for light in NAND:
        light.on()
def NOR_on():
    for light in NOR:
        light.on() 
def XNOR_on():
    for light in XNOR:
        light.on()
def XOR_on():
    for light in XOR:
        light.on()
def Gibberish_on():
    tmp = True
    while tmp:
        attempt = [random.randrange(0,1),random.randrange(0,1),random.randrange(0,1),random.randrange(0,1)]
        if attempt != ANDg and attempt != ORg and attempt != NANDg and attempt != NORg and attempt != XNORg and attempt != XORg:
            tmp = False
    if attempt[0] == 1:
        OP1.on()
    if attempt[1] == 1:
        OP2.on()
    if attempt[2] == 1:
        OP3.on()
    if attempt[3] == 1:
        OP4.on()
def ALL_off():
    for light in ALL:
        light.off()
def ALL_on():
    for light in ALL:
        light.on()
def lightsDefault():
    ALL_off()
    OP5.on()
    OP6.off()
def correctFlash(gate):
    player = play('correct.mp3')
    running = True
    while running:
        randomLight = random.randint(1,6)
        randomStatus = random.randint(1,2)
        if randomLight == 1:
            if randomStatus == 1:
                OP1.on()
            elif randomStatus == 2:
                OP1.off()
        elif randomLight == 2:
            if randomStatus == 1:
                OP2.on()
            elif randomStatus == 2:
                OP2.off()
        elif randomLight == 3:
            if randomStatus == 1:
                OP3.on()
            elif randomStatus == 2:
                OP3.off()
        elif randomLight == 4:
            if randomStatus == 1:
                OP4.on()
            elif randomStatus == 2:
                OP4.off()
        elif randomLight == 5:
            if randomStatus == 1:
                OP5.on()
            elif randomStatus == 2:
                OP5.off()
        elif randomLight == 6:
            if randomStatus == 1:
                OP6.on()
            elif randomStatus == 2:
                OP6.off()
        #strText = "Correct!!"
        #text = font_large.render(strText, 1, black)
        text2 = font_large.render(gate, 1, black)
        screen.fill(green)
        #screen.blit(text, (display_width/2-len(strText), display_height/2))
        screen.blit(text2,(display_width/2-len(gate),display_height/2))
        playing = set([1,2,3,4])
        state = player.get_state()
        pygame.display.update()
        pygame.event.clear()
        if state not in playing:
            running = False
        sleep(.1)
    ALL_off()
def wrongFlash():
    player = play('wrong.mp3')
    running = True
    while running:
        for i in range(1,3):
            ALL_on()
            sleep(0.1)
            ALL_off()
            sleep(0.1)
        strText = "Incorrect!!"
        text = font_large.render(strText, 1, black)
        screen.fill(red)
        screen.blit(text, (display_width/2-len(strText), display_height/2))
        playing = set([1,2,3,4])
        state = player.get_state()
        pygame.display.update()
        pygame.event.clear()
        if state not in playing:
            running = False    
def correct(gate):
    correctFlash(gate)
def wrong():
    wrongFlash()
def waitF(wait):
    i = 0
    broken = 0
    global INA
    global INB
    global INC
    global IND
    startTime = time.time()
    while broken == 0:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                crashed = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    quitF()
                if event.key == pygame.K_LEFT:
                    #print('left')
                    INA = 1
                    pygame.event.clear()
                    broken = 1
                elif event.key == pygame.K_RIGHT:
                    #print('right')  
                    INB = 1
                    pygame.event.clear()
                    broken = 1
                elif event.key == pygame.K_UP:
                    #print('up')
                    INC = 1
                    pygame.event.clear()
                    broken = 1
                elif event.key == pygame.K_DOWN:
                    #print('down')
                    IND = 1
                    pygame.event.clear()
                    broken = 1
        timeSinceStart = time.time()-startTime
        timeRemaining = wait - timeSinceStart
        strText = "Time Remaining: " +format(timeRemaining,'.2f')+"S"
        strText1 = "Tries Remaining: "+str(chances)
        text = font_large.render(strText, 1, black)
        text1= font_large.render(strText1, 1, black)
        screen.fill(white)
        screen.blit(text, (0 , display_height/2))
        screen.blit(text1, (0 , display_height/2-64))
        pygame.display.update()
        #has time passed yet?
        if time.time()-startTime >= wait:
            broken = 1
def waitF2(wait):
    i = 0
    broken = 0
    global INA
    global INB
    global INC
    global IND
    global INE
    global INF
    startTime = time.time()
    while broken == 0:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                crashed = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    quitF()
                if event.key == pygame.K_LEFT:
                    #print('left')
                    INA = 1
                    pygame.event.clear()
                    broken = 1
                elif event.key == pygame.K_RIGHT:
                    #print('right')
                    INB = 1
                    pygame.event.clear()
                    broken = 1
                elif event.key == pygame.K_UP:
                    #print('up')
                    INC = 1
                    pygame.event.clear()
                    broken = 1
                elif event.key == pygame.K_DOWN:
                    #print('down')
                    IND = 1
                    pygame.event.clear()
                    broken = 1
                elif event.key == pygame.K_SPACE:
                    INE = 1
                    pygame.event.clear()
                    broken = 1
                elif event.key == pygame.K_w:
                    INF = 1
                    pygame.event.clear()
                    broken = 1
        timeSinceStart = time.time()-startTime
        timeRemaining = wait - timeSinceStart
        strText = "Time Remaining: " +format(timeRemaining,'.2f')+"S"
        strText1 = "Tries Remaining: "+str(chances)
        text = font_large.render(strText, 1, black)
        text1= font_large.render(strText1, 1, black)
        screen.fill(white)
        screen.blit(text, (0 , display_height/2))
        screen.blit(text1, (0 , display_height/2-64))
        pygame.display.update()
        #has time passed yet?
        if time.time()-startTime >= wait:
            broken = 1
def waitF3(wait):
    i = 0
    broken = 0
    global INA
    global INB
    global INC
    global IND
    global INE
    global INF
    global ING
    startTime = time.time()
    while broken == 0:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                crashed = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    quitF()
                if event.key == pygame.K_LEFT:
                    #print('left')
                    INA = 1
                    pygame.event.clear()
                    broken = 1
                elif event.key == pygame.K_RIGHT:
                    #print('right')
                    INB = 1
                    pygame.event.clear()
                    broken = 1
                elif event.key == pygame.K_UP:
                    #print('up')
                    INC = 1
                    pygame.event.clear()
                    broken = 1
                elif event.key == pygame.K_DOWN:
                    #print('down')
                    IND = 1
                    pygame.event.clear()
                    broken = 1
                elif event.key == pygame.K_SPACE:
                    INE = 1
                    pygame.event.clear()
                    broken = 1
                elif event.key == pygame.K_w:
                    INF = 1
                    pygame.event.clear()
                    broken = 1
                elif event.key == pygame.K_a:
                    ING = 1
                    pygame.event.clear()
                    broken = 1
        timeSinceStart = time.time()-startTime
        timeRemaining = wait - timeSinceStart
        strText = "Time Remaining: " +format(timeRemaining,'.2f')+"S"
        strText1 = "Tries Remaining: "+str(chances)
        text = font_large.render(strText, 1, black)
        text1= font_large.render(strText1, 1, black)
        screen.fill(white)
        screen.blit(text, (0 , display_height/2))
        screen.blit(text1, (0 , display_height/2-64))
        pygame.display.update()
        #has time passed yet?
        if time.time()-startTime >= wait:
            broken = 1
def inputTimeG():
    running = True
    num = 1
    confirm = 0
    while running:
        for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        quitF()
                    if event.key == pygame.K_LEFT:
                        pass
                    elif event.key == pygame.K_RIGHT:
                        pass
                    elif event.key == pygame.K_UP:
                        num = num+1
                    elif event.key == pygame.K_DOWN:
                        confirm = 1
        if num > 9:
            num = 1
        if confirm == 1:
            return(num)
            running = False
        strText = "How many seconds do you need to think about this?"
        strText2 = "1 to 9 Seconds (Press Up to cycle number, down to confirm): "+str(num)
        text = font.render(strText, 1, black)
        text2 = font.render(strText2, 1, black)
        screen.fill(white)
        screen.blit(text, (len(strText) , display_height/2))
        screen.blit(text2, (len(strText2), display_height/2+32))
        pygame.display.update()
    screen.fill(white)
    pygame.display.update()
def play(filename):
    vlcI = vlc.Instance()
    player = vlcI.media_player_new()
    media = vlcI.media_new(filename)
    player.set_media(media)
    player.play()
    playing = set([1,2,3,4])
    sleep(0.25)
    return player
def restart():
    os.system('bash restart_code.sh &')
    quitF()
def scoreDisplay(PScore):
    while True:
        for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        quitF()
                    if event.key == pygame.K_LEFT:
                        restart()
                    elif event.key == pygame.K_RIGHT:
                        restart()
                    elif event.key == pygame.K_UP:
                        restart()
                    elif event.key == pygame.K_DOWN:
                        restart()
                    elif event.key == pygame.K_SPACE:
                            restart()
                    elif event.key == pygame.K_w:
                        restart()
                    elif event.key == pygame.K_e:
                        restart()
    
        strText = "Touch Any Fruit To Continue"
        strText2 = "Your Final Score Was: "+str(PScore)
        text = font.render(strText, 1, black)
        text2 = font.render(strText2, 1, black)
        screen.fill(white)
        screen.blit(text, (len(strText) , display_height/2))
        screen.blit(text2, (len(strText2), display_height/2+32))
        pygame.display.update()
def gameLoopDifficultyOne(wait):
    print('1')
    global chances
    global OP_old
    global INA
    global INB
    global INC
    global IND
    global score
    
    while True:
        lightsDefault()
        chances=chances-1
        #print("You have",chances,"chances left")
        #OP= random.randint(1, 4)
        OP = 1
        while OP == OP_old:
            OP= random.randint(1, 8)
        #print(OP)
        OP_old = OP
    
        if OP==1 or OP==5: # For an AND column
            AND_on()
            waitF(wait)
            if INA != 0:
                correct('and')
                score=score+10
                sleep(.25)
                INA = 0
            elif INB == 1 or INC == 1 or IND == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                wrong()
            
        elif OP==2 or OP==6: # For an OR column
            OR_on()
            waitF(wait)
            if INB != 0:
                correct('OR')
                score=score+10
                sleep(.25)
                INB = 0
            elif INA == 1 or INC == 1 or IND == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                wrong()
        
        elif OP==3 or OP==7: # For a NAND column
            NAND_on()
            waitF(wait)
            if INC !=0:
                correct('NAND')
                score=score+10
                sleep(.25)
                INC = 0
            elif INA == 1 or INB == 1 or IND == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                wrong()
            
        elif OP==4 or OP==8:# For a NOR column
            NOR_on()
            waitF(wait)
            if IND !=0:
                correct('NOR')
                score=score+10
                sleep(.25)
                IND = 0
            elif INA == 1 or INB == 1 or INC == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                wrong()
 
        if chances==0:
            getFinalScore(score,1,wait)
            print ("Your Final Score is:", score/wait, ", Game Over")
            ALL_off()
            break
        screen.fill(white)
        pygame.display.update()
def gameLoopDifficultyTwo(wait):
    print('2')
    global chances
    global OP_old
    global INA
    global INB
    global INC
    global IND
    global INE
    global INF
    global score
    
    while True:
        lightsDefault()
        chances=chances-1
        #print("You have",chances,"chances left")
        OP= random.randint(1, 6)
        #OP = 1
        while OP == OP_old:
            OP= random.randint(1, 6)
        #print(OP)
        OP_old = OP
    
        if OP==1: # For an AND column
            AND_on()
            waitF2(wait)
            if INA != 0:
                correct('and')
                score=score+10
                sleep(.25)
                INA = 0
            elif INB == 1 or INC == 1 or IND == 1 or INE == 1 or INF == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                INE = 0
                INF = 0
                wrong()
            
        elif OP==2: # For an OR column
            OR_on()
            waitF2(wait)
            if INB != 0:
                correct('OR')
                score=score+10
                sleep(.25)
                INB = 0
            elif INA == 1 or INC == 1 or IND == 1 or INE == 1 or INF == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                INE = 0
                INF = 0
                wrong()
        
        elif OP==3: # For a NAND column
            NAND_on()
            waitF2(wait)
            if INC !=0:
                correct('NAND')
                score=score+10
                sleep(.25)
                INC = 0
            elif INA == 1 or INB == 1 or IND == 1 or INE == 1 or INF == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                INE = 0
                INF = 0
                wrong()
            
        elif OP==4:# For a NOR column
            NOR_on()
            waitF2(wait)
            if IND !=0:
                correct('NOR')
                score=score+10
                sleep(.25)
                IND = 0
            elif INA == 1 or INB == 1 or INC == 1 or INE == 1 or INF == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                INE = 0
                INF = 0
                wrong()
        elif OP==5:# For a XOR column
            XOR_on()
            waitF2(wait)
            if INE !=0:
                correct('XOR')
                score=score+10
                sleep(.25)
                INE = 0
            elif INA == 1 or INB == 1 or INC == 1 or IND == 1 or INF == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                INE = 0
                INF = 0
                wrong()
        elif OP==6:# For a XNOR column
            XNOR_on()
            waitF2(wait)
            if INF !=0:
                correct('XNOR')
                score=score+10
                sleep(.25)
                INE = 0
            elif INA == 1 or INB == 1 or INC == 1 or IND == 1 or INE == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                INE = 0
                INF = 0
                wrong()
        if chances==0:
            getFinalScore(score,2,wait)
            print ("Your Final Score is:", score/wait, ", Game Over")
            ALL_off()
            break
        screen.fill(white)
        pygame.display.update()
def gameLoopDifficultyThree(wait):
    print('3')
    global chances
    global OP_old
    global INA
    global INB
    global INC
    global IND
    global INE
    global INF
    global ING
    global score
    
    while True:
        lightsDefault()
        chances=chances-1
        #print("You have",chances,"chances left")
        OP= random.randint(1, 7)
        #OP = 1
        while OP == OP_old:
            OP= random.randint(1, 7)
        #print(OP)
        OP_old = OP
    
        if OP==1: # For an AND column
            AND_on()
            waitF3(wait)
            if INA != 0:
                correct('and')
                score=score+10
                sleep(.25)
                INA = 0
            elif INB == 1 or INC == 1 or IND == 1 or INE == 1 or INF == 1 or ING == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                INE = 0
                INF = 0
                wrong()
            
        elif OP==2: # For an OR column
            OR_on()
            waitF3(wait)
            if INB != 0:
                correct('OR')
                score=score+10
                sleep(.25)
                INB = 0
            elif INA == 1 or INC == 1 or IND == 1 or INE == 1 or INF == 1 or ING == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                INE = 0
                INF = 0
                ING = 0
                wrong()
        
        elif OP==3: # For a NAND column
            NAND_on()
            waitF3(wait)
            if INC !=0:
                correct('NAND')
                score=score+10
                sleep(.25)
                INC = 0
            elif INA == 1 or INB == 1 or IND == 1 or INE == 1 or INF == 1 or ING == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                INE = 0
                INF = 0
                ING = 0
                wrong()
            
        elif OP==4:# For a NOR column
            NOR_on()
            waitF3(wait)
            if IND !=0:
                correct('NOR')
                score=score+10
                sleep(.25)
                IND = 0
            elif INA == 1 or INB == 1 or INC == 1 or INE == 1 or INF == 1 or ING == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                INE = 0
                INF = 0
                ING = 0
                wrong()
        elif OP==5:# For a XOR column
            XOR_on()
            waitF3(wait)
            if INE !=0:
                correct('XOR')
                score=score+10
                sleep(.25)
                INE = 0
            elif INA == 1 or INB == 1 or INC == 1 or IND == 1 or INF == 1 or ING == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                INE = 0
                INF = 0
                ING = 0
                wrong()
        elif OP==6:# For a XNOR column
            XNOR_on()
            waitF3(wait)
            if INF !=0:
                correct('XNOR')
                score=score+10
                sleep(.25)
                INE = 0
            elif INA == 1 or INB == 1 or INC == 1 or IND == 1 or INE == 1 or ING == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                INE = 0
                INF = 0
                ING = 0
                wrong()
        elif OP==7:# For a Gibberish column
            Gibberish_on()
            waitF3(wait)
            if ING !=0:
                correct('Gibberish')
                score=score+10
                sleep(.25)
                INE = 0
            elif INA == 1 or INB == 1 or INC == 1 or IND == 1 or INE == 1:
                INA = 0
                INB = 0
                INC = 0
                IND = 0
                INE = 0
                INF = 0
                ING = 0
                wrong()
        if chances==0:
            getFinalScore(score,3,wait)
            print ("Your Final Score is:", score/wait, ", Game Over")
            ALL_off()
            break
        screen.fill(white)
        pygame.display.update()
def menu(wait):
    menuPos=1
    while True:
        confirmed = 0
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_DOWN:
                    if menuPos != 3:
                        menuPos = menuPos+1
                if event.key == pygame.K_UP:
                    if menuPos != 1:
                        menuPos = menuPos-1
                if event.key == pygame.K_SPACE:
                    if confirmed != 1:
                        confirmed = 1
                if event.key == pygame.K_ESCAPE:
                    quitF()
        screen.fill(white)
        if menuPos == 1:
            text2 = font_large.render(">> Lunatic Mode", 1, blue)
            screen.blit(text2,(50,10))
            text2 = font_large.render("   Normal Mode", 1, black)
            screen.blit(text2,(50,120))
            text2 = font_large.render("   Easy Mode", 1, black)
            screen.blit(text2,(50,230))
        if menuPos == 2:
            text2 = font_large.render("   Lunatic Mode", 1, black)
            screen.blit(text2,(50,10))
            text2 = font_large.render(">> Normal Mode", 1, blue)
            screen.blit(text2,(50,120))
            text2 = font_large.render("   Easy Mode", 1, black)
            screen.blit(text2,(50,230))
        if menuPos == 3:
            text2 = font_large.render("   Lunatic Mode", 1, black)
            screen.blit(text2,(50,10))
            text2 = font_large.render("   Normal Mode", 1, black)
            screen.blit(text2,(50,120))
            text2 = font_large.render(">> Easy Mode", 1, blue)
            screen.blit(text2,(50,230))
        if confirmed == 1:
            print(menuPos)
            if menuPos == 3:
                gameLoopDifficultyOne(wait)
            elif menuPos == 2:
                gameLoopDifficultyTwo(wait)
            elif menuPos == 1:
                gameLoopDifficultyThree(wait)
        pygame.display.update()
 
 
lightsDefault()
wait = 9
#gameLoopDifficultyOne(wait)
menu(wait)

Comments