You are on page 1of 18

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB

Videojuego de gato o tic tac toe usando deteccin de


color con webcam con GUI en MATLAB
Leonardo M. Delgadillo De la torre
18/Junio/2015

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB

Videojuego de gato o tic tac toe usando deteccin de


color con webcam con GUI en MATLAB
Leonardo Misael Delgadillo De la torre
Resumen
Se desarroll un videojuego con GUI en MATLAB a travs de la deteccin de
marcadores de color a travs de a webcam para poder elegir la casilla a marcar. El
videojuego fue creado para dos jugadores. El objetivo es tratar de alinear tres
marcas de tu color para ganar el juego.
Procedimiento
Se cre un videojuego que detectara un marcador de color azul que pudiera hacer
las veces de apuntador para elegir una casilla de un juego de gato. Este marcador
es detectado a travs de la webcam y su eleccin se visualiza en pantalla a travs
de una GUI. Tanto el videojuego como la GUI fueron desarrollados en la
plataforma MATLAB .
El videojuego fue creado para dos jugadores. El objetivo es tratar de alinear tres
marcas de tu color para ganar el juego como en cualquier otro juego de gato
convencional.
Dentro de la descripcin del mtodo de la realizacin del proyecto se puede
comenzar mencionando el uso de sentencias que ayudaron a registrar capturas a
una velocidad registro de un frame por cada frame de la secuencia de video:
% Las capturas se realizan rapidamente y se retornan matrices RGB
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 1;

Para permitir el uso del tiempo dentro del ciclo principal del programa se utiliz la
funcin tic-toc, as el conteo en segundos reales realizado por la funcin fue
utilizado en varios procesos dentro del ciclo mencionado:
% Comienza un ciclo de 120 segundos
seg=120;
tic
while toc <= seg

Despus se invirtieron las coordenadas para poder permitir la proyeccin en


espejo de tanto de la imagen del usuario como de las coordenadas de la matriz en
pantalla:
% Se invierten las coordenadas para la proyeccin de la imagen
set(gca,'xDir','reverse')%,'yDir','reverse')

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB

Posteriormente se hicieron una serie de instrucciones que permitieron detectar los


objetos de color azul, filtrarlos, ajustar su alcance de deteccin, conectar los
pixeles que crearan un marcador as como tambin obtener el marco y el
centroide del objeto detectado:
%Se detectan los objetos azules
diff_im = imsubtract(data(:,:,3), rgb2gray(data));
%Se usa un fitro para la adquisicin de los objetos
diff_im = medfilt2(diff_im, [3 3]);
% Convirtiendo el resultado a imagen binaria
diff_im = im2bw(diff_im,0.18);
% Discriminar objetos azules menores a 13000 px de rea (distancia de
% reconocimiento)
diff_im = bwareaopen(diff_im,13000);
% Conecta los pixeles en objetos que se compongan de mnimo 8 px
bw = bwlabel(diff_im, 8);
% Se obtiene el marco y el centroide de los objetos identificados
stats = regionprops(bw, 'BoundingBox', 'Centroid');

Para que el usuario identifique los que el objeto ha sido reconocido como
marcador se enmarca en un paralelogramo verde y su centroide es resaltado con
una cruz:
%Los objetos azules reconocidos son marcados con mrgenes verdes
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','g','LineWidth',5)
xx=bc(1);
yy=bc(2);
end

plot(bc(1),bc(2), '-m+', 'MarkerSize',15)

Se enumeraron las casillas del gato del 1 al 9 y a cada casilla se le program su


propio segmento de cdigo para detectar el marcador en su regin delimitada
correspondiente, como se puede apreciar en la siguiente tabla:
3
6
9

2
5
8

1
4
7

Si el marcador se posiciona 3 segundos sobre una casilla, esta se marcar con el


smbolo del jugador en turno. A continuacin se puede observar el segmento de
cdigo de la casilla nmero 1:
% Casilla 1

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
if x/5*2>xx && xx>=x/5 && y/5*2>yy && yy>=y/5 && band1==0
if toc>=cont1+3
cont1=round(toc);
segValido1=1;
segValido2=0;
segValido3=0;
segValido4=0;
segValido5=0;
segValido6=0;
segValido7=0;
segValido8=0;
segValido9=0;
end
if round(toc)-cont1==3 && segValido1==1
if turno==1
band1=1;
turno=2;
mat(1,1)=1;
else
turno=1;
band1=2;
mat(1,1)=7;
end
end
end

