Ivy League Battle

Yao Yao
Data Mining the City
3 min readSep 25, 2019

Individually produced by Yao Yao, Sep. 2019.

To be brief: this is a simple video game.

As the first assignment, I know this is mainly for practice how to use Unity and C# coding, so I can’t help to imagine just build a simple game by myself.

When I watched the online course “CS 50” of Harvard, their students built a game with Scratch, but now we get our own game!

The idea and graphics are from a game I played when I was a child, it’s called “Battle City”, is anyone familiar with it?

I am very happy to have some GSAPP classmates to play and test my game.

Wish every one enjoy the class, and if anyone is interested, below is part of the boring part: my code snippets.

Part of the Player script:

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Player : MonoBehaviour{//Datapublic float MoveSpeed=3;private Vector3 BulletEulerAngles;private float shieldTimeVal = 3;private bool isProtected=true;  //shield setting//List of Referencepublic GameObject BulletPrefab;public GameObject ExplosionPrefab;public GameObject ShieldEffectPrefab;private void Awake(){}// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){//with Shield or notif (isProtected){ShieldEffectPrefab.SetActive(true);shieldTimeVal -= Time.deltaTime;if (shieldTimeVal<=0){isProtected = false;ShieldEffectPrefab.SetActive(false); //Close the shield}}}// Call of Playerprivate void FixedUpdate(){Move();Attack();}//Attack of Playerprivate void Attack(){if (Input.GetKeyDown(KeyCode.Space)){//rotation of bullet = the orientation of player+the rotation of bulletInstantiate(BulletPrefab, transform.position, Quaternion.Euler(transform.eulerAngles+BulletEulerAngles));}}//Move of the Playerprivate void Move(){float v=Input.GetAxisRaw("Vertical");transform.Translate(Vector3.up * v * MoveSpeed * Time.deltaTime, Space.World);if (v < 0) //down{BulletEulerAngles = new Vector3 (0, 0, 180);}else if (v > 0) //up{BulletEulerAngles = new Vector3 (0, 0, 0);}//Vertical move controlfloat h = Input.GetAxisRaw("Horizontal");transform.Translate(Vector3.right * h * MoveSpeed * Time.deltaTime, Space.World);if (h < 0){BulletEulerAngles = new Vector3 (0, 0, 90);}else if (h > 0){BulletEulerAngles = new Vector3 (0, 0, -90);}//Horizontal move control}//Break of the Playerprivate void Die(){if(isProtected)  //with shield{return;}//Explosion EffectInstantiate(ExplosionPrefab, transform.position, transform.rotation);//DieDestroy(gameObject);}}

Bullet (with Collider, Rigidbody, Trigger and etc):

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Bullet : MonoBehaviour{//Speed of Bulletpublic float MoveSpeed = 20;public bool isPlayerBullet;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){ //Move of Bullettransform.Translate(transform.up * MoveSpeed * Time.deltaTime, Space.World);}private void OnTriggerEnter2D(Collider2D collision){switch (collision.tag){case"Columbia":collision.SendMessage("Die");break;case"Heart":break;case"Enemy":if (isPlayerBullet){collision.SendMessage("Die");}Destroy(gameObject);break;case"Bricks":Destroy(gameObject);break;case"Wall":Destroy(gameObject);break;default:break;}}}

Random Born:

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Born : MonoBehaviour{public GameObject PlayerPrefab;public GameObject[] EnemyPrefabList;public bool createPlayer;// Start is called before the first frame updatevoid Start(){Invoke("BornPlayer", 1f);Destroy(gameObject, 1);}// Update is called once per framevoid Update(){}private void BornPlayer(){if (createPlayer){Instantiate(PlayerPrefab, transform.position, Quaternion.identity);}else{int num = Random.Range(0, 7);Instantiate(EnemyPrefabList[num], transform.position, Quaternion.identity);}}}

--

--