Day 4th -2015-1-19-openframeworks Series Third Lecture-Object-oriented ball

Source: Internet
Author: User

Yesterday's tutorial, there are so many things to talk about, so instead of talking about nothing. One big reason is--I'm delusional to let all

The level of the reader can read. This is in fact absolutely impossible. Because, everyone has different knowledge, has mastered the skills are also different, then the

The learning curve of adaptation is actually different.

Personally, I am a cautious person, in the course of learning is generally not aggressive, which means that my basic knowledge will be more

Solid, but the pace of progress will be slow. I write a blog here, in fact, is a way of learning, but undoubtedly, this way of learning is actually very

Inefficient (and no one is watching). However, for me is a very good supplement ... I feel a little bit off the topic, actually I want to express

It is a very good way to learn, but you want to be able to see the reader in this, and do not know that he already knows

In the case of how much content, that is completely impossible. But if you want to adapt the content of the teaching to everyone, it is actually more arrogant idea,

is more difficult to achieve-so I can only teach with one of my standards (and try to keep it low but not too much on teaching)

Then everyone will picking.

Today said a lot of irrelevant topics, the main thing is to say: I will strive to improve the quality of these tutorials!

So, today's Day more, began!

Object-Oriented Ball

What is object-oriented

In simple terms, object-oriented is the practice of putting some data together with the operations of that data. So, why do you do this? Why are you putting

are data and operations put together? The answer is that doing so helps to relieve the burden of thinking.

A lot of people are talking about object-oriented, like to say, this is a real-world simulation, is a simulation of the reality of the actual existence of something of an abstract approach.

That's true, but I don't like it--it does make the person who hears it feel bad, but it's not conducive to understanding the concept, it may even hinder

They think about how to write an object. In the case of neural networks, if it is a system modeled after human neurons, it is true that a large wave of people can be fooled,

But it's not an easy thing to say-it's a brute force solution without knowing the real function. Through a layer of judgment, gradually generate more

Approximate real-world functions, thus obtaining almost correct approximate solutions.

So let's say how this approach reduces the burden.

Take the last code as a chestnut:

1 voidofapp::update () {2Windx =ofgetwindowwidth ();3Windy =ofgetwindowheight ();4     //Gets the current window width height5hue++;6     //Hue Increase7     if(Hue >255)8     {9Hue =0;Ten     } One     //if the hue value is out of bounds, re-fetch 0 A  - Outrange (); -     //Check if the ball is out of bounds the  -PosX + =Speedx; -PosY + =SpeedY; -     //Ball Update coordinates +      - slow (); +     //Small ball slows down speed A changesize (); at     //Change Ball size -}

This function is a function to control the change of the ball. What's it going to do? First get the window width high there, in fact, is changing the value of two global variables,

In this way, you can reduce unnecessary expenses elsewhere (for example, when judging whether or not to cross the border, you don't have to get a wide height). and Outrange ()

In fact, to check whether the ball is out of bounds, Posx,posy's two sentences are used to update the ball position. Slow () is used to slow down the ball speed, changesize ()

is used to change the size of the ball.

Maybe you would say, isn't that nice? Yes, in the present case there is no problem. So, what if you want to make a lot of small balls? Someone might know.

Array, he says, simply change each value to an array, and then you can manipulate all the balls through the array. Of course, we need to do the function

Some transformation makes it an operation for an array instead of a number.

This is really a workaround. However, there are some problems with this approach-for example, if I want to operate a small ball alone, what should I do this time?

One solution is to increase the number of functions and then call the corresponding function of each ball through a loop.

There are some aspects of the idea of the object of the embryonic, but still not-if I now have hundreds of small balls, then you will write a hundreds of lines?

Of course, this is not a good solution.

So we're proposing "class," which represents the same type of object. For example, we have a lot of small balls, each of them is an object, all have the same data type,

Have the same way of doing things--but the contents of the store are different. In this way, we can create a class to describe the common characteristics of these balls, and then provide the corresponding

function to manipulate the data. So we can use loops instead of a long string of code to do what we want to do.

Does it feel better?

Of course, there are many benefits to object-oriented programming, such as the fact that you want to generate a structure dynamically, so you can actually release it in the destructor of the class in which it resides. But

One thing to note is--not that the more thorough the object-oriented, the better--that sometimes it loses some flexibility. How to use, or to see

Personal habits and specific content to set

(On the left is the class, the right is the object)

Design class

Then start designing our little ball game!

Because objects in C + + are generated by classes, if we want to build the object of a small ball, we need to build the ball class first.

So, let's think about it, what do we need this little ball for?

    • Properties: Size
    • Properties: Color
    • Properties: Location
    • Properties: Speed
    • Method: Get random initial size, color, position, speed
    • Method: Update location based on speed
    • Method: Collision Detection (can be found in the following considerations, from the angle of the ball to achieve more trouble)
    • Method: Cross-border detection

Well, let's just follow this to write our class. (This time we will not change the color size)

Yes, there's a handy way to add classes in VS (it's much easier to use this in a lot of projects), I'll show you again.

Select

(This wizard will appear when you click Create, and you'll get a good name, usually the default capitalization)

and began to write! ~\ (≧▽≦)/~ la La.

Here I don't care if you understand, I will write directly, please read the code!

Generally speaking, just write code is not used, because it has not experienced the test of practice. So I'm writing here code that's actually I've

Modified code-don't feel like you can write it all at once.

Then let's think about what we want to do. This time we expected the effect to be: we have 10 small balls, the size of different colors;

Have their own color, size, speed, position, and can collide with each other. If we click the left mouse button, we can have a ball, and then this ball

You can also collide with those small balls, and there is a quicker setting to drag faster.

Then we need the ball of the mouse and the class of the whole game (for personal habits, I summarize some of the global control of things, like to add a

The classes that manage them, if not added, can be implemented with functions)

So we think about how to write the class of the ball produced by the mouse:

    • Properties: Color
    • Properties: Size
    • Properties: Location
    • Properties: Speed
    • Method: Collision Ball
    • Method: Invisible
    • Method: Reveal

As you can see, there are a lot of parts that are very close to the class of the ball above--some people may know inheritance, but I'm not going to use this one,

Because there is no need.

So, here's the code:

After that, we're going to think about the whole game class, what does it need?

    • initialization function
    • End Function
    • The operation of the ball
    • Collision handling for mouse and small balls
    • For the ball's own collision treatment

Then, the code is as follows.

After that, put its code in the main program and you'll see the results of the run.

You can try to change the number of balls to see if it's still possible.

Above, through this three-period tutorial, we initially understand the C + + programming, openframeworks programming ideas and some very basic

Graphical interface. Can be considered as a primer for openframeworks. Well, next, we want to use this framework, we need to

Continue to study and explore. One of the methods is to study the document, and the second is to learn the program examples. In the next day and more, we will combine this

Two ways to do it. It might be a bit confusing, but after the collection becomes meaningful, I'll sort it out as a series of content, individually

Release. Readers who think that the day is more attractive than the lower, can go straight to see the information I have sorted out--but strongly suggest that beginners continue to see my daily part,

Because there are a lot of programming ideas in it.

However, in order to be able to learn openframeworks better, we must first learn some irrelevant knowledge. That is, what is FQ, how to FQ,

And what GitHub is, how to use the GitHub problem. It is estimated to take about one weeks. During this period, I will try to introduce the simple and efficient

A variety of knowledge and technology, please look forward to.

In addition, as a result of today's more content, long editing time, it has not been fully completed, part of the content was added on January 20. and January 20

Because of some personal affairs, can not complete a full day more, so take a day off. Also, when the network part of the Knowledge tutorial is explained, I may consider

Start a new blog of different nature (such as translation, reprint, News), the specific content of the day statement.

Above

Day 4th -2015-1-19-openframeworks Series Third Lecture-Object-oriented ball

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.