Para conservar la proyeccin de las marcas de cada jugador se agregan las


sentencias para graficarlas cada iteracin del ciclo, pues ya que cada que se
ejecuta el mismo se dejan de mantener las imgenes sobre la figura desplegada:
%Graficando las 'O' o las 'X'
if band1==1
plot(x/10*3,y/10*3,'yo','MarkerSize',45,'LineWidth',5)
elseif band1==2
plot(x/10*3,y/10*3,'cx','MarkerSize',50,'LineWidth',5)
end
if band2==1
plot(x/10*5,y/10*3,'yo','MarkerSize',45,'LineWidth',5)
elseif band2==2
plot(x/10*5,y/10*3,'cx','MarkerSize',50,'LineWidth',5)
end
if band3==1
plot(x/10*7,y/10*3,'yo','MarkerSize',45,'LineWidth',5)
elseif band3==2
plot(x/10*7,y/10*3,'cx','MarkerSize',50,'LineWidth',5)
end
if band4==1
plot(x/10*3,y/10*5,'yo','MarkerSize',45,'LineWidth',5)
elseif band4==2
plot(x/10*3,y/10*5,'cx','MarkerSize',50,'LineWidth',5)
end
if band5==1
plot(x/10*5,y/10*5,'yo','MarkerSize',45,'LineWidth',5)
elseif band5==2
plot(x/10*5,y/10*5,'cx','MarkerSize',50,'LineWidth',5)
end

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
if band6==1
plot(x/10*7,y/10*5,'yo','MarkerSize',45,'LineWidth',5)
elseif band6==2
plot(x/10*7,y/10*5,'cx','MarkerSize',50,'LineWidth',5)
end
if band7==1
plot(x/10*3,y/10*7,'yo','MarkerSize',45,'LineWidth',5)
elseif band7==2
plot(x/10*3,y/10*7,'cx','MarkerSize',50,'LineWidth',5)
end
if band8==1
plot(x/10*5,y/10*7,'yo','MarkerSize',45,'LineWidth',5)
elseif band8==2
plot(x/10*5,y/10*7,'cx','MarkerSize',50,'LineWidth',5)
end
if band9==1
plot(x/10*7,y/10*7,'yo','MarkerSize',45,'LineWidth',5)
elseif band9==2
plot(x/10*7,y/10*7,'cx','MarkerSize',50,'LineWidth',5)
end
hold off

Finalmente se dan instrucciones que condicionen el triunfo de cada jugador o su


empate y tambin se vacan los datos del buffer para que no se sature la memoria:
% Condicionales para ganar o para empatar (GATO)
if mat(1,1)+mat(2,2)+mat(3,3)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(3,1)+mat(2,2)+mat(1,3)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/5*4 y/5],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,1)+mat(1,2)+mat(1,3)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*3 y/10*3],'LineWidth',10,'Color', 'r')
break;
elseif mat(2,1)+mat(2,2)+mat(2,3)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*5 y/10*5],'LineWidth',10,'Color', 'r')
break;
elseif mat(3,1)+mat(3,2)+mat(3,3)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*7 y/10*7],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,1)+mat(2,1)+mat(3,1)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/10*3 x/10*3],[y/5 y/5*4],'LineWidth',10,'Color', 'r')

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
break;
elseif mat(1,2)+mat(2,2)+mat(3,2)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/10*5 x/10*5],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,3)+mat(2,3)+mat(3,3)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/10*7 x/10*7],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
end
if mat(1,1)+mat(2,2)+mat(3,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(3,1)+mat(2,2)+mat(1,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/5*4 y/5],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,1)+mat(1,2)+mat(1,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*3 y/10*3],'LineWidth',10,'Color', 'r')
break;
elseif mat(2,1)+mat(2,2)+mat(2,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*5 y/10*5],'LineWidth',10,'Color', 'r')
break;
elseif mat(3,1)+mat(3,2)+mat(3,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*7 y/10*7],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,1)+mat(2,1)+mat(3,1)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/10*3 x/10*3],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,2)+mat(2,2)+mat(3,2)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/10*5 x/10*5],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,3)+mat(2,3)+mat(3,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/10*7 x/10*7],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
end
if band1~=0 && band2~=0 && band3~=0 && band4~=0 && band5~=0 &&...

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
band6~=0&&band7~=0 && band8~=0 && band9~=0
title('GATO','FontSize',16,'BackgroundColor','y')
line([x/10*7 x/10*3],[y/10*3 y/10*3],'LineWidth',10,'Color',
line([x/10*3 x/10*3],[y/10*3 y/10*7],'LineWidth',10,'Color',
line([x/10*3 x/10*7],[y/10*7 y/10*7],'LineWidth',10,'Color',
line([x/10*7 x/10*7],[y/10*7 y/10*5],'LineWidth',10,'Color',
line([x/10*7 x/10*5],[y/10*5 y/10*5],'LineWidth',10,'Color',
break;
end

'r')
'r')
'r')
'r')
'r')

