Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Need your help to solve some problems.  (Read 2862 times)

0 Members and 1 Guest are viewing this topic.

vovanx500

  • Newbie
  • *
  • Posts: 4
    • View Profile
Need your help to solve some problems.
« on: March 28, 2013, 02:12:50 pm »
Hello guys!
I am trying to apply to game development university to learn programming, but in order to get there I need to make a game. What a paradox.  They have a specific topic for the game called "bubbles".
Well I learned a bit of C++, and then I was playing with different Allegro and SFML , now I stopped on SFML.

My idea of the game is simple: You control a bubble, and there are other bubbles on the screen. If enemy bubble is smaller he'll try to escape from you, as you can eat him. If you eat him you will grow.
But if enemy bubble is bigger than you, he will try to eat you!

Well, I've managed to make bubbles to follow and run away from player, but I have problems which I do not know how to solve:

  • Small bubbles stuck in corners. (you can see one in top right corner)
    Bubbles which are following you after a while will be in one spot.
    Don't know how to make player to eat bubbles.
    Don't really know how to redraw bubbles after they died.
(basically I do know how to deal with collision)

Other problem that my string with variable does not updating( You can also see it in the video), I was trying to display player position in order to understand what's happening, but it does not updating.

Here is the video of my "game", so you can understan what I am talking about.



I will attach the code as a file, as I can't make the minimum code, because everything sucks in my code  ;D

I've read and watched a lot of tutorial, but guys, I need your help in order to solve my problems, which are most likely about collision. I was thinking about using box2d, but I guess my game is too simple for it.

[attachment deleted by admin]

vovanx500

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Need your help to solve some problems.
« Reply #1 on: April 02, 2013, 01:20:34 pm »
Ok, seems like no one wants to answer my question.
I have another one: How do I refresh things?
For example my text string does not want to update, and I want to have always for example 10 enemies in the screen, but ether they exist,ether they don't...
« Last Edit: April 02, 2013, 01:25:27 pm by vovanx500 »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Need your help to solve some problems.
« Reply #2 on: April 02, 2013, 02:15:30 pm »
Collision with circles is very simple: If the distance between two circles' centers is smaller than the sum of their radiuses, they collide.

To store the bubbles, you should use STL containers. If a bubble is eaten, you remove it from the container. You iterate through the container in order to update and draw each bubble.

As for uploaded files: Usually, nobody looks at them. If you request help, you should also be ready to minimize your code as much as possible. If you already know that your code sucks, why don't you clean it up?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Need your help to solve some problems.
« Reply #3 on: April 02, 2013, 02:22:38 pm »
Ok, seems like no one wants to answer my question.
I will attach the code as a file, as I can't make the minimum code, because everything sucks in my code  ;D
There's most likely the problem. We don't want to minimize your code, just because you didn't want to/felt lazy about it/thought it can't be done. It's always possible to minimize code to just the essential parts.
Besides that your problems are not really programming problems, but you're just having a hard time, getting the logic you have in mind in code and onto the screen. You should just sit down again, without PC, and think about what you're trying to do and how you're doing it and how you should be doing it. Programming is not an easy topic and if you're serious about it, then you need to be able to transform ideas into code. That's also why it's not a paradox, that one needs to hand in a game for attending a game development course. They don't require you to know everything and all, but they want to see, that you're comfortable with programming and the logic transformation involved with it. ;)

I don't really feel like going through your code to figure out where the problem could be, so I can't answer your opening questions.

I have another one: How do I refresh things?
For example my text string does not want to update, and I want to have always for example 10 enemies in the screen, but ether they exist,ether they don't...
I'm not quite sure what you mean with 'refresh'. The way SFML works, is that you draw every object, every single frame and if you want to 'refresh' an object you just update it's content/position/property and it will be 'refreshed' on the next frame. If you want something not to be drawn you simple don't draw it.
Here's a little example that should demonstrate things. If you press space the text will change. If you press enter the shape will be drawn, if you don't press Enter the shape won't be drawn.
sf::Text text;
text.setString("Hello");
sf::RectangleShape shape(sf::Vector2f(100.f, 100.f));
shape.setPositino(sf::Vector2f(200.f, 200.f));

while(window.isOpen())
{
    // Handle Events

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)
        text.setString("New string");

    window.clear();
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
        window.draw(shape);
    window.draw(text);
    window.display();
}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

