-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTiro.java
More file actions
235 lines (181 loc) · 5.95 KB
/
Copy pathTiro.java
File metadata and controls
235 lines (181 loc) · 5.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* Tiro al piattello - classe del gioco vero e proprio. Crea lo sfondo, aggiunge
* il fucile e gestisce il lancio dei piattelli con relativa gestione dei punti.
* Il punteggio parte da un numero definito per poi salire di 1 se il piattello
* viene preso o scendere di 1 se il piattello viene mancato.
*
* @author GB Factory (gbfactory.net)
* @version 2.0
* @since 2021-01-05
*/
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.Random;
public class Tiro extends JPanel implements ActionListener, MouseListener, MouseMotionListener {
public final int width = 1280;
public final int height = 720;
private int mouseX = 0;
private int mouseY = 0;
private int clickX = 0;
private int clickY = 0;
private int piattelloX = 0;
private int piattelloY = 0;
private int punti = 0;
private int puntiVecchi = 0;
private Timer timer;
boolean game = true;
boolean sparo = false;
boolean rotto = false;
public Tiro() {
setFocusable(true);
addMouseListener(this);
addMouseMotionListener(this);
setPreferredSize(new Dimension(width, height));
startGame();
}
private void startGame() {
punti = 1;
puntiVecchi = punti;
resettaPiattello();
timer = new Timer(20, this);
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
private void doDrawing(Graphics g) {
drawScenario(g);
if (game) {
drawPiattello(g);
drawFucile(g);
drawMirino(g);
g.setFont(new Font("Verdana", Font.BOLD, 20));
g.setColor(Color.WHITE);
g.drawString("Punteggio: " + punti, 10, 25);
} else {
gameOver(g);
drawMirino(g);
}
}
private void drawScenario(Graphics g) {
ImageIcon sfondo = new ImageIcon("./src/img/sfondo.jpg");
g.drawImage(sfondo.getImage(), 0, 0, width, height, null);
}
private void drawFucile(Graphics g) {
String imgName = sparo ? "fucile_sparo.png" : "fucile.png";
ImageIcon fucile = new ImageIcon("./src/img/" + imgName);
g.drawImage(fucile.getImage(), mouseX, height - 264, 450, 264, null);
sparo = false;
}
private void drawMirino(Graphics g) {
ImageIcon mirino = new ImageIcon("./src/img/mirino.png");
g.drawImage(mirino.getImage(), mouseX - 40, mouseY - 40, 80, 80, null);
}
private void drawPiattello(Graphics g) {
ImageIcon piattello = new ImageIcon("./src/img/piattello.png");
g.drawImage(piattello.getImage(), piattelloX, piattelloY, 100, 100, null);
}
private void gameOver(Graphics g) {
String stringa = "HAI PERSO!";
String strPunti = "Clicca per ripartire!";
Font font = new Font("Verdana", Font.BOLD, 20);
Font fontPunti = new Font("Verdana", Font.BOLD, 15);
g.setColor(Color.RED);
g.setFont(font);
g.drawString(stringa, (width - getFontMetrics(font).stringWidth(stringa)) / 2, height / 2);
g.setColor(Color.WHITE);
g.setFont(fontPunti);
g.drawString(strPunti, (width - getFontMetrics(fontPunti).stringWidth(strPunti)) / 2, height / 2 + 35);
}
private void playAudio(String nomeFile) {
File audio = new File("./src/audio/" + nomeFile);
try {
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(audio));
clip.start();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (!game) {
game = true;
startGame();
}
}
private void resettaPiattello() {
piattelloX = -300;
piattelloY = new Random().nextInt(400);
}
@Override
public void mousePressed(MouseEvent e) {
clickX = e.getX();
clickY = e.getY();
System.out.println(timer.getDelay());
if (clickX >= piattelloX && clickX <= piattelloX + 100 && clickY >= piattelloY && clickY <= piattelloY + 100) {
playAudio("hit.wav");
resettaPiattello();
punti++;
puntiVecchi = punti;
if (punti > 0 && timer.getDelay() > 0 && punti % 5 == 0) {
timer.setDelay(timer.getDelay() - 1);
}
}
sparo = true;
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
piattelloX += 10;
if (piattelloX > 0 && piattelloX <= 400 ) {
piattelloY += 1;
}
if (piattelloX > 400 && piattelloX <= 800 ) {
piattelloY += 2;
}
if (piattelloX > 800) {
piattelloY += 3;
}
if (piattelloX >= width) {
playAudio("miss.wav");
resettaPiattello();
if (punti == puntiVecchi) {
punti--;
puntiVecchi = punti;
}
}
if (punti <= 0) {
playAudio("stop.wav");
game = false;
timer.stop();
}
if (sparo) {
//playAudio("fire.wav");
}
repaint();
}
}