درود دوستان!!!
من بازی پیتزا خور فضایی جادی رو یک بار با سی پلاس پلاس نوشتم ( خیلی خیلی کثیف نوشتم ) و الان سعی کردم یک بازی جدبد رو بنویسم.
اول از همه؛خوشحال میشم که جادی یک ویدیو درمورد ساخت این بازی با هر زبانی که دوست داره درست کنه.
خب بگذریم. من این بازی رو با کتابخانه 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 شلیک میکنه.
یکی از کار هایی که میخوام بکنم اینه که وقتی برنامه کامل شد از اشاره گر های هوشمند استفاده کنم. فعلا نمیتونم استفاده کنم چون نمیدونم چند چندم با خودم و برنامم
](https://forum.ubuntu-ir.org/Smileys/default/eusa_wall.gif)
خوشحال میشم اگر ایرادی دیدی بهم بگید!!