9 Replies - 3245 Views - Last Post: 05 August 2012 - 03:13 AM Rate Topic: -----

#1 weizhou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-August 12

Replicating stacker game in C

Posted 04 August 2012 - 01:53 AM

Hello and good day. I am currently having programming classes studying C++. I need help on a certain project that i am given. I have searched online and tried reading C resource books and they didn't help me as much. Since i have started with C++, i am having a hard time understanding C, and it would be a big help if you guys could help me on this.

So far, i need to know how to make a moving object, (as it is one of the most important points in the project)
i would also like to receive your opinions on how i should start doing this project, thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Replicating stacker game in C

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2219
  • View blog
  • Posts: 4,302
  • Joined: 30-May 10

Re: Replicating stacker game in C

Posted 04 August 2012 - 02:26 AM

> So far, i need to know how to make a moving object, (as it is one of the most important points in the project)
Well to do anything "graphics", you need to tell us more about your environment.
Such as
- what is your operating system?
- what compiler are you using?
- what graphics library are you using (or are you still undecided)?
Was This Post Helpful? 0
  • +
  • -

#3 weizhou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-August 12

Re: Replicating stacker game in C

Posted 04 August 2012 - 02:34 AM

I am running windows 7,
using DevC++ for my C compiler.

As for the graphics library, the project only requires you to run it on cmd, since its still just a simple project (although i admit i still don't know how to work around in C)
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6449
  • View blog
  • Posts: 22,080
  • Joined: 05-May 12

Re: Replicating stacker game in C

Posted 04 August 2012 - 02:42 AM

Are you supposed to use the keyboard for entering directions, or also use the mouse for moving the blocks?
Was This Post Helpful? 0
  • +
  • -

#5 weizhou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-August 12

Re: Replicating stacker game in C

Posted 04 August 2012 - 03:26 AM

The blocks are supposed to move constantly from left to right in a predefined space until the spacebar is pressed.

You start with three 'blocks' and it gets reduced as you progress or make errors.
For each consecutive press, the moving blocks stay behind and act as a tower, that when you overshoot a block, it gets reduced.

* I am sorry for the double post, i couldn't find the edit button*

I can also show the whole problem file for you guys to better understand my problem, if that helps.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6449
  • View blog
  • Posts: 22,080
  • Joined: 05-May 12

Re: Replicating stacker game in C

Posted 04 August 2012 - 11:21 AM

To get back to your basic question: how do you make an object move?

You do that by tracking the position of the object. In the code below, I don't actually track the position, but compute where the object should be based an an angle increment. The angle increment is adjusted every 1/10 of second.

Have fun!

#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <cmath>

using namespace std;

float ElapsedSeconds(clock_t now, clock_t past)
{
    return float(now - past) / CLOCKS_PER_SEC;
}

int ComputeX(float angle, int screenWidth, int nameLength)
{
    screenWidth -= nameLength;
    screenWidth /= 2;
    return int(sin(angle) * screenWidth + screenWidth);
}

void PrintName(const string & name, int x)
{
    for(int i = 0; i < x; i++)
        cout << " ";
    cout << name << endl;
}

int main()
{
    const float Pi = 3.14159;
    const float Increment = (Pi / 180.0) * 5.0;
    const float UpdateRate = 0.1;
    const int ScreenWidth = 80;
    const string name = "weizhou";

    float angle = 0.0f;
    clock_t lastTime = clock();
    PrintName(name, ComputeX(angle, ScreenWidth, name.length()));
    while(true)
    {
        clock_t currentTime = clock();
        if (ElapsedSeconds(currentTime, lastTime) > UpdateRate)
        {
            angle += Increment;
            if (angle > 2 * Pi)
                angle -= 2 * Pi;
            PrintName(name, ComputeX(angle, ScreenWidth, name.length()));
            lastTime = currentTime;
        }
    }
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#7 weizhou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-August 12

Re: Replicating stacker game in C

Posted 05 August 2012 - 01:55 AM

Wow, thanks!
This helps me a lot for my C++ subjects, although i don't mean to offend but it doesn't for my current project, which requires it to be in C.
I am sorry for making such a greedy request, but i really need help doing this in C.

Thank you!
On another note, what compiler do you use for C?
Was This Post Helpful? 0
  • +
  • -

#8 weizhou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-August 12

Re: Replicating stacker game in C

Posted 05 August 2012 - 02:20 AM

Btw, can i just add a do-while on the output process so that when i press the spacebar, it stops and records its position? (since i need it for the actual game, which will be done on an array)

I am sorry but i am new to programming
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6449
  • View blog
  • Posts: 22,080
  • Joined: 05-May 12

Re: Replicating stacker game in C

Posted 05 August 2012 - 02:26 AM

Only the I/O and string manipulation was C++ specific in the previous code. Here is a C version:

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>

float ElapsedSeconds(clock_t now, clock_t past)
{
    return float(now - past) / CLOCKS_PER_SEC;
}

int ComputeX(float angle, int screenWidth, int nameLength)
{
    screenWidth -= nameLength;
    screenWidth /= 2;
    return int(sin(angle) * screenWidth + screenWidth);
}

void PrintName(const char * name, int x)
{
    for(int i = 0; i < x; i++)
        putchar(' ');
    printf("%s\n", name);
}

int main()
{
    const float Pi = 3.14159;
    const float Increment = (Pi / 180.0) * 5.0;
    const float UpdateRate = 0.1;
    const int ScreenWidth = 80;
    const char *name = "weizhou";
    const int nameLength = strlen(name);

    float angle = 0.0f;
    clock_t lastTime = clock();
    PrintName(name, ComputeX(angle, ScreenWidth, nameLength));
    while(true)
    {
        clock_t currentTime = clock();
        if (ElapsedSeconds(currentTime, lastTime) > UpdateRate)
        {
            angle += Increment;
            if (angle > 2 * Pi)
                angle -= 2 * Pi;
            PrintName(name, ComputeX(angle, ScreenWidth, nameLength));
            lastTime = currentTime;
        }
    }
    return 0;
}



View Postweizhou, on 05 August 2012 - 02:20 AM, said:

Btw, can i just add a do-while on the output process so that when i press the spacebar, it stops and records its position? (since i need it for the actual game, which will be done on an array)

I am sorry but i am new to programming


You can't continue to loop and wait for the spacebar in a platform independent manner.

To find out if the spacebar has been pressed, you'll have to pick your platform and see what your compiler's runtime library offers you for I/O routines. Most of them will have some form of _kbhit() function that will tell you if a key has been pressed, and a _getch() function that will let you get a key from the keyboard without waiting. If you'r compiler's library doesn't have any built in support, and you know you'll be on Windows, you can also call the Windows API's to determine keypresses.
Was This Post Helpful? 0
  • +
  • -

#10 weizhou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-August 12

Re: Replicating stacker game in C

Posted 05 August 2012 - 03:13 AM

Thanks, i will post an update when i do any progress on my code.

Thanks a lot for the help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1