- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJogo.java
More file actions
Latest commit
214 lines (180 loc) · 6.95 KB
/
Copy pathJogo.java
File metadata and controls
214 lines (180 loc) · 6.95 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
publicclassJogoextendsJFrameimplementsKeyListener {
privateJLabelstatusBar;
privateMapamapa;
privatefinalColorfogColor = newColor(192, 192, 192, 150); // Cor cinza claro com transparência para nevoa
privatefinalColorcharacterColor = Color.BLACK; // Cor preta para o personagem
publicJogo(StringarquivoMapa) {
setTitle("Jogo de Aventura");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
// Cria o mapa do jogo
mapa = newMapa(arquivoMapa);
// Painel para desenhar o mapa do jogo
JPanelmapPanel = newJPanel() {
@Override
protectedvoidpaintComponent(Graphicsg) {
super.paintComponent(g);
// Define a fonte para garantir que o caractere caiba em 10x10 pixels
Fontfont = newFont("Roboto", Font.BOLD, 12);
g.setFont(font);
desenhaMapa(g);
desenhaPersonagem(g);
}
};
mapPanel.setPreferredSize(newDimension(800, 600));
// Botões de movimento
JButtonbtnUp = newJButton("Cima (W)");
JButtonbtnDown = newJButton("Baixo (S)");
JButtonbtnRight = newJButton("Direita (D)");
JButtonbtnLeft = newJButton("Esquerda (A)");
JButtonbtnInterect = newJButton("Interagir (E)");
JButtonbtnAttack = newJButton("Ação Secundária (J)");
// Evita que os botões recebam o foco e interceptem os eventos de teclado
btnUp.setFocusable(false);
btnDown.setFocusable(false);
btnRight.setFocusable(false);
btnLeft.setFocusable(false);
btnInterect.setFocusable(false);
btnAttack.setFocusable(false);
// Listeners para os botões
btnUp.addActionListener(e -> move(Direcao.CIMA));
btnDown.addActionListener(e -> move(Direcao.BAIXO));
btnRight.addActionListener(e -> move(Direcao.DIREITA));
btnLeft.addActionListener(e -> move(Direcao.ESQUERDA));
btnInterect.addActionListener(e -> interage());
btnAttack.addActionListener(e -> ataca());
// Layout dos botões
JPanelbuttonPanel = newJPanel(newGridLayout(2, 2));
buttonPanel.add(btnUp);
buttonPanel.add(btnDown);
buttonPanel.add(btnInterect);
buttonPanel.add(btnRight);
buttonPanel.add(btnLeft);
buttonPanel.add(btnAttack);
// Barra de status
statusBar = newJLabel("Posição: (" + mapa.getX() + "," + mapa.getY() + ")");
statusBar.setBorder(BorderFactory.createEtchedBorder());
statusBar.setHorizontalAlignment(SwingConstants.LEFT);
// Painel para botões e barra de status
JPanelsouthPanel = newJPanel();
southPanel.setLayout(newBoxLayout(southPanel, BoxLayout.Y_AXIS));
southPanel.add(buttonPanel);
southPanel.add(statusBar);
// Adiciona os paineis ao JFrame
add(mapPanel, BorderLayout.CENTER);
add(southPanel, BorderLayout.SOUTH);
// Ajusta o tamanho do JFrame para acomodar todos os componentes
pack();
// Adiciona o listener para eventos de teclado
addKeyListener(this);
}
publicvoidmove(Direcaodirecao) {
if (mapa == null)
return;
// Modifica posição do personagem no mapa
if (!mapa.move(direcao))
return;
// Atualiza a barra de status
if (statusBar != null)
statusBar.setText("Posição: (" + mapa.getX() + "," + mapa.getY() + ")");
// Redesenha o painel
repaint();
}
publicvoidinterage() {
if (mapa == null)
return;
// Cria um diálogo para exibir a mensagem de interação
Stringmensagem = mapa.interage();
if (mensagem != null) {
JOptionPane.showMessageDialog(this, mensagem);
}
if (mapa.getContador() == 0) {
termina();
}
if (mapa.getContador() == 2) {
JOptionPane.showMessageDialog(this, "Você achou uma chave! O que será que ela faz?");
}
}
publicvoidataca() {
if (mapa == null)
return;
Stringstatus = mapa.ataca();
// Atualiza a barra de status
if (statusBar != null)
statusBar.setText(status);
}
publicvoidtermina() {
if (mapa == null)
return;
JOptionPane.showMessageDialog(this, "Você matou todos os cupins e salvou o Seu Zé!");
System.exit(0);
}
privatevoiddesenhaMapa(Graphicsg) {
inttamanhoCelula = mapa.getTamanhoCelula();
for (inti = 0; i < mapa.getNumLinhas(); i++) {
for (intj = 0; j < mapa.getNumColunas(); j++) {
intposX = j * tamanhoCelula;
intposY = (i + 1) * tamanhoCelula;
if (mapa.estaRevelado(j, i)) {
ElementoMapaelemento = mapa.getElemento(j, i);
if (elemento != null) {
//System.out.println("Elemento: " + elemento.getSimbolo() + " " + elemento.getCor());
g.setColor(elemento.getCor());
g.drawString(elemento.getSimbolo().toString(), posX, posY);
}
} else {
// Pinta a área não revelada
g.setColor(fogColor);
g.fillRect(j * tamanhoCelula, i * tamanhoCelula, tamanhoCelula, tamanhoCelula);
}
}
}
}
privatevoiddesenhaPersonagem(Graphicsg) {
g.setColor(characterColor);
g.drawString("☺", mapa.getX(), mapa.getY());
}
@Override
publicvoidkeyTyped(KeyEvente) {
// Não necessário
}
@Override
publicvoidkeyPressed(KeyEvente) {
intkey = e.getKeyCode();
switch (key) {
caseKeyEvent.VK_W: // Tecla 'W' para cima
move(Direcao.CIMA);
break;
caseKeyEvent.VK_S: // Tecla 'S' para baixo
move(Direcao.BAIXO);
break;
caseKeyEvent.VK_A: // Tecla 'A' para esquerda
move(Direcao.ESQUERDA);
break;
caseKeyEvent.VK_D: // Tecla 'D' para direita
move(Direcao.DIREITA);
break;
caseKeyEvent.VK_E: // Tecla 'E' para interagir
interage();
break;
caseKeyEvent.VK_J: // Tecla 'J' para ação secundária
ataca();
break;
}
}
@Override
publicvoidkeyReleased(KeyEvente) {
// Não necessário
}
publicstaticvoidmain(String[] args) {
SwingUtilities.invokeLater(() -> {
newJogo("mapa3.txt").setVisible(true);
});
}
}