Endstate and restart for small game
in
Programming Questions
•
2 years ago
I have a problem with my first processing project, which is a small game. Try it out yourself, its somewhat working right now, but Im missing some features im not sure how to add, specifically:
1) How do I go about making and endstate gameover when boxes reach screen width and have it show score, plus press spacebar to start over?
2) How do I fix it so the mouse cannot be held down and just moved over the objects but you have to actively click each object?
I know width/height of the sketch are kind of wierd, but its values determined by our teacher, not of my own choosing.
1) How do I go about making and endstate gameover when boxes reach screen width and have it show score, plus press spacebar to start over?
2) How do I fix it so the mouse cannot be held down and just moved over the objects but you have to actively click each object?
I know width/height of the sketch are kind of wierd, but its values determined by our teacher, not of my own choosing.
- // Declare four Circle objects as global variables
Circle myCircle1;
Circle myCircle2;
Circle myCircle3;
Circle myCircle4;
// Declare eight Box objects as global variables
Box myBox1;
Box myBox2;
Box myBox3;
Box myBox4;
Box myBox5;
Box myBox6;
Box myBox7;
Box myBox8;
void setup() {
size(800,100); // Sketch x and y size
frameRate(60); // Sketch framerate
// Initialize Circle objects by calling the Circle constructor
myCircle1 = new Circle(color(255,0,0),random(100,700),random(10,90),2,2);
myCircle2 = new Circle(color(255,80,0),random(100,700),random(10,90),2,2);
myCircle3 = new Circle(color(255,160,0),random(100,700),random(10,90),2,2);
myCircle4 = new Circle(color(255,240,0),random(100,700),random(10,90),2,2);
// Initialize Box objects by calling the Box constructor
myBox1 = new Box(color(255,40,0),random(5,10),random(15,85),15,15);
myBox2 = new Box(color(255,120,0),random(5,10),random(15,85),15,15);
myBox3 = new Box(color(255,200,0),random(5,10),random(15,85),15,15);
myBox4 = new Box(color(255,40,0),random(5,10),random(15,85),15,15);
myBox5 = new Box(color(255,200,0),random(5,10),random(15,85),15,15);
myBox6 = new Box(color(255,120,0),random(5,10),random(15,85),15,15);
myBox7 = new Box(color(255,40,0),random(5,10),random(15,85),15,15);
myBox8 = new Box(color(255,200,0),random(5,10),random(15,85),15,15);
}
void draw() {
cursor(CROSS); // Replace the normal cursor with a cross
background(0,0,0); // Set background to black
fill(0,255,0); // Make the score count green
text("Score:",720,20); // Place score text in upper right corner
text(score,760,20); // Place score integer next to score text in upper right corner
// Draw green text in upper left corner after 0.5 seconds
// After eight seconds make the text go away again
if (millis() > 500 && millis() < 8000){
fill(0,255,0);
text("Shoot the circles before they grow too big!",10,20);
}
// Draw green text in the upper left corner after 9.5 seconds
// After 17 seconds make the text go away again
if (millis() > 9500 && millis() < 17000){
fill(0,255,0);
text("Shoot the boxes before they reach the other side!",10,20);
}
// Spawm the boxes and make them move
// Each Box Object calls the object method using dot syntax
// Interval between each box spawn is 10 seconds
// Done by checking how long since applet started
float boxm = millis();
if (boxm > 10000) {
myBox1.boxmove();
myBox1.boxdisplay();
}
if (boxm > 20000) {
myBox2.boxmove();
myBox2.boxdisplay();
}
if (boxm > 30000) {
myBox3.boxmove();
myBox3.boxdisplay();
}
if (boxm > 40000) {
myBox4.boxmove();
myBox4.boxdisplay();
}
if (boxm > 50000) {
myBox5.boxmove();
myBox5.boxdisplay();
}
if (boxm > 60000) {
myBox6.boxmove();
myBox6.boxdisplay();
}
if (boxm > 70000) {
myBox7.boxmove();
myBox7.boxdisplay();
}
if (boxm > 80000) {
myBox8.boxmove();
myBox8.boxdisplay();
}
// Spawn circles and make them grow
// Each Circle Object calls the object method using dot syntax
// Time since applet start checked to make Circles spawn at specific time intervals
float m = millis();
if (m > 2500) {
myCircle1.grow();
myCircle1.display();
}
if (m > 5000) {
myCircle2.grow();
myCircle2.display();
}
if (m > 7500) {
myCircle3.grow();
myCircle3.display();
}
if (m > 10000) {
myCircle4.grow();
myCircle4.display();
}
if (mousePressed){
// Create variables for the distance between mouse position and the position of the circles and boxes with cool dox syntax
float distance1 = dist(mouseX, mouseY, myCircle1.xpos, myCircle1.ypos);
float distance2 = dist(mouseX, mouseY, myCircle2.xpos, myCircle2.ypos);
float distance3 = dist(mouseX, mouseY, myCircle3.xpos, myCircle3.ypos);
float distance4 = dist(mouseX, mouseY, myCircle4.xpos, myCircle4.ypos);
float bdist1 = dist(mouseX, mouseY, myBox1.xpos, myBox1.ypos);
float bdist2 = dist(mouseX, mouseY, myBox2.xpos, myBox2.ypos);
float bdist3 = dist(mouseX, mouseY, myBox3.xpos, myBox3.ypos);
float bdist4 = dist(mouseX, mouseY, myBox4.xpos, myBox4.ypos);
float bdist5 = dist(mouseX, mouseY, myBox5.xpos, myBox5.ypos);
float bdist6 = dist(mouseX, mouseY, myBox6.xpos, myBox6.ypos);
float bdist7 = dist(mouseX, mouseY, myBox7.xpos, myBox7.ypos);
float bdist8 = dist(mouseX, mouseY, myBox8.xpos, myBox8.ypos);
// If mouse is within the boxes when clicked reset the boxes to their original values in void Setup and add 1 to score integer
if (bdist1 < myBox1.xsize/2){
myBox1 = new Box(color(255,40,0),random(5,10),random(15,85),15,15);
score = score + 1;
}
if (bdist2 < myBox2.xsize/2){
myBox2 = new Box(color(255,40,0),random(5,10),random(15,85),15,15);
score = score + 1;
}
if (bdist3 < myBox3.xsize/2){
myBox3 = new Box(color(255,40,0),random(5,10),random(15,85),15,15);
score = score + 1;
}
if (bdist4 < myBox4.xsize/2){
myBox4 = new Box(color(255,40,0),random(5,10),random(15,85),15,15);
score = score + 1;
}
if (bdist5 < myBox5.xsize/2){
myBox5 = new Box(color(255,40,0),random(5,10),random(15,85),15,15);
score = score + 1;
}
if (bdist6 < myBox6.xsize/2){
myBox6 = new Box(color(255,40,0),random(5,10),random(15,85),15,15);
score = score + 1;
}
if (bdist7 < myBox7.xsize/2){
myBox7 = new Box(color(255,40,0),random(5,10),random(15,85),15,15);
score = score + 1;
}
if (bdist8 < myBox8.xsize/2){
myBox8 = new Box(color(255,40,0),random(5,10),random(15,85),15,15);
score = score + 1;
}
// If mouse is within the circles when clicked reset the circles to their original values in void Setup and add 1 to score integer
if (distance1 < myCircle1.xgrowth/2){
myCircle1 = new Circle(color(255,0,0),random(100,700),random(10,90),2,2);
score = score + 1;
}
if (distance2 < myCircle2.xgrowth/2){
myCircle2 = new Circle(color(255,80,0),random(100,700),random(10,90),2,2);
score = score + 1;
}
if (distance3 < myCircle3.xgrowth/2){
myCircle3 = new Circle(color(255,160,0),random(100,700),random(10,90),2,2);
score = score + 1;
}
if (distance4 < myCircle4.xgrowth/2){
myCircle4 = new Circle(color(255,240,0),random(100,700),random(10,90),2,2);
score = score + 1;
}
}
}
// Variable to keep count of the score initialized. Integer, not float, as we don't want decimal numbers displayed
int score = 0;
// Define the Circle class - Below the rest of the program - Note: need to put in new tab, keeps fucking it up though
class Circle{
// Variables for Circle class
color c;
float xpos;
float ypos;
float xgrowth;
float ygrowth;
// Constructor for the Circle class
Circle(color tempC, float tempXpos, float tempYpos, float tempXgrowth, float tempYgrowth) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
xgrowth = tempXgrowth;
ygrowth = tempYgrowth;
}
void display() {
// Ellipse function to actually draw the things
fill(c);
ellipse(xpos,ypos,xgrowth,ygrowth);
}
void grow() {
// Function to make the Circle grow and pop back to small size when it gets over 300 high as well as assign new random x and y values for respawn
xgrowth = xgrowth +0.5;
ygrowth = ygrowth +0.5;
if (xgrowth > 200) {
score = score -10;
xpos = random(100,700);
ypos = random(10,90);
xgrowth = 0.5;
ygrowth = 0.5;
}
}
}
// Define the Box class - Below the rest of the program - Note: need to put in new tab, keeps fucking it up though
class Box{
// Variables for Box class
color c;
float xpos;
float ypos;
float xsize;
float ysize;
// Constructor for the Box class
Box(color tempC, float tempXpos, float tempYpos, float tempXsize, float tempYsize) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
xsize = tempXsize;
ysize = tempYsize;
}
void boxdisplay() {
// Rect function to actually draw the things, rectmode center important so clicking outside the boxes won't work
fill(c);
rectMode(CENTER);
rect(xpos,ypos,xsize,ysize);
}
void boxmove() {
// Function to make the Box move and pop back to start when it goes beyond width as well as assign new random x and y values for respawn
xpos = xpos +0.5;
if (xpos > 800) {
xpos = random(5,10);
ypos = random(15,85);
xsize = 15;
ysize = 15;
}
}
}
1