- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我试图在每次玩家移动时更新一个文本变量。问题是每次我这样做时,新字符串都会出现在前一个字符串之上。当我调整或扩展游戏窗口时,字符串正常显示。
问题状态:
扩展状态(期望状态):
下面我提供问题所在的代码。我知道我的代码可能不是最好的,但我刚开始学习 sfml,所以有时我会硬编码一点以获得快速结果。一旦完成,我将用更好的编程实践重写整个应用程序。处理上述字符串的地方在void handle_plmove_request(game_packet packet, listen_parameters param)和void render_graphics(...)和int main()中。我在 main() 中创建了文本变量,并将它们作为参数传递给执行函数 listen_to_server() 的线程
#include <list>
#include <sstream>
#include "player.h"
std::string PLAYER_MOVE_REQUEST = "PLAYER_MOVE";
std::string CHIP_CREATION_REQUEST = "CHIP_CREATION";
std::string CHIP_DELETION_REQUEST = "CHIP_DELETION";
std::string MONSTER_ENTITY_MY = "monster";
std::string PLAYER_ENTITY_MY= "player";
struct game_packet {
std::string type;
std::string player_move;
double chip_position_x; // change it to string
double chip_position_y;
double pl1_position_x;
double pl1_position_y;
double pl2_position_x;
double pl2_position_y;
int score_pl1;
int score_pl2;
};
struct listen_parameters {
sf::TcpSocket* socket;
player* player1;
player* player2;
sf::RenderWindow* what_window;
std::list<player>* chipList;
bool* lock_movement;
sf::Text* pl1_score;
sf::Text* pl2_score;
};
sf::Packet& operator <<(sf::Packet& packet, const game_packet& pack)
{
return packet << pack.type << pack.player_move << pack.chip_position_x << pack.chip_position_y
<< pack.pl1_position_x << pack.pl1_position_y << pack.pl2_position_x << pack.pl2_position_y
<< pack.score_pl1 << pack.score_pl2;
}
sf::Packet& operator >>(sf::Packet& packet, game_packet& pack)
{
return packet >> pack.type >> pack.player_move >> pack.chip_position_x >> pack.chip_position_y
>> pack.pl1_position_x >> pack.pl1_position_y >> pack.pl2_position_x >> pack.pl2_position_y
>> pack.score_pl1 >> pack.score_pl2;
}
void handle_plmove_request(game_packet packet, listen_parameters param) {
std::stringstream int_to_string_pl1;
std::stringstream int_to_string_pl2;
double set_xpos = packet.pl1_position_x * 100 + 50;
double set_ypos = packet.pl1_position_y * 100 + 50;
param.player1->setCoordinates(set_xpos, set_ypos);
set_xpos = packet.pl2_position_x * 100 + 50;
set_ypos = packet.pl2_position_y * 100 + 50;
param.player2->setCoordinates(set_xpos, set_ypos);
int_to_string_pl1 << packet.score_pl1;
param.pl1_score->setString(int_to_string_pl1.str());
int_to_string_pl2 << packet.score_pl2;
param.pl2_score->setString(int_to_string_pl2.str());
}
void handle_chip_deletion(game_packet packet, listen_parameters param) {
std::cout << "handling chip_deletion_rquest" << std::endl;
packet.chip_position_x = packet.chip_position_x * 100 + 50;
packet.chip_position_y = packet.chip_position_y * 100 + 50;
player chip_to_delete(packet.chip_position_x, packet.chip_position_y, MONSTER_ENTITY_MY, "0");
std::cout << "deleting" << packet.chip_position_x << ","
<< packet.chip_position_y;
param.chipList->remove(chip_to_delete);
}
void handle_chip_creation(game_packet packet, listen_parameters param) {
param.chipList->clear();
std::string string_number;
int new_integer;
int xpos_newchip;
int ypos_newchip;
std::cout << "received integers" << std::endl;
param.chipList->clear();
for (auto& it : packet.player_move) {
if (it != '_')
string_number += it;
else {
std::stringstream string_to_int(string_number);
string_to_int >> new_integer;
std::cout << new_integer << ",";
xpos_newchip = (new_integer % 8) * 100 + 50;
ypos_newchip = (new_integer / 8) * 100 + 50;
string_number.clear();
player new_chip(xpos_newchip, ypos_newchip, MONSTER_ENTITY_MY , "0");
param.chipList->push_back(new_chip);
}
}
std::cout << std::endl;
}
void listen_to_server(listen_parameters param){
while (true) {
sf::Packet packet;
game_packet gamePacket;
if (param.socket->receive(packet) != sf::Socket::Done)
std::cout << "data couldn't be received" << std::endl;
packet >> gamePacket;
if (gamePacket.type == PLAYER_MOVE_REQUEST)
handle_plmove_request(gamePacket, param);
else if (gamePacket.type == CHIP_DELETION_REQUEST) {
handle_chip_deletion(gamePacket, param);
}
else if (gamePacket.type == CHIP_CREATION_REQUEST) {
handle_chip_creation(gamePacket, param);
}
}
}
void render_graphics(listen_parameters param) {
sf::Clock clock;
sf::Clock bot_clock;
sf::Font font;
if (!font.loadFromFile("arial.ttf"))
std::cout << "can't load" << std::endl;
sf::Text player1_text;
sf::Text player2_text;
player1_text.setFont(font);
player1_text.setString("PLAYER_1 SCORE");
player1_text.setFillColor(sf::Color::Red);
player1_text.setPosition(150, 850);
player2_text.setFont(font);
player2_text.setString("PLAYER_2 SCORE");
player2_text.setFillColor(sf::Color::Blue);
player2_text.setPosition(500, 850);
sf::RectangleShape white_square(sf::Vector2f(100, 100));
white_square.setFillColor(sf::Color(0, 0, 0));
sf::RectangleShape black_square(sf::Vector2f(100, 100));
black_square.setFillColor(sf::Color(255, 255, 255));
while (param.what_window->isOpen()) {
float two = 2;
sf::Time elapsed1 = clock.getElapsedTime();
sf::Time bot_elapsed = bot_clock.getElapsedTime();
sf::Time two_seconds = sf::seconds(2);
sf::Time one_millisecond = sf::milliseconds(10);
if (elapsed1 > one_millisecond) {
bool isBlack = true;
for (int i = 0; i < 800; i += 100) {
for (int j = 0; j < 800; j += 100) {
if (isBlack == true) {
black_square.setPosition(i + 50, j + 50);
param.what_window->draw(black_square);
if (j + 100 != 800)
isBlack = false;
}
else {
white_square.setPosition(i + 50, j + 50);
param.what_window->draw(white_square);
if (j + 100 != 800)
isBlack = true;
}
}
}
//std::cout << "displaying child thread" << std::endl;
param.player1->setShapePosition();
param.what_window->draw(param.player1->getPlayerShape());
param.player2->setShapePosition();
param.what_window->draw(param.player2->getPlayerShape());
param.what_window->draw(player1_text);
param.what_window->draw(player2_text);
param.what_window->draw(*(param.pl1_score)); // where the score is drawn
param.what_window->draw(*(param.pl2_score)); // where the score is drawn
for (auto& it : *(param.chipList)) {
it.setShapePosition();
param.what_window->draw(it.getPlayerShape());
}
param.what_window->display();
clock.restart();
}
}
}
int main()
{
int port_number = 53004;
sf::TcpSocket socket;
sf::Socket::Status status = socket.connect("192.168.0.14", port_number);
if (status != sf::Socket::Done)
std::cout << "awaiting connection" << std::endl;
std::cout << "succesfull connection" << std::endl;
sf::Packet packet;
int xpos;
int ypos;
std::string player_number;
if (socket.receive(packet) != sf::Socket::Done)
std::cout << "can't receive packet" << std::endl;
packet >> xpos >> ypos >> player_number;
std::cout << "your player number is" << player_number << std::endl;
sf::RenderWindow window(sf::VideoMode(900, 1000), "SFML works!");
window.setKeyRepeatEnabled(false);
window.setActive(false);
player player1(50,750,PLAYER_ENTITY_MY, "1");
player player2(750,750, PLAYER_ENTITY_MY, "2");
sf::RenderWindow* ptr_window = &window;
std::list<player> chipList;
bool lock_movement = false;
sf::Text pl1_score;
sf::Text pl2_score;
sf::Font font;
if (!font.loadFromFile("arial.ttf"))
std::cout << "can't load" << std::endl;
pl1_score.setFont(font);
pl1_score.setFillColor(sf::Color::Red);
pl1_score.setPosition(250, 950);
pl2_score.setFont(font);
pl2_score.setFillColor(sf::Color::Blue);
pl2_score.setPosition(600, 950);
pl1_score.setString("0");
pl2_score.setString("0");
listen_parameters param{&socket, &player1, &player2, ptr_window,&chipList,&lock_movement,
&pl1_score, &pl2_score};
sf::Thread server_listener_thread{ &listen_to_server, param };
server_listener_thread.launch();
sf::Thread graphics(&render_graphics, param);
graphics.launch();
while (window.isOpen()){
sf::Event event;
while (window.pollEvent(event))
{
std::string ins;
sf::Packet packet;
if (lock_movement == false) {
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
game_packet gamePacket;
gamePacket.type = PLAYER_MOVE_REQUEST;
ins = "r" + player_number;
gamePacket.player_move = ins;
packet << gamePacket;
if (socket.send(packet) != sf::Socket::Done)
std::cout << "couldn't send packet" << std::endl;
std::cout << "sending r instruction" << std::endl;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
game_packet gamePacket;
gamePacket.type = PLAYER_MOVE_REQUEST;
ins = "l" + player_number;
gamePacket.player_move = ins;
packet << gamePacket;
if (socket.send(packet) != sf::Socket::Done)
std::cout << "couldn't send packet" << std::endl;
std::cout << "sending l instruction" << std::endl;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
game_packet gamePacket;
gamePacket.type = PLAYER_MOVE_REQUEST;
ins = "u" + player_number;
gamePacket.player_move = ins;
packet << gamePacket;
if (socket.send(packet) != sf::Socket::Done)
std::cout << "couldn't send packet" << std::endl;
std::cout << "sending u instruction" << std::endl;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
game_packet gamePacket;
gamePacket.type = PLAYER_MOVE_REQUEST;
ins = "d" + player_number;
gamePacket.player_move = ins;
packet << gamePacket;
if (socket.send(packet) != sf::Socket::Done)
std::cout << "couldn't send packet" << std::endl;
std::cout << "sending d instruction" << std::endl;
}
break;
default:
break;
}
}
}
}
return 0;
}
总结:我只是通过更改和重绘来更新文本。
最佳答案
解决方案很简单:只需在每次主循环迭代开始时调用 window.clear()
。现在你永远不会清除屏幕,只是在前一个屏幕上画画,只要你填满所有东西你就不会注意到。问题是,您永远不会填充比赛 field 下方的区域,即显示分数的区域。
关于c++ - 更新 SFML 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46107055/
我有一个包含纹理的文件。我加载到 sf::Texture并用 setTextureRect 拆分成 Sprite . 现在假设一个 Sprite 包含 20 像素宽的纹理的一部分。如何渲染它以适应例如
一段时间以来,我一直在使用 SDL 来制作小东西的原型(prototype)并了解有关图形编程的更多信息。 然后我看到了SFML并想试一试,发现它更面向对象并修复了我认为 SDL 存在的一些问题。 但
我正在使用 rust-sfml ( rsfml::graphics ) 目前,将像素绘制到屏幕上。 (我只是从 Rust 和项目开始。)我将数据存储在 Image 中。 , 然后复制到 Texture
我试图在 SFML 2 中获取关键输入,在 SFML 1.6 im 中使用 while (App.GetEvent(Event)) { if (App.GetI
我做了一个非常简单的双人游戏,一个是水母,另一个是SFML中的鲨鱼。唯一的问题是一次只能有一个 Sprite 移动。这可能修复吗?我该如何修复? 编辑:我的尝试是这样的: if (event.type
我已经尝试了几个小时,但似乎无法做到我已经下载了扩展程序并寻求帮助,但此时一切都让我感到困惑。 我想在我的项目中包含 SFML 库,我正在尝试使用 Visual Studio Code 编辑器,但由于
下面链接中的图片包含 20 多张图片(每个 body 部位和盔甲类型都在单独的文件中)。这个角色在名为“Spriter”的软件中被动画化。该代码采用 spriter 的输出文件 (.scml),然后计
我的第一个问题是 C++ 是否: pTexture.getSize().x 等于 C#(因为这对我来说没有结果): pTexture.Size().X 第二个问题是关于C++代码的(如何在SFML.n
我正在尝试在 Windows 10 下用 sfml 构建游戏。它在构建时显示成功。每当我调试解决方案时,它都会显示如下错误消息: Unhandled exception at 0x00007FFA62
我开始使用 SFML 和 C++ 创建游戏。但是,我遇到了一个问题。我有基本的骨架类代码,但由于某种原因,当我创建一个窗口(sf::Window)时,我立即崩溃了!此代码不包含任何事件检查代码,但它包
我的项目中有 2 个 CMakeLists.txt 文件。 CMakeLists.txt cmake_minimum_required(VERSION 3.2) project(Game) set(C
所以我正在尝试编写一些与网络相关的代码,特别是使用 SFML 的端口扫描器。我正在运行 VS 2017 并下载了 2015 年的 SFML 版本,但是人们说这应该适用于 2017 年。 到目前为止,我
我刚刚开始使用 C++ 和 SFML,最终一切都运行良好。在出现 undefined reference 错误之前,我意识到我一直在下载错误类型的 SFML,得到的是 SJLJ 而不是 DW2。问题已
尝试将新的 C++ 项目与 SFML 链接,我执行了以下操作: 创建了一个空项目。 C/C++ >> 常规:附加包含目录:添加了 SFML 包含目录并从项目默认值继承。 链接器 >> 附加库目录:添加
当我运行代码时,除了不播放声音外,一切正常。链接很好,当我放置断点并查看声音对象时,它显示 .我在正确的位置添加了 sfml-audio-d.lib 和 sfml-audio.lib。欢迎提出任何建议
下面的代码给出了未捕获的异常(特别是 txtr.loadFromFile("C:/Users/kidz/Documents/Visual Studio 2012/Projects/SFML/Debug
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。
我想知道是否可以使用 SFML 库在 C 中使用颜色设置碰撞?因为我使用的是带有图 block 的 map ,我不知道它是否可以一起工作。 谢谢! 最佳答案 据我所知,您使用具有两种颜色的图像来定义碰
我正在制作 Frogger 的 C++ SFML 版本,目前正在尝试实现 Frog 和其他物体(汽车、卡车、原木等)之间的碰撞检测。我为 Frog 和游戏对象有单独的类,并希望使用边界框来检测交叉点,
嗨,大家好 为了缩放我的 sfml 应用程序,我正在尝试解决这个问题,该程序最初使用 运行(640, 480) 方面 显示这样的窗口... 我的目标是以全屏模式执行程序并放置旧的 '窗口' (检查图像
我是一名优秀的程序员,十分优秀!