Ранее я заканчиваю программу, щелкая по мячу, чтобы он двигался и отскакивал всякий раз, когда мяч достигал поля. Однако новая проблема заключается в том, чтобы заставить мяч двигаться после перетаскивания и отпускания кнопки мыши. После отпускания кнопки мыши мяч должен двигаться в противоположном направлении.
вот мой код в файл main.cpp и ball.h
main.cpp
#include
#include
#include
#include "defs.h"
#include "graphics.h"
#include "object.h"
#include "ball.h"
using namespace std;
void waitUntilKeyPressed() {
SDL_Event e;
while (true) {
if (SDL_PollEvent(&e) != 0 && (e.type == SDL_KEYDOWN || e.type == SDL_QUIT))
return;
SDL_Delay(100);
}
}
int main(int argc, char* argv[])
{
Graphics graphics;
graphics.init();
SDL_Texture* background = graphics.loadTexture(BACKGROUND_IMG);
SDL_Texture* ball_img = graphics.loadTexture(BALL_IMG);
Ball ball;
bool mouseDown;
bool mousePressed;
bool quit = false;
SDL_Event event;
while (!quit) {
while (SDL_PollEvent(&event) != 0) {
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT) {
mouseDown = true;
mousePressed = true;
}
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT) {
mouseDown = false;
}
break;
}
}
graphics.prepareScene(background);
graphics.renderTexture(ball_img, ball.getPosition().x, ball.getPosition().y);
graphics.presentScene();
ball.update(mouseDown, mousePressed);
}
graphics.quit();
return 0;
}
ball.h
#ifndef _GOLFBALL__H
#define _GOLFBALL__H
#include
#include
#include
#include "defs.h"
#include "object.h"
class Ball
{
public:
Ball(){
velocity.x = 0;
velocity.y = 0;
position.x = initialPositionX;
position.y = initialPositionY;
}
void update(bool mouseDown, bool mousePressed) {
if (mousePressed && !moving) {
int mouseX = 0;
int mouseY = 0;
SDL_GetMouseState(&mouseX, &mouseY);
setInitialMousePosition(mouseX, mouseY);
}
if (mouseDown && moving) {
int mouseX = 0;
int mouseY = 0;
SDL_GetMouseState(&mouseX, &mouseY);
setFinalMousePosition(mouseX, mouseY);
double velocity1D = getDistance(initialMousePosition, finalMousePosition);
setVelocity(velocity1D, velocity1D);
} else {
float absVelocityX = std::abs(velocity.x);
float absVelocityY = std::abs(velocity.y);
velocity.x *= friction;
velocity.y *= friction;
if (absVelocityX < velocityThreshold && absVelocityY < velocityThreshold) {
velocity.x = 0;
velocity.y = 0;
moving = false;
}
position.x += velocity.x;
position.y += velocity.y;
if (position.x = SCREEN_WIDTH - ballSize) velocity.x *= -1;
if (position.y = SCREEN_HEIGHT - ballSize) velocity.y *= -1;
}
}
void setVelocity(double _x, double _y) {
velocity.x = _x;
velocity.y = _y;
}
Vector getPosition() {
return position;
}
void startMoving() {
moving = true;
}
bool clicked(int mouseX, int mouseY) {
return (mouseX >= position.x && mouseX = position.y && mouseY
Подробнее здесь: https://stackoverflow.com/questions/782 ... -in-sdl2-c
Перетащите, чтобы переместить мяч в SDL2/C++ [закрыто] ⇐ C++
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение