1
General / I have a problem with background
« on: May 19, 2024, 03:09:15 pm »
I need to make the background rise with the platforms when jumping, but I do not know how to do this. You also need the background to be drawn infinitely many times. Can you help me please
main.cpp
main.cpp
(#include <SFML/Graphics.hpp>
#include "hero.h"
#include "game.h"
#include <iostream>
int main() {
sf::RenderWindow window(sf::VideoMode(1920, 1080), "WayofDarkness");
game(window);
return 0;
})
hero.h#include "hero.h"
#include "game.h"
#include <iostream>
int main() {
sf::RenderWindow window(sf::VideoMode(1920, 1080), "WayofDarkness");
game(window);
return 0;
})
(#ifndef HERO_H
#define HERO_H
#include <SFML/Graphics.hpp>
class Hero {
private:
sf::Sprite sprite;
sf::Texture texture;
float x, y;
public:
Hero(std::string spritePath, float posX, float posY) {
sprite.setTextureRect(sf::IntRect(0, 2048, 1024 ,1024));
sprite.scale(sf::Vector2f(0.15, 0.15));
if (!texture.loadFromFile(spritePath)) {
}
sprite.setTexture(texture);
x = posX;
y = posY;
sprite.setPosition(x, y);
}
void Idle(sf::RenderWindow& window, float, float&);
void draw(sf::RenderWindow& window);
void move(sf::RenderWindow& window, float&, float&, float, float&, bool&);
};
#endif HERO_H)
hero.cpp#define HERO_H
#include <SFML/Graphics.hpp>
class Hero {
private:
sf::Sprite sprite;
sf::Texture texture;
float x, y;
public:
Hero(std::string spritePath, float posX, float posY) {
sprite.setTextureRect(sf::IntRect(0, 2048, 1024 ,1024));
sprite.scale(sf::Vector2f(0.15, 0.15));
if (!texture.loadFromFile(spritePath)) {
}
sprite.setTexture(texture);
x = posX;
y = posY;
sprite.setPosition(x, y);
}
void Idle(sf::RenderWindow& window, float, float&);
void draw(sf::RenderWindow& window);
void move(sf::RenderWindow& window, float&, float&, float, float&, bool&);
};
#endif HERO_H)
(#include <iostream>
#include "hero.h"
#include "update.h"
void Hero::Idle(sf::RenderWindow& window, float time, float& CurrentFrame)
{
}
void Hero::draw(sf::RenderWindow& window)
{
window.draw(sprite);
}
void Hero::move(sf::RenderWindow& window, float& playerX, float& playerY, float time, float& CurrentFrame, bool& Idle)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
playerX -= 0.1 * time;
CurrentFrame += 0.005 * time; //служит для прохождения по "кадрам". переменная доходит до трех суммируя произведение времени и скорости. изменив 0.005 можно изменить скорость анимации
if (CurrentFrame > 4) CurrentFrame -= 3; //если пришли к третьему кадру - откидываемся назад.
if (CurrentFrame != 0) {
sprite.setTextureRect(sf::IntRect(1024 * int(CurrentFrame), 3072, 1024, 1024));
}
Idle = true;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
playerX += 0.1 * time;
CurrentFrame += 0.005 * time; //служит для прохождения по "кадрам". переменная доходит до трех суммируя произведение времени и скорости. изменив 0.005 можно изменить скорость анимации
if (CurrentFrame > 4) CurrentFrame -= 3; //если пришли к третьему кадру - откидываемся назад.
sprite.setTextureRect(sf::IntRect(1024 * int(CurrentFrame), 3072, -1024, 1024));
Idle = false;
}
else {
if (Idle == true) {
CurrentFrame += 0.005 * time; //служит для прохождения по "кадрам". переменная доходит до трех суммируя произведение времени и скорости. изменив 0.005 можно изменить скорость анимации
if (CurrentFrame > 3) CurrentFrame -= 2; //если пришли к третьему кадру - откидываемся назад.
sprite.setTextureRect(sf::IntRect(1024 * int(CurrentFrame), 2048, 1024, 1024));
}
if (Idle == false) {
CurrentFrame += 0.005 * time; //служит для прохождения по "кадрам". переменная доходит до трех суммируя произведение времени и скорости. изменив 0.005 можно изменить скорость анимации
if (CurrentFrame > 3) CurrentFrame -= 2; //если пришли к третьему кадру - откидываемся назад.
sprite.setTextureRect(sf::IntRect(1024 * int(CurrentFrame), 2048, -1024, 1024));
}
}
sprite.setPosition(playerX, playerY);
}
)
game.h#include "hero.h"
#include "update.h"
void Hero::Idle(sf::RenderWindow& window, float time, float& CurrentFrame)
{
}
void Hero::draw(sf::RenderWindow& window)
{
window.draw(sprite);
}
void Hero::move(sf::RenderWindow& window, float& playerX, float& playerY, float time, float& CurrentFrame, bool& Idle)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
playerX -= 0.1 * time;
CurrentFrame += 0.005 * time; //служит для прохождения по "кадрам". переменная доходит до трех суммируя произведение времени и скорости. изменив 0.005 можно изменить скорость анимации
if (CurrentFrame > 4) CurrentFrame -= 3; //если пришли к третьему кадру - откидываемся назад.
if (CurrentFrame != 0) {
sprite.setTextureRect(sf::IntRect(1024 * int(CurrentFrame), 3072, 1024, 1024));
}
Idle = true;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
playerX += 0.1 * time;
CurrentFrame += 0.005 * time; //служит для прохождения по "кадрам". переменная доходит до трех суммируя произведение времени и скорости. изменив 0.005 можно изменить скорость анимации
if (CurrentFrame > 4) CurrentFrame -= 3; //если пришли к третьему кадру - откидываемся назад.
sprite.setTextureRect(sf::IntRect(1024 * int(CurrentFrame), 3072, -1024, 1024));
Idle = false;
}
else {
if (Idle == true) {
CurrentFrame += 0.005 * time; //служит для прохождения по "кадрам". переменная доходит до трех суммируя произведение времени и скорости. изменив 0.005 можно изменить скорость анимации
if (CurrentFrame > 3) CurrentFrame -= 2; //если пришли к третьему кадру - откидываемся назад.
sprite.setTextureRect(sf::IntRect(1024 * int(CurrentFrame), 2048, 1024, 1024));
}
if (Idle == false) {
CurrentFrame += 0.005 * time; //служит для прохождения по "кадрам". переменная доходит до трех суммируя произведение времени и скорости. изменив 0.005 можно изменить скорость анимации
if (CurrentFrame > 3) CurrentFrame -= 2; //если пришли к третьему кадру - откидываемся назад.
sprite.setTextureRect(sf::IntRect(1024 * int(CurrentFrame), 2048, -1024, 1024));
}
}
sprite.setPosition(playerX, playerY);
}
)
(#pragma once
#include <SFML/Graphics.hpp>
#include "hero.h"
#include "scence.h"
#include <iostream>
float playerX = 250;
float playerY = 151;
void game(sf::RenderWindow& window) {
window.setFramerateLimit(60);
sf::Clock clock;
while (window.isOpen()) {
std::cout << time<< std::endl;
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear();
scence(window, playerX, playerY);
window.display();
}
}
)
scene.h#include <SFML/Graphics.hpp>
#include "hero.h"
#include "scence.h"
#include <iostream>
float playerX = 250;
float playerY = 151;
void game(sf::RenderWindow& window) {
window.setFramerateLimit(60);
sf::Clock clock;
while (window.isOpen()) {
std::cout << time<< std::endl;
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear();
scence(window, playerX, playerY);
window.display();
}
}
)
(#pragma once
#include <SFML\Graphics.hpp>
#include <SFML\Audio.hpp>
#include <random>
#include <ctime>
#include <sstream>
#include "hero.h"
float CurrentFrame = 0;
bool Idle;
float backGroundY = 0;
void scence(sf::RenderWindow& window, float& playerX, float& playerY) {
Hero hero("ALL/images/heroAnimation.png", 100, 100);
sf::Texture backgroundTexture;
sf::Texture platformTexture;
backgroundTexture.loadFromFile("ALL/images//BG.jpg");
platformTexture.loadFromFile("ALL/images/platform.png");
sf::Sprite background(backgroundTexture);
sf::Sprite platform(platformTexture);
sf::RectangleShape gameoverBackground(sf::Vector2f(1920, 1080));
gameoverBackground.setFillColor(sf::Color::White);
sf::Clock clock;
sf::Time t1 = sf::microseconds(10000);
sf::Font font;
font.loadFromFile("ALL/font/arial.ttf");
sf::Text scoreText;
scoreText.setFont(font);
scoreText.setCharacterSize(50);
scoreText.setFillColor(sf::Color::Red);
sf::Text gameoverText;
gameoverText.setFont(font);
gameoverText.setString("Game Over!");
gameoverText.setCharacterSize(80);
gameoverText.setFillColor(sf::Color::Red);
sf::SoundBuffer buffer;
buffer.loadFromFile("ALL/sound/jump.wav");
sf::Sound sound;
sound.setBuffer(buffer);
sf::Vector2u platformPosition[20];
std::uniform_int_distribution<unsigned> x(0, 1920); //-platformTexture.getSize().x
std::uniform_int_distribution<unsigned> y(100, 1080);
std::default_random_engine e(time(0));
for (size_t i = 0; i < 20; ++i)
{
platformPosition[i].x = x(e);
platformPosition[i].y = y(e);
}
float dy = 0;
int height = 150;
int score = 0;
//const int PLAYER_LEFT_BOUNDING_BOX = 20;
//const int PLAYER_RIGHT_BOUNDING_BOX = 60;
//const int PLAYER_BOTTOM_BOUNDING_BOX = 70;
const int PLAYER_LEFT_BOUNDING_BOX = 20;
const int PLAYER_RIGHT_BOUNDING_BOX = 80;
const int PLAYER_BOTTOM_BOUNDING_BOX = 100;
while (window.isOpen())
{
float time = clock.restart().asMicroseconds();
time = time / 800;
clock.restart();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
playerX -= 10;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
playerX += 10;
if (playerX > 1800)
playerX = 1800; //0
if (playerX < -40)
playerX = -40; // 1920
if (playerY == height && dy < (-1.62))
{
score += 1;
scoreText.setString("Score: " + std::to_string(score));
}
// player's jump mechanism
dy += 0.2;
playerY += dy;
backGroundY += dy;
if (playerY < height)
for (size_t i = 0; i < 20; ++i)
{
playerY = height;
platformPosition[i].y -= dy; // vertical translation
if (platformPosition[i].y > 1080) // set new platform on the top
{
platformPosition[i].y = 0;
platformPosition[i].x = x(e);
}
}
// detect jump on the platform
for (size_t i = 0; i < 20; ++i)
{
if ((playerX + PLAYER_RIGHT_BOUNDING_BOX > platformPosition[i].x) && (playerX + PLAYER_LEFT_BOUNDING_BOX < platformPosition[i].x + platformTexture.getSize().x) // player's horizontal range can touch the platform
&& (playerY + PLAYER_BOTTOM_BOUNDING_BOX > platformPosition[i].y) && (playerY + PLAYER_BOTTOM_BOUNDING_BOX < platformPosition[i].y + platformTexture.getSize().y) // player's vertical range can touch the platform
&& (dy > 0)) // player is falling
{
sound.play();
dy = -20;
}
}
hero.move(window, playerX, playerY, time, CurrentFrame, Idle);
hero.Idle(window, time, CurrentFrame);
window.draw(background);
background.setPosition(0, backGroundY);
hero.draw(window);
// set and draw platforms
for (size_t i = 0; i < 20; ++i)
{
platform.setPosition(platformPosition[i].x, platformPosition[i].y);
window.draw(platform);
}
// game over
if (playerY > 1080)
{
gameoverText.setPosition(30, 200);
scoreText.setPosition(150, 400);
goto gameover;
}
window.draw(scoreText);
window.display();
}
// Game Over
gameover:
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(gameoverBackground);
window.draw(gameoverText);
window.draw(scoreText);
window.display();
}
})
#include <SFML\Graphics.hpp>
#include <SFML\Audio.hpp>
#include <random>
#include <ctime>
#include <sstream>
#include "hero.h"
float CurrentFrame = 0;
bool Idle;
float backGroundY = 0;
void scence(sf::RenderWindow& window, float& playerX, float& playerY) {
Hero hero("ALL/images/heroAnimation.png", 100, 100);
sf::Texture backgroundTexture;
sf::Texture platformTexture;
backgroundTexture.loadFromFile("ALL/images//BG.jpg");
platformTexture.loadFromFile("ALL/images/platform.png");
sf::Sprite background(backgroundTexture);
sf::Sprite platform(platformTexture);
sf::RectangleShape gameoverBackground(sf::Vector2f(1920, 1080));
gameoverBackground.setFillColor(sf::Color::White);
sf::Clock clock;
sf::Time t1 = sf::microseconds(10000);
sf::Font font;
font.loadFromFile("ALL/font/arial.ttf");
sf::Text scoreText;
scoreText.setFont(font);
scoreText.setCharacterSize(50);
scoreText.setFillColor(sf::Color::Red);
sf::Text gameoverText;
gameoverText.setFont(font);
gameoverText.setString("Game Over!");
gameoverText.setCharacterSize(80);
gameoverText.setFillColor(sf::Color::Red);
sf::SoundBuffer buffer;
buffer.loadFromFile("ALL/sound/jump.wav");
sf::Sound sound;
sound.setBuffer(buffer);
sf::Vector2u platformPosition[20];
std::uniform_int_distribution<unsigned> x(0, 1920); //-platformTexture.getSize().x
std::uniform_int_distribution<unsigned> y(100, 1080);
std::default_random_engine e(time(0));
for (size_t i = 0; i < 20; ++i)
{
platformPosition[i].x = x(e);
platformPosition[i].y = y(e);
}
float dy = 0;
int height = 150;
int score = 0;
//const int PLAYER_LEFT_BOUNDING_BOX = 20;
//const int PLAYER_RIGHT_BOUNDING_BOX = 60;
//const int PLAYER_BOTTOM_BOUNDING_BOX = 70;
const int PLAYER_LEFT_BOUNDING_BOX = 20;
const int PLAYER_RIGHT_BOUNDING_BOX = 80;
const int PLAYER_BOTTOM_BOUNDING_BOX = 100;
while (window.isOpen())
{
float time = clock.restart().asMicroseconds();
time = time / 800;
clock.restart();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
playerX -= 10;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
playerX += 10;
if (playerX > 1800)
playerX = 1800; //0
if (playerX < -40)
playerX = -40; // 1920
if (playerY == height && dy < (-1.62))
{
score += 1;
scoreText.setString("Score: " + std::to_string(score));
}
// player's jump mechanism
dy += 0.2;
playerY += dy;
backGroundY += dy;
if (playerY < height)
for (size_t i = 0; i < 20; ++i)
{
playerY = height;
platformPosition[i].y -= dy; // vertical translation
if (platformPosition[i].y > 1080) // set new platform on the top
{
platformPosition[i].y = 0;
platformPosition[i].x = x(e);
}
}
// detect jump on the platform
for (size_t i = 0; i < 20; ++i)
{
if ((playerX + PLAYER_RIGHT_BOUNDING_BOX > platformPosition[i].x) && (playerX + PLAYER_LEFT_BOUNDING_BOX < platformPosition[i].x + platformTexture.getSize().x) // player's horizontal range can touch the platform
&& (playerY + PLAYER_BOTTOM_BOUNDING_BOX > platformPosition[i].y) && (playerY + PLAYER_BOTTOM_BOUNDING_BOX < platformPosition[i].y + platformTexture.getSize().y) // player's vertical range can touch the platform
&& (dy > 0)) // player is falling
{
sound.play();
dy = -20;
}
}
hero.move(window, playerX, playerY, time, CurrentFrame, Idle);
hero.Idle(window, time, CurrentFrame);
window.draw(background);
background.setPosition(0, backGroundY);
hero.draw(window);
// set and draw platforms
for (size_t i = 0; i < 20; ++i)
{
platform.setPosition(platformPosition[i].x, platformPosition[i].y);
window.draw(platform);
}
// game over
if (playerY > 1080)
{
gameoverText.setPosition(30, 200);
scoreText.setPosition(150, 400);
goto gameover;
}
window.draw(scoreText);
window.display();
}
// Game Over
gameover:
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(gameoverBackground);
window.draw(gameoverText);
window.draw(scoreText);
window.display();
}
})