[C# Tutorial] Mini shooter game in 30 lines of code

begallegal1 Dec 4, 2013

  1. begallegal1

    begallegal1 Medicine Man Lifetime Gold TeamXPG
    0/47

    Joined:
    Oct 31, 2011
    Messages:
    3,694
    Likes Received:
    3,124
    Trophy Points:
    0
    Gender:
    Male
    Location:
    In a Field of Green
    Console:
    Xbox
    Hello all, today I have another quick tut on a very very basic mini game in C#, requires only 3 picture boxes and about 30 lines of written code ,the notes in the code should pretty well cover it , but as always feel free to ask questions or give suggestions on alternate methods etc.

    http://www.youtube.com/watch?v=Ja9RmmsZIp8

    example image
    [​IMG]


    /************begallegal1 @ XPGamesaves.com**************/
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    namespace Shootz
    {
    public partial class Form1 : Form
    {
    //a random number for our enemy location later,
    Random rnd = new Random();
    //and our player score variable
    int score = 0;

    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    //add the scoreboard to our forms text
    this.Text = "Shootz....Score: " + score.ToString();
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
    //lock our players Y axis location
    var Y = 500;
    //create variable for player X axis location
    var X = player.Location.X;

    //if left arrow key is pressed
    if (e.KeyCode == Keys.Left)
    {
    //and player is within the forms bounds
    if (player.Location.X <= 530 && player.Location.X >= 10)
    {
    //move player 10 to the left
    player.Location = new Point(X - 10, Y);
    }else{//if above cannot be met
    //this is our way of looping the player
    //to the other side of the form
    player.Location = new Point(530, Y);
    }
    }
    //if right arrow key is pressed
    if (e.KeyCode == Keys.Right)
    {
    //and player is within the forms bounds
    if (player.Location.X <= 530 && player.Location.X >= 10)
    {
    //move player 10 to the right
    player.Location = new Point(X + 10, Y);
    }else{//if above cannot be met
    //this is our way of looping the player
    //to the other side of the form
    player.Location = new Point(10, Y);
    }
    }
    //if spacebar is pressed
    if (e.KeyCode == Keys.Space)
    {
    //give our "shot" picture a new location in front of our player
    shot.Location = new Point(player.Location.X + 15, 460);
    //show "shot" picture
    shot.Visible = true;
    //start timer to move shot on screen
    shotMove.Start();
    }
    }

    private void shotMove_Tick(object sender, EventArgs e)
    {
    //create a new location for our "shot" in front of the player
    //and -10 from last timer tick
    shot.Location = new Point(player.Location.X + 15, shot.Location.Y - 10);
    //check for collision using method below
    CheckCollision();
    }

    public void CheckCollision()
    {
    //if our shot collides with our enemy
    if (shot.Bounds.IntersectsWith(enemy.Bounds))
    {
    //hide both shot and enemy
    enemy.Visible = false;
    shot.Visible = false;
    //stop shot movement timer
    shotMove.Stop();
    //add 1 to our score and update the text to reflect current score
    score++;
    this.Text = "Shootz....Score: " + score.ToString();
    //set a new enemy position in a random location and un-hide it
    enemy.Location = new Point(rnd.Next(10,530),rnd.Next(20,100));
    enemy.Visible = true;
    }
    }
    }
    }

    So we created a player, a weapon, and an enemy, we gave them controls, we made a basic collision detection system, and we made a scoreboard. Of course this is the absolute most basic way of handling these things and has little purpose in the real world, but all these concepts are the building blocks of nearly every game ever made

    see you all next time,

    enjoy
    :begal:
     

    Attached Files:

    • Like Like x 4
  2. Bu

    Bullet Guest

    Great tut Begal nice work ;)
     
    • Like Like x 2
  3. KillerVidz

    KillerVidz Rocky BANNED
    0/47

    Joined:
    Feb 20, 2012
    Messages:
    3,312
    Likes Received:
    1,277
    Trophy Points:
    0
    Gender:
    Male
    Location:
    XPG-UK
    Console:
    Xbox One
    Nice stuff man ;) You are C# God ;)
     
  4. InfernoMods

    InfernoMods Newbie BANNED
    0/47

    Joined:
    Dec 12, 2013
    Messages:
    35
    Likes Received:
    4
    Trophy Points:
    0
    Gender:
    Male
    Location:
    Hell Michigan
    Console:
    Xbox
    Great tut man gotta try this out when i'm not to lazy to open up visual studio :D
     

Share This Page

Close