-1
私はゲームをプログラムしたいです。私はすでに敵とプレイヤーをプログラムしています。ゲームオブジェクトは、敵やプレーヤーがコーナー(左、上、右、上、左、下、右、下)に対して移動するかどうかは関係ありませんが、ゲームオブジェクトはフィールド外に移動します。私は衝突検知に何が間違っていますか?
第2の問題もあります。時には、敵が短時間フィールドの端にくっつく
#include <Windows.h>
#include <SFML/Graphics.hpp>
#include <vector>
#include <ctime>
#include <cstdlib>
#include "Player.hpp"
#include "Square.hpp"
#define WIDTH 800
#define HEIGHT 600
bool kUp = false, kDown = false, kLeft = false, kRight = false;
Player player(WIDTH/2 - 10, HEIGHT/2 - 10, 10, sf::Color::Green);
std::vector<Square> squares(10);
sf::RenderWindow window;
void update(float elapsedTime) {
if (kUp == true) player.move(0, -300.0f * elapsedTime);
if (kDown == true) player.move(0, 300.0f * elapsedTime);
if (kLeft == true) player.move(-300.0f * elapsedTime, 0);
if (kRight == true) player.move(300.0f * elapsedTime, 0);
sf::Vector2f playerPos = player.getPosition();
if (playerPos.x < 0) player.setPosition(0, playerPos.y);
if (playerPos.y < 0) player.setPosition(playerPos.x, 0);
if (playerPos.x > WIDTH - 20) player.setPosition(WIDTH - 20, playerPos.y);
if (playerPos.y > HEIGHT - 20) player.setPosition(playerPos.x, HEIGHT - 20);
for (int i = 0; i < squares.size(); ++i) {
sf::Vector2f squareSpeed = squares[i].getSpeed();
squares[i].move(squareSpeed.x * elapsedTime, squareSpeed.y * elapsedTime);
sf::Vector2f squarePos = squares[i].getPosition();
if (squarePos.x < 0) squares[i].setSpeed(-squareSpeed.x, squareSpeed.y);
if (squarePos.y < 0) squares[i].setSpeed(squareSpeed.x, -squareSpeed.y);
if (squarePos.x > WIDTH - 5) squares[i].setSpeed(-squareSpeed.x, squareSpeed.y);
if (squarePos.y > HEIGHT - 5) squares[i].setSpeed(squareSpeed.x, -squareSpeed.y);
}
}
void draw() {
for (int i = 0; i < squares.size(); ++i) {
window.draw(squares[i]);
}
window.draw(player);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
window.create(sf::VideoMode(WIDTH, HEIGHT), "Runner - The Red Squares", sf::Style::Titlebar | sf::Style::Close, settings);
window.setVerticalSyncEnabled(true);
srand(time(0));
for (int i = 0; i < squares.size(); ++i) {
squares[i] = Square(rand() % WIDTH, rand() % HEIGHT, 5, 5, sf::Color::Red);
}
sf::Clock clock;
while (window.isOpen()) {
sf::Time elapsedTime = clock.restart();
window.clear(sf::Color::Black);
update(elapsedTime.asSeconds());
draw();
window.display();
sf::Event evt;
while (window.pollEvent(evt)) {
if (evt.type == sf::Event::Closed) {
window.close();
}
if (evt.type == sf::Event::KeyPressed) {
if (evt.key.code == sf::Keyboard::Up) kUp = true;
if (evt.key.code == sf::Keyboard::Down) kDown = true;
if (evt.key.code == sf::Keyboard::Left) kLeft = true;
if (evt.key.code == sf::Keyboard::Right) kRight = true;
}
if (evt.type == sf::Event::KeyReleased) {
if (evt.key.code == sf::Keyboard::Up) kUp = false;
if (evt.key.code == sf::Keyboard::Down) kDown = false;
if (evt.key.code == sf::Keyboard::Left) kLeft = false;
if (evt.key.code == sf::Keyboard::Right) kRight = false;
}
}
sf::sleep(sf::milliseconds(15));
}
return 0;
}
私が間違っていたことを誰かが知っていますか?それぞれの
おかげでも(player.getPosition()
を使用して)現在のプレーヤーの位置を更新しながら、あなたは(playerPos
に格納されている)プレイヤー位置のコピーを使用しているので、
検討: 'player.setPosition()'を呼び出した後、 'playerPos'は' player.getPosition() 'の値と同じですか?必要に応じて、デバッガを使用して参照してください。 – 1201ProgramAlarm
正方形が「外れ」であるが、その速度が次のフレームの「内側」に戻すのに十分高くない場合を考える。 – molbdnilo