From 47e6af7301cfd73a8c7a4f8480e86dc7cdc57c4d Mon Sep 17 00:00:00 2001 From: Næþ'n Lasseter Date: Wed, 30 Mar 2016 15:47:29 +0100 Subject: first commit --- application.linux64/source/KeyboardDefense.java | 264 ++++++++++++++++++++++++ application.linux64/source/KeyboardDefense.pde | 239 +++++++++++++++++++++ 2 files changed, 503 insertions(+) create mode 100644 application.linux64/source/KeyboardDefense.java create mode 100644 application.linux64/source/KeyboardDefense.pde (limited to 'application.linux64/source') diff --git a/application.linux64/source/KeyboardDefense.java b/application.linux64/source/KeyboardDefense.java new file mode 100644 index 0000000..0e44c63 --- /dev/null +++ b/application.linux64/source/KeyboardDefense.java @@ -0,0 +1,264 @@ +import processing.core.*; +import processing.data.*; +import processing.event.*; +import processing.opengl.*; + +import java.util.HashMap; +import java.util.ArrayList; +import java.io.File; +import java.io.BufferedReader; +import java.io.PrintWriter; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.IOException; + +public class KeyboardDefense extends PApplet { + +int[][] stars; +int starcount; +char[][] board = { + {'Q','W','E','R','T','Y','U','I','O','P'}, + {'A','S','D','F','G','H','J','K','L',' '}, + {'Z','X','C','V','B','N','M',' ',' ',' '} +}; + +int starsize = 5; +int boardsize = 28; + +int score, level; +boolean playing; + +int[][] keyCoords = { + {0, 1}, //a + {4, 2}, //b + {2, 2}, //c + {2, 1}, //d + {2, 0}, //e + {3, 1}, //f + {4, 1}, //g + {5, 1}, //h + {7, 0}, //i + {6, 1}, //j + {7, 1}, //k + {8, 1}, //l + {6, 2}, //m + {5, 2}, //n + {8, 0}, //o + {9, 0}, //p + {0, 0}, //q + {3, 0}, //r + {1, 1}, //s + {4, 0}, //t + {6, 0}, //u + {3, 2}, //v + {1, 0}, //w + {1, 2}, //x + {5, 0}, //y + {0, 2} //z +}; + +int[] cols = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 24, 25, 27 +}; + +int[] baseKeyColours = { + 0xffFFFFFF, 0xffFFFFFF, 0xffFFFFFF, 0xffFFFFFF, 0xffFFFFFF, + 0xffFFFFFF, 0xffFFFFFF, 0xffFFFFFF, 0xffFFFFFF, 0xffFFFFFF, + 0xffFFFFFF, 0xffFFFFFF, 0xffFFFFFF, 0xffFFBBBB, 0xffFFFFFF, + 0xffFFFFFF, 0xffFFBBBB, 0xffFFFFFF, 0xffFFFFFF, 0xffFFFFFF, + 0xffFFFFFF, 0xffFFFFFF, 0xffFFFFFF, 0xffFFFFFF, 0xffFFFFFF, 0xffFFFFFF +}; + +int[] keyColours; + +int keyMap[] = { + 10, 23, 21, 12, 2, + 13, 14, 15, 7, 16, + 17, 18, 25, 24, 8, + 9, 0, 3, 11, 4, + 6, 22, 1, 20, 5, 19 +}; + +long time, startime; + +public void setup() { + size(784, 612); + stars = new int[100][2]; + keyColours = new int[26]; + starcount = 0; + score = 0; + level = 5000; + playing = true; + textFont(createFont("FreeMono Bold", 20)); + genStar(); + startime = time = millis(); +} + +public void makeStar(int[] star) { + line( + star[0] - starsize, star[1] - starsize, + star[0] + starsize, star[1] + starsize + ); + line( + star[0] - starsize, star[1] + starsize, + star[0] + starsize, star[1] - starsize + ); +} + +public void makeBoard() { + noStroke(); + for(int i = 0; i < 28; i+=2) { + fill(0xffCCCCCC); + rect(i*boardsize, 0, boardsize, 612); + fill(0xffAAAAAA); + rect(i*boardsize + boardsize, 0, boardsize, 612); + } + for(int i = 0; i < 28; i+=2) { + fill(0xffEEEEEE); + rect(i*boardsize, 500, boardsize, 84); + fill(0xffCCCCCC); + rect(i*boardsize + boardsize, 500, boardsize, 84); + } + + stroke(0xff000000); + int box = 0; + for(int i = 0; i < 10; i++) { + fill(keyColours[box]); + rect(i*3*boardsize, 500, boardsize, boardsize); + fill(0xff000000); + text(board[0][i], (i*3*boardsize)+2, 500+15); + box++; + } + for(int i = 0; i < 9; i++) { + fill(keyColours[box]); + rect((i*3*boardsize)+boardsize, 500+boardsize, boardsize, boardsize); + fill(0xff000000); + text(board[1][i], (i*3*boardsize)+boardsize+2, 500+boardsize+15); + box++; + } + for(int i = 0; i < 7; i++) { + fill(keyColours[box]); + rect((i*3*boardsize)+(2*boardsize), 500+(2*boardsize), boardsize, boardsize); + fill(0xff000000); + text(board[2][i], (i*3*boardsize)+(2*boardsize)+2, 500+(2*boardsize)+15); + box++; + } + + for (int i = 0; i < 26; i++) + keyColours[i] = baseKeyColours[i]; +} + +public void makeScore() { + text("Score: " + score, 644, 610); +} + +public void genStar() { + int col = cols[PApplet.parseInt(random(26))]; + stars[starcount][0] = (col*boardsize) + (boardsize/2); + stars[starcount][1] = 0; + starcount++; +} + +public void checkFallen() { + for (int i = 0; i < starcount; i++) + if (stars[i][1] > 600) + gameOver(); +} + +public void gameOver() { + stars = new int[100][2]; + starcount = 0; + playing = false; +} + +public void makeGOScreen() { + clear(); + background(0xffCCCCCC); + fill(0xff000000); + text("GAME OVER", 350, 300); + text("Score: " + score, 350, 330); +} + +public void draw() { + if (playing) { + checkFallen(); + + clear(); + background(0xffCCCCCC); + + if (millis() > (startime + level) && playing) { + genStar(); + startime = millis(); + } + + if (millis() > time) { + for(int i = 0; i < starcount; i++) + stars[i][1] += 1; + time = millis(); + } + + makeBoard(); + + for(int i = 0; i < starcount; i++) + makeStar(stars[i]); + + makeScore(); + } else { + makeGOScreen(); + } +} + +public void shiftStars(int star) { + for (int i = star; i < starcount; i++) { + stars[i][0] = stars[i+1][0]; + stars[i][1] = stars[i+1][1]; + } + starcount--; +} + +public int checkStars(int keyIndex) { + int sc = -1; + int + l = keyCoords[keyIndex][0]*3*boardsize + (keyCoords[keyIndex][1]*boardsize), + r = (keyCoords[keyIndex][0]*3*boardsize) + (keyCoords[keyIndex][1]*boardsize) + boardsize, + t = 500 + (keyCoords[keyIndex][1]*boardsize), + b = 500 + (keyCoords[keyIndex][1]*boardsize) + boardsize; + + for (int i = 0; i < starcount; i++) + if(stars[i][0] > l && stars[i][0] < r) { + if(stars[i][1] > 500 && stars[i][1] < 584) { + if(stars[i][1] > t && stars[i][1] < b) + sc = 2; + else + sc = 1; + shiftStars(i); + level -= (level == 1000 ? 0 : 100); + } + } + + return sc; +} + +public void keyPressed() { + int keyIndex = -1; + if (key >= 'A' && key <= 'Z') { + keyIndex = key - 'A'; + } else if (key >= 'a' && key <= 'z') { + keyIndex = key - 'a'; + } + if (keyIndex != -1) { + score += checkStars(keyIndex); + keyColours[keyMap[keyIndex]] = 0xff00FF00; + } +} + static public void main(String[] passedArgs) { + String[] appletArgs = new String[] { "KeyboardDefense" }; + if (passedArgs != null) { + PApplet.main(concat(appletArgs, passedArgs)); + } else { + PApplet.main(appletArgs); + } + } +} diff --git a/application.linux64/source/KeyboardDefense.pde b/application.linux64/source/KeyboardDefense.pde new file mode 100644 index 0000000..01e9400 --- /dev/null +++ b/application.linux64/source/KeyboardDefense.pde @@ -0,0 +1,239 @@ +int[][] stars; +int starcount; +char[][] board = { + {'Q','W','E','R','T','Y','U','I','O','P'}, + {'A','S','D','F','G','H','J','K','L',' '}, + {'Z','X','C','V','B','N','M',' ',' ',' '} +}; + +int starsize = 5; +int boardsize = 28; + +int score, level; +boolean playing; + +int[][] keyCoords = { + {0, 1}, //a + {4, 2}, //b + {2, 2}, //c + {2, 1}, //d + {2, 0}, //e + {3, 1}, //f + {4, 1}, //g + {5, 1}, //h + {7, 0}, //i + {6, 1}, //j + {7, 1}, //k + {8, 1}, //l + {6, 2}, //m + {5, 2}, //n + {8, 0}, //o + {9, 0}, //p + {0, 0}, //q + {3, 0}, //r + {1, 1}, //s + {4, 0}, //t + {6, 0}, //u + {3, 2}, //v + {1, 0}, //w + {1, 2}, //x + {5, 0}, //y + {0, 2} //z +}; + +int[] cols = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 24, 25, 27 +}; + +int[] baseKeyColours = { + #FFFFFF, #FFFFFF, #FFFFFF, #FFFFFF, #FFFFFF, + #FFFFFF, #FFFFFF, #FFFFFF, #FFFFFF, #FFFFFF, + #FFFFFF, #FFFFFF, #FFFFFF, #FFBBBB, #FFFFFF, + #FFFFFF, #FFBBBB, #FFFFFF, #FFFFFF, #FFFFFF, + #FFFFFF, #FFFFFF, #FFFFFF, #FFFFFF, #FFFFFF, #FFFFFF +}; + +int[] keyColours; + +int keyMap[] = { + 10, 23, 21, 12, 2, + 13, 14, 15, 7, 16, + 17, 18, 25, 24, 8, + 9, 0, 3, 11, 4, + 6, 22, 1, 20, 5, 19 +}; + +long time, startime; + +void setup() { + size(784, 612); + stars = new int[100][2]; + keyColours = new int[26]; + starcount = 0; + score = 0; + level = 5000; + playing = true; + textFont(createFont("FreeMono Bold", 20)); + genStar(); + startime = time = millis(); +} + +void makeStar(int[] star) { + line( + star[0] - starsize, star[1] - starsize, + star[0] + starsize, star[1] + starsize + ); + line( + star[0] - starsize, star[1] + starsize, + star[0] + starsize, star[1] - starsize + ); +} + +void makeBoard() { + noStroke(); + for(int i = 0; i < 28; i+=2) { + fill(#CCCCCC); + rect(i*boardsize, 0, boardsize, 612); + fill(#AAAAAA); + rect(i*boardsize + boardsize, 0, boardsize, 612); + } + for(int i = 0; i < 28; i+=2) { + fill(#EEEEEE); + rect(i*boardsize, 500, boardsize, 84); + fill(#CCCCCC); + rect(i*boardsize + boardsize, 500, boardsize, 84); + } + + stroke(#000000); + int box = 0; + for(int i = 0; i < 10; i++) { + fill(keyColours[box]); + rect(i*3*boardsize, 500, boardsize, boardsize); + fill(#000000); + text(board[0][i], (i*3*boardsize)+2, 500+15); + box++; + } + for(int i = 0; i < 9; i++) { + fill(keyColours[box]); + rect((i*3*boardsize)+boardsize, 500+boardsize, boardsize, boardsize); + fill(#000000); + text(board[1][i], (i*3*boardsize)+boardsize+2, 500+boardsize+15); + box++; + } + for(int i = 0; i < 7; i++) { + fill(keyColours[box]); + rect((i*3*boardsize)+(2*boardsize), 500+(2*boardsize), boardsize, boardsize); + fill(#000000); + text(board[2][i], (i*3*boardsize)+(2*boardsize)+2, 500+(2*boardsize)+15); + box++; + } + + for (int i = 0; i < 26; i++) + keyColours[i] = baseKeyColours[i]; +} + +void makeScore() { + text("Score: " + score, 644, 610); +} + +void genStar() { + int col = cols[int(random(26))]; + stars[starcount][0] = (col*boardsize) + (boardsize/2); + stars[starcount][1] = 0; + starcount++; +} + +void checkFallen() { + for (int i = 0; i < starcount; i++) + if (stars[i][1] > 600) + gameOver(); +} + +void gameOver() { + stars = new int[100][2]; + starcount = 0; + playing = false; +} + +void makeGOScreen() { + clear(); + background(#CCCCCC); + fill(#000000); + text("GAME OVER", 350, 300); + text("Score: " + score, 350, 330); +} + +void draw() { + if (playing) { + checkFallen(); + + clear(); + background(#CCCCCC); + + if (millis() > (startime + level) && playing) { + genStar(); + startime = millis(); + } + + if (millis() > time) { + for(int i = 0; i < starcount; i++) + stars[i][1] += 1; + time = millis(); + } + + makeBoard(); + + for(int i = 0; i < starcount; i++) + makeStar(stars[i]); + + makeScore(); + } else { + makeGOScreen(); + } +} + +void shiftStars(int star) { + for (int i = star; i < starcount; i++) { + stars[i][0] = stars[i+1][0]; + stars[i][1] = stars[i+1][1]; + } + starcount--; +} + +int checkStars(int keyIndex) { + int sc = -1; + int + l = keyCoords[keyIndex][0]*3*boardsize + (keyCoords[keyIndex][1]*boardsize), + r = (keyCoords[keyIndex][0]*3*boardsize) + (keyCoords[keyIndex][1]*boardsize) + boardsize, + t = 500 + (keyCoords[keyIndex][1]*boardsize), + b = 500 + (keyCoords[keyIndex][1]*boardsize) + boardsize; + + for (int i = 0; i < starcount; i++) + if(stars[i][0] > l && stars[i][0] < r) { + if(stars[i][1] > 500 && stars[i][1] < 584) { + if(stars[i][1] > t && stars[i][1] < b) + sc = 2; + else + sc = 1; + shiftStars(i); + level -= (level == 1000 ? 0 : 100); + } + } + + return sc; +} + +void keyPressed() { + int keyIndex = -1; + if (key >= 'A' && key <= 'Z') { + keyIndex = key - 'A'; + } else if (key >= 'a' && key <= 'z') { + keyIndex = key - 'a'; + } + if (keyIndex != -1) { + score += checkStars(keyIndex); + keyColours[keyMap[keyIndex]] = #00FF00; + } +} -- cgit v1.2.3