c++小游戏制作02:涂鸦跳跳

先看效果图:

首先需要先下载安装SFML安装包,这里我下载的是64位的

 然后解压放在一个文件夹下,记好路径

然后要在VS中加入路径

在VC++目录中配置包含目录和库目录

包含目录的路径:D:\SFML-2.5.1\include

库目录的路径:D:\SFML-2.5.1\lib

然后在链接器-输入-添加依赖项中配置以下几个:

sfml-window-d.lib

sfml-system-d.lib

sfml-graphics-d.lib

sfml-audio-d.lib

 

 运行代码时弹出报错

由于找不到sfml-window-d-2.dll,无法继续执行代码。重新安装程序可能会解决此问题。

 这时候我们要把解压的sfml的bin文件夹下的内容,拷贝到当前代码程序的x64/Debug中去。

 再次运行就会发现可以了。

完整的代码如下:

#include<SFML/Graphics.hpp>
#include<time.h>

using namespace sf;

struct point
{
	int x, y;
};

int main()
{
	srand(time(0));

	RenderWindow app(VideoMode(400, 533), "Doodle Game!");
	app.setFramerateLimit(60);


	Texture t1, t2, t3;
	t1.loadFromFile("./images/background.png");
	t2.loadFromFile("./images/platform.png");
	t3.loadFromFile("./images/doodle.png");

	Sprite sBackground(t1), sPlat(t2), sPers(t3);

	point plat[20];

	for (int i = 0; i < 10; i++)
	{
		plat[i].x = rand() % 400;
		plat[i].y = rand() % 533;
	}

	int x = 100;
	int y = 100;
	int h = 200;
	float dx = 0;
	float dy = 0;

	while (app.isOpen())
	{
		Event e;
		while (app.pollEvent(e))
		{
			if (e.type == Event::Closed)
			{
				app.close();
			}
		}

		if (Keyboard::isKeyPressed(Keyboard::Right))
		{
			x += 3;
		}
		if (Keyboard::isKeyPressed(Keyboard::Left))
		{
			x -= 3;
		}

		dy += 0.2;
		y += dy;
		if (y > 500)
		{
			dy = -10;
		}

		if (y < h)
		{

			for (size_t i = 0; i < 10; i++)
			{
				y = h;
				plat[i].y = plat[i].y - dy;
				if (plat[i].y > 533)
				{
					plat[i].y = 0;
					plat[i].x = rand() % 400;
				}
			}
		}


		for (int i = 0; i < 10; i++)
		{
			if ((x + 50 > plat[i].x) && (x + 20 < plat[i].x + 68) && (y + 70 > plat[i].y) && (y + 70 < plat[i].y + 14) && (dy > 0))
			{
				dy = -10;
			}
		}


		sPers.setPosition(x, y);


		app.draw(sBackground);
		app.draw(sPers);

		for (int i = 0; i < 10; i++)
		{
			sPlat.setPosition(plat[i].x, plat[i].y);
			app.draw(sPlat);
		}

		app.display();

	}

	return 0;
}

首先需要3个图片,分别是涂鸦、背景和跳板,跳板要在界面上随机分布

#include<SFML/Graphics.hpp>
#include<time.h>

using namespace sf;

struct point
{
    int x, y;
};

int main()
{
    srand(time(0));

    RenderWindow app(VideoMode(400, 533), "Doodle Game!");
    app.setFramerateLimit(60);


    Texture t1, t2, t3;
    t1.loadFromFile("./images/background.png");
    t2.loadFromFile("./images/platform.png");
    t3.loadFromFile("./images/doodle.png");

    Sprite sBackground(t1), sPlat(t2), sPers(t3);

    point plat[20];

    for (int i = 0; i < 10; i++)
    {
        plat[i].x = rand() % 400;
        plat[i].y = rand() % 533;
    }

    while (app.isOpen())
    {
        Event e;
        while (app.pollEvent(e))
        {
            if (e.type == Event::Closed)
            {
                app.close();
            }
        }


        app.draw(sBackground);
        app.draw(sPers);

        for (int i = 0; i < 10; i++)
        {
            sPlat.setPosition(plat[i].x, plat[i].y);
            app.draw(sPlat);
        }

        app.display();

    }

    return 0;
}

然后实现涂鸦自动跳功能

#include<SFML/Graphics.hpp>
#include<time.h>

using namespace sf;

struct point
{
    int x, y;
};

int main()
{
    srand(time(0));

    RenderWindow app(VideoMode(400, 533), "Doodle Game!");
    app.setFramerateLimit(60);


    Texture t1, t2, t3;
    t1.loadFromFile("./images/background.png");
    t2.loadFromFile("./images/platform.png");
    t3.loadFromFile("./images/doodle.png");

    Sprite sBackground(t1), sPlat(t2), sPers(t3);

    point plat[20];

    for (int i = 0; i < 10; i++)
    {
        plat[i].x = rand() % 400;
        plat[i].y = rand() % 533;
    }

    int x = 100;
    int y = 100;
    int h = 200;
    float dx = 0;
    float dy = 0;

    while (app.isOpen())
    {
        Event e;
        while (app.pollEvent(e))
        {
            if (e.type == Event::Closed)
            {
                app.close();
            }

        dy += 0.2;
        y += dy;
        if (y > 500)
        {
            dy = -10;
        }
        sPers.setPosition(x, y);


        }


        app.draw(sBackground);
        app.draw(sPers);

        for (int i = 0; i < 10; i++)
        {
            sPlat.setPosition(plat[i].x, plat[i].y);
            app.draw(sPlat);
        }

        app.display();

    }

    return 0;
}

 实现左右按键跳功能

#include<SFML/Graphics.hpp>
#include<time.h>

using namespace sf;

struct point
{
    int x, y;
};

int main()
{
    srand(time(0));

    RenderWindow app(VideoMode(400, 533), "Doodle Game!");
    app.setFramerateLimit(60);


    Texture t1, t2, t3;
    t1.loadFromFile("./images/background.png");
    t2.loadFromFile("./images/platform.png");
    t3.loadFromFile("./images/doodle.png");

    Sprite sBackground(t1), sPlat(t2), sPers(t3);

    point plat[20];

    for (int i = 0; i < 10; i++)
    {
        plat[i].x = rand() % 400;
        plat[i].y = rand() % 533;
    }

    int x = 100;
    int y = 100;
    int h = 200;
    float dx = 0;
    float dy = 0;

    while (app.isOpen())
    {
        Event e;
        while (app.pollEvent(e))
        {
            if (e.type == Event::Closed)
            {
                app.close();
            }

            if (Keyboard::isKeyPressed(Keyboard::Right))
            {
                x += 3;
            }
            if (Keyboard::isKeyPressed(Keyboard::Left))
            {
                x -= 3;
            }

        dy += 0.2;
        y += dy;
        if (y > 500)
        {
            dy = -10;
        }
        sPers.setPosition(x, y);


        }


        app.draw(sBackground);
        app.draw(sPers);

        for (int i = 0; i < 10; i++)
        {
            sPlat.setPosition(plat[i].x, plat[i].y);
            app.draw(sPlat);
        }

        app.display();

    }

    return 0;
}

 然后实现能踩在跳板上

#include<SFML/Graphics.hpp>
#include<time.h>

using namespace sf;

struct point
{
    int x, y;
};

int main()
{
    srand(time(0));

    RenderWindow app(VideoMode(400, 533), "Doodle Game!");
    app.setFramerateLimit(60);


    Texture t1, t2, t3;
    t1.loadFromFile("./images/background.png");
    t2.loadFromFile("./images/platform.png");
    t3.loadFromFile("./images/doodle.png");

    Sprite sBackground(t1), sPlat(t2), sPers(t3);

    point plat[20];

    for (int i = 0; i < 10; i++)
    {
        plat[i].x = rand() % 400;
        plat[i].y = rand() % 533;
    }

    int x = 100;
    int y = 100;
    int h = 200;
    float dx = 0;
    float dy = 0;

    while (app.isOpen())
    {
        Event e;
        while (app.pollEvent(e))
        {
            if (e.type == Event::Closed)
            {
                app.close();
            }

            if (Keyboard::isKeyPressed(Keyboard::Right))
            {
                x += 3;
            }
            if (Keyboard::isKeyPressed(Keyboard::Left))
            {
                x -= 3;
            }

        dy += 0.2;
        y += dy;
        if (y > 500)
        {
            dy = -10;
        }
        for (int i = 0; i < 10; i++)
        {
            if ((x + 50 > plat[i].x) && (x + 20 < plat[i].x + 68) && (y + 70 > plat[i].y) && (y + 70 < plat[i].y + 14) && (dy > 0))
            {
                dy = -10;
            }
        }
        sPers.setPosition(x, y);


        }


        app.draw(sBackground);
        app.draw(sPers);

        for (int i = 0; i < 10; i++)
        {
            sPlat.setPosition(plat[i].x, plat[i].y);
            app.draw(sPlat);
        }

        app.display();

    }

    return 0;
}

 最后实现向上刷新跳板

#include<SFML/Graphics.hpp>
#include<time.h>

using namespace sf;

struct point
{
    int x, y;
};

int main()
{
    srand(time(0));

    RenderWindow app(VideoMode(400, 533), "Doodle Game!");
    app.setFramerateLimit(60);


    Texture t1, t2, t3;
    t1.loadFromFile("./images/background.png");
    t2.loadFromFile("./images/platform.png");
    t3.loadFromFile("./images/doodle.png");

    Sprite sBackground(t1), sPlat(t2), sPers(t3);

    point plat[20];

    for (int i = 0; i < 10; i++)
    {
        plat[i].x = rand() % 400;
        plat[i].y = rand() % 533;
    }

    int x = 100;
    int y = 100;
    int h = 200;
    float dx = 0;
    float dy = 0;

    while (app.isOpen())
    {
        Event e;
        while (app.pollEvent(e))
        {
            if (e.type == Event::Closed)
            {
                app.close();
            }
        }

        if (Keyboard::isKeyPressed(Keyboard::Right))
        {
            x += 3;
        }
        if (Keyboard::isKeyPressed(Keyboard::Left))
        {
            x -= 3;
        }

        dy += 0.2;
        y += dy;
        if (y > 500)
        {
            dy = -10;
        }

        if (y < h)
        {

            for (size_t i = 0; i < 10; i++)
            {
                y = h;
                plat[i].y = plat[i].y - dy;
                if (plat[i].y > 533)
                {
                    plat[i].y = 0;
                    plat[i].x = rand() % 400;
                }
            }
        }


        for (int i = 0; i < 10; i++)
        {
            if ((x + 50 > plat[i].x) && (x + 20 < plat[i].x + 68) && (y + 70 > plat[i].y) && (y + 70 < plat[i].y + 14) && (dy > 0))
            {
                dy = -10;
            }
        }


        sPers.setPosition(x, y);


        app.draw(sBackground);
        app.draw(sPers);

        for (int i = 0; i < 10; i++)
        {
            sPlat.setPosition(plat[i].x, plat[i].y);
            app.draw(sPlat);
        }

        app.display();

    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_34904125/article/details/125795271