انجمن‌های فارسی اوبونتو

لطفاً به انجمن‌ها وارد شده و یا جهت ورود ثبت‌نام نمائید

لطفاً جهت ورود نام کاربری و رمز عبورتان را وارد نمائید


ارائه ۲۵٫۰۴ اوبونتو منتشر شد 🎉

نویسنده موضوع: بازی astroid  (دفعات بازدید: 228 بار)

0 کاربر و 1 مهمان درحال مشاهده موضوع.

آفلاین learner:~$

  • Hero Member
  • *
  • ارسال: 828
  • جنسیت : پسر
  • روزی روز گاری در گنو/لینوکس
بازی astroid
« : 16 تیر 1404، 05:11 ب‌ظ »
درود دوستان!!!

من بازی پیتزا خور فضایی جادی رو یک بار با سی پلاس پلاس نوشتم ( خیلی خیلی کثیف نوشتم ) و الان سعی کردم یک بازی جدبد رو بنویسم.
اول از همه؛خوشحال میشم که جادی یک ویدیو درمورد ساخت این بازی با هر زبانی که دوست داره درست کنه.
خب بگذریم. من این بازی رو با کتابخانه sfml  دارم مینویسم. همه دارن با raylib  کار میکنند اما خب من با sfml  چه میشه کرد :)

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <math.h>
using namespace sf;
struct Bullet
{
        CircleShape shape;
        Vector2f    velocity;
};
class astroid
{
      protected:
        Vector2f  pos;
        VideoMode desktop;
        FloatRect bunds;

      public:
        astroid(VideoMode desk) : desktop{desk} {};
        virtual void  Input(ConvexShape* shape, RenderWindow* win, int& playing);
        RenderWindow* init_window();

        ConvexShape* player_ship(const RenderWindow& win);
};
ConvexShape* astroid::player_ship(const RenderWindow& win)
{

        Vector2u     size = win.getSize();
        float        x    = static_cast<float>(size.x) / 2.f;
        float        y    = static_cast<float>(size.y) / 2.f;
        ConvexShape* ship = new ConvexShape;
        ship->setPointCount(3);
        ship->setPoint(0, {0.f, -7.f});
        ship->setPoint(1, {-2.5f, 4.f});
        ship->setPoint(2, {2.5f, 4.f});
        ship->setFillColor(Color::Transparent);
        ship->setOutlineThickness(1.7f);
        ship->setOutlineColor(Color::White);
        FloatRect local = ship->getLocalBounds();
        ship->setOrigin(local.width / 2.f, local.height / 2.f);

        ship->setPosition(x, y);
        return ship;
}
RenderWindow* astroid::init_window()
{

        RenderWindow* window = new RenderWindow;
        window->create(desktop, "astroid", Style::Default);
        return window;
}
void astroid::Input(ConvexShape* shape, RenderWindow* win, int& playing)
{
        Vector2u size = win->getSize();
        float    winw = static_cast<float>(size.x);
        float    winh = static_cast<float>(size.y);
        pos           = shape->getPosition();
        float speed   = 0.4f;
        bunds         = shape->getGlobalBounds();
        if (Keyboard::isKeyPressed(Keyboard::Right) && bunds.left + bunds.width < winw)
                shape->move(speed, 0.f);
        if (Keyboard::isKeyPressed(Keyboard::Left) && bunds.left > 0)
                shape->move(-speed, 0.f);
        if (Keyboard::isKeyPressed(Keyboard::Up) && bunds.top > 0)
                shape->move(0.f, -speed);
        if (Keyboard::isKeyPressed(Keyboard::Down) && bunds.top + bunds.height < winh)
                shape->move(0.f, speed);
        if (Keyboard::isKeyPressed(Keyboard::Escape))
        {
                playing = false;
        }
}
class rotation : public astroid
{
      protected:
        std::vector<Bullet> bullets;
        Clock               fireCooldown;
        float               firedelay = 0.3f;

      public:
        std::vector<Bullet>& getBullets() { return bullets; }

