Snake Game Tutorial 14a

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 4266
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Snake Game Tutorial 14a

Post by albinopapa » October 2nd, 2018, 8:39 pm

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

ArmSun
Posts: 5
Joined: August 5th, 2018, 7:41 pm

Re: Snake Game Tutorial 14a

Post by ArmSun » October 7th, 2018, 4:26 pm

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.

TATO
Posts: 1
Joined: October 29th, 2019, 2:49 pm

Re: Snake Game Tutorial 14a

Post by TATO » November 6th, 2019, 5:39 pm

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.

albinopapa
Posts: 4266
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Snake Game Tutorial 14a

Post by albinopapa » November 6th, 2019, 7:40 pm

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();
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4266
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Snake Game Tutorial 14a

Post by albinopapa » November 6th, 2019, 7:42 pm

I make this mistake quite often when I'm not paying attention or when I copy/paste code around.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

ttdgrin
Posts: 3
Joined: September 1st, 2020, 4:18 pm

Re: Snake Game Tutorial 14a +b

Post by ttdgrin » September 1st, 2020, 4:53 pm

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.

albinopapa
Posts: 4266
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Snake Game Tutorial 14a

Post by albinopapa » September 1st, 2020, 7:29 pm

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

ttdgrin
Posts: 3
Joined: September 1st, 2020, 4:18 pm

Re: Snake Game Tutorial 14a

Post by ttdgrin » September 1st, 2020, 7:59 pm

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,

albinopapa
Posts: 4266
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Snake Game Tutorial 14a

Post by albinopapa » September 2nd, 2020, 8:22 am

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
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

ttdgrin
Posts: 3
Joined: September 1st, 2020, 4:18 pm

Re: Snake Game Tutorial 14a

Post by ttdgrin » September 2nd, 2020, 11:54 am

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 =)

Post Reply