Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Coding an Entire Game in One Line of Code | Indie Game Dev Challenge

Discussion in 'Made With Unity' started by daniel_lochner, Sep 3, 2019.

  1. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    170
    Hey everyone!

    I decided to challenge myself and code an entire game in only one line of code!

    Don't believe me? Check out the video for yourself here:


    You can play the game for yourself on itch.io here:
    https://daniellochner.itch.io/flappy-bird

    Thanks for your time!
     
  2. Deleted User

    Deleted User

    Guest

    Click bait?
     
  3. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    170
    It's only click-bait, if I didn't do it! I actually coded this entire game in one line of code! Check out the video for yourself :)
     
  4. Deleted User

    Deleted User

    Guest

    What about posting the code here directly? :)
     
  5. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    170
    Definitely makes more sense watching the video, as you could just condense the entire script into a single line of code... but here it is anyway:
    Code (CSharp):
    1. using UnityEngine; using UnityEngine.SceneManagement; using TMPro; public class Game : MonoBehaviour { public GameObject cameraPrefab; public GameObject canvasPrefab; public GameObject basePrefab; public GameObject backgroundPrefab; public GameObject pipePrefab; private GameObject[] baseGOs = new GameObject[2]; private GameObject[] pipeGOs = new GameObject[2]; private GameObject canvasGO; private float rotation; private Rigidbody2D birdRB; public float rotationSpeed; public float movementSpeed; private bool lose; private bool rotatable = true; private bool flying = false; public AudioClip hit; public AudioClip die; private int score; private TextMeshProUGUI scoreText; public float flapTime; private float flapTimeLeft; private int flaps; public Sprite[] flapSprites = new Sprite[3]; private void Start() { Instantiate(cameraPrefab); Instantiate(backgroundPrefab); canvasGO = Instantiate(canvasPrefab); for (int i = 0; i < baseGOs.Length; i++) { baseGOs[i] = Instantiate(basePrefab, new Vector3(i * (10f * (9f / 16f)), -4, -2), Quaternion.identity); } for (int i = 0; i < pipeGOs.Length; i++) { float height = UnityEngine.Random.Range(-1f, 3.5f); pipeGOs[i] = Instantiate(pipePrefab, new Vector3(4 * (i + 1), height, -1), Quaternion.identity); birdRB = GetComponent<Rigidbody2D>(); birdRB.isKinematic = true; scoreText = canvasGO.transform.GetChild(0).GetComponent<TextMeshProUGUI>(); flapTimeLeft = flapTime; } } private void Update() { if ((Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) && !flying) { flying = true; birdRB.isKinematic = false; } if (!flying) return; if (rotatable) { rotation = Mathf.Clamp(rotation - rotationSpeed * Time.deltaTime, lose ? -90 : -30, 30); transform.localRotation = Quaternion.Euler(0, 0, rotation); } if ((Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) && lose) { SceneManager.LoadScene(0); } if (lose) return; if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) { birdRB.velocity = Vector3.zero; birdRB.AddForce(Vector3.up * 5, ForceMode2D.Impulse); GetComponent<AudioSource>().Play(); } if (birdRB.velocity.y > 0) { rotation = 30; } for (int i = 0; i < baseGOs.Length; i++) { if (baseGOs[i].transform.position.x <= - (10 * 9f / 16)) { Destroy(baseGOs[i]); baseGOs[i] = Instantiate(basePrefab, new Vector3((10 * 9f / 16), -4, -2), Quaternion.identity); baseGOs[(i + 1) % 2].transform.position = new Vector3(0, -4, -2); } baseGOs[i].transform.position -= new Vector3(movementSpeed * Time.deltaTime, 0); } for (int i = 0; i < pipeGOs.Length; i++) { if (pipeGOs[i].transform.position.x < -4) { float height = UnityEngine.Random.Range(-1f, 3.5f); Destroy(pipeGOs[i]); pipeGOs[i] = Instantiate(pipePrefab, new Vector3(4, height, -1), Quaternion.identity); } pipeGOs[i].transform.position -= new Vector3(movementSpeed * Time.deltaTime, 0); } if (flapTimeLeft > 0) { flapTimeLeft -= Time.deltaTime; } else { flaps++; flapTimeLeft = flapTime; GetComponent<SpriteRenderer>().sprite = flapSprites[flaps % 3]; } } void OnTriggerEnter2D(Collider2D collider) { collider.GetComponent<AudioSource>().Play(); score++; scoreText.text = score.ToString(); } void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.name.Contains("Base")) { rotatable = false; } if (lose) return; GetComponent<AudioSource>().PlayOneShot(hit); GetComponent<AudioSource>().PlayOneShot(die); foreach (GameObject pipeGO in pipeGOs) { for (int i = 0; i < 3; i++) { pipeGO.transform.GetChild(i).GetComponent<BoxCollider2D>().enabled = false; } } if (score > PlayerPrefs.GetInt("high_score")) { PlayerPrefs.SetInt("high_score", score); } canvasGO.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = "Best: " + PlayerPrefs.GetInt("high_score").ToString(); lose = true; } }
     
  6. Deleted User

    Deleted User

    Guest

    So, this is what you call "one line of code", everything put on one line? Anybody can do that, how is it supposed to be a performance?
     
  7. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    170
    It's a challenge, because you have to control everything from a single script, and also only work on one line at all times. Gets very confusing at times!

    You should have a look at the other game-dev challenges on YouTube! An example of a recent one was coding an entire game in 10 minutes! Even though they definitely didn't code an entire game in 10 minutes (because of all the preparation that went into it etc.) it was still a challenge to record yourself creating it under 10 minutes.

    These challenges are mainly for the YouTube community that record how they code the game. In the video, I also explain step by step how I did it, and also show myself coding on the one line! I definitely recommend checking it out so you understand where I'm coming from.

    I agree, just blindly posting that you "coded a game in under 10 minutes" or "coded a game in one line of code" makes no sense. You need to post video proof along with it! :)
     
  8. khos

    khos

    Joined:
    May 10, 2016
    Posts:
    1,476
    Hi there: In my view nobody thought of that before (and for good reason, e.g. reading / editing the code would not be easy..) which does make it interesting. Maybe some good can come of this: e.g. test the compiler limit, can it handle this kind of code formatting, also OS limits, how much in one line until something breaks?
     
    daniel_lochner likes this.
  9. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    950
    Nothing would break. The only real difference is legibility. The OS has no concept of 'lines'. It's just what your text editor does after it encounters a line break.
     
    daniel_lochner likes this.
  10. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    back in the 80s, magazines used to have 1 and 2-liner contests for Apple ][ basic. But on that computer, there was a buffer-limit on how long a line could be, so really the idea is to see how powerful a program you could write with just 128 or 256 bytes. I don't really see the point if you have unlimited line length.

    In C# it makes no difference whether you have one line or a thousand. The only difference is a bunch of carriage-return characters that the compiler ignores anyway. It makes no difference to the compiler or OS how many lines you have, since neither processes the file in terms of lines. Maybe your text editor has some limitation, but is that actually very interesting?
     
    daniel_lochner likes this.
  11. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    @daniel_lochner weld done and nice presented.
    But what is your conclusion and lesson learned from it?