Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Code Tips and Movement?  (Read 4083 times)

0 Members and 1 Guest are viewing this topic.

vipar

  • Newbie
  • *
  • Posts: 23
    • View Profile
Code Tips and Movement?
« on: May 19, 2013, 03:09:19 am »
I would like both!

I am a noob and have just started using SFML for .NET!

So far it seems fairly simple to use for the kind of game I am doing to learn.
The game I am doing at the moment is a car going back and forth on the X axis and then I guess will be able to pick up points and stuff or simply just avoid objects coming towards the car until it eventually dies.

Someone said in another thread to me that I should have designed my program differently seeing as I was handling things somewhat wrong. Here is my code:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using SFML.Audio;
using SFML.Graphics;
using SFML.Window;

namespace SFMLCarGame
{
    class Program
    {
        private static RenderWindow window;
        private static Sprite car;

        static void Main(string[] args)
        {
            window = new RenderWindow(new VideoMode(256,512), "Car Game");
            window.Closed += new EventHandler(OnClose);
            window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);

            Sprite bg = new Sprite(new Texture("road.png"));
            car = new Sprite(new Texture("car.png"));
            car.Position = new Vector2f(window.Size.X / 2, window.Size.Y - 96);
            while (window.IsOpen())
            {
                window.DispatchEvents();

                window.Clear();

                window.Draw(bg);
                window.Draw(car);

                window.Display();
            }
        }

        static void OnClose(object sender, EventArgs e)
        {
            RenderWindow window = (RenderWindow)sender;
            window.Close();
        }

        static void OnKeyPressed(object sender, EventArgs e)
        {
            Vector2f newPos = new Vector2f(0, car.Position.Y);
            KeyEventArgs ke = (KeyEventArgs)e;
            if (ke.Code.Equals(Keyboard.Key.A))
            {
                if (car.Position.X != 0)
                {
                    newPos.X = car.Position.X - 8;
                    car.Position = newPos;
                }
                else if (car.Position.X < 0)
                {
                    newPos.X = 0;
                    car.Position = newPos;
                }
                else if(car.Position.X == 0)
                {
                    // Do nothing
                }
            }
            else if (ke.Code.Equals(Keyboard.Key.D))
            {
                if (car.Position.X != window.Size.X - 32)
                {
                    newPos.X = car.Position.X + 8;
                    car.Position = newPos;
                }
                else if (car.Position.X > window.Size.X)
                {
                    newPos.X = window.Size.X;
                    car.Position = newPos;
                }
                else if (car.Position.X == window.Size.X)
                {
                    // Do nothing
                }
            }
        }
    }
}
 

Probably got something to do with my Main() loop.

My second question is: How do I make the movement smoother? Currently the car will move 8 units first, before moving 8 units in the desired direction until I let go of the button. It's the same when I do it the other way. I would like it to just move instantly in the opposite direction if I switch keys.

Thanks in advance!
« Last Edit: May 19, 2013, 03:11:06 am by vipar »

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Code Tips and Movement?
« Reply #1 on: May 19, 2013, 08:56:23 am »
I don't know if others would agree with me, but in my opinion code design is something that you kind of pick up on as you write more and more code. There are, of course, things that can help in addition to writing code such as reading and understanding other peoples' code, various book, and perhaps even classes.

The way your program is now, I wouldn't really say it's designed poorly. There isn't much too it yet. Sure, you might get some things that don't quite work how expected, but I say finding the problems and fixing them are part of the fun of programming. My advice is to keep working on what you're working on, and at the end of it take a step back and think on what you could have improved on in the various sections of code you had set up to get your game working. My only real experience with C# was with XNA so I can't really comment on how you are handling your events, but...


window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);

Maybe I am not familiar enough with SFML.net, but doesn't the KeyPress event only trigger when you press down on a key and then let go? If you are looking for fluid movement I think what you would want is the KeyDown event, which should trigger as long as a key is being pressed down. Something else you might consider is using real time Keyboard/Mouse/Joystick classes rather than processing events, but that's entirely up to you.
DSFML - SFML for the D Programming Language.

vipar

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Code Tips and Movement?
« Reply #2 on: May 19, 2013, 12:50:05 pm »
I don't know if others would agree with me, but in my opinion code design is something that you kind of pick up on as you write more and more code. There are, of course, things that can help in addition to writing code such as reading and understanding other peoples' code, various book, and perhaps even classes.

The way your program is now, I wouldn't really say it's designed poorly. There isn't much too it yet. Sure, you might get some things that don't quite work how expected, but I say finding the problems and fixing them are part of the fun of programming. My advice is to keep working on what you're working on, and at the end of it take a step back and think on what you could have improved on in the various sections of code you had set up to get your game working. My only real experience with C# was with XNA so I can't really comment on how you are handling your events, but...


window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);

Maybe I am not familiar enough with SFML.net, but doesn't the KeyPress event only trigger when you press down on a key and then let go? If you are looking for fluid movement I think what you would want is the KeyDown event, which should trigger as long as a key is being pressed down. Something else you might consider is using real time Keyboard/Mouse/Joystick classes rather than processing events, but that's entirely up to you.

There is no "KeyDown" Delegate though. Only KeyPressed and KeyReleased. KeyReleased wouldn't work seeing as I want continuous movement while the button is being pressed.

And to answer your doubts: No. The event is fired while the button is being held down until it is released.

krzat

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Code Tips and Movement?
« Reply #3 on: May 19, 2013, 02:37:14 pm »
KeyPressed repeats keys notepad-like (if you have SetKeyRepeatEnabled active), but you want to poll key every frame.

1. Use Keyboard.IsKeyPressed.
2. Use == instead of Equals.
3. Read something about game loop.
4. Don't mix input handling and collisions. First move your car, then check if borders have been crossed.
SFML.Utils - useful extensions for SFML.Net

Lamonte

  • Newbie
  • *
  • Posts: 46
    • View Profile
Re: Code Tips and Movement?
« Reply #4 on: August 15, 2013, 10:24:09 pm »
KeyPressed repeats keys notepad-like (if you have SetKeyRepeatEnabled active), but you want to poll key every frame.

1. Use Keyboard.IsKeyPressed.
2. Use == instead of Equals.
3. Read something about game loop.
4. Don't mix input handling and collisions. First move your car, then check if borders have been crossed.

Took me a while to figure out why my movement was bugged.  Creating my own function and just using the keyboard.iskeypressed method solved my issues.

                public void update_pos() {

                        float x, y;
                        float speed = 10;

                        x = display_sprite.Position.X;
                        y = display_sprite.Position.Y;

                        if(Keyboard.IsKeyPressed(Keyboard.Key.W)) {
                                y -= speed;
                        }

                        if(Keyboard.IsKeyPressed(Keyboard.Key.S)) {
                                y += speed;
                        }

                        if(Keyboard.IsKeyPressed(Keyboard.Key.A)) {
                                x -= speed;
                        }

                        if(Keyboard.IsKeyPressed(Keyboard.Key.D)) {
                                x += speed;
                        }

                        y = y < 0 ? 0 : y;
                        y = y + 10 > 720 ? 720 - 10 : y;

                        x = x < 0 ? 0 : x;
                        x = x + 10 > 1280 ? 1280 - 10 : x;

                        set_pos(x, y);
                }