vovanx500

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Need your help to solve some problems.
« Reply #4 on: April 02, 2013, 03:00:36 pm »
Thank you guys for advises.
Never thought of containers before, gonna google it now.
I've cleaned code as much as possible.
For now my collision is based on x, and y coordinates, it works sometimes.
But now I need to solve one problem: when bubble is eaten, the new bubble should be drawn, so there are always 10 bubbles on the screen.  I guess that container thing gonna help.


#include <SFML/Graphics.hpp>
#include "objects.h"
#include <stdlib.h>
#include <math.h>
#include <Thor/Math.hpp>



//Globals
sf::VideoMode mode = sf::VideoMode::getDesktopMode(); // Get Your Desktop resolution
int Score;
int speed = 15;
const int NUM_ENEMIES = 10;


int main()
{       float timer;

//Playerdata
        Player p;
        p.x = mode.width / 2;
        p.y = -mode.height / 2;
        p.speed = speed + 10;
        p.scale = 1.5;
        p.SWidth = mode.width;
        p.SHeight = mode.height;
//EnemyData
        AngryBubble enemy[10];
        float a, b;
        a = 0.2;
        b = 3.5;
        for( int i = 0; i < NUM_ENEMIES; i++)
        {
                enemy[i].position.x = thor::random(0,mode.width);
                enemy[i].position.y = thor::random(0,mode.height);
                enemy[i].speed = 3;
                enemy[i].scale = thor::random(a, b);
                enemy[i].angry = false;
                enemy[i].alive = false;

       


               
        }
       
        //Open window with your display resolution
    sf::RenderWindow window(sf::VideoMode(mode.width,mode.height), "Bubble!");
        sf::Clock clock;
        p.DrawPlayer();
        for( int i = 0; i < NUM_ENEMIES; i++)
        { if (!enemy[i].alive) // need to change later
        enemy[i].alive = true; 
        enemy[i].DrawEnemy();
               
        }
       
    while (window.isOpen())
    {
               
        sf::Event event;
                sf::Keyboard keyevent;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
       }
                 
                //Controlls
                p.PlayerMove();

                //Enemy logic
               
                //     Movement
               
                for( int i = 0; i < NUM_ENEMIES; i++)  //Angry Enemies ( if enemy is bigger than you it going to angry and it'll try to eat you...)
                { if(enemy[i].enemy.getScale().x > p.PlayerBubble.getScale().x)
                {
               
                if(p.PlayerBubble.getPosition().y < enemy[i].enemy.getPosition().y)
                {
                        enemy[i].enemy.move(0, -enemy[i].speed);
                        if(enemy[i].enemy.getPosition().y < 0) // Enemy won't go off the screen
                                enemy[i].enemy.setPosition(enemy[i].enemy.getPosition().x, 0);
                                       
                }
                if(p.PlayerBubble.getPosition().y > enemy[i].enemy.getPosition().y)
                {
                        enemy[i].enemy.move(0, enemy[i].speed);
                        if(enemy[i].enemy.getPosition().y > mode.height)
                                enemy[i].enemy.setPosition(enemy[i].enemy.getPosition().x, mode.height);
                       
                }
                if(p.PlayerBubble.getPosition().x > enemy[i].enemy.getPosition().x)
                {
                        enemy[i].enemy.move(enemy[i].speed, 0);
                        if(enemy[i].enemy.getPosition().x > mode.width)
                                enemy[i].enemy.setPosition(mode.width, enemy[i].enemy.getPosition().y);
                                       
                }
                        if(p.PlayerBubble.getPosition().x < enemy[i].enemy.getPosition().x)
                {
                        enemy[i].enemy.move(-enemy[i].speed, 0);
                if(enemy[i].enemy.getPosition().x < 0)
                                enemy[i].enemy.setPosition(0, enemy[i].enemy.getPosition().y);
                if(enemy[i].enemy.getPosition().x == p.PlayerBubble.getPosition().x || enemy[i].enemy.getPosition().y == p.PlayerBubble.getPosition().y)
                        { enemy[i].alive = false;
                p.PlayerBubble.setScale(p.scale - 1, p.scale - 1);
                }
                                       
                }
                }
                }

               

                 for( int i = 0; i < NUM_ENEMIES; i++)  //Non-Angry Enemies ( if enemy is smaller than you it going to run away from you...)
                { if(enemy[i].enemy.getScale().x < p.PlayerBubble.getScale().x) {
               
                if(p.PlayerBubble.getPosition().y > enemy[i].enemy.getPosition().y)
                {
                        enemy[i].enemy.move(0, -enemy[i].speed);
                        if(enemy[i].enemy.getPosition().y < 0)
                                enemy[i].enemy.setPosition(enemy[i].enemy.getPosition().x, 0);
                                       
                }
                if(p.PlayerBubble.getPosition().y < enemy[i].enemy.getPosition().y)
                {
                        enemy[i].enemy.move(0, enemy[i].speed);
                        if(enemy[i].enemy.getPosition().y > mode.height)
                                enemy[i].enemy.setPosition(enemy[i].enemy.getPosition().x, mode.height);
                       
                }
                if(p.PlayerBubble.getPosition().x < enemy[i].enemy.getPosition().x)
                {
                        enemy[i].enemy.move(enemy[i].speed, 0);
                        if(enemy[i].enemy.getPosition().x > mode.width)
                                enemy[i].enemy.setPosition(mode.width, enemy[i].enemy.getPosition().y);
                                       
                }
                        if(p.PlayerBubble.getPosition().x > enemy[i].enemy.getPosition().x)
                {
                        enemy[i].enemy.move(-enemy[i].speed, 0);
                if(enemy[i].enemy.getPosition().x < 0)
                                enemy[i].enemy.setPosition(0, enemy[i].enemy.getPosition().y);
                if(enemy[i].enemy.getPosition().x == p.PlayerBubble.getPosition().x || enemy[i].enemy.getPosition().y == p.PlayerBubble.getPosition().y)
                        { enemy[i].alive = false;
                p.PlayerBubble.setScale(p.scale + 1, p.scale + 1);
                }
                                       
                        }
                       
                 } }
                 
               
               

                window.clear(sf::Color::Blue);
                window.setVerticalSyncEnabled(true); //For Smooth animation
                timer = clock.getElapsedTime().asMilliseconds();
                clock.restart();
                window.draw(p.PlayerBubble);
               
                for( int i = 0; i < NUM_ENEMIES; i++)
                { if(enemy[i].alive)
                        window.draw(enemy[i].enemy);
                }
                window.display();
    }

    return 0;
}


