Raspi commit...
BIN
raspi/media/apple.gif
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
raspi/media/congratulation_background.gif
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
raspi/media/dynamo_background.jpg
Normal file
After Width: | Height: | Size: 132 KiB |
BIN
raspi/media/foambtn_background.png
Normal file
After Width: | Height: | Size: 276 KiB |
BIN
raspi/media/hello_background.png
Normal file
After Width: | Height: | Size: 122 KiB |
BIN
raspi/media/key_background.gif
Normal file
After Width: | Height: | Size: 31 KiB |
BIN
raspi/media/keyboard_background.png
Normal file
After Width: | Height: | Size: 278 KiB |
BIN
raspi/media/keypad_background.png
Normal file
After Width: | Height: | Size: 228 KiB |
BIN
raspi/media/maze_background.png
Normal file
After Width: | Height: | Size: 198 KiB |
BIN
raspi/media/pot_background.png
Normal file
After Width: | Height: | Size: 289 KiB |
BIN
raspi/media/snake.gif
Normal file
After Width: | Height: | Size: 1023 B |
BIN
raspi/media/snake_background.png
Normal file
After Width: | Height: | Size: 124 KiB |
BIN
raspi/media/sound_background.jpg
Normal file
After Width: | Height: | Size: 61 KiB |
BIN
raspi/media/win.mp3
Normal file
6
raspi/mgmt/ansible.cfg
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[defaults]
|
||||||
|
remote_user=pi
|
||||||
|
hostfile=hosts
|
||||||
|
|
||||||
|
[ssh_connection]
|
||||||
|
pipelining=True
|
5
raspi/mgmt/hosts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
[raspi]
|
||||||
|
192.168.43.12
|
||||||
|
#192.168.43.44
|
||||||
|
192.168.43.219
|
||||||
|
192.168.43.231
|
607
raspi/pySejf.py
Normal file
@ -0,0 +1,607 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
import os
|
||||||
|
os.chdir(os.path.dirname(os.path.realpath(__file__)))
|
||||||
|
|
||||||
|
import subprocess
|
||||||
|
subprocess.call('(date; pwd) >> /tmp/log', shell=True)
|
||||||
|
|
||||||
|
import random, math, pygame
|
||||||
|
from pygame.locals import *
|
||||||
|
from virtualKeyboard import VirtualKeyboard
|
||||||
|
|
||||||
|
import threading
|
||||||
|
from math import cos
|
||||||
|
import time
|
||||||
|
import RPi.GPIO as GPIO
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
#gameplay
|
||||||
|
#wynik w snake, ktory wygrywa
|
||||||
|
SNAKE_SCORE = 2
|
||||||
|
#predkosc gry w snake - w zakresie od 1 do 10
|
||||||
|
TICK_SNAKE = 10
|
||||||
|
#ilosc segmentow dodanych do snake po zlapaniu jablka
|
||||||
|
SNAKE_ADDSIZE = 10
|
||||||
|
|
||||||
|
#hasla klawiatury ekranowej dla obu zagadek
|
||||||
|
password1 = 'one'
|
||||||
|
password2 = 'two'
|
||||||
|
|
||||||
|
#ilosc powtorzen mp3
|
||||||
|
MP3_LOOPS = 4
|
||||||
|
|
||||||
|
#layout
|
||||||
|
#font i komunikat do snake
|
||||||
|
FONT_SNAKE = pygame.font.SysFont("Courier", 28)
|
||||||
|
MSG_SNAKE = 'Koniec gry! Twoj wynik: '
|
||||||
|
|
||||||
|
#font klawiatury
|
||||||
|
FONT_KEYBOARD = pygame.font.SysFont("Purisa", 28)
|
||||||
|
#kolor klawiatury w RGB
|
||||||
|
COLOR_KEYBOARD = [0, 0, 127]
|
||||||
|
#kolor tekstu na klawiaturze (na klawiszach, guzikach i wpisywanego hasla) w RGB
|
||||||
|
COLOR_KEYBOARD_TEXT = (0, 255, 0)
|
||||||
|
#tekst na przyciskach klawiatury
|
||||||
|
MSG_BTN1 = "Wpisz ja"
|
||||||
|
MSG_BTN2 = "Kasuj"
|
||||||
|
|
||||||
|
#grafika - jablko do snake, segmenty snake
|
||||||
|
IMG_APPLE = pygame.image.load("media/apple.gif")
|
||||||
|
IMG_SNAKEBODY = pygame.image.load("media/snake.gif")
|
||||||
|
|
||||||
|
#obrazki poszczegolnych slajdow
|
||||||
|
IMG_HELLO = pygame.image.load('media/hello_background.png')
|
||||||
|
IMG_KEY = pygame.image.load('media/key_background.gif')
|
||||||
|
IMG_WIN = pygame.image.load('media/congratulation_background.gif')
|
||||||
|
IMG_POT = pygame.image.load('media/pot_background.png')
|
||||||
|
IMG_FOAMBTN = pygame.image.load('media/foambtn_background.png')
|
||||||
|
IMG_KEYPAD = pygame.image.load('media/keypad_background.png')
|
||||||
|
IMG_SNAKE = pygame.image.load('media/snake_background.png')
|
||||||
|
IMG_MAZE = pygame.image.load('media/maze_background.png')
|
||||||
|
IMG_MP3 = pygame.image.load('media/sound_background.jpg')
|
||||||
|
IMG_KEYBOARD = pygame.image.load('media/keyboard_background.png')
|
||||||
|
IMG_DYNAMO = pygame.image.load('media/dynamo_background.jpg')
|
||||||
|
|
||||||
|
wall_a = 0 #czy to RPi jest na scianie A?
|
||||||
|
|
||||||
|
#pinologia
|
||||||
|
#przypisania pinow - fizyczne przyciski
|
||||||
|
PIN_BTNY = 17
|
||||||
|
PIN_BTNG = 27
|
||||||
|
|
||||||
|
#przypisania pinow - piny z RPi do Ardu zapalajace diode
|
||||||
|
PIN_SUC4 = 2
|
||||||
|
PIN_SUC9 = 3
|
||||||
|
PIN_SUC10 = 4
|
||||||
|
|
||||||
|
#przypisania pinow - piny z Ardu do Rpi wywolujace slajd
|
||||||
|
PIN_SET1 = 25
|
||||||
|
PIN_SET2 = 8
|
||||||
|
PIN_SET3 = 7
|
||||||
|
PIN_SET5 = 12
|
||||||
|
PIN_SET6 = 16
|
||||||
|
PIN_SET_WIN = 20
|
||||||
|
|
||||||
|
#przypisania pinow - zworka do GND na tym pinie ustawia, ze ta Pi jest na scianie A (uruchamia dzwiek i obrotowa lampe)
|
||||||
|
PIN_WALL_A = 26
|
||||||
|
#przypisania pinow - na tym pinie jest przekaznik wlaczajacy lampe obrotowa i otwierajacy szuflade #5
|
||||||
|
PIN_RELAY = 21
|
||||||
|
|
||||||
|
#ustawianie stanow i trybow pracy pinow
|
||||||
|
GPIO.setmode(GPIO.BCM) # set up BCM GPIO numbering
|
||||||
|
GPIO.setup(PIN_BTNY, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO2 as input (button)
|
||||||
|
GPIO.setup(PIN_BTNG, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO2 as input (button)
|
||||||
|
GPIO.setup(PIN_SET1, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO2 as input (button)
|
||||||
|
GPIO.setup(PIN_SET2, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO2 as input (button)
|
||||||
|
GPIO.setup(PIN_SET3, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO2 as input (button)
|
||||||
|
GPIO.setup(PIN_SET5, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO2 as input (button)
|
||||||
|
GPIO.setup(PIN_SET6, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO2 as input (button)
|
||||||
|
GPIO.setup(PIN_WALL_A, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set GPIO2 as input (button)
|
||||||
|
|
||||||
|
if GPIO.input(PIN_WALL_A) == 0:
|
||||||
|
print "this RPi is on wall A."
|
||||||
|
wall_a = 1
|
||||||
|
|
||||||
|
if(wall_a):
|
||||||
|
GPIO.setup(PIN_SET_WIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # ten pin jest dzielony miedzy wszystkie rpi i arduina - pullup robi tylko RPi A
|
||||||
|
else:
|
||||||
|
GPIO.setup(PIN_SET_WIN, GPIO.IN)
|
||||||
|
print "this RPi is."
|
||||||
|
|
||||||
|
GPIO.setup(PIN_SUC4, GPIO.OUT)
|
||||||
|
GPIO.output(PIN_SUC4, 1)
|
||||||
|
GPIO.setup(PIN_SUC9, GPIO.OUT)
|
||||||
|
GPIO.setup(PIN_SUC10, GPIO.OUT)
|
||||||
|
GPIO.setup(PIN_RELAY, GPIO.OUT)
|
||||||
|
GPIO.output(PIN_SUC9, 1)
|
||||||
|
GPIO.output(PIN_SUC10, 1)
|
||||||
|
GPIO.output(PIN_RELAY, 1)
|
||||||
|
|
||||||
|
WINSIZEX, WINSIZEY = 900, 680
|
||||||
|
counter = 0
|
||||||
|
timeSec = 0
|
||||||
|
UP = 1
|
||||||
|
DOWN = 3
|
||||||
|
RIGHT = 2
|
||||||
|
LEFT = 4
|
||||||
|
lastJoyState = RIGHT
|
||||||
|
score = 0
|
||||||
|
dynamoValSPI = [0,0,0]
|
||||||
|
|
||||||
|
#numery slajdow
|
||||||
|
SLIDE_START = 0
|
||||||
|
SLIDE_KEY = 1
|
||||||
|
SLIDE_POT = 2
|
||||||
|
SLIDE_FOAMBTN = 3
|
||||||
|
SLIDE_SNAKE = 4
|
||||||
|
SLIDE_DYNAMO = 5
|
||||||
|
SLIDE_KEYPAD = 6
|
||||||
|
SLIDE_MAZE = 7
|
||||||
|
SLIDE_MP3 = 8
|
||||||
|
SLIDE_KEYBOARD = 9
|
||||||
|
SLIDE_CONGRATS = 11
|
||||||
|
|
||||||
|
SLIDE_SNAKE_DEAD = 10 #to nie slajd, tylko wartosc potrzebna do resetowania snake po przegranej
|
||||||
|
|
||||||
|
global slide
|
||||||
|
#slajd poczatkowy
|
||||||
|
slide = SLIDE_START
|
||||||
|
slides = [SLIDE_START,SLIDE_KEY,SLIDE_POT,SLIDE_FOAMBTN,SLIDE_SNAKE,SLIDE_KEYPAD,SLIDE_MAZE,SLIDE_MP3,SLIDE_KEYBOARD,SLIDE_CONGRATS]
|
||||||
|
|
||||||
|
|
||||||
|
def win():
|
||||||
|
print "GAME OVER - YOU WIN"
|
||||||
|
GPIO.output(PIN_RELAY, 0)
|
||||||
|
|
||||||
|
|
||||||
|
def signalWin(success):
|
||||||
|
pin = 0
|
||||||
|
if success == 4:
|
||||||
|
pin = PIN_SUC4
|
||||||
|
elif success == 9:
|
||||||
|
pin = PIN_SUC9
|
||||||
|
elif success == 10:
|
||||||
|
pin = PIN_SUC10
|
||||||
|
print("signalling win for task: ")
|
||||||
|
print(success)
|
||||||
|
print("using GPIO pin: ")
|
||||||
|
print(pin)
|
||||||
|
GPIO.output(pin, 0)
|
||||||
|
pygame.time.wait(200)
|
||||||
|
GPIO.output(pin, 1)
|
||||||
|
print("done signalling.")
|
||||||
|
|
||||||
|
def waitForEvents():
|
||||||
|
global slide
|
||||||
|
print("waiting for events")
|
||||||
|
while 1:
|
||||||
|
if GPIO.input(PIN_SET1) == 0 and slide != SLIDE_KEY:
|
||||||
|
print "Ardu said go to key slide"
|
||||||
|
slide = SLIDE_KEY
|
||||||
|
return
|
||||||
|
if GPIO.input(PIN_SET2) == 0 and slide != SLIDE_POT:
|
||||||
|
print "Ardu said go to pot slide"
|
||||||
|
slide = SLIDE_POT
|
||||||
|
return
|
||||||
|
if GPIO.input(PIN_SET3) == 0 and slide != SLIDE_FOAMBTN:
|
||||||
|
print "Ardu said go to foam button slide"
|
||||||
|
slide = SLIDE_FOAMBTN
|
||||||
|
return
|
||||||
|
if GPIO.input(PIN_SET5) == 0 and slide != SLIDE_DYNAMO:
|
||||||
|
print "Ardu said go to dynamo slide"
|
||||||
|
slide = SLIDE_DYNAMO
|
||||||
|
return
|
||||||
|
if GPIO.input(PIN_SET6) == 0 and slide != SLIDE_KEYPAD:
|
||||||
|
print "Ardu said go to keypad slide"
|
||||||
|
slide = SLIDE_KEYPAD
|
||||||
|
return
|
||||||
|
if GPIO.input(PIN_SET_WIN) == 0 and slide != SLIDE_CONGRATS:
|
||||||
|
print "Ardu said win"
|
||||||
|
slide = SLIDE_CONGRATS
|
||||||
|
return
|
||||||
|
if GPIO.input(PIN_BTNY) == 0 and slide != SLIDE_MAZE:
|
||||||
|
print "yellow button pressed - go to maze slide"
|
||||||
|
slide = SLIDE_MAZE
|
||||||
|
return
|
||||||
|
if GPIO.input(PIN_BTNG) == 0 and slide != SLIDE_MP3:
|
||||||
|
print "green button pressed - go to mp3 slide"
|
||||||
|
slide = SLIDE_MP3
|
||||||
|
return
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
slide = SLIDE_KEYBOARD
|
||||||
|
print("mouse event")
|
||||||
|
return
|
||||||
|
|
||||||
|
elif event.type == pygame.KEYDOWN:
|
||||||
|
if slide != SLIDE_SNAKE:
|
||||||
|
slide = SLIDE_SNAKE
|
||||||
|
else:
|
||||||
|
slide = SLIDE_SNAKE_DEAD
|
||||||
|
print("keydown event")
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def checkEvents():
|
||||||
|
global slide
|
||||||
|
if GPIO.input(PIN_SET1) == 0 and slide != SLIDE_KEY:
|
||||||
|
print "Ardu said go to key slide"
|
||||||
|
slide = SLIDE_KEY
|
||||||
|
return
|
||||||
|
if GPIO.input(PIN_SET2) == 0 and slide != SLIDE_POT:
|
||||||
|
print "Ardu said go to pot slide"
|
||||||
|
slide = SLIDE_POT
|
||||||
|
return
|
||||||
|
if GPIO.input(PIN_SET3) == 0 and slide != SLIDE_FOAMBTN:
|
||||||
|
print "Ardu said go to foam button slide"
|
||||||
|
slide = SLIDE_FOAMBTN
|
||||||
|
return
|
||||||
|
if GPIO.input(PIN_SET5) == 0 and slide != SLIDE_DYNAMO:
|
||||||
|
print "Ardu said go to dynamo slide"
|
||||||
|
slide = SLIDE_DYNAMO
|
||||||
|
return
|
||||||
|
if GPIO.input(PIN_SET6) == 0 and slide != SLIDE_KEYPAD:
|
||||||
|
print "Ardu said go to keypad slide"
|
||||||
|
slide = SLIDE_KEYPAD
|
||||||
|
return
|
||||||
|
if GPIO.input(PIN_SET_WIN) == 0 and slide != SLIDE_CONGRATS:
|
||||||
|
print "Ardu said win"
|
||||||
|
slide = SLIDE_KEYPAD
|
||||||
|
return
|
||||||
|
if GPIO.input(PIN_BTNY) == 0 and slide != SLIDE_MAZE:
|
||||||
|
print "yellow button pressed - go to maze slide"
|
||||||
|
slide = SLIDE_MAZE
|
||||||
|
return
|
||||||
|
if GPIO.input(PIN_BTNG) == 0 and slide != SLIDE_MP3:
|
||||||
|
print "green button pressed - go to mp3 slide"
|
||||||
|
slide = SLIDE_MP3
|
||||||
|
return
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN and slide != SLIDE_KEYBOARD:
|
||||||
|
slide = SLIDE_KEYBOARD
|
||||||
|
print("mouse event")
|
||||||
|
return 1
|
||||||
|
|
||||||
|
elif event.type == pygame.KEYDOWN:
|
||||||
|
slide = SLIDE_SNAKE
|
||||||
|
print("keydown event")
|
||||||
|
return
|
||||||
|
else: return 0
|
||||||
|
|
||||||
|
def drawSlide(n):
|
||||||
|
print("drawing slide:")
|
||||||
|
print(n)
|
||||||
|
screen.fill(WHITE)
|
||||||
|
screen.blit(slides[n], (0, 0))
|
||||||
|
pygame.display.flip()
|
||||||
|
waitForEvents()
|
||||||
|
|
||||||
|
def collide(x1, x2, y1, y2, w1, w2, h1, h2):
|
||||||
|
if x1+w1>x2 and x1<x2+w2 and y1+h1>y2 and y1<y2+h2:return True
|
||||||
|
else:return False
|
||||||
|
|
||||||
|
def die(screen, score):
|
||||||
|
f=FONT_SNAKE
|
||||||
|
t=f.render(MSG_SNAKE+str(score), True, (0, 0, 0))
|
||||||
|
if(score >= SNAKE_SCORE):
|
||||||
|
signalWin(4)
|
||||||
|
screen.blit(t, (10, 270))
|
||||||
|
pygame.display.update()
|
||||||
|
pygame.time.wait(2000)
|
||||||
|
waitForEvents()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
global slide
|
||||||
|
|
||||||
|
startonce = 0
|
||||||
|
showstartscreen = 0
|
||||||
|
|
||||||
|
while startonce == 0:
|
||||||
|
startonce = 0
|
||||||
|
######## CONSTANTS
|
||||||
|
WINSIZE = [WINSIZEX,WINSIZEY]
|
||||||
|
BLOCKSIZE = [18,18]
|
||||||
|
UP = 1
|
||||||
|
DOWN = 3
|
||||||
|
RIGHT = 2
|
||||||
|
LEFT = 4
|
||||||
|
WHITE = [255, 255, 255]
|
||||||
|
|
||||||
|
MINX = 100
|
||||||
|
MAXX = WINSIZEX - MINX
|
||||||
|
MINY = 100
|
||||||
|
MAXY = WINSIZEY - MINY
|
||||||
|
|
||||||
|
|
||||||
|
SNAKESTEP = 20
|
||||||
|
TRUE = 1
|
||||||
|
FALSE = 0
|
||||||
|
|
||||||
|
|
||||||
|
######## VARIABLES
|
||||||
|
direction = RIGHT # 1=up,2=right,3=down50,4=left
|
||||||
|
snakexy = [300,400]
|
||||||
|
snakelist = [[300,400],[280,400],[260,400]]
|
||||||
|
counter = 0
|
||||||
|
score = 0
|
||||||
|
appleonscreen = 0
|
||||||
|
applexy = [0,0]
|
||||||
|
newdirection = RIGHT
|
||||||
|
snakedead = FALSE
|
||||||
|
gameregulator = 6
|
||||||
|
gamepaused = 0
|
||||||
|
growsnake = 0 # added to grow tail by two each time
|
||||||
|
snakegrowunit = 2 # added to grow tail by two each time
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
screen = pygame.display.set_mode(WINSIZE)#FULLSCREEN)
|
||||||
|
#screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
|
||||||
|
pygame.display.set_caption('PozySejf')
|
||||||
|
pygame.FULLSCREEN
|
||||||
|
#screen = display.set_mode([1024,768], FULLSCREEN)
|
||||||
|
#screen.fill(BLACK)
|
||||||
|
dynamoValSPI = [0,0,0]
|
||||||
|
dynamoRectY = [50, 50, 50]
|
||||||
|
dynamoRectX = 50
|
||||||
|
dynamoXpos = [-100, 0, 100]
|
||||||
|
distanceDynamoRect = 300
|
||||||
|
textInputDotNumber = 0
|
||||||
|
pushButtonNumber = 100
|
||||||
|
|
||||||
|
valueSound = 100
|
||||||
|
|
||||||
|
passwordLetter = ''
|
||||||
|
#### show initial start screen
|
||||||
|
### every while loop for each slide
|
||||||
|
while slide == SLIDE_START:
|
||||||
|
print("showing slide 0")
|
||||||
|
screen.fill(WHITE)
|
||||||
|
screen.blit(IMG_HELLO, (0, 0))
|
||||||
|
pygame.display.flip()
|
||||||
|
pygame.time.wait(5000)
|
||||||
|
waitForEvents()
|
||||||
|
break
|
||||||
|
|
||||||
|
while slide == SLIDE_KEY:
|
||||||
|
print("showing slide 1 - key")
|
||||||
|
screen.fill(WHITE)
|
||||||
|
screen.blit(IMG_KEY, (0, 0))
|
||||||
|
pygame.display.flip()
|
||||||
|
waitForEvents()
|
||||||
|
break
|
||||||
|
|
||||||
|
while slide == SLIDE_POT:
|
||||||
|
print("showing slide 2 - pot")
|
||||||
|
screen.fill(WHITE)
|
||||||
|
screen.blit(IMG_POT, (0, 0))
|
||||||
|
pygame.display.flip()
|
||||||
|
waitForEvents()
|
||||||
|
break
|
||||||
|
|
||||||
|
while slide == SLIDE_FOAMBTN:
|
||||||
|
print("showing slide 3 - foam button")
|
||||||
|
screen.fill(WHITE)
|
||||||
|
screen.blit(IMG_FOAMBTN, (0, 0))
|
||||||
|
pygame.display.flip()
|
||||||
|
waitForEvents()
|
||||||
|
break
|
||||||
|
|
||||||
|
while slide == SLIDE_SNAKE: ## snake
|
||||||
|
print("snake is starting...")
|
||||||
|
|
||||||
|
xs = [290, 290, 290, 290, 290];
|
||||||
|
ys = [290, 270, 250, 230, 210];
|
||||||
|
dirs = 0
|
||||||
|
score = 0
|
||||||
|
applepos = (random.randint(0, WINSIZEX), random.randint(0, WINSIZEY))
|
||||||
|
pygame.init()
|
||||||
|
#s=pygame.display.set_mode((600, 600))
|
||||||
|
appleimage = IMG_APPLE
|
||||||
|
img = IMG_SNAKEBODY#pygame.Surface((20, 20))
|
||||||
|
#img.fill((255, 0, 0))
|
||||||
|
f = FONT_SNAKE
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
while slide == SLIDE_SNAKE:
|
||||||
|
clock.tick(TICK_SNAKE)
|
||||||
|
if GPIO.input(PIN_SET1) == 0 and slide != SLIDE_KEY:
|
||||||
|
print "Ardu said go to key slide"
|
||||||
|
slide = SLIDE_KEY
|
||||||
|
break
|
||||||
|
if GPIO.input(PIN_SET2) == 0 and slide != SLIDE_POT:
|
||||||
|
print "Ardu said go to pot slide"
|
||||||
|
slide = SLIDE_POT
|
||||||
|
break
|
||||||
|
if GPIO.input(PIN_SET3) == 0 and slide != SLIDE_FOAMBTN:
|
||||||
|
print "Ardu said go to foam button slide"
|
||||||
|
slide = SLIDE_FOAMBTN
|
||||||
|
break
|
||||||
|
if GPIO.input(PIN_SET5) == 0 and slide != SLIDE_DYNAMO:
|
||||||
|
print "Ardu said go to dynamo slide"
|
||||||
|
slide = SLIDE_DYNAMO
|
||||||
|
break
|
||||||
|
if GPIO.input(PIN_SET6) == 0 and slide != SLIDE_KEYPAD:
|
||||||
|
print "Ardu said go to keypad slide"
|
||||||
|
slide = SLIDE_KEYPAD
|
||||||
|
break
|
||||||
|
if GPIO.input(PIN_SET_WIN) == 0 and slide != SLIDE_CONGRATS:
|
||||||
|
print "Ardu said win"
|
||||||
|
slide = SLIDE_KEYPAD
|
||||||
|
break
|
||||||
|
if GPIO.input(PIN_BTNY) == 0:
|
||||||
|
print "yellow button pressed - go to maze slide"
|
||||||
|
slide = SLIDE_MAZE
|
||||||
|
break
|
||||||
|
if GPIO.input(PIN_BTNG) == 0:
|
||||||
|
print "green button pressed - go to mp3 slide"
|
||||||
|
slide = SLIDE_MP3
|
||||||
|
break
|
||||||
|
for e in pygame.event.get():
|
||||||
|
#if e.type == QUIT:
|
||||||
|
# sys.exit(0) #quit - nie chcemy miec mozliwosci wyjscia z aplikacji
|
||||||
|
if e.type == KEYDOWN:
|
||||||
|
if e.key == K_UP and dirs != 0:dirs = 2
|
||||||
|
elif e.key == K_DOWN and dirs != 2:dirs = 0
|
||||||
|
elif e.key == K_LEFT and dirs != 1:dirs = 3
|
||||||
|
elif e.key == K_RIGHT and dirs != 3:dirs = 1
|
||||||
|
elif e.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
slide = SLIDE_KEYBOARD
|
||||||
|
print("mouse event")
|
||||||
|
break
|
||||||
|
i = len(xs)-1
|
||||||
|
while i >= 2:
|
||||||
|
if collide(xs[0], xs[i], ys[0], ys[i], 20, 20, 20, 20):
|
||||||
|
die(screen, score)
|
||||||
|
score = 0
|
||||||
|
break
|
||||||
|
i-= 1
|
||||||
|
if collide(xs[0], applepos[0], ys[0], applepos[1], 20, 10, 20, 10):
|
||||||
|
score+=1
|
||||||
|
for i in range(0, SNAKE_ADDSIZE):
|
||||||
|
xs.append(700)
|
||||||
|
ys.append(700)
|
||||||
|
applepos=(random.randint(0,WINSIZEX),random.randint(0,WINSIZEY))
|
||||||
|
if xs[0] < 0 or xs[0] > WINSIZEX or ys[0] < 0 or ys[0] > WINSIZEY:
|
||||||
|
die(screen, score)
|
||||||
|
score = 0
|
||||||
|
break
|
||||||
|
i = len(xs)-1
|
||||||
|
while i >= 1:
|
||||||
|
xs[i] = xs[i-1];ys[i] = ys[i-1];i -= 1
|
||||||
|
if dirs==0:ys[0] += 20
|
||||||
|
elif dirs==1:xs[0] += 20
|
||||||
|
elif dirs==2:ys[0] -= 20
|
||||||
|
elif dirs==3:xs[0] -= 20
|
||||||
|
#rysuj tlo
|
||||||
|
screen.fill(WHITE)
|
||||||
|
screen.blit(IMG_SNAKE, (0, 0))
|
||||||
|
print("drawing snake...")
|
||||||
|
if(score > SNAKE_SCORE):
|
||||||
|
die(screen, score)
|
||||||
|
break
|
||||||
|
for i in range(0, len(xs)):
|
||||||
|
screen.blit(img, (xs[i], ys[i]))
|
||||||
|
screen.blit(appleimage, applepos);t=f.render(str(score), True, (0, 0, 0));screen.blit(t, (10, 10));pygame.display.update()
|
||||||
|
|
||||||
|
while slide == SLIDE_SNAKE_DEAD:
|
||||||
|
print("resetting snake.")
|
||||||
|
slide = SLIDE_SNAKE
|
||||||
|
while slide == SLIDE_DYNAMO:
|
||||||
|
print("showing slide 5 - dynamo")
|
||||||
|
screen.fill(WHITE)
|
||||||
|
screen.blit(IMG_DYNAMO, (0, 0))
|
||||||
|
pygame.display.flip()
|
||||||
|
waitForEvents()
|
||||||
|
break
|
||||||
|
while slide == SLIDE_KEYPAD:
|
||||||
|
print("showing slide 6 - drawer keypad")
|
||||||
|
screen.fill(WHITE)
|
||||||
|
screen.blit(IMG_KEYPAD, (0, 0))
|
||||||
|
pygame.display.flip()
|
||||||
|
waitForEvents()
|
||||||
|
break
|
||||||
|
while slide == SLIDE_MAZE:
|
||||||
|
print("showing slide 7 - maze")
|
||||||
|
screen.fill(WHITE)
|
||||||
|
screen.blit(IMG_MAZE, (0, 0))
|
||||||
|
pygame.display.flip()
|
||||||
|
waitForEvents()
|
||||||
|
break
|
||||||
|
|
||||||
|
while slide == SLIDE_MP3:
|
||||||
|
print("showing slide 8 - mp3 player")
|
||||||
|
screen.fill(WHITE)
|
||||||
|
screen.blit(IMG_MP3, (0, 0))
|
||||||
|
pygame.display.flip()
|
||||||
|
waitForEvents()
|
||||||
|
break
|
||||||
|
|
||||||
|
while slide == SLIDE_KEYBOARD: ## keyboard
|
||||||
|
#print("checking events")
|
||||||
|
checkEvents()
|
||||||
|
|
||||||
|
#render backdrop
|
||||||
|
screen.fill(WHITE)
|
||||||
|
screen.blit(IMG_KEYBOARD, (0, 0))
|
||||||
|
|
||||||
|
font = FONT_KEYBOARD
|
||||||
|
#render keys
|
||||||
|
height1 = 480-40
|
||||||
|
height2 = 560-40
|
||||||
|
height3 = 640-40
|
||||||
|
letterMeaning = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ' ']
|
||||||
|
letterQM = []
|
||||||
|
for i in range(10):
|
||||||
|
letterQM.append(pygame.draw.rect(screen,COLOR_KEYBOARD,Rect([i*80 +120,height1],[70 , 70])))
|
||||||
|
tt = font.render(letterMeaning[i], True, COLOR_KEYBOARD_TEXT)
|
||||||
|
#tt = font.render(str(i), True, (0, 0, 0))
|
||||||
|
screen.blit(tt, (i*80 +120+20,height1+15))
|
||||||
|
for j in range(9):
|
||||||
|
letterQM.append(pygame.draw.rect(screen,COLOR_KEYBOARD,Rect([j*80 +160,height2],[70 , 70])))
|
||||||
|
tt = font.render(letterMeaning[j+10], True, COLOR_KEYBOARD_TEXT)
|
||||||
|
#tt = font.render(str(j+10), True, (0, 0, 0))
|
||||||
|
screen.blit(tt, (j*80 +160+20,height2+15))
|
||||||
|
for k in range(8):
|
||||||
|
letterQM.append(pygame.draw.rect(screen,COLOR_KEYBOARD,Rect([k*80 +200,height3],[70 , 70])))
|
||||||
|
tt = font.render(letterMeaning[k+19], True, COLOR_KEYBOARD_TEXT)
|
||||||
|
#tt = font.render(str(k+19), True, (0, 0, 0))
|
||||||
|
screen.blit(tt, (k*80 +200+20,height3+15))
|
||||||
|
|
||||||
|
#render buttons
|
||||||
|
enterButton = pygame.draw.rect(screen,COLOR_KEYBOARD,Rect([WINSIZEX/2 - 100 - 70,300],[140 , 70]))
|
||||||
|
tt = font.render(MSG_BTN1, True, COLOR_KEYBOARD_TEXT)
|
||||||
|
screen.blit(tt, [WINSIZEX/2 - 100 - 70,300])
|
||||||
|
clearButton = pygame.draw.rect(screen,COLOR_KEYBOARD,Rect([WINSIZEX/2 + 100 - 70,300],[140 , 70]))
|
||||||
|
tt = font.render(MSG_BTN2, True, COLOR_KEYBOARD_TEXT)
|
||||||
|
screen.blit(tt, [WINSIZEX/2 + 100 - 70,300])
|
||||||
|
#render password as it is typed
|
||||||
|
tt = font.render(passwordLetter, True, COLOR_KEYBOARD_TEXT)
|
||||||
|
screen.blit(tt, [WINSIZEX/2 - 70,380])
|
||||||
|
|
||||||
|
#interactive features
|
||||||
|
for event in pygame.event.get():
|
||||||
|
#if event.type == pygame.QUIT:
|
||||||
|
# exit()
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
# Set the x, y postions of the mouse click
|
||||||
|
x, y = event.pos
|
||||||
|
if enterButton.collidepoint(x, y):
|
||||||
|
if passwordLetter == password1:
|
||||||
|
print 'password correct for task 9.'
|
||||||
|
signalWin(9)
|
||||||
|
|
||||||
|
if passwordLetter == password2:
|
||||||
|
print 'password correct for task 10.'
|
||||||
|
signalWin(10)
|
||||||
|
|
||||||
|
passwordLetter = ''
|
||||||
|
if clearButton.collidepoint(x, y):
|
||||||
|
passwordLetter = ''
|
||||||
|
|
||||||
|
for i in range(10+9+8):
|
||||||
|
if letterQM[i].collidepoint(x, y):
|
||||||
|
passwordLetter = passwordLetter + letterMeaning[i]
|
||||||
|
print passwordLetter
|
||||||
|
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
while slide == SLIDE_CONGRATS: ## congratulation
|
||||||
|
print("showing slide 10 - win")
|
||||||
|
screen.fill(WHITE)
|
||||||
|
screen.blit(IMG_WIN, (0, 0))
|
||||||
|
pygame.display.flip()
|
||||||
|
if(wall_a):
|
||||||
|
win()
|
||||||
|
pygame.mixer.init()
|
||||||
|
pygame.mixer.music.load("media/win.mp3")
|
||||||
|
pygame.mixer.music.play(MP3_LOOPS)
|
||||||
|
pygame.mixer.music.set_volume(1.0)
|
||||||
|
while pygame.mixer.music.get_busy() == True:
|
||||||
|
continue
|
||||||
|
while 1:
|
||||||
|
print "you win."
|
||||||
|
waitForEvents()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
||||||
|
|
40
raspi/snake.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import pygame, random, sys
|
||||||
|
from pygame.locals import *
|
||||||
|
def collide(x1, x2, y1, y2, w1, w2, h1, h2):
|
||||||
|
if x1+w1>x2 and x1<x2+w2 and y1+h1>y2 and y1<y2+h2:return True
|
||||||
|
else:return False
|
||||||
|
def die(screen, score):
|
||||||
|
f=pygame.font.SysFont('Courier', 30);t=f.render('Your score was: '+str(score), True, (0, 0, 0));screen.blit(t, (10, 270));pygame.display.update();pygame.time.wait(2000);sys.exit(0)
|
||||||
|
xs = [290, 290, 290, 290, 290];ys = [290, 270, 250, 230, 210];dirs = 0;score = 0;applepos = (random.randint(0, 590), random.randint(0, 590));pygame.init();s=pygame.display.set_mode((600, 600));pygame.display.set_caption('Snake');appleimage = pygame.image.load("media/apple.gif");img = pygame.Surface((20, 20));img.fill((255, 255, 0));f = pygame.font.SysFont('Courier', 20);clock = pygame.time.Clock()
|
||||||
|
while True:
|
||||||
|
clock.tick(10)
|
||||||
|
for e in pygame.event.get():
|
||||||
|
#if e.type == QUIT:
|
||||||
|
# sys.exit(0) #quit - nie chcemy miec mozliwosci wyjscia z aplikacji
|
||||||
|
if e.type == KEYDOWN:
|
||||||
|
if e.key == K_UP and dirs != 0:dirs = 2
|
||||||
|
elif e.key == K_DOWN and dirs != 2:dirs = 0
|
||||||
|
elif e.key == K_LEFT and dirs != 1:dirs = 3
|
||||||
|
elif e.key == K_RIGHT and dirs != 3:dirs = 1
|
||||||
|
i = len(xs)-1
|
||||||
|
while i >= 2:
|
||||||
|
if collide(xs[0], xs[i], ys[0], ys[i], 20, 20, 20, 20):die(s, score)
|
||||||
|
i-= 1
|
||||||
|
if collide(xs[0], applepos[0], ys[0], applepos[1], 20, 10, 20, 10):score+=1;xs.append(700);ys.append(700);applepos=(random.randint(0,590),random.randint(0,590))
|
||||||
|
if xs[0] < 0 or xs[0] > 580 or ys[0] < 0 or ys[0] > 580: die(s, score)
|
||||||
|
i = len(xs)-1
|
||||||
|
while i >= 1:
|
||||||
|
xs[i] = xs[i-1];ys[i] = ys[i-1];i -= 1
|
||||||
|
if dirs==0:ys[0] += 20
|
||||||
|
elif dirs==1:xs[0] += 20
|
||||||
|
elif dirs==2:ys[0] -= 20
|
||||||
|
elif dirs==3:xs[0] -= 20
|
||||||
|
s.fill((255, 255, 255))
|
||||||
|
for i in range(0, len(xs)):
|
||||||
|
s.blit(img, (xs[i], ys[i]))
|
||||||
|
s.blit(appleimage, applepos);t=f.render(str(score), True, (0, 0, 0));s.blit(t, (10, 10));pygame.display.update()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
511
raspi/virtualKeyboard.py
Normal file
@ -0,0 +1,511 @@
|
|||||||
|
"""
|
||||||
|
|
||||||
|
(C) Copyright 2007 Anthony Maro
|
||||||
|
(C) Copyright 2014 William B Phelps
|
||||||
|
|
||||||
|
Version 2.1 - March 2014 - for PiTFT 320x240 touchscreen
|
||||||
|
Version 2.2 - March 2014 - generalized for "any" touchscreen
|
||||||
|
|
||||||
|
Now has 2 line input area (code specific for 2 lines)
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU General Public License as
|
||||||
|
published by the Free Software Foundation; either version 2 of the
|
||||||
|
License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful, but
|
||||||
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
||||||
|
02111-1307, USA.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
|
||||||
|
from virtualKeyboard import VirtualKeyboard
|
||||||
|
|
||||||
|
vkeybd = VirtualKeyboard(screen)
|
||||||
|
userinput = vkeybd.run(default_text)
|
||||||
|
|
||||||
|
screen is a full screen pygame screen. The VirtualKeyboard will shade out the current screen and overlay
|
||||||
|
a transparent keyboard. default_text gets fed to the initial text import - used for editing text fields
|
||||||
|
If the user clicks the escape hardware button, the default_text is returned
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import pygame, time
|
||||||
|
from pygame.locals import *
|
||||||
|
|
||||||
|
from string import maketrans
|
||||||
|
Uppercase = maketrans("abcdefghijklmnopqrstuvwxyz`1234567890-=[]\;\',./",
|
||||||
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+{}|:"<>?')
|
||||||
|
|
||||||
|
#_keyWidth = 27 # default key width including borders
|
||||||
|
#_keyHeight = 29 # default key height
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class VirtualKeyboard():
|
||||||
|
''' Implement a basic full screen virtual keyboard for touchscreens '''
|
||||||
|
|
||||||
|
def __init__(self, screen):
|
||||||
|
|
||||||
|
self.screen = screen
|
||||||
|
self.rect = self.screen.get_rect()
|
||||||
|
self.w = self.rect.width
|
||||||
|
self.h = self.rect.height
|
||||||
|
|
||||||
|
# make a copy of the screen
|
||||||
|
self.screenCopy = screen.copy()
|
||||||
|
|
||||||
|
# create a background surface
|
||||||
|
self.background = pygame.Surface(self.rect.size)
|
||||||
|
self.background.fill((0,0,0)) # fill with black
|
||||||
|
self.background.set_alpha(127) # 50% transparent
|
||||||
|
# blit background to screen
|
||||||
|
self.screen.blit(self.background,(0,0))
|
||||||
|
|
||||||
|
self.keyW = int(self.w/12+0.5) # key width with border
|
||||||
|
self.keyH = int(self.h/8+0.5) # key height
|
||||||
|
|
||||||
|
self.x = (self.w-self.keyW*12)/2 # centered
|
||||||
|
self.y = 5 # stay away from the edges (better touch)
|
||||||
|
# print 'keys x {} w {} keyW {} keyH {}'.format(self.x, self.w, self.keyW, self.keyH)
|
||||||
|
|
||||||
|
pygame.font.init() # Just in case
|
||||||
|
self.keyFont = pygame.font.Font(None, self.keyW) # keyboard font
|
||||||
|
|
||||||
|
# set dimensions for text input box
|
||||||
|
# self.textW = self.w-(self.keyW+2) # leave room for escape key (?)
|
||||||
|
self.textW = self.keyW*11+2 # leave room for escape key
|
||||||
|
self.textH = self.keyH*2-6
|
||||||
|
|
||||||
|
self.caps = False
|
||||||
|
self.keys = []
|
||||||
|
# self.textbox = pygame.Surface((self.rect.width,self.keyH*2))
|
||||||
|
self.addkeys() # add all the keys
|
||||||
|
self.paintkeys() # paint all the keys
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
|
||||||
|
def run(self, text=''):
|
||||||
|
|
||||||
|
self.text = text
|
||||||
|
# create an input text box
|
||||||
|
# create a text input box with room for 2 lines of text. leave room for the escape key
|
||||||
|
self.input = TextInput(self.screen,self.text,self.x,self.y,self.textW,self.textH)
|
||||||
|
|
||||||
|
counter = 0
|
||||||
|
# main event loop (hog all processes since we're on top, but someone might want
|
||||||
|
# to rewrite this to be more event based...
|
||||||
|
while True:
|
||||||
|
time.sleep(0.1) # 10/second is often enough
|
||||||
|
events = pygame.event.get()
|
||||||
|
if events <> None:
|
||||||
|
for e in events:
|
||||||
|
# touch screen does not have these events...
|
||||||
|
# if (e.type == KEYDOWN):
|
||||||
|
# if e.key == K_ESCAPE:
|
||||||
|
# self.clear()
|
||||||
|
# return self.text # Return what we started with
|
||||||
|
# if e.key == K_RETURN:
|
||||||
|
# self.clear()
|
||||||
|
# return self.input.text # Return what the user entered
|
||||||
|
# if e.key == K_LEFT:
|
||||||
|
# self.input.deccursor()
|
||||||
|
# pygame.display.flip()
|
||||||
|
# if e.key == K_RIGHT:
|
||||||
|
# self.input.inccursor()
|
||||||
|
# pygame.display.flip()
|
||||||
|
if (e.type == MOUSEBUTTONDOWN):
|
||||||
|
self.selectatmouse()
|
||||||
|
if (e.type == MOUSEBUTTONUP):
|
||||||
|
if self.clickatmouse():
|
||||||
|
# user clicked enter or escape if returns True
|
||||||
|
self.clear()
|
||||||
|
return self.input.text # Return what the user entered
|
||||||
|
if (e.type == MOUSEMOTION):
|
||||||
|
if e.buttons[0] == 1:
|
||||||
|
# user click-dragged to a different key?
|
||||||
|
self.selectatmouse()
|
||||||
|
|
||||||
|
counter += 1
|
||||||
|
if counter > 5:
|
||||||
|
self.input.flashcursor()
|
||||||
|
counter = 0
|
||||||
|
## gtk.main_iteration(block=False)
|
||||||
|
|
||||||
|
def unselectall(self, force = False):
|
||||||
|
''' Force all the keys to be unselected
|
||||||
|
Marks any that change as dirty to redraw '''
|
||||||
|
for key in self.keys:
|
||||||
|
if key.selected:
|
||||||
|
key.selected = False
|
||||||
|
key.dirty = True
|
||||||
|
|
||||||
|
def clickatmouse(self):
|
||||||
|
''' Check to see if the user is pressing down on a key and draw it selected '''
|
||||||
|
self.unselectall()
|
||||||
|
for key in self.keys:
|
||||||
|
keyrect = Rect(key.x,key.y,key.w,key.h)
|
||||||
|
if keyrect.collidepoint(pygame.mouse.get_pos()):
|
||||||
|
key.dirty = True
|
||||||
|
if key.bskey:
|
||||||
|
# Backspace
|
||||||
|
self.input.backspace()
|
||||||
|
self.paintkeys()
|
||||||
|
return False
|
||||||
|
if key.fskey:
|
||||||
|
self.input.inccursor()
|
||||||
|
self.paintkeys()
|
||||||
|
return False
|
||||||
|
if key.spacekey:
|
||||||
|
self.input.addcharatcursor(' ')
|
||||||
|
self.paintkeys()
|
||||||
|
return False
|
||||||
|
if key.shiftkey:
|
||||||
|
self.togglecaps()
|
||||||
|
self.paintkeys()
|
||||||
|
return False
|
||||||
|
if key.escape:
|
||||||
|
self.input.text = '' # clear input
|
||||||
|
return True
|
||||||
|
if key.enter:
|
||||||
|
return True
|
||||||
|
if self.caps:
|
||||||
|
keycap = key.caption.translate(Uppercase)
|
||||||
|
else:
|
||||||
|
keycap = key.caption
|
||||||
|
self.input.addcharatcursor(keycap)
|
||||||
|
self.paintkeys()
|
||||||
|
return False
|
||||||
|
|
||||||
|
self.paintkeys()
|
||||||
|
return False
|
||||||
|
|
||||||
|
def togglecaps(self):
|
||||||
|
''' Toggle uppercase / lowercase '''
|
||||||
|
if self.caps:
|
||||||
|
self.caps = False
|
||||||
|
else:
|
||||||
|
self.caps = True
|
||||||
|
for key in self.keys:
|
||||||
|
key.dirty = True
|
||||||
|
|
||||||
|
def selectatmouse(self):
|
||||||
|
# User has touched the screen - is it inside the textbox, or inside a key rect?
|
||||||
|
self.unselectall()
|
||||||
|
pos = pygame.mouse.get_pos()
|
||||||
|
# print 'touch {}'.format(pos)
|
||||||
|
if self.input.rect.collidepoint(pos):
|
||||||
|
# print 'input {}'.format(pos)
|
||||||
|
self.input.setcursor(pos)
|
||||||
|
else:
|
||||||
|
for key in self.keys:
|
||||||
|
keyrect = Rect(key.x,key.y,key.w,key.h)
|
||||||
|
if keyrect.collidepoint(pos):
|
||||||
|
key.selected = True
|
||||||
|
key.dirty = True
|
||||||
|
self.paintkeys()
|
||||||
|
return
|
||||||
|
|
||||||
|
self.paintkeys()
|
||||||
|
|
||||||
|
def addkeys(self): # Add all the keys for the virtual keyboard
|
||||||
|
|
||||||
|
x = self.x
|
||||||
|
y = self.y + self.textH + self.keyH/4
|
||||||
|
|
||||||
|
row = ['1','2','3','4','5','6','7','8','9','0','-','=']
|
||||||
|
for item in row:
|
||||||
|
onekey = VKey(item,x,y,self.keyW,self.keyH,self.keyFont)
|
||||||
|
self.keys.append(onekey)
|
||||||
|
x += self.keyW
|
||||||
|
|
||||||
|
y += self.keyH # overlap border
|
||||||
|
x = self.x
|
||||||
|
|
||||||
|
row = ['q','w','e','r','t','y','u','i','o','p','[',']']
|
||||||
|
for item in row:
|
||||||
|
onekey = VKey(item,x,y,self.keyW,self.keyH,self.keyFont)
|
||||||
|
self.keys.append(onekey)
|
||||||
|
x += self.keyW
|
||||||
|
|
||||||
|
y += self.keyH
|
||||||
|
x = self.x
|
||||||
|
|
||||||
|
row = ['a','s','d','f','g','h','j','k','l',';','\'','`']
|
||||||
|
for item in row:
|
||||||
|
onekey = VKey(item,x,y,self.keyW,self.keyH,self.keyFont)
|
||||||
|
self.keys.append(onekey)
|
||||||
|
x += self.keyW
|
||||||
|
|
||||||
|
x = self.x + self.keyW/2
|
||||||
|
y += self.keyH
|
||||||
|
|
||||||
|
row = ['z','x','c','v','b','n','m',',','.','/','\\']
|
||||||
|
for item in row:
|
||||||
|
onekey = VKey(item,x,y,self.keyW,self.keyH,self.keyFont)
|
||||||
|
self.keys.append(onekey)
|
||||||
|
x += self.keyW
|
||||||
|
|
||||||
|
x = self.x + 1
|
||||||
|
y += self.keyH + self.keyH/4
|
||||||
|
|
||||||
|
# print 'addkeys keyW {} keyH {}'.format(self.keyW, self.keyH)
|
||||||
|
|
||||||
|
onekey = VKey('Shift',x,y,int(self.keyW*2.5),self.keyH,self.keyFont)
|
||||||
|
onekey.special = True
|
||||||
|
onekey.shiftkey = True
|
||||||
|
self.keys.append(onekey)
|
||||||
|
x += onekey.w + self.keyW/6
|
||||||
|
|
||||||
|
onekey = VKey('Space',x,y,self.keyW*5,self.keyH,self.keyFont)
|
||||||
|
onekey.special = True
|
||||||
|
onekey.spacekey = True
|
||||||
|
self.keys.append(onekey)
|
||||||
|
x += onekey.w + self.keyW/6
|
||||||
|
|
||||||
|
onekey = VKey('Enter',x,y,int(self.keyW*2.5),self.keyH,self.keyFont)
|
||||||
|
onekey.special = True
|
||||||
|
onekey.enter = True
|
||||||
|
self.keys.append(onekey)
|
||||||
|
x += onekey.w + self.keyW/3
|
||||||
|
|
||||||
|
onekey = VKey('<-',x,y,int(self.keyW*1.2+0.5),self.keyH,self.keyFont)
|
||||||
|
onekey.special = True
|
||||||
|
onekey.bskey = True
|
||||||
|
self.keys.append(onekey)
|
||||||
|
x += onekey.w + self.keyW/3
|
||||||
|
|
||||||
|
xfont = pygame.font.SysFont('Courier', 22, bold=True) # I like this X better #TODO???
|
||||||
|
onekey = VKey('X',self.x+self.textW-1,self.y,self.keyW,self.keyH,xfont) # exit key TODO???
|
||||||
|
onekey.special = True
|
||||||
|
onekey.escape = True
|
||||||
|
self.keys.append(onekey)
|
||||||
|
|
||||||
|
|
||||||
|
def paintkeys(self):
|
||||||
|
''' Draw the keyboard (but only if they're dirty.) '''
|
||||||
|
for key in self.keys:
|
||||||
|
key.draw(self.screen, self.background, self.caps)
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
def clear(self):
|
||||||
|
''' Put the screen back to before we started '''
|
||||||
|
self.screen.blit(self.screenCopy,(0,0))
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class TextInput():
|
||||||
|
''' Handles the text input box and manages the cursor '''
|
||||||
|
def __init__(self, screen, text, x, y, w, h):
|
||||||
|
self.screen = screen
|
||||||
|
self.text = text
|
||||||
|
self.cursorpos = len(text)
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
self.w = w
|
||||||
|
self.h = h
|
||||||
|
self.rect = Rect(x,y,w,h)
|
||||||
|
self.layer = pygame.Surface((self.w,self.h))
|
||||||
|
self.background = pygame.Surface((self.w,self.h))
|
||||||
|
self.background.fill((0,0,0)) # fill with black
|
||||||
|
|
||||||
|
# self.font = pygame.font.Font(None, fontsize) # use this if you want more text in the line
|
||||||
|
rect = screen.get_rect()
|
||||||
|
fsize = int(rect.height/12+0.5) # font size proportional to screen height
|
||||||
|
self.txtFont = pygame.font.SysFont('Courier New', fsize, bold=True)
|
||||||
|
# attempt to figure out how many chars will fit on a line
|
||||||
|
# this does not work with proportional fonts
|
||||||
|
tX = self.txtFont.render("XXXXXXXXXX", 1, (255,255,0)) # 10 chars
|
||||||
|
rtX = tX.get_rect() # how big is it?
|
||||||
|
self.lineChars = int(self.w/(rtX.width/10))-1 # chars per line (horizontal)
|
||||||
|
self.lineH = rtX.height # pixels per line (vertical)
|
||||||
|
# print 'txtinp: width={} rtX={} font={} lineChars={} lineH={}'.format(self.w,rtX,fsize, self.lineChars,self.lineH)
|
||||||
|
|
||||||
|
self.cursorlayer = pygame.Surface((2,22)) # thin vertical line
|
||||||
|
self.cursorlayer.fill((255,255,255)) # white vertical line
|
||||||
|
self.cursorvis = True
|
||||||
|
|
||||||
|
self.cursorX = len(text)%self.lineChars
|
||||||
|
self.cursorY = int(len(text)/self.lineChars) # line 1
|
||||||
|
|
||||||
|
self.draw()
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
''' Draw the text input box '''
|
||||||
|
# self.layer.fill([255, 255, 255, 127]) # 140
|
||||||
|
self.layer.fill((0,0,0)) # clear the layer
|
||||||
|
pygame.draw.rect(self.layer, (255,255,255), (0,0,self.w,self.h), 1) # draw the box
|
||||||
|
|
||||||
|
# should be more general, but for now, just hack it for 2 lines
|
||||||
|
txt1 = self.text[:self.lineChars] # line 1
|
||||||
|
txt2 = self.text[self.lineChars:] # line 2
|
||||||
|
t1 = self.txtFont.render(txt1, 1, (255,255,0)) # line 1
|
||||||
|
self.layer.blit(t1,(4,4))
|
||||||
|
t2 = self.txtFont.render(txt2, 1, (255,255,0)) # line 1
|
||||||
|
self.layer.blit(t2,(4,4+self.lineH))
|
||||||
|
|
||||||
|
self.screen.blit(self.background, self.rect)
|
||||||
|
self.screen.blit(self.layer, self.rect)
|
||||||
|
self.drawcursor()
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
def flashcursor(self):
|
||||||
|
''' Toggle visibility of the cursor '''
|
||||||
|
if self.cursorvis:
|
||||||
|
self.cursorvis = False
|
||||||
|
else:
|
||||||
|
self.cursorvis = True
|
||||||
|
|
||||||
|
self.screen.blit(self.background,self.rect)
|
||||||
|
self.screen.blit(self.layer,self.rect)
|
||||||
|
|
||||||
|
if self.cursorvis:
|
||||||
|
self.drawcursor()
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
def addcharatcursor(self, letter):
|
||||||
|
''' Add a character whereever the cursor is currently located '''
|
||||||
|
if self.cursorpos < len(self.text):
|
||||||
|
# Inserting in the middle
|
||||||
|
self.text = self.text[:self.cursorpos] + letter + self.text[self.cursorpos:]
|
||||||
|
self.cursorpos += 1
|
||||||
|
self.draw()
|
||||||
|
return
|
||||||
|
self.text += letter
|
||||||
|
self.cursorpos += 1
|
||||||
|
self.draw()
|
||||||
|
|
||||||
|
def backspace(self):
|
||||||
|
''' Delete a character before the cursor position '''
|
||||||
|
if self.cursorpos == 0: return
|
||||||
|
self.text = self.text[:self.cursorpos-1] + self.text[self.cursorpos:]
|
||||||
|
self.cursorpos -= 1
|
||||||
|
self.draw()
|
||||||
|
return
|
||||||
|
|
||||||
|
def deccursor(self):
|
||||||
|
''' Move the cursor one space left '''
|
||||||
|
if self.cursorpos == 0: return
|
||||||
|
self.cursorpos -= 1
|
||||||
|
self.draw()
|
||||||
|
|
||||||
|
def inccursor(self):
|
||||||
|
''' Move the cursor one space right (but not beyond the end of the text) '''
|
||||||
|
if self.cursorpos == len(self.text): return
|
||||||
|
self.cursorpos += 1
|
||||||
|
self.draw()
|
||||||
|
|
||||||
|
def drawcursor(self):
|
||||||
|
''' Draw the cursor '''
|
||||||
|
line = int(self.cursorpos/self.lineChars) # line number
|
||||||
|
if line>1: line = 1
|
||||||
|
x = 4
|
||||||
|
y = 4 + self.y + line*self.lineH
|
||||||
|
# Calc width of text to this point
|
||||||
|
if self.cursorpos > 0:
|
||||||
|
linetext = self.text[line*self.lineChars:self.cursorpos]
|
||||||
|
rtext = self.txtFont.render(linetext, 1, (255,255,255))
|
||||||
|
textpos = rtext.get_rect()
|
||||||
|
x = x + textpos.width + 1
|
||||||
|
self.screen.blit(self.cursorlayer,(x,y))
|
||||||
|
|
||||||
|
def setcursor(self,pos): # move cursor to char nearest position (x,y)
|
||||||
|
line = int((pos[1]-self.y)/self.lineH) # vertical
|
||||||
|
if line>1: line = 1 # only 2 lines
|
||||||
|
x = pos[0]-self.x + line*self.w # virtual x position
|
||||||
|
p = 0
|
||||||
|
l = len(self.text)
|
||||||
|
# print 'setcursor {} x={},y={}'.format(pos,x,y)
|
||||||
|
# print 'text {}'.format(self.text)
|
||||||
|
while p < l:
|
||||||
|
text = self.txtFont.render(self.text[:p+1], 1, (255,255,255)) # how many pixels to next char?
|
||||||
|
rtext = text.get_rect()
|
||||||
|
textX = rtext.x + rtext.width
|
||||||
|
# print 't = {}, tx = {}'.format(t,textX)
|
||||||
|
if textX >= x: break # we found it
|
||||||
|
p += 1
|
||||||
|
self.cursorpos = p
|
||||||
|
self.draw()
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class VKey(object):
|
||||||
|
''' A single key for the VirtualKeyboard '''
|
||||||
|
# def __init__(self, caption, x, y, w=67, h=67):
|
||||||
|
def __init__(self, caption, x, y, w, h, font):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.caption = caption
|
||||||
|
self.w = w+1 # overlap borders
|
||||||
|
self.h = h+1 # overlap borders
|
||||||
|
self.special = False
|
||||||
|
self.enter = False
|
||||||
|
self.bskey = False
|
||||||
|
self.fskey = False
|
||||||
|
self.spacekey = False
|
||||||
|
self.escape = False
|
||||||
|
self.shiftkey = False
|
||||||
|
self.font = font
|
||||||
|
self.selected = False
|
||||||
|
self.dirty = True
|
||||||
|
self.keylayer = pygame.Surface((self.w,self.h)).convert()
|
||||||
|
self.keylayer.fill((128, 128, 128)) # 0,0,0
|
||||||
|
## self.keylayer.set_alpha(160)
|
||||||
|
# Pre draw the border and store in the key layer
|
||||||
|
pygame.draw.rect(self.keylayer, (255,255,255), (0,0,self.w,self.h), 1)
|
||||||
|
|
||||||
|
def draw(self, screen, background, shifted=False, forcedraw=False):
|
||||||
|
''' Draw one key if it needs redrawing '''
|
||||||
|
if not forcedraw:
|
||||||
|
if not self.dirty: return
|
||||||
|
|
||||||
|
keyletter = self.caption
|
||||||
|
if shifted:
|
||||||
|
if self.shiftkey:
|
||||||
|
self.selected = True # highlight the Shift button
|
||||||
|
if not self.special:
|
||||||
|
keyletter = self.caption.translate(Uppercase)
|
||||||
|
|
||||||
|
position = Rect(self.x, self.y, self.w, self.h)
|
||||||
|
|
||||||
|
# put the background back on the screen so we can shade properly
|
||||||
|
screen.blit(background, (self.x,self.y), position)
|
||||||
|
|
||||||
|
# Put the shaded key background into key layer
|
||||||
|
if self.selected:
|
||||||
|
color = (200,200,200)
|
||||||
|
else:
|
||||||
|
color = (0,0,0)
|
||||||
|
|
||||||
|
# Copy key layer onto the screen using Alpha so you can see through it
|
||||||
|
pygame.draw.rect(self.keylayer, color, (1,1,self.w-2,self.h-2))
|
||||||
|
screen.blit(self.keylayer,(self.x,self.y))
|
||||||
|
|
||||||
|
# Create a new temporary layer for the key contents
|
||||||
|
# This might be sped up by pre-creating both selected and unselected layers when
|
||||||
|
# the key is created, but the speed seems fine unless you're drawing every key at once
|
||||||
|
templayer = pygame.Surface((self.w,self.h))
|
||||||
|
templayer.set_colorkey((0,0,0))
|
||||||
|
|
||||||
|
color = (255,255,255)
|
||||||
|
text = self.font.render(keyletter, 1, (255, 255, 255))
|
||||||
|
textpos = text.get_rect()
|
||||||
|
blockoffx = (self.w / 2)
|
||||||
|
blockoffy = (self.h / 2)
|
||||||
|
offsetx = blockoffx - (textpos.width / 2)
|
||||||
|
offsety = blockoffy - (textpos.height / 2)
|
||||||
|
templayer.blit(text,(offsetx, offsety))
|
||||||
|
|
||||||
|
screen.blit(templayer, (self.x,self.y))
|
||||||
|
self.dirty = False
|
||||||
|
|