make dot change position

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
TheNewGuy
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 4
Joined: Thu Apr 02, 2009 7:08 pm

make dot change position

Post by TheNewGuy »

i wanted to make the dot go from that position to the next. sort of like walking into a shop.

i tried this, but it would just show the dot at the position (100, 100)

how would i make the dot move to that position?

Code: Select all

if( x == 0 && y == 0 )
{
    apply_surface( x + 100, y + 100, dot, screen );
}
User avatar
Spikey
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 98
Joined: Sat Dec 13, 2008 6:39 am
Programming Language of Choice: C++
Location: Ottawa, Canada
Contact:

Re: make dot change position

Post by Spikey »

Something like this?

Code: Select all

// Earlier in program, declare variables
int x = 0;     // Declare variables, they should start at 0 or something, not left blank.
int y = 0;  


// Later in program, Update function
if ( x == 0 && y == 0 )      // If position is 0,0 then set position to 100,100
{
     x = 100;
     y = 100;
}

if ( x == 100 && y == 100 )  // If position is 100,100 then set position to 0,0
{
     x = 0;
     y = 0;
}

// Render surface
apply_surface( x, y , dot, screen );

This might be better, handle key inputs and update dot position based on what keys are pressed.

Code: Select all

//Somewhere earlier on
int x = 0;     // Declare variables, they should start at 0 or something, not left blank.
int y = 0;  


// Handle Input (Pseudo code)
GetInput();                                 // Get input

if ( PlayerPressesUp == true )       // Decide how input effects dot position 
{
     y = y + 100;
}

if ( PlayerPressesDown == true )
{
     y = y - 100;
}


// Render surface
apply_surface( x, y, dot, screen );

TheNewGuy
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 4
Joined: Thu Apr 02, 2009 7:08 pm

Re: make dot change position

Post by TheNewGuy »

i get the dot to move and all, but when the dots position is 0,0 i want the dot to jump to 100, 100. and the way i had it, it would just create a new instance of the dot at position 100,100. sorry if my first post was not clear on that.
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: make dot change position

Post by RyanPridgeon »

Are you clearing the background every time you draw? Otherwise the new dot will just be written straight to the screen, and the old one will remain, unil it's overwritten.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
TheNewGuy
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 4
Joined: Thu Apr 02, 2009 7:08 pm

Re: make dot change position

Post by TheNewGuy »

how do I clear the screen? i tried freeing the surface of the dot but that didn't work.
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: make dot change position

Post by RyanPridgeon »

No. Freeing the surface would delete the image data for the dot. That would not solve anything.

To clear the screen, you can either apply a background image, or draw a large black rectangle (using SDL_FillRect())

I also suggest you learn more of the C++ language and practise more programming before you attempt any more SDL... I don't mean to be offensive. Sorry if I am. But I made your mistake and it wasted a good month or so. Learn the language first.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
thejahooli
Chaos Rift Junior
Chaos Rift Junior
Posts: 265
Joined: Fri Feb 20, 2009 7:45 pm
Location: London, England

Re: make dot change position

Post by thejahooli »

RyanPridgeon wrote:No. Freeing the surface would delete the image data for the dot. That would not solve anything.

To clear the screen, you can either apply a background image, or draw a large black rectangle (using SDL_FillRect())

I also suggest you learn more of the C++ language and practise more programming before you attempt any more SDL... I don't mean to be offensive. Sorry if I am. But I made your mistake and it wasted a good month or so. Learn the language first.
exactly the same with me,watch the first minute and a half or so of gyro vorbis' getting started part 2 video ( http://www.youtube.com/watch?v=OaxckMNq0eU ) to see what you should already know.
I'll make your software hardware.
Ewan
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Mon Mar 23, 2009 11:46 am

Re: make dot change position

Post by Ewan »

Maybe I don't quite understand your problem, but why are you drawing it at x+100 and y+100? Don't you want to MOVE the dot to 100,100, not just draw it there?

If you want to make it move, you just do this:

Code: Select all

if( x == 0 && y == 0 )
{
    x = 100;
    y = 100;
}
and presumably you have

Code: Select all

apply_surface( x, y, dot, screen );
in your game loop, so it will automatically be drawn wherever it is.
They pull out the truncheon, that's when the trouble starts.

'Cause when you've got a badge, the laws don't apply.
Post Reply