win32 breakout

Started by
30 comments, last by jbadams 4 years, 9 months ago
6 minutes ago, phil67rpg said:

I am coding in c++,  should I use a list or a vector?

You CAN use both. You won't feel any difference for such a simple game.

But I would suggest that you use a list. Reason: Vectors may invalidate pointers and references to their members and this is an error which is hard to find for beginners.

However, if you get more experienced and aim for performance, you should avoid lists because of how they use memory.

Greetings

Advertisement

well can someone give me an example of a list in c++ using objects. here  is my code for c#


        IList<Bug> bugs = new List<Bug>() {
            new Bug() { px=120, py=0 },
            new Bug() { px=180, py=0 },
            new Bug() { px=240, py=0 },
            new Bug() { px=300, py=0 },
            new Bug() { px=360, py=0 },
            new Bug() { px=420, py=0 },
            new Bug() { px=480, py=0 },
            new Bug() { px=540, py=0 },
            new Bug() { px=600, py=0 },

here is my code for c++ so far


list<Brick> bricks= list<Brick>();

 

Just a small example


#include <iterator>
#include <list>
#include <string>

struct Object
{
    std::string importantMessage;

    Object(std::string message)
        : importantMessage{message}
    {
    }
};



int main(int, char**)
{
    std::list<Object> theList;

    // insert values
    theList.emplace_front(" TIME");
    theList.emplace_back(" USE");
    theList.emplace_front("NEXT");
    theList.emplace_back("ARRR ");
    theList.emplace_front("FRONT");
    theList.emplace_back(" GOOGLE!");
    theList.emplace_back("BACK");

    // access first, fifth and last member
    std::cout << theList.front().importantMessage << std::endl;
    std::cout << std::next(theList.begin(), 4)->importantMessage << std::endl;
    std::cout << theList.back().importantMessage << std::endl;

    // erase first, fourth and last member
    theList.pop_front();
    theList.erase(std::next(theList.begin(), 3));
    theList.pop_back();


    // loop over all elements
    for (const auto& member : theList)
        std::cout << member.importantMessage;
}

 

Output:


FRONT
ARRR 
BACK
NEXT TIME USE GOOGLE!

 

thanks dertroll but can I ask how do I incorporate px and py?


list<Brick> bricks;
bricks.emplace_front(px=0,py=200);

bricks. gives me an error.

Your Brick class needs a constructor that sets px and py internally. Then you just write


bricks.emplace_front(0,200);

No assignment there.

Just in case... a brick class with a corresponding constructor could look like this (might contain coding errors - didn't test it):

 


class Brick // <--- replace "class" with "struct" if you want to access px and py directly
{
    int px;
    int py;

public:

    Brick(int x, int y)
        : px{x}
        , py{y}
    {}

};

 

45 minutes ago, DerTroll said:

bricks.emplace_front(0,200);

well I tried this in the game loop and it does work. thanks here is how I initialize my list.


	for(int i=0; i<=725;i+=225)
	{
	DrawBitmap_one("red.bmp", i, 200);
	bricks.emplace_front(i,200);
	}
	for(int i=75; i<=725;i+=225)
	{
	DrawBitmap_two("green.bmp", i, 200);
	bricks.emplace_front(i,200);
	}
	for(int i=150; i<=725;i+=225)
	{
	DrawBitmap_three("blue.bmp", i, 200);
	bricks.emplace_front(i,200);
	}

	for(int i=0; i<=725;i+=225)
	{
	DrawBitmap_two("green.bmp", i, 165);
	bricks.emplace_front(i,165);
	}
	for(int i=75; i<=725;i+=225)
	{
	DrawBitmap_three("blue.bmp", i, 165);
	bricks.emplace_front(i,165);
	}
	for(int i=150; i<=725;i+=225)
	{
	DrawBitmap_one("red.bmp", i, 165);
	bricks.emplace_front(i,165);
	}

	for(int i=0; i<=725;i+=225)
	{
	DrawBitmap_three("blue.bmp", i, 130);
	bricks.emplace_front(i,130);
	}
	for(int i=75; i<=725;i+=225)
	{
	DrawBitmap_one("red.bmp", i, 130);
	bricks.emplace_front(i,130);
	}
	for(int i=150; i<=725;i+=225)
	{
	DrawBitmap_two("green.bmp", i, 130);
	bricks.emplace_front(i,130);
	}

 

I am trying to create a Boolean list, is this how I make the struct?


struct Brick
{
	bool px;
	bool py;
public:
	Brick(bool px,bool py)
	{}
};

here is my collision routine


	for(int i=0; i<=9;i+=3)
	{
	if((bricks.begin(),i)==1)
	{
	DrawBitmap("paddle.bmp",600,200);
	}
	}

 

6 hours ago, phil67rpg said:

I am trying to create a Boolean list, is this how I make the struct?



struct Brick
{
	bool px;
	bool py;
public:
	Brick(bool px,bool py)
	{}
};

here is my collision routine



	for(int i=0; i<=9;i+=3)
	{
	if((bricks.begin(),i)==1)
	{
	DrawBitmap("paddle.bmp",600,200);
	}
	}

 

Phew... well... honestly, I think you should take a big step back and start doing some basic C++ tutorials before you try to make a game. There are so many problems in your code. For a simple boolean list, you don't need a struct, just:


std::list<bool> booleanList;

Why are you doing this:


bricks.emplace_front(i,130);

if your brick struct takes bools? I suppose the compiler will make an implicit cast to bool here but he should also emit a lot of warnings if you have not turned them off.


if((bricks.begin(),i)==1)

What is that supposed to do? I tested it and was really astonished that it even compiles (with warnings). I had to look up what the comma operator actually does. Apart from using commas in function declarations, definitions, calls and some other use cases, I have never thought about a comma operator being a thing (Here is a short explanation: https://www.fluentcpp.com/2018/07/31/how-to-get-along-with-the-comma-operator/). From what I got so far, you are getting an iterator to the first object, discard it, get the value of i and compare it to 2. So it's basically the same as


if(i == 1)

If you wanted to compare the i-th member of the list to 1, you have to look into my last post how you use std::next. Then you have to dereference the iterator and either use a member of the Brick class for comparison or define the operator == for the Brick class. If you don't understand what I am talking about then again:

Stop your current project and do some C++ tutorials until you start understanding what I want to tell you.

 

Greetings

I am trying to use a Boolean to detect if a  brick is hit or not and then turn it off.

I have got the brick to blink on and off. I am sorry but the paddle bmp is supposed to be black.bmp


	for(int i=0; i<=9;i+=3)
	{
	if(next(bricks.begin(),i)->px)
	{
	DrawBitmap("paddle.bmp",600,200);
	}
	}

 

This topic is closed to new replies.

Advertisement