Need help with basic movement (without key or mouse)

Hello guys, i’m new here, and i trying learn SDL2 for make my own games.
Long short history. I have two years code on pygame, is fun but is not power enough. On python i have, maybe, three years on experience… Not much, but i trying my best to learn.
I never code on c/c++. On the past three month’s i trying figure out how c++ works, and how sdl2 too.
I’m on basic’s. I have some class’s, for the player and etc.

My question is,
I have this code: (is just a test)

And move the player on the x axis, when the destination rect X make the screen boundaries, i want the player move back, and when make 0 move forward.
The problem is, when the player make 640 he stuck. Won’t move back at all. I look on Aaron’s tutorial’s, on Lazy Foo’, on reddit, google. I search for bounce ball, but i can’t figure out how make this simple movement.

PS: If needed, the player inherits from base_sprite, all my code is on github.

Have a movement/speed variable that is added to x. First movement.x could be positive (like 1), and your objects moves to right. When it’s far enough, change your movement.x to negative (like -1), and now the object should move “backwards”.

What your doing is
Move the x position 1 pixel to the right
If the x position is greater then 640, move the x position back 1 pixel
repeat.

As soon as it gets to the edge of the screen it will stay still because all your doing is moving it forward 1 pixel and back 1 pixel each update.

Here is a quick fix to get the intended behaviour

int _xvel = 1;
void Player::update() {
   _dstRect.x += _xvel;
   if (_dstRect.x > 640) {
       _xvel = -1;
   } else if (_dstRect.x < 0) {
       _xvel = 1;
   }
}
1 Like

Oh, thank a lot.
Now i see my mistake.
The object/player must have a velocity and the velocity must be initialize outside of update.

Again, thanks for have time to help me! =]

“and the velocity must be initialize outside of update.”

Not necessarily, you could do this

void Player::update() {
   int32_t_xvel = 1;
   if (_dstRect.x > 640) {
       _xvel = -1;
   } 
   _dstRect.x += _xvel;
}

I am just using this example to help you understand fully,
I would recommend you use the _xvel and _yvel in the sprite base class.

No worries, it happens to the best of us.
Good luck with the game development, looking forward to see what you create :slight_smile:

Keep us posted

1 Like

Thanks again! Sorry for the delay.
Yeah, my history is quite crazy… I start using javascript, p5.js is the library, but i never like java or js, not work for me.
When i discovery python i like it, is friendly and etc, but is not too power for this kind of thing. Is a lot of fun code on python, is easy, you don’t need to worry about “low staff” like memory management and etc.

And i keep back and forward with c/c++. I tried to learn a long time ago, but for study alone and my 14 years old me, i simply gave up.
My child’s dream is to make games, and now i realize if I’m being serious i have to make my own tool, api/engine and work with my own creation. I know godot, rpg maker, coco2d are good tools but i want to make my own you know.

Sorry for the text.
Again, thanks a lot making time for help me.

Maybe you should give Go a try (https://golang.org/). It fills a niche
between the very low level of C++ where you have to be concerned about
memory management and the high level of scripting or virtual machine
languages like Python and Java.

MSB

If you ever need help with cpp just message. I will be more then happy to help you create your game.

1 Like

Oh that’s nice. Thank you.
Can you pass me your discord?

I will not bother you much hahaha!