        using astroid::astroid;
        virtual void Input(ConvexShape* shape, RenderWindow* win, int& playing) override
        {

                astroid::Input(shape, win, playing);
                if (Keyboard::isKeyPressed(Keyboard::Space) && Keyboard::isKeyPressed(Keyboard::Up))
                        shape->rotate(1.f);
                if (Keyboard::isKeyPressed(Keyboard::Space) &&
                    Keyboard::isKeyPressed(Keyboard::Left))
                        shape->rotate(-1.f);

                if (Keyboard::isKeyPressed(Keyboard::Space) &&
                    Keyboard::isKeyPressed(Keyboard::Right))
                        shape->rotate(1.f);
                if (Keyboard::isKeyPressed(Keyboard::Space) &&
                    Keyboard::isKeyPressed(Keyboard::Down))
                        shape->rotate(-1.f);
                if (Keyboard::isKeyPressed(Keyboard::F) &&
                    fireCooldown.getElapsedTime().asSeconds() > firedelay)
                {
                        Bullet bullet;
                        bullet.shape = CircleShape(2.f);
                        bullet.shape.setOrigin(bullet.shape.getRadius(), bullet.shape.getRadius());
                        bullet.shape.setFillColor(Color::Red);

                        Transform trans = shape->getTransform();
                        Vector2f  tip   = trans.transformPoint(shape->getPoint(0));
                        bullet.shape.setPosition(tip);
                        Vector2f dir = trans.transformPoint(shape->getPoint(0)) -
                                       trans.transformPoint(shape->getPoint(1));
                        float    length = std::sqrt(dir.x * dir.x + dir.y * dir.y);
                        Vector2f norm   = dir / length;

                        bullet.velocity = norm * 1.5f;

                        bullets.push_back(bullet);
                        fireCooldown.restart();
                }
                for (auto& b : bullets)
                {
                        b.shape.move(b.velocity);
                }
        }
};
class sound : protected rotation
{
      protected:
        SoundBuffer buffer;
        Sound       sound;

      public:
        void voice()
        {
                if (!buffer.loadFromFile("/home/learner/Downloads/sound"))
                        std::cerr << "no soundEfect" << std::endl;
                std::exit(-1);
                sound.setBuffer(buffer);
        }
};

int main()
{
        VideoMode desktop = VideoMode::getDesktopMode();

        astroid*      world   = new rotation(desktop);
        RenderWindow* window  = world->init_window();
        ConvexShape*  ship    = world->player_ship(*window);
        int           playing = true;
        while (window->isOpen() && playing)
        {
                Event event;
                while (window->pollEvent(event))
                {
                        if (event.type == Event::Closed)
                                window->close();
                }
                window->clear(Color::Black);
                window->draw(*ship);
                if (rotation* r = dynamic_cast<rotation*>(world))
                {
                        for (auto& b : r->getBullets())
                                window->draw(b.shape);
                }
                world->Input(ship, window, playing);
                window->display();
        }

        delete window;
        delete ship;
        delete world;
        return 0;
}


g++ main.cpp -o game -lsfml-graphics -lsfml-window -lsfml-system


قرار بود ظهر بخش صدا رو تکمیل کنم اما خب نت یاری نمیکرد صدایی رو دانلود و استفاده کنم.پر از ایراده؟!‌ تقریبا میدونم!!تازه دارم مفهوم شی گرایی رو یاد میگیرم. و تقریبا چیز تمیزی دراومده. البته خیلی کار داره. الان که فکر میکنم میتونستم برنامه رو ارتقا بدم اما فعلا فقط میخوام حریصانه این بازی رو درست کنم.
الان چندتا بخش مونده:
۱:صدای شلیک و برخورد.
۲: موانع
۳: امتیاز دهی
با کلید esc  از بازی خارج میشه.  با  arrow keys  ها حرکت میکنه  و با اسپیس میچرخه (‌ حس میکنم باگ داره شما چطور؟  )‌  و با  f  شلیک میکنه.
یکی از کار هایی که میخوام بکنم اینه که وقتی برنامه کامل شد از اشاره گر های هوشمند استفاده کنم. فعلا نمیتونم استفاده کنم چون نمیدونم چند چندم با خودم و برنامم  ](*,)
خوشحال میشم اگر ایرادی دیدی  بهم بگید!! :)
مثل سمی کالونم( ; ).
خواستار پایان.
محکوم به کامپایل.

آفلاین learner:~$

  • Hero Member
  • *
  • ارسال: 828
  • جنسیت : پسر
  • روزی روز گاری در گنو/لینوکس
پاسخ : بازی astroid
« پاسخ #1 : 16 تیر 1404، 06:37 ب‌ظ »
خب بخش صدا رو تکمیل کردم!!!!!!!! بالاخره فیلترچی پلید یکم رفت هوا بخوره  :D

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <math.h>
using namespace sf;
struct Bullet
{
        CircleShape shape;
        Vector2f    velocity;
};
class Soundmanager
{
      protected:
        SoundBuffer shootBuffer;
        Sound       shootSound;

      public:
        void Shootsound(const std::string& full_path)
        {
                if (!shootBuffer.loadFromFile(full_path))
                {
                        std::cerr << "no such file!" << std::endl;
                        std::exit(-1);
                }
                shootSound.setBuffer(shootBuffer);
        }
        void Play() { shootSound.play(); }
};

class astroid
{
      protected:
        Vector2f  pos;
        VideoMode desktop;
        FloatRect bunds;

      public:
        astroid(VideoMode desk) : desktop{desk} {};
        virtual void  Input(ConvexShape* shape, RenderWindow* win, int& playing);
        RenderWindow* init_window();

        ConvexShape* player_ship(const RenderWindow& win);
};
ConvexShape* astroid::player_ship(const RenderWindow& win)
{

        Vector2u     size = win.getSize();
        float        x    = static_cast<float>(size.x) / 2.f;
        float        y    = static_cast<float>(size.y) / 2.f;
        ConvexShape* ship = new ConvexShape;
        ship->setPointCount(3);
        ship->setPoint(0, {0.f, -7.f});
        ship->setPoint(1, {-2.5f, 4.f});
        ship->setPoint(2, {2.5f, 4.f});
        ship->setFillColor(Color::Transparent);
        ship->setOutlineThickness(1.7f);
        ship->setOutlineColor(Color::White);
        FloatRect local = ship->getLocalBounds();
        ship->setOrigin(local.width / 2.f, local.height / 2.f);

        ship->setPosition(x, y);
        return ship;
}
RenderWindow* astroid::init_window()
{

        RenderWindow* window = new RenderWindow;
        window->create(desktop, "astroid", Style::Default);
        return window;
}
void astroid::Input(ConvexShape* shape, RenderWindow* win, int& playing)
{
        Vector2u size = win->getSize();
        float    winw = static_cast<float>(size.x);
        float    winh = static_cast<float>(size.y);
        pos           = shape->getPosition();
        float speed   = 0.4f;
        bunds         = shape->getGlobalBounds();
        if (Keyboard::isKeyPressed(Keyboard::Right) && bunds.left + bunds.width < winw)
                shape->move(speed, 0.f);
        if (Keyboard::isKeyPressed(Keyboard::Left) && bunds.left > 0)
                shape->move(-speed, 0.f);
        if (Keyboard::isKeyPressed(Keyboard::Up) && bunds.top > 0)
                shape->move(0.f, -speed);
        if (Keyboard::isKeyPressed(Keyboard::Down) && bunds.top + bunds.height < winh)
                shape->move(0.f, speed);
        if (Keyboard::isKeyPressed(Keyboard::Escape))
        {
                playing = false;
        }
}
class rotation : public astroid
{
      protected:
        std::vector<Bullet> bullets;
        Clock               fireCooldown;
        float               firedelay = 0.3f;
        Soundmanager*       sounds;

