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

Author Topic: Where am I meant to change protection level?  (Read 2127 times)

0 Members and 1 Guest are viewing this topic.

Zuzanno

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Where am I meant to change protection level?
« on: April 17, 2020, 08:02:53 pm »
Hello everyone, I'm trying to translate the cars game from the "Lets make 16 games in c++" video, form c++ to c# since I'm trying to learn how to and get familiarized with it, however, I've encountered a major problem and it is, My console is saying that  "Program.Car.move() is not accessible due to protection level", however I already made sure that everything is public so I don't know what really is going on here, I'd like to get some help with that and also with the last part of the code which is saying that "you cant convert the type 'double' to 'int' (check if you're missing a conversion)" or something like that /*Sorry I'm using VS in Spanish*/

I'd be really thankful if someone could tell me what's going on asap, friendly reminder, this is my first time using SFML in general and I knew c++ as well as c# so I'm trying to translate it, if something is wrong please point it out too so I can fix it or at least try to! thank you all

using System;
using SFML.Graphics;
using SFML.Window;
using SFML.Audio;
using SFML.System;

namespace SFMLCars
{
    class Program
    {
        const int num = 8;
        static int[,] points = new int[num, 2] {{300, 610},
                      { 1270, 430},
                      {1380,2380},
                      {1900,2460},
                      {1970,1700},
                      {2550,1680},
                      {2560,3150},
                      {500, 3300}};

        public struct Car
        {
            public double x, y, speed, angle;
            public int n;

            public Car(double speed,double angle, int n)
                : this()
            {  
                speed = 2;
                angle = 0;
                n = 0;
            }

            void move()
            {
                x += Math.Sin(angle) * speed;
                y -= Math.Cos(angle) * speed;
            }

            void findTarget()
            {
                double tx = points[n, 0];
                double ty = points[n, 1];
                double beta = angle - Math.Atan2(tx - x, -ty + y);
                if (Math.Sin(beta) < 0)
                {
                    angle += 0.005 * speed;
                }
                else
                {
                    angle -= 0.005 * speed;
                }
                if ((x - tx) * (x - tx) + (y - ty) * (y - ty) < 25 * 25)
                {
                    n = (n + 1) % num;
                }
            }


        };
        static void OnClose(object sender, EventArgs e)
        {
            RenderWindow window = (RenderWindow)sender;
            window.Close();
        }
        static void Main(string[] args)
        {
            RenderWindow app = new RenderWindow(new VideoMode(640,480), "Car Racing Game", Styles.Default);
            app.SetFramerateLimit(60);
            app.SetVerticalSyncEnabled(true);
            app.DispatchEvents();
            app.Clear();
            Texture t1 = new Texture("c://images/background.png");
            Texture t2 = new Texture("c://images/car.png");
            t1.Smooth = true;
            t2.Smooth = true;

           Sprite sBackground = new Sprite (t1);
           Sprite sCar = new Sprite (t2);
           sBackground.Scale = new Vector2f(2, 2);

           sCar.Origin = new Vector2f(22, 22);
           float R = 22f;

            const int N = 5;
            Car[] car = new Car[N];

            for (int i = 0; 0 < N; ++i)
            {
                car[i].x = 300 + (i * 50);
                car[i].y = 1700 + i * 80;
                car[i].speed = 7 + i;
            }

            float speed = 0f;
            float angle = 0f;
            float maxSpeed = 12.0f;
            float acc = 0.2f;
            float dec = 0.03f;
            float turnSpeed = 0.08f;

           int offsetX = 0;
           int offsetY = 0;

            while (app.IsOpen)
            {
                Event e;
                app.Closed += new EventHandler(OnClose);

                app.Display();
            }

            bool up = false;
            bool down = false;
            bool right = false;
            bool left = false;

            if (Keyboard.IsKeyPressed(Keyboard.Key.Up))
            {
                up = true;
            }
            if (Keyboard.IsKeyPressed(Keyboard.Key.Right))
            {
                right = true;
            }
            if (Keyboard.IsKeyPressed(Keyboard.Key.Down))
            {
                down = true;
            }
            if (Keyboard.IsKeyPressed(Keyboard.Key.Left))
            {
                left = true;
            }

            //movement

            if (up && speed < maxSpeed)
            {
                if (speed < 0)
                {
                    speed += dec;
                }
                else
                {
                    speed -= acc;
                }
            }
            if (down && speed >- maxSpeed)
            {
                if (speed > 0)  
                {
                    speed -= dec;
                }
                else
                {
                    speed -= acc;
                }
            }
            if (!up && !down)
            {
                if (speed - dec > 0)
                {
                    speed -= dec;
                }
                else if(speed + dec < 0)
                {
                    speed += dec;
                }
                else
                {
                    speed = 0;
                }
            }
            if (right && speed != 0)
            {
                angle += turnSpeed * speed / maxSpeed;
            }
            if (left && speed != 0)
            {
                angle -= turnSpeed * speed / maxSpeed;
            }

            car[0].speed = speed;
            car[0].angle = angle;

            for (int i = 0; i < N; ++i)
            {
                car[i].move();
            }
            for (int i = 0; i < N; ++i)
            {
                car[i].findTarget();
            }

            for (int i = 0; i < N; ++i)
            {
                for(int j = 0; i < N; ++j)
                {
                    int dx = 0;
                    int dy = 0;
                    while (dx * dx + dy * dy < 4 * R * R)
                    {
                        car[i].x += dx / 10.0;
                        car[i].x += dy / 10.0;
                        car[j].x += dx / 10.0;
                        car[j].x += dy / 10.0;
                        dx = car[i].x - car[j].x;
                        dy = car[i].y - car[j].y;
                    }
                }
            }
        }
    }  
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Where am I meant to change protection level?
« Reply #1 on: April 18, 2020, 09:17:59 pm »
In C# you need to specify the protection level for each function, since you didn't provide a protection level for the move() function of the Car class.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Zuzanno

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Where am I meant to change protection level?
« Reply #2 on: April 24, 2020, 03:57:05 am »
Do you know where can I find the SMFL Documentation for C#?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Where am I meant to change protection level?
« Reply #3 on: April 24, 2020, 10:40:30 am »
You'd need to build it from source. I might look into providing the documentation on the website download page as well.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Zuzanno

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Where am I meant to change protection level?
« Reply #4 on: April 27, 2020, 05:46:15 am »
It'd be great if you do, A documentation would come handy for those who have no clue on what they're doing, like me LOL thank you

 

anything