And here what I mean by refresh:
 
        sf:: Font MyFont;
        MyFont.loadFromFile("font.ttf");
        sf::Text myText;


       
        myText.setFont(MyFont);
        myText.setCharacterSize(30);
std::string s;
std::stringstream out;
out << p.PlayerBubble.getPosition().x << " : " <<  p.PlayerBubble.getPosition().y;

        myText.setString(out.str());
        myText.setPosition( mode.width - 300, mode.height -100); // y = close to 0
        myText.setColor(sf::Color::Black);
 
I for example want to display current x and y coordinates of player, but they are not updating.
I hope now my explanation is a bit better.
« Last Edit: April 02, 2013, 03:11:24 pm by vovanx500 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Need your help to solve some problems.
« Reply #5 on: April 02, 2013, 03:57:20 pm »
And here what I mean by refresh:
I for example want to display current x and y coordinates of player, but they are not updating.
I hope now my explanation is a bit better.
C++ is not a functional programming language, thus if you want to change the x/y position, you'll have to convert the ints to string and set the string to the sf::Text every frame iteration. You can't set it once and then figure that it will update automatically. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

vovanx500

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Need your help to solve some problems.
« Reply #6 on: April 06, 2013, 12:37:41 am »
Hello guys, it's me again :)
I have more questions.
First one is about collision:
here is the code:
               
enemy[i].radius = (enemy[i].enemy.getGlobalBounds().width + enemy[i].enemy.getGlobalBounds().height) / 4;
enemy[i].xd = enemy[i].enemy.getPosition().x - p.PlayerBubble.getPosition().x;
enemy[i].yd = enemy[i].enemy.getPosition().y - p.PlayerBubble.getPosition().y;
enemy[i].distance = sqrt(enemy[i].xd * enemy[i].xd + enemy[i].yd * enemy[i].yd);

//and this part
if(enemy[i].distance <= (p.radius + enemy[i].radius))
{ enemy[i].alive = false;
p.PlayerBubble.setScale(p.scale + 1, p.scale + 1);
 

It works, but if I go under group of  enemy-bubbles, it seems like collision is happening. even if it does not.

Other problem is about redrawing enemies.
if I put this code outsie of my game loop:
       
for( int i = 0; i < NUM_ENEMIES; i++)
{ if (!enemy[i].alive) // need to change later 
enemy[i].alive = true;
enemy[i].DrawEnemy(); }
Works fine, but enemies are not redrawing.
If I am putting this inside my main game loop, redrawing is working, but bubbles are not moving at all, they just standing there, until I collide with them and then they appear on the different position.