% Se vacan los datos del buffer para que no se sature la memoria


flushdata(vid);

Resultados
Se realizaron pruebas simulando que se utilizaban dos marcadores distintos de
color azul para determinar si eran reconocidos (Fig. 1).

Fig. 1. Deteccin del marcador azul

Al comprobarse la correcta identificacin de los marcadores se continu con la


determinacin de la eleccin correcta del jugador ganador y del empate (tambin
llamado GATO) como se observa en las figuras 2, 3 y 4.

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB

Fig. 2. La figura indica que gan el jugador 1

Fig. 3. La figura indica que gan el jugador 2

Fig. 4. La figura indica que se sucedi un empate

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB

Como prueba definitiva se comprob que el juego terminaba en empate cuando a


los 2 minutos de comenzar no se determinaba un ganador (Fig. 5).

Fig. 5. Se indica empate si terminan los 2 minutos de la ejecucin del programa

Conclusiones
En este proyecto se pusieron en prctica conceptos aprendidos a lo largo del
curso como la captura de frames de una secuencia de video y la inversin de la
imagen proyectada en pantalla para simular el efecto de un espejo y as facilitar la
manipulacin del videojuego. Otros conceptos como el procesamiento en tiempo
real y el filtrado tambin fueron imprescindibles para dar al usuario una
experiencia agradable al hacer que manipulacin adquiriera ms realismo al
interactuar con la interfaz. En conclusin se observa la utilidad del procesamiento
de imgenes al hacer uso de sus conceptos hasta en proyectos de slo ndole
recreacional como lo es un videojuego.
Cdigo fuente
%Delgadillo De la torre Leonardo Misael
%CINVESTAV
%Depto: Ingeniera elctrica
%Seccin: Bioelectrnica
clear all; close all; clc;
a = imaqhwinfo;
[camera_name, camera_id, format] = getCameraInfo(a);
% Graba las capturas de video
vid = videoinput(camera_name, camera_id, format);
% Las capturas se realizan rapidamente y se retornan matrices RGB

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 1;
% Comienza la adquisicin
start(vid)
% Declaracin de variables
band1=0;
band2=0;
band3=0;
band4=0;
band5=0;
band6=0;
band7=0;
band8=0;
band9=0;
cont1=0;
cont2=0;
cont3=0;
cont4=0;
cont5=0;
cont6=0;
cont7=0;
cont8=0;
cont9=0;
segValido1=0;
segValido2=0;
segValido3=0;
segValido4=0;
segValido5=0;
segValido6=0;
segValido7=0;
segValido8=0;
segValido9=0;
turno=1;
mat=zeros(3);
figure('NumberTitle','off','Name','Tic Tac Toe')
% Comienza un ciclo de 120 segundos
seg=120;
tic
while toc <= seg
% Se invierten las coordenadas para la proyeccin de la imagen
set(gca,'xDir','reverse')%,'yDir','reverse')
% Se toma la imagen
data = getsnapshot(vid);
%Se detectan los objetos azules
diff_im = imsubtract(data(:,:,3), rgb2gray(data));
%Se usa un fitro para la adquisicin de los objetos
diff_im = medfilt2(diff_im, [3 3]);
% Convirtiendo el resultado a imagen binaria
diff_im = im2bw(diff_im,0.18);
% Discriminar objetos azules menores a 13000 px de rea (distancia de

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
% reconocimiento)
diff_im = bwareaopen(diff_im,13000);
% Conecta los pixeles en objetos que se compongan de mnimo 8 px
bw = bwlabel(diff_im, 8);
% Se obtiene el marco y el centroide de los objetos identificados
stats = regionprops(bw, 'BoundingBox', 'Centroid');
% Se despliega la imagen
imshow(data)
hold on
if turno == 1
title('Jugador 1 "O"','FontSize',15)
else
title('Jugador 2 "X"','FontSize',15)
end
% Creacin de las lneas del gato
[y,x]=size(data(:,:,3));
line([x/5 x/5*4],[y/5*2 y/5*2],'LineWidth',10)
line([x/5 x/5*4],[y/5*3 y/5*3],'LineWidth',10)
line([x/5*3 x/5*3],[y/5 y/5*4],'LineWidth',10)
line([x/5*2 x/5*2],[y/5 y/5*4],'LineWidth',10)
yy=y+1;
xx=x+1;
%Los objetos azules reconocidos son marcados con mrgenes verdes
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','g','LineWidth',5)
xx=bc(1);
yy=bc(2);
end

