gpt4 book ai didi

C++ SFML Sprite 不显示/工作

转载 作者:太空宇宙 更新时间:2023-11-04 13:05:43 25 4
gpt4 key购买 nike

我正在尝试制作一个程序,让一个红色方 block 从屏幕顶部掉落,玩家必须避开它。当我运行程序时,我可以看到并移动玩家(桨),但看不到敌人或“东西” Sprite 。请帮助我,因为我已尝试使用代码进行所有操作,但仍然无法正常工作:/

事物.h

#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
using namespace std;
#pragma once

class Thing
{
public:
void thingspawn(RenderWindow &gameDisplay, int &enemystartx, int &enemystarty, int &enemywidth, int &enemyheight, int &blockcolor)
{
RectangleShape thing(Vector2f(enemywidth, enemyheight));
thing.setFillColor(Color(blockcolor));
thing.setPosition(enemystartx, enemystarty);
gameDisplay.clear();
gameDisplay.draw(thing);



}
};

主要.cpp

#include <iostream>
#include <SFML/Graphics.hpp>
#include "things.h"
using namespace std;
using namespace sf;

int main()
{
RenderWindow gameDisplay(VideoMode(1366, 768), "Game", Style::Fullscreen);
gameDisplay.setMouseCursorVisible(false);
gameDisplay.clear();
int enemystarty = -200;
int enemystartx = 300;
int enemyheight = 100;
int enemywidth = 100;
int enemyspeed = 0.3f;
int enemycount = 1;
int dodged = 0;
int blockcolor = (255, 0, 0);

RectangleShape player(Vector2f(300, 30));
player.setFillColor(Color(0, 0, 255));
player.setPosition(400, 728);


while (gameDisplay.isOpen())
{
Event evnt;
while (gameDisplay.pollEvent(evnt))
{
switch (evnt.type)
{
case Event::Closed:
gameDisplay.close();
case Event::KeyPressed:
if (Keyboard::isKeyPressed(Keyboard::Q))
gameDisplay.close();

}
}


if (Keyboard::isKeyPressed(Keyboard::Right))
if (player.getPosition().x < 1000)
player.move(0.2f, 0.0f);
if (Keyboard::isKeyPressed(Keyboard::Left))
if (player.getPosition().x > 50)
player.move(-0.2f, 0.0f);

Thing thingobject;
thingobject.thingspawn(gameDisplay, enemystartx, enemystarty, enemywidth, enemyheight, blockcolor);

enemystarty += enemyspeed;


gameDisplay.draw(player);
gameDisplay.display();


}

最佳答案

问题是您在函数 Thing::thingspawn(...) 中创建了 RectangleShape 事物,它在结束后被销毁。尝试将其声明为 Thing 类的成员

关于C++ SFML Sprite 不显示/工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42537644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com