      public:
        std::vector<Bullet>& getBullets() { return bullets; }
        rotation(VideoMode desk, Soundmanager* sm) : astroid(desk), sounds(sm){};
        using astroid::astroid;
        virtual void Input(ConvexShape* shape, RenderWindow* win, int& playing) override
        {

                astroid::Input(shape, win, playing);
                if (Keyboard::isKeyPressed(Keyboard::Space) && Keyboard::isKeyPressed(Keyboard::Up))
                        shape->rotate(1.f);
                if (Keyboard::isKeyPressed(Keyboard::Space) &&
                    Keyboard::isKeyPressed(Keyboard::Left))
                        shape->rotate(-1.f);

                if (Keyboard::isKeyPressed(Keyboard::Space) &&
                    Keyboard::isKeyPressed(Keyboard::Right))
                        shape->rotate(1.f);
                if (Keyboard::isKeyPressed(Keyboard::Space) &&
                    Keyboard::isKeyPressed(Keyboard::Down))
                        shape->rotate(-1.f);
                if (Keyboard::isKeyPressed(Keyboard::F) &&
                    fireCooldown.getElapsedTime().asSeconds() > firedelay)
                {
                        Bullet bullet;
                        bullet.shape = CircleShape(2.f);
                        bullet.shape.setOrigin(bullet.shape.getRadius(), bullet.shape.getRadius());
                        bullet.shape.setFillColor(Color::Red);

                        Transform trans = shape->getTransform();
                        Vector2f  tip   = trans.transformPoint(shape->getPoint(0));
                        bullet.shape.setPosition(tip);
                        Vector2f dir = trans.transformPoint(shape->getPoint(0)) -
                                       trans.transformPoint(shape->getPoint(1));
                        float    length = std::sqrt(dir.x * dir.x + dir.y * dir.y);
                        Vector2f norm   = dir / length;

                        bullet.velocity = norm * 1.5f;

                        bullets.push_back(bullet);
                        fireCooldown.restart();
                        if (sounds)
                                sounds->Play();
                }
                for (auto& b : bullets)
                {
                        b.shape.move(b.velocity);
                }
        }
};
int main()
{
        VideoMode desktop = VideoMode::getDesktopMode();

        Soundmanager sounds;
        sounds.Shootsound("/home/learner/Downloads/Pew_Pew-DKnight556-1379997159.wav");
        astroid*      world   = new rotation(desktop, &sounds);
        RenderWindow* window  = world->init_window();
        ConvexShape*  ship    = world->player_ship(*window);
        int           playing = true;
        while (window->isOpen() && playing)
        {
                Event event;
                while (window->pollEvent(event))
                {
                        if (event.type == Event::Closed)
                                window->close();
                }
                window->clear(Color::Black);
                window->draw(*ship);
                if (rotation* r = dynamic_cast<rotation*>(world))
                {
                        for (auto& b : r->getBullets())
                                window->draw(b.shape);
                }
                world->Input(ship, window, playing);
                window->display();
        }

        delete window;
        delete ship;
        delete world;
        return 0;
}


https://soundbible.com/1949-Pew-Pew.html
حتما نسخه wav  دانلود کنید. صداش خیلی خنده داره  ;D
مثل سمی کالونم( ; ).
خواستار پایان.
محکوم به کامپایل.

آفلاین learner:~$

  • Hero Member
  • *
  • ارسال: 828
  • جنسیت : پسر
  • روزی روز گاری در گنو/لینوکس
پاسخ : بازی astroid
« پاسخ #2 : 20 تیر 1404، 08:51 ب‌ظ »
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <random>
using namespace sf;
class Astroids
{
      public:
        CircleShape shape;
        Vector2f    velocity;
        Astroids(Vector2f postion, float speed)
        {
                shape.setRadius(25.f);
                shape.setOrigin(25.f, 25.f);
                shape.setFillColor(Color(150, 150, 150));
                shape.setPosition(postion);

                float angle = static_cast<float>(rand() % 360) * 3.14159f / 180.f;
                velocity    = Vector2f(std::cos(angle) * speed, std::sin(angle) * speed);
        };
        void      move() { shape.move(velocity); }
        FloatRect getBunds() const { return shape.getGlobalBounds(); }
        Vector2f  getPosition() const { return shape.getPosition(); }
        void      wrap(const RenderWindow& win)
        {
                Vector2f pos  = shape.getPosition();
                Vector2u size = win.getSize();
                float    w    = static_cast<float>(size.x);
                float    h    = static_cast<float>(size.y);
                if (pos.x < 0)
                        pos.x += w;
                if (pos.x > w)
                        pos.x -= w;
                if (pos.y < 0)
                        pos.y += h;
                if (pos.y > h)
                        pos.y -= h;
                shape.setPosition(pos);
        }
};
struct Bullet
{
        CircleShape shape;
        Vector2f    velocity;
};
class Soundmanager
{
      protected:
        SoundBuffer shootBuffer;
        Sound       shootSound;