plot(bc(1),bc(2), '-m+', 'MarkerSize',15)

% Condicionales para poder marcar cada casilla


% 3 | 2 | 1
%----------% 6 | 5 | 4
%----------% 9 | 8 | 7
% Casilla 1
if x/5*2>xx && xx>=x/5 && y/5*2>yy && yy>=y/5 && band1==0
if toc>=cont1+3
cont1=round(toc);
segValido1=1;
segValido2=0;
segValido3=0;

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
segValido4=0;
segValido5=0;
segValido6=0;
segValido7=0;
segValido8=0;
segValido9=0;

end
if round(toc)-cont1==3 && segValido1==1
if turno==1
band1=1;
turno=2;
mat(1,1)=1;
else
turno=1;
band1=2;
mat(1,1)=7;
end
end

end

% Casilla 2
if x/5*3>xx && xx>=x/5*2 && y/5*2>yy && yy>=y/5 && band2==0
if toc>=cont2+3
cont2=round(toc);
segValido1=0;
segValido2=1;
segValido3=0;
segValido4=0;
segValido5=0;
segValido6=0;
segValido7=0;
segValido8=0;
segValido9=0;
end
if round(toc)-cont2==3 && segValido2==1
if turno==1
band2=1;
turno=2;
mat(1,2)=1;
else
turno=1;
band2=2;
mat(1,2)=7;
end
end
end
%Casilla 3
if x/5*4>=xx && xx>=x/5*3 && y/5*2>yy && yy>=y/5 && band3==0
if toc>=cont3+3
cont3=round(toc);
segValido1=0;
segValido2=0;
segValido3=1;
segValido4=0;
segValido5=0;
segValido6=0;

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
segValido7=0;
segValido8=0;
segValido9=0;
end
if round(toc)-cont3==3 && segValido3==1
if turno==1
band3=1;
turno=2;
mat(1,3)=1;
else
turno=1;
band3=2;
mat(1,3)=7;
end
end
end
% Casilla 4
if x/5*2>xx && xx>=x/5 && y/5*3>yy && yy>=y/5*2 && band4==0
if toc>=cont4+3
cont4=round(toc);
segValido1=0;
segValido2=0;
segValido3=0;
segValido4=1;
segValido5=0;
segValido6=0;
segValido7=0;
segValido8=0;
segValido9=0;
end
if round(toc)-cont4==3 && segValido4==1
if turno==1
band4=1;
turno=2;
mat(2,1)=1;
else
turno=1;
band4=2;
mat(2,1)=7;
end
end
end
% Casilla 5
if x/5*3>xx && xx>=x/5*2 && y/5*3>yy && yy>=y/5*2 && band5==0
if toc>=cont5+3
cont5=round(toc);
segValido1=0;
segValido2=0;
segValido3=0;
segValido4=0;
segValido5=1;
segValido6=0;
segValido7=0;
segValido8=0;
segValido9=0;

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB

