Jump to content
  • Advertisement

si0x

Member
  • Content count

    0
  • Joined

  • Last visited

Community Reputation

102 Neutral

About si0x

  • Rank
    Newbie
  1. si0x

    Need help with creating AI

    Right now it doesnt work, and i want it to follow the ball, but the AI still has to be beatable.
  2. Hello, i need help with creating an AI for my Ping Pong game. I think i have a done a little bit of the AI but im a little stuck now. #ifndef AIPADDLE_HPP #define AIPADDLE_HPP #include "Paddle.h" #include <iostream> #include <SFML/Graphics.hpp> class AIPaddle : public sf::RectangleShape { public: AIPaddle(sf::Vector2f rect, sf::Color col, sf::Vector2f pos, sf::String texture); void Update(sf::RenderWindow &pWindow, sf::Time elapsed, float AIpad); void Init(); private: sf::Vector2f initPos; sf::Time tempElapsedTime; sf::Texture textur; float speed; int frame; int frames; int frameWidth; }; #endif #include "AIPaddle.h" #include "Game.h" #include "Ball.h" AIPaddle::AIPaddle(sf::Vector2f rect, sf::Color col, sf::Vector2f pos, sf::String texture) { setSize(rect); setFillColor(col); setOrigin(getSize().x / 2, getSize().y / 2); setPosition(pos); initPos = pos; speed = 1000.f; textur.loadFromFile(texture); textur.setSmooth(true); setTexture(&textur); setTextureRect(sf::IntRect(0, 0, 53, 192)); frame = 0; frames = 20; frameWidth = 53; } void AIPaddle::Init() { setPosition(initPos); } void AIPaddle::Update(sf::RenderWindow &pWindow, sf::Time elapsed,) { tempElapsedTime += elapsed; { if (tempElapsedTime.asSeconds() >= .05) { if (frame < frames - 1) { frame++; } else { frame = 0; } tempElapsedTime = tempElapsedTime.Zero; setTextureRect(sf::IntRect(frameWidth * frame, 0, frameWidth, 196)); } if (ball.getPosition().y < AIpad.getPosition().y) AIpad.move(sf::Vector2f(0.f, -speed * elapsed.asSeconds)); else if (ball.getPosition().y + Ball.size().y > AIpad.getPosition().y + AIpad.getSize().y) AIpad.move(sf::Vector2f(0.f, speed * elapsed.asSeconds)); } } #ifndef BALL_HPP #define BALL_HPP #include "Paddle.h" #include <memory> #include "AIPaddle.h" class Ball : public sf::CircleShape { public: Ball(float rad, sf::Color col, sf::Vector2f pos, std::shared_ptr<Paddle> pad1, std::shared_ptr<Paddle> pad2, std::shared_ptr<AIPaddle> AIpad); void Init(); void Update(sf::RenderWindow &pWindow, sf::Time elapsed); private: int direction; float speed; sf::Vector2f movement; sf::Texture textur; std::shared_ptr<Paddle> pPad1; std::shared_ptr<Paddle> pPad2; std::shared_ptr<AIPaddle> pAIPad; }; #endif #include "Ball.h" Ball::Ball(float rad, sf::Color col, sf::Vector2f pos, std::shared_ptr<Paddle> pad1, std::shared_ptr<Paddle> pad2, std::shared_ptr<AIPaddle> AIpad) { setRadius(rad); //setFillColor(col); setOrigin(getRadius() / 2, getRadius() / 2); setPosition(pos); pPad1 = pad1; pPad2 = pad2; pAIPad = AIpad; direction = 1; speed = 0.f; movement = sf::Vector2f(0.f, 0.f); textur.loadFromFile("assets\\gfx\\ball2.png"); textur.setSmooth(true); setTexture(&textur); } void Ball::Init() { speed = 500.f; direction = rand() % 2; if (direction == 0) movement = sf::Vector2f(speed, 0.f); else movement = sf::Vector2f(-speed, 0.f); setPosition(640.f, 310.f); } void Ball::Update(sf::RenderWindow &pWindow, sf::Time elapsed) { float bX = getPosition().x; float bY = getPosition().y; sf::Vector2f p1p = pPad1->getPosition(); sf::Vector2f p2p = pPad2->getPosition(); sf::Vector2f pAIp = pAIPad->getPosition(); //Fliegt de ball gegen die horizontalen grenzen if (bY <= 0) { movement.y = speed; } else if (bY >= pWindow.getSize().y) { movement.y = -speed; } //fliegt der ball gegen einen schläger //linker schläger if (getGlobalBounds().left < p1p.x + (pPad1->getSize().x / 2) - 8 && getGlobalBounds().left + getGlobalBounds().width > p1p.x - (pPad1->getSize().x / 2) && getGlobalBounds().top + getGlobalBounds().height >= p1p.y - pPad1->getSize().y / 2 && getGlobalBounds().top <= p1p.y + pPad1->getSize().y / 2) { //oben unten oder mitte? if (bY < p1p.y) { movement.x = speed; movement.y = -speed; speed = speed += 15; } else if (bY > p1p.y) { movement.x = speed; movement.y = speed; speed = speed += 15; } else { movement.x = speed; speed = speed += 15; } // rehter schläger } else if (getGlobalBounds().left < p2p.x + (pPad2->getSize().x / 2) && getGlobalBounds().left + getGlobalBounds().width > p2p.x - (pPad2->getSize().x / 2) + 8 && getGlobalBounds().top + getGlobalBounds().height >= p2p.y - pPad2->getSize().y / 2 && getGlobalBounds().top <= p2p.y + pPad2->getSize().y / 2) { //oben unten oder mitte? if (bY < p2p.y) { movement.x = -speed; movement.y = -speed; speed = speed += 15; } else if (bY > p2p.y) { movement.x = -speed; movement.y = speed; speed = speed += 15; } else { movement.x = -speed; speed = speed += 15; } if (getGlobalBounds().left < pAIp.x + (pAIPad->getSize().x / 2) - 8 && getGlobalBounds().left + getGlobalBounds().width > pAIp.x - (pAIPad->getSize().x / 2) && getGlobalBounds().top + getGlobalBounds().height >= pAIp.y - pAIPad->getSize().y / 2 && getGlobalBounds().top <= pAIp.y + pAIPad->getSize().y / 2) { if (bY < pAIp.y) { movement.x = -speed; movement.y = -speed; speed = speed += 15; } else if (bY > pAIp.y) { movement.x = -speed; movement.y = speed; speed = speed += 15; } else { movement.x = -speed; speed = speed += 15; } move(movement.x * elapsed.asSeconds(), movement.y * elapsed.asSeconds()); } }
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!