Repository navigation
Expand file tree
/
Copy pathBoard.java
More file actions
145 lines (109 loc) · 3.79 KB
/
Copy pathBoard.java
File metadata and controls
145 lines (109 loc) · 3.79 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
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
public class Board extends JPanel implements ActionListener, KeyListener {
private final int DELAY = 25;
public static final int TILE_SIZE = 80;
public static final int ROWS = 13;
public static final int COLUMNS = 25;
public static final int NUM_COINS = 100;
private static final long serialVersionUID = 490905409104883233L;
private Timer timer;
private Player player;
private ArrayList<Coin> coins;
public Board() {
setPreferredSize(new Dimension(TILE_SIZE * COLUMNS, TILE_SIZE * ROWS));
setBackground(new Color(0x404940));
player = new Player();
coins = populateCoins();
timer = new Timer(DELAY, this);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
player.tick();
collectCoins();
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawBackground(g);
drawScore(g);
for (Coin coin : coins) {
coin.draw(g, this);
}
player.draw(g, this);
Toolkit.getDefaultToolkit().sync();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
player.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {
}
private void drawBackground(Graphics g) {
g.setColor(new Color(71, 73, 71));
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLUMNS; col++) {
if ((row + col) % 2 == 1) {
g.fillRect(
col * TILE_SIZE,
row * TILE_SIZE,
TILE_SIZE,
TILE_SIZE
);
}
}
}
}
private void drawScore(Graphics g) {
String text = "Souls " + player.getScore();
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setRenderingHint(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(
RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setColor(new Color(30, 201, 139));
g2d.setFont(new Font("Lato", Font.BOLD, 25));
FontMetrics metrics = g2d.getFontMetrics(g2d.getFont());
Rectangle rect = new Rectangle(0, TILE_SIZE * (ROWS - 1), TILE_SIZE * COLUMNS, TILE_SIZE);
int x = rect.x + (rect.width - metrics.stringWidth(text)) / 2;
int y = rect.y + ((rect.height - metrics.getHeight()) / 2) + metrics.getAscent();
g2d.drawString(text, x, y);
}
private ArrayList<Coin> populateCoins() {
ArrayList<Coin> coinList = new ArrayList<>();
Random rand = new Random();
for (int i = 0; i < NUM_COINS; i++) {
int coinX = rand.nextInt(COLUMNS);
int coinY = rand.nextInt(ROWS);
coinList.add(new Coin(coinX, coinY));
}
return coinList;
}
private void collectCoins() {
ArrayList<Coin> collectedCoins = new ArrayList<>();
for (Coin coin : coins) {
if (player.getPos().equals(coin.getPos())) {
player.addScore(100);
collectedCoins.add(coin);
Random rand = new Random();
int coinX = rand.nextInt(COLUMNS);
int coinY = rand.nextInt(ROWS);
coin.setPos(coinX, coinY);
}
}
}
}