This is the 22nd day of my participation in the August Wen Challenge.More challenges in August

I’ve written a couple of small game blogs in Unity before, and I feel good about it, so I’m going to keep writing. Today it’s pixelbird.

The pixel bird game is also simple to play, just tap the screen, and then the bird will fly up, or it will fall down. And then there are all kinds of hoses, and we have to go through the hoses and score points.

Let’s look at what the scene object has, which is bg01, which is the background, and then gameover, which is the background box that pops up when the game fails. Ready means to be ready. The bird is a bird. Roads are pipelines. Score is to show a score.

Now, what are the objects? Almost every game theme has a script attached to it. Surely the most important of these is the Bird script, which controls the movements of the bird’s main character.


	// Use this for initialization
	void Start () {
		body = gameObject.GetComponent<Rigidbody2D>();
		initPos = gameObject.transform.position;
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetButtonDown("Fire1")){
			body.AddForce(force);
		}
	}

	public void reset() {
		body.isKinematic = true;
		gameObject.transform.position = initPos;
		gameObject.transform.eulerAngles = Vector3.zero;
		GetComponent<Animator>().enabled = true;
	}
Copy the code

Bird actions are basically three functions, starting with the start function to get the rigid body and position of the bird. In update logic, when the “Fire1” button is pressed, a force is added to the bird as a rigid body, which causes the bird to bounce upwards. Notice, the bird has to be rigid in order for it to be stressed.

Finally, the reset() function reconfigures the bird’s position, Euler Angle, animation, etc.


	public void gen() {
		zhuzi[0].SetActive(true);
		zhuzi[1].SetActive(true);
		Vector3 p = zhuzi[0].transform.localPosition;
		float vv = Random.value;
		p.y = Mathf.Lerp(down, upper, vv);
		zhuzi[0].transform.localPosition = p;

		p = zhuzi[1].transform.localPosition;
		 vv = Random.value;
		p.y = Mathf.Lerp(down, upper, vv);
		zhuzi[1].transform.localPosition = p;
	}
Copy the code

The above code is a random generation of column, in fact, is a random generation of column position. This is all about formation.

void Update () { Vector3 pos = trans.position; pos.x -= speed * Time.deltaTime; trans.position = pos; If (pos.x <= -1.6f-3.35f *idx) {vector3pp = roads[idx%2]. Pp. X + = 3.35 f; idx++; roads[idx%2].transform.position = pp; if(isBegin){ roads[idx%2].GetComponent<roadGen>().gen(); }}}Copy the code

And then this piece of code is about column movement. It actually looks like the bird is moving, but the position of the bird in the center has not changed, but the column has been moving to the left.

This is a relatively basic simple game example, interested in learning Unity, you can pay attention to the public number: poetic code, message to me, I teach you systematic learning.