      public:
        void Shootsound(const std::string& full_path)
        {
                if (!shootBuffer.loadFromFile(full_path))
                {
                        std::cerr << "no such file!" << std::endl;
                        std::exit(-1);
                }
                shootSound.setBuffer(shootBuffer);
        }
        void Play() { shootSound.play(); }
};

class astroid
{
      protected:
        Vector2f  pos;
        VideoMode desktop;
        FloatRect bunds;

      public:
        astroid(VideoMode desk) : desktop{desk} {};
        virtual void  Input(ConvexShape* shape, RenderWindow* win, int& playing);
        RenderWindow* init_window();

        ConvexShape* player_ship(const RenderWindow& win);
};
ConvexShape* astroid::player_ship(const RenderWindow& win)
{

        Vector2u     size = win.getSize();
        float        x    = static_cast<float>(size.x) / 2.f;
        float        y    = static_cast<float>(size.y) / 2.f;
        ConvexShape* ship = new ConvexShape;
        ship->setPointCount(3);
        ship->setPoint(0, {0.f, -7.f});
        ship->setPoint(1, {-2.5f, 4.f});
        ship->setPoint(2, {2.5f, 4.f});
        ship->setFillColor(Color::Transparent);
        ship->setOutlineThickness(1.7f);
        ship->setOutlineColor(Color::White);
        FloatRect local = ship->getLocalBounds();
        ship->setOrigin(local.width / 2.f, local.height / 2.f);

        ship->setPosition(x, y);
        return ship;
}
RenderWindow* astroid::init_window()
{

        RenderWindow* window = new RenderWindow;
        window->create(desktop, "astroid", Style::Default);
        return window;
}
void astroid::Input(ConvexShape* shape, RenderWindow* win, int& playing)
{
        Vector2u size = win->getSize();
        float    winw = static_cast<float>(size.x);
        float    winh = static_cast<float>(size.y);
        pos           = shape->getPosition();
        float speed   = 0.4f;
        bunds         = shape->getGlobalBounds();
        if (Keyboard::isKeyPressed(Keyboard::Right) && bunds.left + bunds.width < winw)
                shape->move(speed, 0.f);
        if (Keyboard::isKeyPressed(Keyboard::Left) && bunds.left > 0)
                shape->move(-speed, 0.f);
        if (Keyboard::isKeyPressed(Keyboard::Up) && bunds.top > 0)
                shape->move(0.f, -speed);
        if (Keyboard::isKeyPressed(Keyboard::Down) && bunds.top + bunds.height < winh)
                shape->move(0.f, speed);
        if (Keyboard::isKeyPressed(Keyboard::Escape))
        {
                playing = false;
        }
}
class rotation : public astroid
{
      protected:
        std::vector<Bullet> bullets;
        Clock               fireCooldown;
        float               firedelay = 0.3f;
        Soundmanager*       sounds;
        Clock               astroidsclock;
        float               astroid_dela = 2.f;

      public:
        std::vector<Astroids>& getAsteroids() { return asteroids; }
        std::vector<Astroids>  asteroids;
        std::vector<Bullet>&   getBullets() { return bullets; }
        rotation(VideoMode desk, Soundmanager* sm) : astroid(desk), sounds(sm){};
        using astroid::astroid;

