Jogo de Tabuleiro - Galeria de Exemplos
Jogo de Tabuleiro

Descrição: Exemplo que ilusta como desenhar um tabuleiro de um jogo de damas/xadrez na tela. O jogador pode usar o mouse para colocar as peças no tabuleiro clicando com o mouse na casa desejada.

Autor: Edirlei Soares de Lima

Download: Exemplos05.zip

 

Código Fonte:

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
#include "Graphics.h"
Graphics graphics;
int tabuleiro[8][8];
void MainLoop()
{
    int x, y;
    bool intercala = false;
    for (x = 0; x < 8; x++)
    {
        for (y = 0; y < 8; y++)
        {  
            if (intercala == true)
                graphics.SetColor(0,0,0);
            else
                graphics.SetColor(255,255,255);
            graphics.FillRectangle2D(200 + (x * 50), 100 + (y * 50), 200 + (x * 50) + 50, 100 + (y * 50) + 50);
            if (tabuleiro[x][y] == 1)
            {
                graphics.SetColor(255,0,0);
                graphics.FillCircle2D((200 + (x * 50) + 25), (100 + (y * 50) + 25), 20, 20);
            }
            else if (tabuleiro[x][y] == 2)
            {
                graphics.SetColor(0,255,0);
                graphics.FillCircle2D((200 + (x * 50) + 25), (100 + (y * 50) + 25), 20, 20);
            }
            intercala = !intercala;
        }
        intercala = !intercala;
    }
}
void MouseClickInput(int button, int state, int x, int y)
{
  if ((button == MOUSE_LEFT_BUTTON)&&(state == MOUSE_STATE_DOWN))
  {
     int selecionado_x = ((x / 50) - (200 / 50));
     int selecionado_y = ((y / 50) - (100 / 50));
     if ((selecionado_x >= 0)&&(selecionado_x < 8)&&(selecionado_y >= 0)&&(selecionado_y < 8))
     {
         tabuleiro[selecionado_x][selecionado_y] = 1;
     }
  }
  else if ((button == MOUSE_RIGHT_BUTTON)&&(state == MOUSE_STATE_DOWN))
  {
     int selecionado_x = ((x / 50) - (200 / 50));
     int selecionado_y = ((y / 50) - (100 / 50));
     if ((selecionado_x >= 0)&&(selecionado_x < 8)&&(selecionado_y >= 0)&&(selecionado_y < 8))
     {
         tabuleiro[selecionado_x][selecionado_y] = 2;
     }
  }
}
int main(void)
{
    graphics.CreateMainWindow(800, 600, "Exemplo 05 - Controle pelo Mouse");
    graphics.SetBackgroundColor(100,100,100);
    
    int x, y;
    for (x = 0; x < 8; x++)
    {
        for (y = 0; y < 8; y++)
        {  
            tabuleiro[x][y] = 0; //Inicializa tabuleiro
        }
    }
    graphics.SetMouseClickInput(MouseClickInput);
    graphics.SetMainLoop(MainLoop);
    graphics.StartMainLoop();
    return 0;
}