Programming MOOC: Week 2

Mar 09, 2014 14:27

Week 2 of the programming course focuses on looking at variables, data types and operators. I started going through it on Wednesday and got about half of it done but then I wasn't well and not able to do any until this morning. It's been a mad rush to get it finished off today before next week's starts.

It begins with information on what variables are and how to name them, the different data types and what qualities they have e.g. boolean is either 1 or 0, byte has a value between -128 and 127 inclusive, etc. So you have to pick the right data type depending on the data being stored. There was a quiz on this bit, presumably to check that you'd understood it. I thought it had gone way over my head so I wasn't sure I'd do at all well on the quiz. There were 6 questions with 3 attempts allowed on each question. I got the last question wrong on first attempt because I forgot that integers only deals with whole numbers. Oops. But I got all the rest right first time which surprised me.

After this, it went onto operators which perform different functions: assign a value, do maths stuff (addition, subtraction and division), act on a single unit (the only one we'll be using is invert a Boolean value), and conditional statements - this latter one is coming up next week. It also covered operator precedence - which bit to do when. Then there was another quiz on this section. It had snippets of code so you had to process the code to work out the result, which was way more maths than I wanted to do on a Sunday morning. Or any morning come to that. The final question was the hardest:

int myValue = 100;
myValue = myValue + 99 / 3;
float myResult = (myValue - (1f * 99f))/3f;

Is the answer a)133, b)0, c)11.333... or d)not enough information to answer. I got it right, yay me!

But OMG so much maths. I don't like maths and I struggle with it a lot due to moving from school to school so much when I was younger; I missed out on a lot of the basics, such as long division, and it's hard to build on patchy knowledge. This week has been harder in general than I expected too, rather like we've been thrown in at the deep end a bit. I mostly understand it if I don't think about it too hard. I struggle a bit with understanding what float is used for and == but I think I've got the rest of it.

Finally after that crash course in code basics, I got to work on the game. Rather than actually taking us through the next stage, Karsten (the instructor) sets two tasks for us to do and leaves us to it. Firstly to make the ball appear on the screen where it's touched and have it stand still as opposed to moving towards the touch. Secondly to make the ball move twice as fast as it does now. I didn't expect that we'd just be left to it!

Okay, let's see if I can work this out. In order to make the ball still, the speed would need to be 0. To make the ball appear where the screen is touched, I presume that code needs adding that says to show the ball there. So the relevant bit of code to alter would be:

protected void actionOnTouch(float x, float y) {
//Increase/decrease the speed of the ball making the ball move towards the touch
mBallSpeedX = x - mBallX;
mBallSpeedY = y - mBallY;
}

I've changed it to:

protected void actionOnTouch(float x, float y) {
//Increase/decrease the speed of the ball making the ball move towards the touch
mBallX = x;
mBallY = y;
mBallSpeedX = 0;
mBallSpeedY = 0;
}

Let's run that and see if it works. *waits for emulator to start game... clicks about on the screen* That seems to work! The ball just appears wherever I click. Yay! Actually, thinking about it, it probably doesn't need the speed variable at all, just 'show ball'.

The next bit was to make the ball twice as fast as it was previously. So speed times 2!

protected void actionOnTouch(float x, float y) {
//Increase/decrease the speed of the ball making the ball move towards the touch
mBallSpeedX = (x - mBallX)*2;
mBallSpeedY = (y - mBallY)*2;
}

Once I thought about it logically, it wasn't as difficult as I thought. I think there was initial panic at having to do it without any help which made my brain go 'omgcan'tdothis'. So, yay. Now to see what delights conditional statements will have for us!

online learnings

Previous post Next post
Up