end

end
if round(toc)-cont5==3 && segValido5==1
if turno==1
band5=1;
turno=2;
mat(2,2)=1;
else
turno=1;
band5=2;
mat(2,2)=7;
end
end

% Casilla 6
if x/5*4>=xx && xx>=x/5*3 && y/5*3>yy && yy>=y/5*2 && band6==0
if toc>=cont6+3
cont6=round(toc);
segValido1=0;
segValido2=0;
segValido3=0;
segValido4=0;
segValido5=0;
segValido6=1;
segValido7=0;
segValido8=0;
segValido9=0;
end
if round(toc)-cont6==3 && segValido6==1
if turno==1
band6=1;
turno=2;
mat(2,3)=1;
else
turno=1;
band6=2;
mat(2,3)=7;
end
end
end
% Casila 7
if x/5*2>xx && xx>=x/5 && y/5*4>=yy && yy>=y/5*3 && band7==0
if toc>=cont7+3
cont7=round(toc);
segValido1=0;
segValido2=0;
segValido3=0;
segValido4=0;
segValido5=0;
segValido6=0;
segValido7=1;
segValido8=0;
segValido9=0;
end
if round(toc)-cont7==3 && segValido7==1
if turno==1

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
band7=1;
turno=2;
mat(3,1)=1;
else

turno=1;
band7=2;
mat(3,1)=7;

end

end

end
% Casilla 8
if x/5*3>xx && xx>=x/5*2 && y/5*4>=yy && yy>=y/5*3 && band8==0
if toc>=cont8+3
cont8=round(toc);
segValido1=0;
segValido2=0;
segValido3=0;
segValido4=0;
segValido5=0;
segValido6=0;
segValido7=0;
segValido8=1;
segValido9=0;
end
if round(toc)-cont8==3 && segValido8==1
if turno==1
band8=1;
turno=2;
mat(3,2)=1;
else
turno=1;
band8=2;
mat(3,2)=7;
end
end
end
% Casilla 9
if x/5*4>=xx && xx>=x/5*3 && y/5*4>=yy && yy>=y/5*3 && band9==0
if toc>=cont9+3
cont9=round(toc);
segValido1=0;
segValido2=0;
segValido3=0;
segValido4=0;
segValido5=0;
segValido6=0;
segValido7=0;
segValido8=0;
segValido9=1;
end
if round(toc)-cont9==3 && segValido9==1
if turno==1
band9=1;
turno=2;
mat(3,3)=1;

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
else

end
end

turno=1;
band9=2;
mat(3,3)=7;

end

%Graficando las 'O' o las 'X'


if band1==1
plot(x/10*3,y/10*3,'yo','MarkerSize',45,'LineWidth',5)
elseif band1==2
plot(x/10*3,y/10*3,'cx','MarkerSize',50,'LineWidth',5)
end
if band2==1
plot(x/10*5,y/10*3,'yo','MarkerSize',45,'LineWidth',5)
elseif band2==2
plot(x/10*5,y/10*3,'cx','MarkerSize',50,'LineWidth',5)
end
if band3==1
plot(x/10*7,y/10*3,'yo','MarkerSize',45,'LineWidth',5)
elseif band3==2
plot(x/10*7,y/10*3,'cx','MarkerSize',50,'LineWidth',5)
end
if band4==1
plot(x/10*3,y/10*5,'yo','MarkerSize',45,'LineWidth',5)
elseif band4==2
plot(x/10*3,y/10*5,'cx','MarkerSize',50,'LineWidth',5)
end
if band5==1
plot(x/10*5,y/10*5,'yo','MarkerSize',45,'LineWidth',5)
elseif band5==2
plot(x/10*5,y/10*5,'cx','MarkerSize',50,'LineWidth',5)
end
if band6==1
plot(x/10*7,y/10*5,'yo','MarkerSize',45,'LineWidth',5)
elseif band6==2
plot(x/10*7,y/10*5,'cx','MarkerSize',50,'LineWidth',5)
end
if band7==1
plot(x/10*3,y/10*7,'yo','MarkerSize',45,'LineWidth',5)
elseif band7==2
plot(x/10*3,y/10*7,'cx','MarkerSize',50,'LineWidth',5)
end
if band8==1
plot(x/10*5,y/10*7,'yo','MarkerSize',45,'LineWidth',5)
elseif band8==2
plot(x/10*5,y/10*7,'cx','MarkerSize',50,'LineWidth',5)
end
if band9==1
plot(x/10*7,y/10*7,'yo','MarkerSize',45,'LineWidth',5)
elseif band9==2
plot(x/10*7,y/10*7,'cx','MarkerSize',50,'LineWidth',5)
end
hold off

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB

