Help with a game appreciated. Creating multiple objects with different parameters.
in
Programming Questions
•
5 months ago
Hi,
I am writing a game which is a clone of Russian game from 80's called 'nu pagadi'. It's about a wolf which is catching eggs rolling out of four shelves, two on the each side of the screen. I have crated Egg class and four instances of egg object one for each shelf. Each egg should roll to the end of the shelf' and then, regarding if wolf is turned to the correct shelf egg should fall into the basket or on the ground. I have a problem with removing the egg from the screen, once it has reached end of the shelf, I have tried with assigning null to the class instance but this causes an error. Also how can I have more than one egg rolling on the same shelf. Also eggs cannot appear at the same time at more than on one of the shelves because it will be impossible to catch them. I would be gratefull for any help or hints how can I solve that problem.
Aleksander
Here is my code:
int a=250;
int b=250;
int c=50;
int d=100;
char letter;
Egg eggLeftUp = new Egg ( 100.0, 100.0, 10, 14);
Egg eggLeftDown = new Egg(100.0, 200.0, 10, 14);
Egg eggRightUp = new Egg(500.0, 100.0, 10, 14);
Egg eggRightDown = new Egg(500.0, 200.0, 10, 14);
void setup() {
size(600, 400);
int n = 5; //number of possible eggs on the screen
}
void draw() {
smooth();
frameRate(24);
background(100);
line(100, 100, 200, 150);
line(100, 200, 200, 250);
line(400, 150, 500, 100);
line(400, 250, 500, 200);
rect(a, b, c, d);
int i;
int n = 5;
eggLeftUp.DrawEGG();
eggLeftDown.DrawEGG();
eggRightUp.DrawEGG();
eggRightDown.DrawEGG();
eggLeftUp.MoveEggLeftUP();
eggLeftDown.MoveEggLeftDown();
eggRightUp.MoveEggRightUp();
eggRightDown.MoveEggRightDown();
keyPressed();
moveChar();
}
class Egg {
float eggX, eggY;
int eggW, eggH;
Egg ( float eggX, float eggY, int eggW, int eggH) {
this.eggX = eggX;
this.eggY = eggY;
this.eggW = eggW;
this.eggH = eggH;
}
void MoveEggLeftUP() {
eggX = eggX +0.6;
eggY = eggY+0.3;
if (eggX >200.0){
eggX= 100;
eggY= 100;
}
}
void MoveEggLeftDown() {
eggX = eggX +0.6;
eggY = eggY+0.3;
if (eggX >200.0){
eggX = 100;
eggY = 200;
}
}
void MoveEggRightUp() {
eggX = eggX - 0.6;
eggY = eggY + 0.3;
if (eggX < 400){
eggX = 500;
eggY = 100;
}
}
void MoveEggRightDown() {
eggX = eggX - 0.6;
eggY = eggY + 0.3;
if( eggX < 400){
eggX = 500;
eggY = 200;
}
}
void DrawEGG() {
ellipse(eggX, eggY, eggW, eggH);
}
}
void keyPressed() {
if (key == 'Q' || key == 'q') {
letter = 'q';
print (letter);
}
else if (key =='A' || key =='a') {
letter = 'a';
print (letter);
}
else if (key =='P' || key =='p') {
letter = 'p';
print (letter);
}
else if (key == 'L' || key =='l') {
letter = 'l';
print (letter);
}
}
void moveChar() {
switch(letter) {
case 'q':
a=250;
b=150;
c=50;
d=100;
break;
case 'a':
a=250;
b=200;
c=50;
d=100;
break;
case 'p':
a=300;
b=150;
c=50;
d=100;
break;
case 'l':
a=300;
b=200;
c=50;
d=100;
break;
}
}
I am writing a game which is a clone of Russian game from 80's called 'nu pagadi'. It's about a wolf which is catching eggs rolling out of four shelves, two on the each side of the screen. I have crated Egg class and four instances of egg object one for each shelf. Each egg should roll to the end of the shelf' and then, regarding if wolf is turned to the correct shelf egg should fall into the basket or on the ground. I have a problem with removing the egg from the screen, once it has reached end of the shelf, I have tried with assigning null to the class instance but this causes an error. Also how can I have more than one egg rolling on the same shelf. Also eggs cannot appear at the same time at more than on one of the shelves because it will be impossible to catch them. I would be gratefull for any help or hints how can I solve that problem.
Aleksander
Here is my code:
int a=250;
int b=250;
int c=50;
int d=100;
char letter;
Egg eggLeftUp = new Egg ( 100.0, 100.0, 10, 14);
Egg eggLeftDown = new Egg(100.0, 200.0, 10, 14);
Egg eggRightUp = new Egg(500.0, 100.0, 10, 14);
Egg eggRightDown = new Egg(500.0, 200.0, 10, 14);
void setup() {
size(600, 400);
int n = 5; //number of possible eggs on the screen
}
void draw() {
smooth();
frameRate(24);
background(100);
line(100, 100, 200, 150);
line(100, 200, 200, 250);
line(400, 150, 500, 100);
line(400, 250, 500, 200);
rect(a, b, c, d);
int i;
int n = 5;
eggLeftUp.DrawEGG();
eggLeftDown.DrawEGG();
eggRightUp.DrawEGG();
eggRightDown.DrawEGG();
eggLeftUp.MoveEggLeftUP();
eggLeftDown.MoveEggLeftDown();
eggRightUp.MoveEggRightUp();
eggRightDown.MoveEggRightDown();
keyPressed();
moveChar();
}
class Egg {
float eggX, eggY;
int eggW, eggH;
Egg ( float eggX, float eggY, int eggW, int eggH) {
this.eggX = eggX;
this.eggY = eggY;
this.eggW = eggW;
this.eggH = eggH;
}
void MoveEggLeftUP() {
eggX = eggX +0.6;
eggY = eggY+0.3;
if (eggX >200.0){
eggX= 100;
eggY= 100;
}
}
void MoveEggLeftDown() {
eggX = eggX +0.6;
eggY = eggY+0.3;
if (eggX >200.0){
eggX = 100;
eggY = 200;
}
}
void MoveEggRightUp() {
eggX = eggX - 0.6;
eggY = eggY + 0.3;
if (eggX < 400){
eggX = 500;
eggY = 100;
}
}
void MoveEggRightDown() {
eggX = eggX - 0.6;
eggY = eggY + 0.3;
if( eggX < 400){
eggX = 500;
eggY = 200;
}
}
void DrawEGG() {
ellipse(eggX, eggY, eggW, eggH);
}
}
void keyPressed() {
if (key == 'Q' || key == 'q') {
letter = 'q';
print (letter);
}
else if (key =='A' || key =='a') {
letter = 'a';
print (letter);
}
else if (key =='P' || key =='p') {
letter = 'p';
print (letter);
}
else if (key == 'L' || key =='l') {
letter = 'l';
print (letter);
}
}
void moveChar() {
switch(letter) {
case 'q':
a=250;
b=150;
c=50;
d=100;
break;
case 'a':
a=250;
b=200;
c=50;
d=100;
break;
case 'p':
a=300;
b=150;
c=50;
d=100;
break;
case 'l':
a=300;
b=200;
c=50;
d=100;
break;
}
}
1