Help with programming with objects
in
Programming Questions
•
2 months ago
I'm really new to programming in general, so please have patience.
I was able to make a simple game, it works but has no increase of level difficulty. I thought that without objects it would be really complicated to do it, so I started a new version of the same game.
I read the tutorial and used it as base to write my code,
but I'm having difficulties using class.
The idea of the game is that the player controls a blue ball with the mouse and must dodge the incoming red balls. If he is hit is game over, but if he dodges and the red ball reaches the end of the screen he gains a point. As the score rises more red balls appear and/or their speed increase.
I added the class Player and Enemy and they work fine so far, still no collision check, but they spawn correctly. My first problem is with the score. My enemy class is like this at the moment:
- class Enemy
- {
- color c;
- float xpos, ypos, xspeed;
- Enemy(color tempC, float tempXpos, float tempYpos, float tempXspeed)
- {
- c = tempC;
- xpos = tempXpos;
- ypos = tempYpos;
- xspeed = tempXspeed;
- }
- void display()
- {
- fill(c);
- ellipse(xpos, ypos, 80, 80);
- }
- void drive()
- {
- xpos = xpos - xspeed;
- if (xpos == 0)
- {
- ypos = random(1200)/2;
- xpos = 1000;
- }
- }
- }
I also created a class for the Score, but I'm unsure on how to make it work. On the first version of the game, the score was inside the void enemy, like this:
- if (xpos == 0)
- {
- score = score + 1;
- ypos = int (random (600));
- xpos = 1000;
- }
and with this line on the void draw:
- text("Score = " + score, 20, 40);
I tried adding the score inside the class Enemy but it was printing some other numbers and not the sum of all the red balls that reached the end of the screen. How should I do it? Can I make it a global term that can affect and be affected by any class?
Would it work with a boolean that turns true when the enemy xpos=0 and then check it on the Score class and +1 if it is true? If so, how do I use terms from another class?
sorry for the noob questions and thanks for the help
1