среда, 8 августа 2012 г.

My very first game

After reading up on some sites I decided to clone a pong game. It has single player mode vs Ai and two player mode on same device. It has music and sound effects! Hardest part by far was complicated collision logic using vectors and heavy nested if statements. For example if you hit the ball with top part of the paddle, it will go up, if you hit it with center it will go to center and if you hit with the lower part it will go down, regardless of angle it came with. That adds complexity and nice feeling of control. It also uses small particle effect when the ball collides with paddle for eye candy. The game was written in cocos2d 1.0

Here is the video of it, check it out: 

Looking into other people first game it doesn't seem that bad! It already makes me feel great that I accomplished a little milestone in becoming a programmer. I submitted it to appstore, of course it will be free, if they let it in. Please let me know what you think!

I have faced few difficulties while coding. First, my version of Cocos2d with ARC enabled templates got a little but very annoying bug, where it would crash every time I tried to write anything with custom font.
So I had to make all the menus in photoshop. And being novice at photoshop doesn't help!
Another hard part was collision detection. First I implemented simple collision detection that checks if ball and paddle bounding boxes intersect and if they do, we simply reverse the velocity of the ball.
So if the ball was going right, it has positive velocity, and when it hits paddle, we make it negative, so now it goes left. Then it touches another paddle on the other side and reverses again.
There are couple of drawbacks with this method. First, your ball touching angle with walls and paddles will always be same. So it gets boring very fast. Another fundamental problem with this is that the player has no sense of control. He can not react to gameplay, his only interaction with the game is to try hit the ball with the paddle.
So I wanted to implement another type of interaction. I got the idea from old Arkanoid game. It is a brick-destroying game. Neat trick it uses is if the ball hits paddle on the left end, the ball will always go left, if you hit it with center of the paddle, it will go to center, and if you hit the ball with right end, ball will go to the right, regardless of incoming angle. 
I had hard time thinking how I would implement such a system. But eventually I realized that it's not hard at all. All we have to do is take y part of paddle center position and compare it to ball center y position. This way we will get the difference  in pixels, if it is less then certain amount, for example paddle at position 100 and ball at position 120, so we do 100-120 and get -20, this means that ball is higher then the paddle center, so obviously it must have hit on the upper edge. Same check detects collision for low part of the paddle. As a result we change Y and X velocity in the way we want the ball to go. I had fun times drawing out those vectors on paper:

Another tedious part was menus. Somehow I could not get those menus to move, so I had to change logic a bit.
After watching a video how to spice up your game, where two game developers show how to make simple breakout game more fun, I decided to add little particle effect when the ball collides with the paddle. It took some tweaking to get it right, but now it is pretty fun thing!
I had people tell me that it looks really neat.

Either way, I am more or less happy with my first game, as it took me couple of days to do it, and it is my FIRST game after all.

Комментариев нет:

Отправить комментарий