% Condicionales para ganar o para empatar (GATO)


if mat(1,1)+mat(2,2)+mat(3,3)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(3,1)+mat(2,2)+mat(1,3)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/5*4 y/5],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,1)+mat(1,2)+mat(1,3)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*3 y/10*3],'LineWidth',10,'Color', 'r')
break;
elseif mat(2,1)+mat(2,2)+mat(2,3)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*5 y/10*5],'LineWidth',10,'Color', 'r')
break;
elseif mat(3,1)+mat(3,2)+mat(3,3)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*7 y/10*7],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,1)+mat(2,1)+mat(3,1)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/10*3 x/10*3],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,2)+mat(2,2)+mat(3,2)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/10*5 x/10*5],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,3)+mat(2,3)+mat(3,3)==3
title('El ganador es el Jugador 1','FontSize',16,...
'BackgroundColor','y')
line([x/10*7 x/10*7],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
end
if mat(1,1)+mat(2,2)+mat(3,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(3,1)+mat(2,2)+mat(1,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/5*4 y/5],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,1)+mat(1,2)+mat(1,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*3 y/10*3],'LineWidth',10,'Color', 'r')

Videojuego de gato o tic tac toe usando deteccin de color con webcam con GUI en MATLAB
break;
elseif mat(2,1)+mat(2,2)+mat(2,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*5 y/10*5],'LineWidth',10,'Color', 'r')
break;
elseif mat(3,1)+mat(3,2)+mat(3,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/5 x/5*4],[y/10*7 y/10*7],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,1)+mat(2,1)+mat(3,1)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/10*3 x/10*3],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,2)+mat(2,2)+mat(3,2)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/10*5 x/10*5],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
elseif mat(1,3)+mat(2,3)+mat(3,3)==21
title('El ganador es el Jugador 2','FontSize',16,...
'BackgroundColor','y')
line([x/10*7 x/10*7],[y/5 y/5*4],'LineWidth',10,'Color', 'r')
break;
end
if band1~=0 && band2~=0 && band3~=0 && band4~=0 && band5~=0 &&
band6~=0 &&...
band7~=0 && band8~=0 && band9~=0
title('GATO','FontSize',16,'BackgroundColor','y')
line([x/10*7 x/10*3],[y/10*3 y/10*3],'LineWidth',10,'Color', 'r')
line([x/10*3 x/10*3],[y/10*3 y/10*7],'LineWidth',10,'Color', 'r')
line([x/10*3 x/10*7],[y/10*7 y/10*7],'LineWidth',10,'Color', 'r')
line([x/10*7 x/10*7],[y/10*7 y/10*5],'LineWidth',10,'Color', 'r')
line([x/10*7 x/10*5],[y/10*5 y/10*5],'LineWidth',10,'Color', 'r')
break;
end

end

% Se vacan los datos del buffer para que no se sature la memoria


flushdata(vid);

if round(toc)==120
line([x/10*7 x/10*3],[y/10*3 y/10*3],'LineWidth',10,'Color',
line([x/10*3 x/10*3],[y/10*3 y/10*7],'LineWidth',10,'Color',
line([x/10*3 x/10*7],[y/10*7 y/10*7],'LineWidth',10,'Color',
line([x/10*7 x/10*7],[y/10*7 y/10*5],'LineWidth',10,'Color',
line([x/10*7 x/10*5],[y/10*5 y/10*5],'LineWidth',10,'Color',
title('GATO, el tiempo se ha acabado','FontSize',16,...
'BackgroundColor','y')
end
% Se detiene la adquisicin
stop(vid);
% Se borran todos los datos de la imagen del buffer
flushdata(vid);

'r')
'r')
'r')
'r')
'r')

You might also like