Repository navigation
Expand file tree
/
Copy pathMemoryGameImage.java
More file actions
152 lines (134 loc) · 4.83 KB
/
Copy pathMemoryGameImage.java
File metadata and controls
152 lines (134 loc) · 4.83 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
// This class can be used to draw and manage the image for memory game and stores the answers
// of the randomly used colors
import java.awt.*;
import java.util.*;
public class MemoryGameImage {
private Random r;
private DrawingPanel panel;
private Graphics g;
private Map<String, Integer> figureColor;
public static final Color[] PALETTE = {Color.WHITE, Color.GRAY, Color.RED, Color.ORANGE,
Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE,
Color.PINK, new Color(160, 32, 240)};
public static final String[] NAMES = {"white", "gray", "red", "orange", "yellow",
"green", "cyan", "blue", "pink", "purple"};
public MemoryGameImage() {
r = new Random();
panel = new DrawingPanel(600, 600);
g = panel.getGraphics();
figureColor = new HashMap<String, Integer>();
}
// draws the starting text showing for given time
public void drawText(int time) {
g.setFont(new Font("Monospaced", Font.BOLD, 28));
g.drawString("You have " + time / 1000 + " seconds!", 100, 100);
panel.sleep(2000);
panel.clear();
g.drawString("Try to memorize all colors!", 100, 100);
panel.sleep(2000);
panel.clear();
}
// draws a house
public void drawHouse1() {
int ground = r.nextInt(PALETTE.length);
figureColor.put("ground", ground);
g.setColor(PALETTE[ground]);
g.fillOval(-800, 400, 2200, 2200);
g.setColor(Color.BLACK);
g.drawOval(-800, 400, 2200, 2200);
int chimney = r.nextInt(PALETTE.length);
figureColor.put("chimney", chimney);
g.setColor(PALETTE[chimney]);
g.fillRect(350, 160, 40, 90);
g.fillRect(340, 150, 60, 10);
g.setColor(Color.BLACK);
g.drawRect(350, 160, 40, 90);
g.drawRect(340, 150, 60, 10);
int roof = r.nextInt(PALETTE.length);
figureColor.put("roof", roof);
g.setColor(PALETTE[roof]);
Polygon poly1 = new Polygon();
poly1.addPoint(300, 170);
poly1.addPoint(150, 300);
poly1.addPoint(450, 300);
g.fillPolygon(poly1);
g.setColor(Color.BLACK);
g.drawPolygon(poly1);
int wall = r.nextInt(PALETTE.length);
figureColor.put("wall", wall);
g.setColor(PALETTE[wall]);
g.fillRect(200, 300, 200, 150);
g.setColor(Color.BLACK);
g.drawRect(200, 300, 200, 150);
int window = r.nextInt(PALETTE.length);
figureColor.put("window", window);
g.setColor(PALETTE[window]);
g.fillRect(225, 325, 65, 50);
g.setColor(Color.BLACK);
g.drawRect(225, 325, 65, 50);
g.drawLine(257, 325, 257, 375);
int door = r.nextInt(PALETTE.length);
figureColor.put("door", door);
g.setColor(PALETTE[door]);
g.fillRect(305, 365, 40, 85);
g.setColor(Color.BLACK);
g.drawRect(305, 365, 40, 85);
}
// draws a tree and smokes
public void drawHouse2() {
int smoke = r.nextInt(PALETTE.length);
figureColor.put("smoke", smoke);
Color smokeColor = PALETTE[smoke];
g.setColor(smokeColor);
for (int i = 0; i < 3; i++) {
g.fillOval(400 + i * 50, 100 - i * 35, 50 + i * 10, 30 + i * 5);
g.setColor(Color.BLACK);
g.drawOval(400 + i * 50, 100 - i * 35, 50 + i * 10, 30 + i * 5);
g.setColor(smokeColor);
}
int treeTrunk= r.nextInt(PALETTE.length);
figureColor.put("tree trunk", treeTrunk);
Polygon poly2 = new Polygon();
poly2.addPoint(120, 350);
poly2.addPoint(100, 500);
poly2.addPoint(140, 500);
g.fillPolygon(poly2);
g.setColor(Color.BLACK);
g.drawPolygon(poly2);
int treeCrown = r.nextInt(PALETTE.length);
figureColor.put("tree crown", treeCrown);
Color crownColor = PALETTE[treeCrown];
g.setColor(crownColor);
for (int i = 0; i < 3; i++) {
Polygon poly3 = new Polygon();
poly3.addPoint(120, 350 - i * 50);
poly3.addPoint(30 + i * 15, 430 - i * 40);
poly3.addPoint(210 - i * 15, 430 - i * 40);
g.fillPolygon(poly3);
g.setColor(Color.BLACK);
g.drawPolygon(poly3);
g.setColor(crownColor);
}
}
// returns the color name of the given figure
public String getAnswer(String figure) {
return NAMES[figureColor.get(figure)];
}
// returns a color name
public String getColor() {
return NAMES[r.nextInt(NAMES.length)];
}
// hids the image after given time
public void erase(int time) {
panel.sleep(time);
panel.setVisible(false);
}
// shows the image
public void regain() {
panel.setVisible(true);
}
// returns a set of current used colors' names
public Set<String> getSet() {
return figureColor.keySet();
}
}