Skip to content

Instantly share code, notes, and snippets.

@soundlake
Last active January 21, 2017 23:25
Show Gist options
  • Save soundlake/d73cb57482ecb856656c5900944a6025 to your computer and use it in GitHub Desktop.
Save soundlake/d73cb57482ecb856656c5900944a6025 to your computer and use it in GitHub Desktop.
simple frame for spot difference game in Processing
/*
* 전역변수를 설정하기
*/
boolean is_test = true;
boolean is_display_answer = false;
if (is_test) {
int[][] coordinates = new int[5][2];
}
else {
int[][] coordinates = {
{320, 100},
{100, 50},
{200, 250},
{300, 150},
{130, 180}
};
}
PImage L, R;
ArrayList<PVector> correct_answers = new ArrayList<PVector>();
ArrayList<PVector> checked_answers = new ArrayList<PVector>();
ArrayList<WrongAnswer> wrong_answers = new ArrayList<WrongAnswer>();
final int mark_weight = 8;
final int mark_radius = 15;
final int mark_lifetime = 60;
final int wrong_radius = mark_radius + mark_weight / 2;
final int dist_limit = 30;
PGraphics X, CURSOR;
class WrongAnswer {
float x, y, life;
WrongAnswer(PVector v)
{
x = v.x;
y = v.y;
life = mark_lifetime / 2;
}
}
// Setup
void setup()
{
size(800, 450);
if (is_test) {
test_generate_images();
test_generate_coordinates();
}
for (int[] c: coordinates)
correct_answers.add(new PVector(c[0], c[1]));
// Define X
X = createGraphics(wrong_radius * 2, wrong_radius * 2);
X.beginDraw();
X.background(255, 0);
X.strokeWeight(mark_weight);
X.stroke(255, 0, 0);
int h = mark_weight / 2;
X.line(h, h, X.width - h, X.height - h);
X.line(h, X.height - h, X.width - h, h);
X.endDraw();
// Define CURSOR
CURSOR = createGraphics(20, 20);
CURSOR.beginDraw();
CURSOR.background(255, 0);
CURSOR.stroke(0);
CURSOR.strokeWeight(2);
CURSOR.fill(255);
CURSOR.beginShape();
CURSOR.vertex(0, 0);
CURSOR.vertex(CURSOR.width / 2, CURSOR.height);
CURSOR.vertex(CURSOR.width, CURSOR.height - 5);
CURSOR.endShape(CLOSE);
CURSOR.endDraw();
}
void mouseClicked()
{
boolean is_ok = false;
PVector cur = new PVector(mouseX % (width / 2), mouseY);
for (int i = 0; i < correct_answers.size(); i++) {
PVector v = correct_answers.get(i);
if (v.dist(cur) >= dist_limit)
continue;
correct_answers.remove(i);
checked_answers.add(v);
is_ok = true;
break;
}
if (!is_ok)
wrong_answers.add(new WrongAnswer(cur));
}
void keyPressed()
{
switch (key) {
case ' ':
is_display_answer = !is_display_answer;
default:
}
}
// Draw
void draw()
{
draw_frame();
if (is_test && is_display_answer) {
test_draw_answers();
}
draw_checkeds();
draw_wrongs();
draw_mouse();
}
void draw_frame()
{
image(L, 0, 0);
image(R, width / 2, 0);
}
void draw_checkeds()
{
noFill();
strokeWeight(mark_weight);
stroke(0, 0, 255);
for (PVector v: checked_answers) {
ellipse(v.x, v.y, mark_radius * 2, mark_radius * 2);
ellipse(v.x + width / 2, v.y, mark_radius * 2, mark_radius * 2);
}
}
void draw_wrongs()
{
for (int i = 0; i < wrong_answers.size(); i++) {
WrongAnswer wa = wrong_answers.get(i);
if (wa.life < 1) {
wrong_answers.remove(i);
continue;
}
tint(255, 255 * wa.life / mark_lifetime);
PVector cur = new PVector(wa.x - wrong_radius, wa.y - wrong_radius);
image(X, cur.x, cur.y);
image(X, cur.x + width / 2, cur.y);
wa.life -= 1;
}
}
void draw_mouse()
{
noCursor();
int x = mouseX % (width / 2);
tint(255, 255);
image(CURSOR, x, mouseY);
image(CURSOR, x + width / 2, mouseY);
}
void test_generate_images()
{
L = createImage(width / 2, height, RGB);
R = createImage(width / 2, height, RGB);
L.loadPixels();
R.loadPixels();
for (int i = 0; i < L.pixels.length; i++) {
L.pixels[i] = color(255, 255, 127);
R.pixels[i] = color(127, 255, 255);
}
L.updatePixels();
R.updatePixels();
}
void test_generate_coordinates()
{
for (int i = 0; i < coordinates.length; i++) {
coordinates[i][0] = int(random(width / 2 - mark_radius * 2)) + mark_radius;
coordinates[i][1] = int(random(height - mark_radius * 2)) + mark_radius;
}
}
void test_draw_answers()
{
fill(255);
stroke(0);
strokeWeight(1);
for (PVector v: correct_answers) {
ellipse(v.x, v.y, dist_limit, dist_limit);
ellipse(v.x + width / 2, v.y, dist_limit, dist_limit);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment