Page 7 of 7

Re: Snake Game Tutorial 14a

Posted: October 2nd, 2018, 8:39 pm
by albinopapa
What video card do you have?

I have an integrated GPU ( iGPU ) with about 1GB of system RAM reserved for graphics and don't have this problem, but if I lower the reserved to something like 64MB, then I do start running into the same issues.

Re: Snake Game Tutorial 14a

Posted: October 7th, 2018, 4:26 pm
by ArmSun
Mine is integrated graphics too. I didn't have thus issue until recently. I think it can be visual studio problem. It's 15.7.5 now.

Re: Snake Game Tutorial 14a

Posted: November 6th, 2019, 5:39 pm
by TATO
Hi chili and thanks for all. I'm verry happy to be in your school.
i don't understand somes erros code:
1)Severity Code Description Project File Line Suppression State
Error C3867 'Board::GetGridHeight': non-standard syntax; use '&' to create a pointer to member Engine c:\users\sergetato\desktop\c++\chili framework 2016\engine\game.cpp 59

2)Severity Code Description Project File Line Suppression State
Error C2446 '<': no conversion from 'int (__thiscall Board::* )(void) const' to 'int' Engine c:\users\sergetato\desktop\c++\chili framework 2016\engine\game.cpp 59

3)Severity Code Description Project File Line Suppression State
Error C3867 'Board::GetGridWidth': non-standard syntax; use '&' to create a pointer to member Engine c:\users\sergetato\desktop\c++\chili framework 2016\engine\game.cpp 61

4)Severity Code Description Project File Line Suppression State
Error C2446 '<': no conversion from 'int (__thiscall Board::* )(void) const' to 'int' Engine c:\users\sergetato\desktop\c++\chili framework 2016\engine\game.cpp 61

thank you for your help.

Re: Snake Game Tutorial 14a

Posted: November 6th, 2019, 7:40 pm
by albinopapa
This usually means you tried calling a function without using the () at the end.
Here's probably what you did:
int width = board.GetGridWidth;
Here's what it should be:
int width = board.GetGridWidth();

Re: Snake Game Tutorial 14a

Posted: November 6th, 2019, 7:42 pm
by albinopapa
I make this mistake quite often when I'm not paying attention or when I copy/paste code around.

Re: Snake Game Tutorial 14a +b

Posted: September 1st, 2020, 4:53 pm
by ttdgrin
Hello, new to the forum and to the serie, and dam this is good.
Not sure if someone will see this but here we go.. =)

I've been trying to implement spawning blocks into the code after the 14a+14b + homework without success.
Would like some guidelines if possible. (or stright up a solution)
I've added "Ice" that slow the snake down when eaten and would like to add so that when you do take the ice, a Block will spawn at the end of the snake tail.

I couldn't figure out how to get location for the end tail to use as a spawning point for the new block.
So I tried making something more simple like randomly spawn a block from the Goal class, but could'nt figure out how to make more then one spawn at a time.

I downloaded the complete snek game from git, the code there is very different, since it use the board for all the spawns and also use enum + vector for it, i tried for a few hours to use some of the code for the tutorial code without any progress.

So in short, I would like to spawn blocks at random location when "ice" is eaten (that also can be detected for a hit check for gameover),
If possible instead of random location, spawn block at end of snake tail.

Re: Snake Game Tutorial 14a

Posted: September 1st, 2020, 7:29 pm
by albinopapa
For getting tail segment, add function to Snake class returning a Segment reference or just location if that's all you need.

The segment objects are stored in the std::vector<Segment> member, which means you can get the last element by calling segments.back().

Once you have the tail segment, you can call GetLocation() on that segment.

Code: Select all

class Snake{
private:
    class Segment{
    public:
        Location const& GetLocation(){...;}
    };

    Location const& GetTailLocation()const {
        return segments.back().GetLocation()
    }    
};
Once you have the location, to setup obstacles, you'll probably need to add another class.

Code: Select all

class BaseObstacle{
public:
    virtual void Draw( Board& brd )const = 0;
    Location const& GetLocation()const{ return loc; }
protected:
    Location loc = {};
};

class IceObstacle final: public BaseObstacle{
public:
    IceObstacle()=default;
    IceObstacle( Location const& loc_ )
        :
    loc( loc_ )
    {}
    void Draw( Board& brd )const override { 
        brd.DrawCell( loc, color ); 
    }
        
    }
private:
    static constexpr Color color = Color{ 95, 223, 255 };
};
Now, you'd need to test if snake head hits an ice obstacle.
if( snek.GetNextHeadLocation() == obstacle.GetLocation() ){ gameover = true; }

Not a complete implementation of things, but should give you some direction.

Re: Snake Game Tutorial 14a

Posted: September 1st, 2020, 7:59 pm
by ttdgrin
Thanks for the reply albinopapa, think I could work with that for abit.
Is it possible to get the location from the last piece in a array?(the snake segments are of type array in the tutorial, something like how the segments get the location from previous segment for movement.
(the MoveBy function) or the GetNextHeadLocation.
Thanks again,

Re: Snake Game Tutorial 14a

Posted: September 2nd, 2020, 8:22 am
by albinopapa
If it's an array, then you probably have a nSegments or something, so segments[nSegments-1] since indexing counts from 0 so the range of valid segments is 0 to nSegments-1

Re: Snake Game Tutorial 14a

Posted: September 2nd, 2020, 11:54 am
by ttdgrin
Thanks again for the help, with alot of testing back and forth im starting to grasp what is what.
I've now been able to implement a few functions in the snake class with an added subclass for the creation of blocks,
they now spawn when "ice" is eaten and also can be tested for collision in the game.cpp.
Almost there, trial and error ftw =)