[C# Tutorial] Object movement, scoreboard, and collision in under 20 lines of code

begallegal1 Dec 2, 2013

  1. begallegal1

    begallegal1 Medicine Man Lifetime Gold TeamXPG
    0/47

    Joined:
    Oct 31, 2011
    Messages:
    3,694
    Likes Received:
    3,127
    Trophy Points:
    235
    Gender:
    Male
    Location:
    In a Field of Green
    Console:
    Xbox
    Hello all , today I have a super quick tut that demonstrates some of the key features in your most basic of games. :gamer4:

    sorry the frame rate of the capture is a bit low ,that's why the ball seems to disappear at times.
    http://www.youtube.com/watch?v=2EjUq7n-ZSo

    [​IMG]


    You will need a form sized at 600x600 , a button , a label , an "oval shape object" 100x100, and 2 timers

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace ballExampleTut
    {
    public partial class Form1 : Form
    {
    //create our score object used for counting our bounces
    public int score = 0;

    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    //when the form loads we set the label
    //to show Bounces: + our score (which starts @ 0)
    scoreLbl.Text = "Bounces: " + score.ToString();
    }

    private void go_Click(object sender, EventArgs e)
    {
    //when GO button is clicked we start the timer for downward movement
    downTimer.Start();
    }

    private void downTimer_Tick(object sender, EventArgs e)
    {
    //variables for our "ball" location on the X and Y axis
    var X = ballShape.Location.X;
    var Y = ballShape.Location.Y;

    //if the ball has not reached the bottom of our board
    if (ballShape.Location.Y <= 450)
    {
    //move the ball +15 (down) the Y axis
    ballShape.Location = new Point(X, Y + 15);
    }
    else //meaning if our ball has reached the bottom of our board
    {
    //stop this (down movement) timer from running
    downTimer.Stop();
    //start the up movement timer
    upTimer.Start();
    }
    }

    private void upTimer_Tick(object sender, EventArgs e)
    {
    //variables for our "ball" location on the X and Y axis
    var X = ballShape.Location.X;
    var Y = ballShape.Location.Y;

    //if the ball has not reached the top of our board
    if (ballShape.Location.Y != 8)
    {
    //move the ball -15 (up) the Y axis
    ballShape.Location = new Point(X, Y - 15);
    }
    else //meaning if our ball has reached the top of the board
    {
    //stop this (up movement) timer from running
    upTimer.Stop();
    //start the down movement timer
    downTimer.Start();
    //add 1 to our score count(lookup operators for more info)
    score++;
    //update our score label with our new score total
    scoreLbl.Text = "Bounces: " + score.ToString();
    }
    }


    }
    }


    So with this tiny little bit of code we have created object movement, scoreboard, and a very generic manual type of collision detection all are the most basic things in any type of game, I hope this comes in handy to those looking into code , small things like this can be great skill builders and can be expanded on greatly!

    The notes in the code should pretty well explain how everything works, but as always if you have any questions or would like to share an alternate method for these functions etc,, feel free to drop a comment

    happy coding
    enjoy
    :begal:
     

    Attached Files:

    • Like Like x 6
  2. gold972

    gold972 团队XPG影响 Effect XPG Developer TeamXPG
    205/282

    Joined:
    Dec 6, 2010
    Messages:
    6,832
    Likes Received:
    14,856
    Trophy Points:
    205
    Gender:
    Male
    Location:
    France Kernal XDK
    Console:
    Xbox
    great tuts bro as always ;)
     
    • Like Like x 1
  3. xpghax

    xpghax Gold Section Mod/Uploader
    205/282

    Joined:
    Jan 30, 2011
    Messages:
    8,914
    Likes Received:
    3,972
    Trophy Points:
    205
    Location:
    in sidey's basement :(
    Console:
    Other
    awesome TUT begal ;)
     
    • Like Like x 1
  4. Bu

    Bullet Guest

    Nice clean tutorial Begal.
     
    • Like Like x 1
  5. xIIJazza

    xIIJazza Gods divine one
    0/47

    Joined:
    Jun 7, 2011
    Messages:
    548
    Likes Received:
    149
    Trophy Points:
    0
    Gender:
    Male
    Console:
    Xbox
    All of your tutorials have been helping me so far :) Keep up the amazing work :3
     
    • Like Like x 1

Share This Page

Close