-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypingGame
134 lines (112 loc) · 2.81 KB
/
TypingGame
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
String typing = "";
String typed = "";
float mSpeed = 2;
int level = 0;
ArrayList<Meteor> rocks = new ArrayList<>();
String[] lvlOneWords = {"cat", "dog", "sit", "way", "cake", "moon", "look"};
String[] lvlTwoWords = {"mixed", "given", "paper", "seven", "jacket", "piano", "curtain"};
String[] lvlThreeWords = {"academy", "discover", "coconut", "lightbulb", "regular", "fireplace", "potato"};
String[][] allWords = {lvlOneWords, lvlTwoWords, lvlThreeWords};
boolean pause = false;
void setup(){
size(600, 600);
textAlign(CENTER, CENTER);
}
void draw(){
background(0);
fill(255);
textSize(15);
text("press shift -> p\nto pause the game", 70, 25);
typingControls();
meteorControls();
levelControls();
}
// displaying what is being typed
void typingControls(){
stroke(255, 0, 0);
strokeWeight(5);
line(0, height-55, width, height-55);
textSize(30);
fill(255);
text(typing, width/2, height-30);
}
void levelControls(){
if((frameCount%600 == 0) && (level < allWords.length-1)){
level++;
}
}
// displaying, making, and creating the meteors
void meteorControls(){
Meteor r = new Meteor();
// displaying what exists
for(Meteor m : rocks){
if(!pause){
m.move();
}
m.display();
if(m.getWord().equals(typed)) r = m;
}
// removing what was typed
rocks.remove(r);
typed = "";
// adding a new meteor
if(!pause && frameCount%(120-(level*10)) == 0){
String s = allWords[level][(int)random(0, allWords[0].length)];
Meteor m = new Meteor(s, mSpeed, random(10, width-(s.length()*10)), -20);
rocks.add(m);
}
}
void reset(){
typing = "";
typed = "";
rocks.clear();
level = 0;
mSpeed = 2;
}
void keyPressed(){
if(!pause){
if(key == BACKSPACE){
typing = typing.substring(0, typing.length()-1);
}
else if (key == ENTER){
typed = typing;
typing = "";
}
else if(key >= 'a' && key <= 'z'){
Character c = key;
typing = typing + c;
}
}
if(key == 'P'){
pause = !pause;
}
}
class Meteor{
String word;
float speed, xPos, yPos;
Meteor(){
this.word = "";
this.speed = 0;
}
Meteor(String w, float s, float x, float y){
this.word = w;
this.speed = s;
this.xPos = x;
this.yPos = y;
}
void display(){
int wid = this.word.length() * 20;
fill(255);
noStroke();
ellipse(this.xPos, this.yPos, wid, 40);
textSize(30);
fill(0);
text(this.word, this.xPos, this.yPos);
}
void move(){
this.yPos = this.yPos + this.speed;
}
String getWord(){
return this.word;
}
}