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

Very simple game ideas to learn C#

Discussion in 'General Discussion' started by pustur, Oct 22, 2012.

  1. pustur

    pustur

    Joined:
    Oct 20, 2012
    Posts:
    22
    Hi guys,

    Months ago i almost finished 2 games with unity, the first game was Simon but i didn't finished it because i had a problem with Playmaker (it was a bug, not user error) so i forgot about the project and i never went back. (i was really close to make it work :rolleyes:)

    The second game was Minesweeper and i went pretty far as well, i still used Playmaker but this time i left because i messed up the "code" (i used raycasting to open cells and of course that made the game lag really bad, imagine firing 100 or more rays in a second .-.) and i had other problems and i didn't come beck to fix them.

    (i still have 2 screenshots: Simon Minesweeper)

    BUT, anyway... i'm here because i think that i should learn how to program in C# without using Playmaker (which is really awesome BTW, i'm not saying that's bad!) and i wanted some ideas to start out. as you can see i give up easly and lose motivation quickly but i want to give it a try.

    so here's a list of simple games that came in to my mind, do you have some other ideas? which would you go for if this was your first game?

    snake
    tetris
    memory
    minesweeper
    bomberman (maybe without AI...)
    roll the ball
    cannon shooter (2D, you are a tank and you shoot paratroopers and elicopters that come from the sky)
    simon
    battleship
    space invaders
    pong

    i'll probably try minesweeper again, hopefully without messing up everything :D
     
  2. Kinos141

    Kinos141

    Joined:
    Jun 22, 2011
    Posts:
    969
    Jumpman...namely Mario, one level, you jump and you stomp on something.
     
  3. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,869
    Pac-Man: Still has some AI, but its relatively simple.
    Burgertime: Even easier AI. Maybe just leave out the enemies for now.
     
  4. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Your list is good. My advice however is to learn how to program C#, and how to program in Unity separately.
     
    devjp likes this.
  5. LuisGuimaraes

    LuisGuimaraes

    Joined:
    Oct 9, 2009
    Posts:
    20
    Space Invaders. It'll teach you many things about how stuff works and interacts to create gameplay in Unity.
     
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
    This. Most people who jump straight to learning to program for games skip over a lot of fundamental stuff which is really important. The first step is to understand programming. Once you've done that, then apply what you've learned to making games.
     
  7. SimonAlkemade

    SimonAlkemade

    Joined:
    Feb 4, 2009
    Posts:
    432
    I don't necessarily agree here. I think you can learn programming by making games. Unity is a great way of learning the basic concepts of programming. Just learning the abstract concepts isn't a lot of fun. But when you're actually creating an object that you can move through user input you actually feel like your getting results which is a lot more motivating then say perhaps the learning how to use delegates or inheritence.

    These concepts will follow eventually but learning all these programming principles beforehand might be a too steep learning curve. Atleast for me I learned programming by just asking myself what the results were I wanted to accomplish and then figuring it out. It was only after I completed few games that i wanted to dive deeper into the skill and art of programming itself.

    What i do recommend is that you buy a C# book which goes over all programming concepts.

    When I started out with Unity the first thing I was making was a first person shooter because you already have the standard assets for that and then just add features like a gun shooting, a health bar, a turret that shoots back etc etc.

    just my 2 cents
     
  8. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    I didn't say you couldn't learn programming from making games -you can certainly learn a lot - however I think the resources aren't there to learn the fundamentals.

    By doing both - learning raw C# in console [or form] applications and then learning how to apply that in game dev I've seen some huge benefits IMO.
     
  9. Morning

    Morning

    Joined:
    Feb 4, 2012
    Posts:
    1,141
    If you have zero experience in programming then jumping straight into unity C# will be harder than a form application like npsf3k said. But it can also be confusing because unity works a bit different from classic C# applications in a sense that it uses components and gameobjects instead of doing a "new" on everything.

    I personally learned C# purely in unity but I already had some experience with other languages so that gave me a huge boost.
    C# Yellow book helped me a lot to get a hang of C#. It has nothing to do with unity, but all the concepts can easily be tested in unity so there was little difficulty. Though you also need to read up on how unity works so you know to put stuff into Start() and Update() instead of Main().

    What I would recommend is get a C# book and unity script reference and experiment away.
    It helps a lot if you have a friend in real life or on skype who knows C# and is willing to help. Though this is true when learning anything. If you're in Europe or a timezone near +2 I could help out a bit if you're interested. PM me if so.
     
    Last edited: Oct 23, 2012
  10. pustur

    pustur

    Joined:
    Oct 20, 2012
    Posts:
    22
    Thanks guys for your replies,
    i did not mention that i already have a small knowledge in programming, in fact i learnt C++ at school during the first year. for now i know how to use loops, if-else, variables, arrays...but not much more. I think that i'll start with minesweeper again since i already know how it works (more or less) and then go with something like space invaders.

    if you have any other suggestions or ideas for games please let me know ^^ i really apreciate your help.
     
  11. GeorgeRigato

    GeorgeRigato

    Joined:
    Jul 5, 2012
    Posts:
    58
    One simple game suggestion is Hanoi Towers.

    My simple projects (I won't call them games 'cos they're incomplete) were:

    -A First person explorer/item collector;

    -A sidescroller arcade mix between penguin launcher and Jetpack joyride;

    -A simple one level pinball; (I really want to build a nice pinball game)

    -Some gravitational physics simulation experiment.

    But as said on this thread, I had programming experience before get to unity, so it helped a lot to see how programming in Unity is different from standard console/form programming, and mainly where I could go with unity. To think outside of unity's box (which is quite big!)

    But all that is my experience and POV, so I don't take it as the universal truth.

    That being said, my advice for people that want to learn strictly game programming woud be:

    -Get a big book about the language you want to learn. And it should be Object Oriented paradigm. (My advice here works for C#)
    -Follow book instructions to get the basics. From "Hello nurse!" (I mean world) to something like loops and some basic data structure like array or list.
    -Implement simple console programs as calculator, cash ATM, etc...

    At that point you can get a little unmotivated. But stand tight, things will get a little better. I think this knowledge recently developed would give you the basics to start experimenting game related lessons. For that:

    -Find your way installing Microsoft's XNA game platform. (C#)
    -Learn how XNA's Update and Draw methods works.
    -With the previous knowledge acquired you should start practicing the Object Orientation part of the story. Creating Classes and getting to know inheritance and polymorphism. This is really valuable to see what unity already brings up to you and what you can do with it.
    -Try to implement simple games with XNA. The simplest games already gives you some perspective of what I'm talking about.

    And then you should bring that big backpack of fresh knowledge to unity and (try to) have fun!

    I mostly did that. But I started with java at university. But Java or C# won't matter. The concepts behind them will.

    And good luck!
     
  12. Gigiwoo

    Gigiwoo

    Joined:
    Mar 16, 2011
    Posts:
    2,981
    @OP - You have the right approach. Don't stop to learn all of C#, just pick small things (yer list is fine). Then, learn what you need to learn, as you need it, and focus on completing stuff!

    Instead of dropping projects, drop features or pick smaller projects. Set a date 8 weeks away and built something. My wife goaded me: 'even if it's just your name bouncing up and down'. Completing a project gave me a HUGE boost to my morale. Completing MULTIPLE projects ... well... you'll see.

    Inspirational quote from Joel Spolsky: "Shipping is a feature. A really important feature. Your product must have it."

    Gigi
     
  13. GiusCo

    GiusCo

    Joined:
    Aug 1, 2009
    Posts:
    405
    Get the free C# games package in the Asset Store and mess with them (modding, mixing, playing with scripts)
     
  14. pustur

    pustur

    Joined:
    Oct 20, 2012
    Posts:
    22
    thanks again for all your help, i already started making a simple script that will allow the spaceship to move and shoot (i'm going for space invaders).
    for now the ship can move left/right and can already spawn the bullets.
    however for now they don't move. i have to find a way to make them move forward without using rigidbodies and AddForce.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Spaceship : MonoBehaviour {
    5.     public int speed = 25;
    6.     public Transform bullet;
    7.  
    8.     public void Update () {
    9.         float translation = Input.GetAxis("Horizontal")*speed*Time.deltaTime;
    10.         this.transform.Translate(translation,0,0);
    11.         if(Input.GetKeyDown(KeyCode.W)){
    12.             Transform bulletPrefab = Instantiate(bullet,this.transform.position,Quaternion.identity) as Transform;
    13.         }
    14.     }
    15. }
     
  15. Kinos141

    Kinos141

    Joined:
    Jun 22, 2011
    Posts:
    969
    You should make a script on the spawned bullet to propel it's self forward. It's a little different kind of thinking than in RL. Think of the bullet like a missile. Missiles propel themselves to the target.
     
  16. GeorgeRigato

    GeorgeRigato

    Joined:
    Jul 5, 2012
    Posts:
    58
    To proper set up the collision of the bullet, one of the two objects involved in the collision (bullet or target) must have a rigidbody (uncheck gravity; check kinematic), and make sure you check the collider of the bullet as trigger.

    This is a simple C# script for a simple bullet.

    You can apply this script to your prefab bullet and tweak the values from the editor.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LaserController : MonoBehaviour {
    5.    
    6.     public float damage;
    7.     public float speed;
    8.     public float lifeSpan;
    9.     private float timer = 0.0f;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.    
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.        
    19.         if (timer < lifeSpan)
    20.         {
    21.             transform.Translate(Vector3.forward * speed * Time.deltaTime, Space.Self);
    22.         }
    23.         else
    24.         {
    25.             Destroy(gameObject);
    26.         }
    27.        
    28.         timer += Time.deltaTime;
    29.    
    30.     }
    31. }
    Maybe someone can improve it.
     
    Last edited: Oct 23, 2012
  17. Kinos141

    Kinos141

    Joined:
    Jun 22, 2011
    Posts:
    969
    Line 25
    Code (csharp):
    1.  
    2. Destroy(gameObject, lifeSpan);
    3.  
    It can destroy itself with the allotted time.
     
  18. pustur

    pustur

    Joined:
    Oct 20, 2012
    Posts:
    22
    Thanks i tried it and it works like a charm :)

    the George's script works already (enters the else only when time is >= lifeSpan)
     
  19. Kinos141

    Kinos141

    Joined:
    Jun 22, 2011
    Posts:
    969
    I agree it does the same thing, but this is cleaner code.

    you'd write less lines with this.

    Code (csharp):
    1.  
    2.  
    3. void Update()
    4. {
    5.       transform.Translate(Vector3.forward * speed * Time.deltaTime, Space.Self);
    6.       Destroy(gameObject, lifeSpan);
    7. }
    8.  
    Done.
     
  20. pustur

    pustur

    Joined:
    Oct 20, 2012
    Posts:
    22
    ah ok, now it's clear! thanks :)

    EDIT: i added the possibility to keep space down and let the spaceship shoot every x seconds, however you can shoot faster if you press-and-release the spacebar by yourself.
    the code works for now but i'm sure it needs improvements.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Spaceship : MonoBehaviour {
    5.     public int speed = 25;
    6.     public Transform bullet;
    7.     public bool canShoot = true;
    8.     float reload = 0;
    9.     public float timeBtwnShots = 0.1f;
    10.     // Use this for initialization
    11.     void Start () {
    12.    
    13.     }
    14.     // Update is called once per frame
    15.     public void Update () {
    16.         float translation = Input.GetAxis("Horizontal")*speed*Time.deltaTime;
    17.         transform.Translate(translation,0,0);
    18.         if(Input.GetKeyDown(KeyCode.Space)){
    19.             Transform bulletPrefab = Instantiate(bullet,this.transform.position,Quaternion.identity) as Transform;
    20.         }
    21.         if(Input.GetKey(KeyCode.Space)  canShoot == true){
    22.             Transform bulletPrefab = Instantiate(bullet,this.transform.position,Quaternion.identity) as Transform;
    23.             canShoot = false;
    24.             reload = 0;
    25.         }
    26.         reload = reload + timeBtwnShots; //the name "TimeBtwnShots" is not correct, higher values mean faster shooting. i'll fix this when i'm less tired
    27.         if(reload >= 10){
    28.             canShoot = true;
    29.         }
    30.     }
    31. }
    32.  
    for now i'm very happy with this project, i hope to keep it alive. a BIG THANKS to this amazing community, you're very helpful and kind!
     
    Last edited: Oct 23, 2012
  21. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
    I can't agree with this. Unity and it's API cover up so much of the fundamentals of how a computer program actually works that some of what you need to learn about the fundamentals is buried several layers deep.

    When you jump into Unity you're immediately working with multiple entry points of complex pre-built classes. You don't ever have complete vision of the whole code execution path, because you're working within an existing framework, and it's far too big for any learner to piece together and properly understand an overview in their head. Most of the low-level functionality you'll ever need is provided for you right out of the box, which means you're unlikely to (don't have to, and probably shouldn't) implement it yourself, which means you're skipping over everything that you might learn in doing so.

    This article [ http://worrydream.com/LearnableProgramming/ ] goes over a lot about what it actually is that people are learning when they learn to program. And contrary to intuition, simply giving a learner an environment where it's easy to make things happen does not make it easier for them to understand why those things happen. To the contrary, if you make it easier by building in a lot of pre-existing functionality and layers of design (which is what a tool like Unity does) you make it harder to learn the fundamentals because you never see why 95% of what you're doing actually works.
     
  22. GeorgeRigato

    GeorgeRigato

    Joined:
    Jul 5, 2012
    Posts:
    58
    You are tying the "time" between shots to your framerate. Every frame the reload float will be added the timeBetweenShots. If Your game runs at 60 frames per second i will take 1.4 seconds to your ship shoot again. If your game runs at 160 frames per second it will take 0.5 second for it shoot again. As frames per second vary on what hardware your player uses, your current code will not provide an even, or fair gameplay for different users. Got it?

    Thats why the "Time.deltaTime" is used often. It is the fraction of time elapsed between the previous and the current frame. So I would rather write:

    Code (csharp):
    1.  
    2.  
    3.     public float rateOfFire = 0.5f;
    4.     private float timer = 0.0f;
    5.     private bool hasShooted = false;
    6.  
    7.     void Update()
    8.     {
    9.         if(Input.GetKey(KeyCode.Space))
    10.         {
    11.             if (hasShooted)
    12.             {
    13.                 timer += Time.deltaTime;
    14.                 if (timer >= rateOfFire)
    15.                 {
    16.                     hasShooted = false;
    17.                 }
    18.             }
    19.             else
    20.             {
    21.                 GameObject bullet = (GameObject) Instantiate (prefab, position, rotation);
    22.                                 hasShooted = true;
    23.                 timer = 0.0f;
    24.             }
    25.         }
    26.        
    27.         if (Input.GetKeyUp(KeyCode.Space))
    28.         {
    29.             hasShooted = false;
    30.             timer = 0.0f;
    31.         }
    32.     }
    33.  
    And this should fire every 0.5 second no matter how many frames roll on that time interval. And the feature of letting the button up to press again and fire faster is still on with that last bit of code "Input.GetKeyUp".

    Shoot your questions away. Maybe in another thread?

    Good luck!
     
    Last edited: Oct 24, 2012
  23. Kinos141

    Kinos141

    Joined:
    Jun 22, 2011
    Posts:
    969
    @Angrypenguin - no one wants to reinvent the wheel. While it's good to know lower level programming, it's dependent on what you are using it for. If you want to be the best programmer in the world, then it's necessary. IF you want to make games, then it's a different story, remember "shipping is an important feature"(read that somewhere :) ) and you customers don't give two S***s about how you made the game(something I learned the hard way), they only care about the end product.
    @anyone really - Unity is a good tool to learn programming because of the asset store's pre-made assets and code. Get a package, read the code, try and understand where the author was coming from and where they were going with it, and try to write your own. Get on the forums and ask more seasoned people questions about how to code. Get a mentor who will sit down and really breakdown how to think abstractly. It's better than working in a vacuum.
    After a person gets more comfortable programming(or really scripting in Unity's case), they can take their skills to other scripting programs, like what I'm doing with web development and learning VB.
     
  24. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Actually, reinventing the wheel imo can be an incredibly useful task when trying to learn... which is the context of this thread.
     
  25. pustur

    pustur

    Joined:
    Oct 20, 2012
    Posts:
    22
    yes i forgot the Time.deltaTime, bad mistake :(
    anyway you gave me a good starting point, thanks a lot!
    and sorry for the questions, i'll make a new thread next time :)
     
  26. GeorgeRigato

    GeorgeRigato

    Joined:
    Jul 5, 2012
    Posts:
    58
    Got it sorted out?

    I just suggested to post your coding issues at another thread as this one has another subject. But I'm not mad or anything.

    Keep asking! I'll help as I can.
     
  27. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
    Yes re: shipping products and customers not caring how they're made, but what's that got to do with learning to program? I'm not suggesting that we all reinvent the wheel for production purposes (though reimplementing a wheel can be a great learning exercise), I'm suggesting that people should understand the tools they're using. And starting to learn programming at Space Invaders rather than Hello World is a fundamentally bad idea in that respect.

    It's even worse when you make Space Invaders where 95% of the code is already written for you and locked away in a nice black box where you can't even see it.

    Unity is a great way to learn how to make games, including game programming. But it is my belief that game programming is something best learned after you've got the fundamentals of general programming down pat. Because learning how a for loop works and what classes do and how variables and datatypes work and the ins and outs of various data structures and how (and when) to write your own is not "game programming", but it is stuff you really ought to know beforehand.

    By the way, "Hello World" is not "low level programming". "Low level programming" is stuff like using hand coded assembly language to write an optimised set of transform operations - things which you wouldn't know even happened if you started to learn programming by making a game in Unity.
     
  28. typane

    typane

    Joined:
    Nov 25, 2011
    Posts:
    297
    Learn C++
    Obtain epiphany
    ???
    Profit