        virtual void Input(ConvexShape* shape, RenderWindow* win, int& playing) override
        {

                astroid::Input(shape, win, playing);
                FloatRect shipBounds = shape->getGlobalBounds();

                if (Keyboard::isKeyPressed(Keyboard::Space) && Keyboard::isKeyPressed(Keyboard::Up))
                        shape->rotate(1.f);
                if (Keyboard::isKeyPressed(Keyboard::Space) &&
                    Keyboard::isKeyPressed(Keyboard::Left))
                        shape->rotate(-1.f);

                if (Keyboard::isKeyPressed(Keyboard::Space) &&
                    Keyboard::isKeyPressed(Keyboard::Right))
                        shape->rotate(1.f);
                if (Keyboard::isKeyPressed(Keyboard::Space) &&
                    Keyboard::isKeyPressed(Keyboard::Down))
                        shape->rotate(-1.f);
                if (Keyboard::isKeyPressed(Keyboard::F) &&
                    fireCooldown.getElapsedTime().asSeconds() > firedelay)
                {
                        if (asteroids.size() < 10 &&
                            astroidsclock.getElapsedTime().asSeconds() > astroid_dela)
                        {

                                Vector2f pos(rand() % win->getSize().x, rand() % win->getSize().y);
                                asteroids.emplace_back(pos, 0.6f);
                                astroidsclock.restart();
                        }
                        Bullet bullet;
                        bullet.shape = CircleShape(2.f);
                        bullet.shape.setOrigin(bullet.shape.getRadius(), bullet.shape.getRadius());
                        bullet.shape.setFillColor(Color::Red);

                        Transform trans = shape->getTransform();
                        Vector2f  tip   = trans.transformPoint(shape->getPoint(0));
                        bullet.shape.setPosition(tip);
                        Vector2f dir = trans.transformPoint(shape->getPoint(0)) -
                                       trans.transformPoint(shape->getPoint(1));
                        float    length = std::sqrt(dir.x * dir.x + dir.y * dir.y);
                        Vector2f norm   = dir / length;

                        bullet.velocity = norm * 1.5f;

                        bullets.push_back(bullet);
                        fireCooldown.restart();
                        if (sounds)
                                sounds->Play();
                }

                for (auto& a : asteroids)
                {
                        a.move();
                        a.wrap(*win);
                        if (shipBounds.intersects(a.getBunds()))
                        {
                                playing = false;
                                std::cout << "GameOver!" << std::endl;
                        }
                }
                for (auto& b : bullets)
                {
                        b.shape.move(b.velocity);
                }
                for (auto bull = bullets.begin(); bull != bullets.end();)
                {
                        bool hit = false;
                        for (auto ast = asteroids.begin(); ast != asteroids.end();)
                        {
                                if (bull->shape.getGlobalBounds().intersects(ast->getBunds()))
                                {
                                        ast = asteroids.erase(ast);
                                        hit = true;
                                        break;
                                }
                                else
                                        ++ast;
                        }

                        if (hit)
                                bull = bullets.erase(bull);
                        else
                                ++bull;
                }
        }
};
int main()
{
        VideoMode desktop = VideoMode::getDesktopMode();
        srand(time(0));

        Soundmanager sounds;
        sounds.Shootsound("/home/learner/Downloads/Pew_Pew-DKnight556-1379997159.wav");
        astroid*      world   = new rotation(desktop, &sounds);
        RenderWindow* window  = world->init_window();
        ConvexShape*  ship    = world->player_ship(*window);
        int           playing = true;
        while (window->isOpen() && playing)
        {
                Event event;
                while (window->pollEvent(event))
                {
                        if (event.type == Event::Closed)
                                window->close();
                }
                window->clear(Color::Black);
                window->draw(*ship);
                if (rotation* r = dynamic_cast<rotation*>(world))
                {
                        for (auto& b : r->getBullets())
                                window->draw(b.shape);
                        for (auto& a : r->getAsteroids())
                                window->draw(a.shape);
                }
                world->Input(ship, window, playing);
                window->display();
        }

        delete window;
        delete ship;
        delete world;
        return 0;
}

دبگه بیشتر از این کشش ندارم  :-X

برای کامپایل هم میتونید این دستور رو بزنید:
g++ main.cpp -o game -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio

مثل سمی کالونم( ; ).
خواستار پایان.
محکوم به کامپایل.