get two objects to update at the same time

Hello, I am doing a remake of the classic game: Frogger. Currently, I have the world set up, the frog moves about in the world how I want it to, but I cannot get the cars or the logs to move simultaneously with the frog. I have posted my source code on GitHub. If anyone would take a look at it and offer suggestions or tips, that would be greatly appreciated.

https://github.com/lanmonster/frogger

Please and thank you!
closed account (3hM2Nwbp)
If you are using the console, then you will have to thread your user input and gameplay separately. Perhaps you could use a 2D library to accomplish this for you? SFML comes to mind -- great for simple 2D drawing. If you are dead-set on using the console, check out std::thread.
You're trying to use the console for something that it was never designed to do. Switching to a library like SDL or SFML is the correct solution.

In main.cpp line 51 you enter a loop to draw logs:
1
2
    for( int i = 1; i < 6; i++ )
		game.r.logs[i-1].drawLog(i);


After the loop has finished you then check movement

1
2
3
4
5
6
7
8
    while( ch != 'q' && ch != 'Q' && game.f.getWinCount() != 5 && game.f.getNumLives() > 0)
    {
        if( _kbhit() )
        {
            ch = _getch();
            game.update(ch);
        }
    }


What you need to do is perform those log updates without getting locked in a loop.

I agree with Computergeek01 but since you have done a lot of work already I am guessing you would prefer to continue and try complete it.

Just to extend on my last post to throw some ideas at you; what you can do to avoid this loop lock is to modify your log class slightly to look like below. The modification is basically removing the class loops and updating the log positions each time the drawLog member function is called.

log.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef LOG_H_INCLUDED
#define LOG_H_INCLUDED
#include "Object.h"

#define LOG_SPEED	10;  // tweak this

class Log : public Object
{
private:
	int timer;
public:
	Log() { timer = LOG_SPEED; }
    void drawLog(int);
};

#endif // LOG_H_INCLUDED 


Log.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "Log.h"

void Log::drawLog(int lane)
{
    p.setColor(white);
	// reduce timer counter
	timer--;
    switch(lane)
    {
    case 1:
        rowNum = 3;
		// is timer 0?
		if (timer==0) {
			// yes its zero, time to update
			p.plot(colNum, 3, SQUARE);
			p.plot(colNum, 4, SQUARE);
			p.setColor(darkblue);
			p.plot(colNum - 10, 3, SQUARE);
			p.plot(colNum - 10, 4, SQUARE);
			p.setColor(white);
			colNum++;
			if (colNum == 85) colNum = 0;
			timer = LOG_SPEED; // reset back timer
		}

        break;

    case 2:
        rowNum = 8;
		if (timer == 0) {
			p.plot(colNum, 8, SQUARE);
			p.plot(colNum, 9, SQUARE);
			Sleep(50);
			p.setColor(darkblue);
			p.plot(colNum - 10, 8, SQUARE);
			p.plot(colNum - 10, 9, SQUARE);
			p.setColor(white);
			colNum++;
			if (colNum == 85) colNum = 0;
			timer = LOG_SPEED;
		}
        break;

    case 3:
        rowNum = 13;
		if (timer == 0) {
            p.plot(colNum, 13, SQUARE);
            p.plot(colNum, 14, SQUARE);
            Sleep(50);
            p.setColor(darkblue);
            p.plot(colNum-10, 13, SQUARE);
            p.plot(colNum-10, 14, SQUARE);
            p.setColor(white);
            colNum++;
			if (colNum == 85) colNum = 0;
			timer = LOG_SPEED;
        }
        break;

    case 4:
        rowNum = 18;
		if (timer == 0) {
			p.plot(colNum, 18, SQUARE);
            p.plot(colNum, 19, SQUARE);
            Sleep(50);
            p.setColor(darkblue);
            p.plot(colNum-10, 18, SQUARE);
            p.plot(colNum-10, 19, SQUARE);
            p.setColor(white);
            colNum++;
			if (colNum == 85) colNum = 0;
			timer = LOG_SPEED;
        }
        break;

    case 5:
        rowNum = 23;
		if (timer == 0) {
            p.plot(colNum, 23, SQUARE);
            p.plot(colNum, 24, SQUARE);
            Sleep(50);
            p.setColor(darkblue);
            p.plot(colNum-10, 23, SQUARE);
            p.plot(colNum-10, 24, SQUARE);
            p.setColor(white);
            colNum++;
			if (colNum == 85) colNum = 0;
			timer = LOG_SPEED;
        }
        break;
    }
}


and finally changes to Main.cpp, modify the While loop so that the drawing is done in there like...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

    while( ch != 'q' && ch != 'Q' && game.f.getWinCount() != 5 && game.f.getNumLives() > 0)
    {
        if( _kbhit() )
        {
            ch = _getch();
            game.update(ch);
        }
		else
		{
			// frog
			game.f.drawFrog(game.f.getColor());
			// logs
			for (int i = 1; i < 6;i++)
				game.r.logs[i-1].drawLog(i);
		}

    }


Basically avoid loops that will hold up your code because regardless of where or why, you will hold up the rest of the drawing. Also avoid Sleep as that slows your whole game.

With the above it would be easy to adapt it so that the logs can move at different speeds.

Luc, I agree that the console is not the best option, but it was for my comp sci class. Prof said we had to use the console.

Softrix, thank you for your help! However, when the logs are drawn, some funky things are happening. they are not in the shape of rectangles anymore.

Here is a picture of the code running after a little bit. http://imgur.com/nE1b14x

Have you made the modifications correctly, worked fine earlier when I wrote the changes in.

Heres the modified code that I did - I didnt do any extra coding other than remove the loops in the class.

https://www.dropbox.com/s/yrp1e0at4u5n76k/frogger.zip

all I did was copy from your post earlier, and then paste into my source

Try the dropbox one I linked, I just tried it again and logs draw as they should and you can move the frog. Ive not made any other alterations such as clearing out the log at position 85 before starting again - that's just minor things which you can easily fix.

The code was really in connection with your problem of movement.

:)
I just tried it and it is still not working. I even rebuilt the entire project.

Thats weird as hell lol - I did a 17 second recording of it here, it runs slower because of the crappy free recorder I used on this machine - I usually use camtasia but not got it back on yet.

Anyway, video shows the logs moving (be in in the same column positions but that's not the point of this) and demonstrates that the frog and logs can move together.

https://www.dropbox.com/s/2i6ngf0cigysfc9/test.avi

I have no idea why its failing your end - out of interest what compiler are you using?

Edit: Video shows a resize of the start screen; I did that because when I pressed space it would resize off the initial window recording dimensions.
Last edited on
I figured out the problem. I had not initialised colNum so it was using trash values. derp :P

My compiler is mingw32-g++
Great :)


Actually just noticed the file date changes and realised you had updated your code, so I though id have a peek.. ;-)

I see you have the cars now working, and both the logs and cars move in different directions... you've been busy lol

Glad your making progress with it.



Yeah, I have been working on it. My next issue is getting it to handle collisions with the cars and getting/staying on the logs as they move. That is going to take some thinking.

Loop through the objects of log reading the row and columns and compare with your frogs column and row, this would give what log you have collided with, with that information you could update the frogs column position with that of the log until the frog moves off in the row direction.

Same with the cars apart from updating the frogs position.
Topic archived. No new replies allowed.