Pause a game with an array of objects?
in
Programming Questions
•
7 months ago
Hi, guys! I'm making a game with an array of objects that are falling from the top of the screen and you need to catch it with your mouse. I've made a boolean paused and controlled it with a key press. Everything worked fine, but when I put a condition "if(!paused){//draw the rectangles", then paused and resumed the game: A big amount of overlapped rectangles started falling from the top of the screen. What should I do? Where to put the condition? Maybe there's something with the timer?
Here's a simplified version of that game without all the features and images:
- Head [] en;
- Head [] po;
- boolean gamePause = false;
- //Timer
- int t = 1000;
- float speed = 1;
- void setup() {
- en = new Head[500];
- po = new Head[500];
- size(700, 600);
- //Enemies
- for (int i = 0; i < en.length; i++) {
- en[i] = new Head();
- }
- for (int i = 0; i < po.length; i++) {
- po[i] = new Head();
- }
- }
- void draw() {
- background(0);
- redd();
- greenn();
- player();
- }
- void redd() {
- int i = 0;
- for (t=0; millis() > t; t=t+1950) {
- en[i].appear(2);
- i++;
- }
- }
- void greenn() {
- int i = 0;
- for (t=2000; millis() > t; t=t+2150) {
- po[i].appear(1);
- i++;
- }
- }
- void player() {
- rect(mouseX, mouseY, 56, 143);
- }
- void keyPressed() {
- gamePause = !gamePause;
- }
- //Class
- class Head {
- float yloc = -50;
- float xloc = random(100, 680);
- float sel;
- float switch1 = 1;
- void appear(float Tsel) {
- sel = Tsel;
- //Selector
- if (sel == 1) {
- fill(0, 255, 0);
- }
- else if (sel == 2) {
- fill(255, 0, 0);
- }
- if (!gamePause) {
- rect(xloc, yloc, 40, 57);
- yloc=yloc+speed;
- // 1 --- DAMAGE
- if (mouseY<yloc+40 && mouseY+90>yloc && sel == 2 && xloc+30>mouseX && xloc < mouseX+30 || switch1 == 0) {
- yloc++;
- rect(xloc, yloc, 40, 57);
- switch1 = 0;
- }
- // 1 --- FIX
- if (mouseY<yloc+40 && mouseY+90>yloc && sel == 1 && xloc+30>mouseX && xloc < mouseX+30 || switch1 == 0) {
- yloc+=2;
- rect(xloc, yloc, 40, 57);
- switch1 = 0;
- }
- }
- }
- }
1