-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMouse_touchscreen ver 1.1.py
More file actions
169 lines (141 loc) · 5.76 KB
/
Mouse_touchscreen ver 1.1.py
File metadata and controls
169 lines (141 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'''
Mouse Touchscreen Program, Moorman Lab 2020
Code written by Jason Biundo, version 1.1 10/2020
This program utilizes the pygame library to create an interactive program that mice can
interact with on a touchscreen that is run by a Raspberry pi. This program is designed to
eventually be generalizable to a variety of different tasks.
'''
# Import and initialize the pygame library
import pygame
import time
import logging
pygame.init()
pygame.display.set_caption('Mouse Touchscreen Program')
pi=False
#Initialize logging
logging.basicConfig(filename ='test.log', level= logging.INFO, format='%(asctime)s:%(levelname)s:%(message)s')
#format='%(asctime)s:%(levelname)s:%(message)s'
# Import pygame.locals for easier access to key coordinates
from pygame.locals import (
K_UP, K_DOWN, K_LEFT, K_RIGHT, K_ESCAPE, KEYDOWN, QUIT,
)
#colors
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
black = (0,0,0)
yellow = (255,255,0)
purple = (255,0,255)
white = (255,255,255)
deepskyblue = (0,191,255)
#sound effects - uncomment if you have the sound file
#effect = pygame.mixer.Sound('chime.wav')
#Functions to add
'''
- Add shapes (one function for each shape most likely, i.e circle, square, polygon)
- Check collison
- Update screen?
-Play sound
'''
# Set up the display window. Touchscreen dimensions = 800x400
screen_width = 800
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
logging.info('Program Started')
#make fullscreen on touchscreen
#screen = pygame.display.set_mode((800, 400), pygame.FULLSCREEN)
# Function to check collision of mouse with shape
def check_collision(object, mouse_pos, left_click, color=(0,0,0)):
if object.collidepoint(mouse_pos) and left_click:
'''
Function to check if mouse click collides with one of the objects.
Takes in object, mouse position (tuple of coordinates) and pressed (boolean, True if mouse was clicked)
Default argument for color is black, but can be changed.
'''
print('You pressed', object)
screen.fill(color)
#effect.play() #plays sound if uncommented
pygame.display.flip()
pygame.mouse.set_pos(0,0)
pygame.time.wait(1000) #pauses program for 1000ms for flash
logging.info('Shape: {}'.format(object))
running = True
off = False
# Main loop, run until the user asks to quit
while running:
#turn on/off visibility of mouse cursor (True=visible, False=hidden)
pygame.mouse.set_visible(running)
#draw test shapes
obj1= pygame.draw.circle(screen, yellow, (75,250), 57)
obj2= pygame.draw.circle(screen, purple , (260, 250), 57)
obj3 = pygame.draw.rect(screen, red, (400,200, 100,100))
obj4 = pygame.draw.rect(screen, blue, (600,200, 125,100))
objList = [obj1,obj2,obj3,obj4]
mouse_pos = pygame.mouse.get_pos()
for event in pygame.event.get():
#check for mousebutton
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos= pygame.mouse.get_pos()
print('The position of the cursor is', mouse_pos)
logging.info('Coordinates:' + str(mouse_pos))
#mouse_pos = pygame.mouse.get_pos()
left_click, pressed2, right_click = pygame.mouse.get_pressed() #pressed 1 is left click, pressed 3 is right click
# Check if the object "collided" with the mouse pos and if the left mouse button was pressed
check_collision(obj1,mouse_pos,left_click, yellow)
check_collision(obj2,mouse_pos,left_click, purple)
check_collision(obj3,mouse_pos,left_click, red)
check_collision(obj4,mouse_pos,left_click, blue)
# for obj in objList:
# check_collision(obj,mouse_pos,left_click,white)
#escape from program
if event.type == KEYDOWN:
# Was it the Escape key? If so, stop the loop.
if event.key == K_ESCAPE:
running = False
# elif event.key == K_UP:
# screen = pygame.display.set_mode([screen_width, screen_height],pygame.FULLSCREEN)
elif event.type == pygame.QUIT:
running = False
# Fill the background with black
screen.fill(black)
# Draw circles
pygame.draw.circle(screen, purple, (260, 250), 57)
pygame.draw.circle(screen, yellow, (75,250), 57)
#Draw squares
pygame.draw.rect(screen, red, (400,200, 100,100))
pygame.draw.rect(screen, blue, (600,200, 125,100))
#Reset mouse position every loop to avoid problems with touchscreens. Comment this if using a computer
#pygame.mouse.set_pos(0,0)
# Update the display
pygame.display.flip()
# Done! Time to quit.
pygame.quit()
# Check if the object "collided" with the mouse pos and if the left mouse button was pressed
# if circ1.collidepoint(mouse_pos) and left_click:
# print('You pressed the circle')
# screen.fill(purple)
# pygame.display.flip()
# effect.play()
# time.sleep(1)
# pygame.mouse.set_pos(0,0)
# if rec1.collidepoint(mouse_pos) and left_click:
# print('You pressed the square')
# screen.fill(red)
# pygame.display.flip()
# pygame.mouse.set_pos(0,0)
# effect.play()
# time.sleep(.5)
# if circ2.collidepoint(mouse_pos) and left_click:
# print('You pressed the yellow circle')
# screen.fill(yellow)
# pygame.display.flip()
# pygame.mouse.set_pos(0,0)
# effect.play()
# time.sleep(.5)
# if rec2.collidepoint(mouse_pos) and left_click:
# print('You pressed the rectangle')
# screen.fill(blue)
# pygame.display.flip()
# pygame.mouse.set_pos(0,0)
# effect.play()
# time.sleep(.5)