4 Replies - 3981 Views - Last Post: 16 March 2012 - 04:07 AM Rate Topic: -----

#1 MMag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-March 12

How to make more than one pictureBox that move in a Form

Posted 14 March 2012 - 01:30 AM

Hello,

I (and other 4 people) am making a game, more like this game. I'm now trying to make the cars (enemies that will hit the frog). I'm a pretty much beginner in C#, so I just made a timer and when it ticks, the pictureBox.Location.X's would increase by 2, but I don't think what I did is right, as I could only make one picture box that moves infinitely.

So, how can I approach making several cars that move horizontally?

I also want to know how to make each car as an object.

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: How to make more than one pictureBox that move in a Form

#2 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: How to make more than one pictureBox that move in a Form

Posted 14 March 2012 - 03:16 AM

Each car should definitely be it's own object class, that way you can create one object with all the properties the car requires e.g. size of car image, start position, speed, etc.

And any generic methods for the car, for example... if you had a method to swap out the car image with a different one.

An example, this won't build.
class Car
{
public string carSpeed { get; set; }
public Image carType { get; set; }

public void TurnOnHeadlights(bool x)
{
if (!x)
 {
   carimage = lightsOn.jpg
 }
else
 {
  carimage = lightsOff.jpg
 }
}

Car() {}

}


Then you can create multiple instances of the Car object (with different referencing names) but still set their properties differently, for example you wanted a faster car or you wanted some cars to look like Ferarri's and some to be Ford's.

Then you would treat the Car object like you are using PictureBox now.

I'm not going to work out the logic of your program for you, as to be honest that is the hard part but I'd suggest you look into loops, especially the while loop if you decide to continue using a timer.

This post has been edited by negligible: 14 March 2012 - 03:25 AM

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5523
  • View blog
  • Posts: 11,834
  • Joined: 02-June 10

Re: How to make more than one pictureBox that move in a Form

Posted 14 March 2012 - 05:31 AM

Something from an earlier question on a similar question.

tlhIn`toq said:

If what you really need is an explaination of the concepts of classes as the title suggests, maybe this will help:

Think of objects in coding just as you would objects in the real world.

A Dodge Ram is an object.
It is made up of smaller objects: Engine, doors, tires
Each of those is made up of smaller objects: Bolts, pistons, etc.

Objects in coding can inherit from each other, usually from the general to the specific.

  • Class vehicle
    • Class truck : vehicle
      • Class Ram : Truck
        • class 2500FWD : Ram


A class is the blueprint for instanciating (making an instance of) the object.

DodgeRam is a class describing how to make an instance, but itself is not an actual thing you can interact with.

myDodgeRam is an instance of a the class DodgeRam

I can do things with the object instance myDodgeRam

myDodgeRam.SerialNumber = 123456789;
myDodgeRam.FillUpTank();
float fuelLevel = myDodgeRam.FuelTankPercentageFull;
if (myDodgeRam.IsReady) myDodgeRam.StartEngine();


Anything defined in the base class is available to a child.

class truck : vehicle
{

    public float FuelTankPercentageFull
    {
       get; set;
    }
}

class DodgeRam : truck
{
    // I don't have to define a FuelTankPercentageFull here because I inherit it from my parent
}


Methods defined as virtual in the base class can be overridden by the child class (at least in .NET languages like C#). This is often to account for more specific needs.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Do something to start the engine
       return true; // No checks or requirements
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return true;
        }
        return false;
    }
}


You can even have a child class call the base classes methods which is often the smart way to go.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Notice there are no safety checks before trying to start up.
       try
       {
          // Do something to start the engine
          return true; // because we succeeded
       }
       catch(exception error)
       {
           return false; // because there was an error
       }
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return base.StartEngine();
            // Now all the electrical work is in the base class
            // and not repeated in every child.
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#4 MMag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-March 12

Re: How to make more than one pictureBox that move in a Form

Posted 16 March 2012 - 03:40 AM

Thanks very much for the help, really appreciated.

So, it will be like a car starts moving, a couple of seconds pass, then another car moves, etc. for infinite times. How could this be done? As I mentioned, I'm a beginner in C#, so I thought about, maybe, a timer and when it ticks, a new object is created.

Is this a good idea to approach this? or not? and are there any other ways concerning this idea (dynamically creating objects)?

Thanks very much again for help, and if there's anything unclear you can tell me about it.
Was This Post Helpful? 0
  • +
  • -

#5 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: How to make more than one pictureBox that move in a Form

Posted 16 March 2012 - 04:07 AM

well yes timer is a good idea. at every tick of the timer you create a new object (dynamically). you can make your object to be in array that way you can easy move all of them on your timer tick or when ever you want.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1