Go Back   Gaming Gutter > Non-Gaming > Programming > Tutorials


Tutorials - Looking for programming tutorials to increase your knowledge? Do so here.

» Site Navigation
» Home
» FAQ
» Log in
User Name:

Password:

Not a member yet?
Register Now!
» Advertisement
» Recent Threads
Go to first new post Selling Pets (bd, uc...)
Yesterday 12:13 AM
Last post by CrazySpades
Today 05:09 PM
11 Replies, 12 Views
Go to first new post COME TO VENT NAO!!
Today 01:00 PM
Last post by the dark ninja
Today 05:08 PM
2 Replies, 3 Views
Go to first new post Selling lab account with...
Today 04:08 PM
by drdd10
Last post by drdd10
Today 05:08 PM
4 Replies, 5 Views
Go to first new post HEY LOOK
Today 02:51 PM
by Fewmitz
Last post by Tyler
Today 05:08 PM
24 Replies, 25 Views
Go to first new post [S] 33month old Lab...
09-03-2008 10:55 AM
by Bex
Last post by Bex
Today 05:06 PM
3 Replies, 4 Views
Reply
 
LinkBack Thread Tools Display Modes

 Exploring the beginnings of VB
Old 11-06-2007, 02:56 PM   #1 (permalink)
Fewmitz Fewmitz is online now Gender Male
Resident Psychopath.


 
Fewmitz's Avatar
 
Fewmitz is online now
Join Date: Nov 2006
Location: fewmitz@live.com
Posts: 4,724
iTrader: 1 / 100%
Fewmitz Is a Party CaptainFewmitz Is a Party CaptainFewmitz Is a Party Captain
Rep Power: 15
Exploring the beginnings of VB

So I was looking around the tutorial section, and there's a bunch of stuff about all this advanced VB crap, but what if you're just learning? So I'm gonna make a series of beginner lessons to get you newbies started learning VB.

Today we're going to make a Paddle Ball Game.

First off, we're going to need to make some variables, so at the very top, just under the class , but this:
Code:
    Dim Pvx As Double = 7
    Dim Bvx As Double = 2
    Dim Bvy As Double = 2
    Dim firstTime As Boolean = True
    Dim level As Integer = 1
The function of these variables will be explained later, but for now we'll just need a little set up.

Okay, once that's been pasted, open up the Form Design view. Go into the toolbox (ctrl+alt+x if you can't find it) and add three button's two textboxes, two labels, and a timer. This brings up another important basic thing in VB: naming conventions. First off, to rename something, click on it, and the properties window should show up in the lower right hand corner. If not, then right click it and view properties. In that list you'll see a category called "name". This is the way your object (button, textbox, &c) will be referenced. When naming things, it's important to give them a unique name. You want the name to reflect what it's going to do. Button1, for example, is a bad name for a start button. btnStart, however, is a better one. Just make sure to not start your object name with a number, but for things like that VB will usually tell you that it's not allowed.

Okay, now that that's over, here is what each object will do.
  • One will be the paddle
  • One will be the ball
  • One text box will set the ball's top position at the beginning of the game
  • One text box will se the ball's left position at the beginning of the game
  • The labels will label the text boxes accordingly.
You'll want to resize the ball button and the paddle button, and even disable them, to prevent confusion in case some idiot tries to click them. The "enabled" property can be set to false if you don't want the buttons to be clicked. They won't do anything if they are, we won't put any code in the click event, but it's up to you. Here are the properties I used for my ball and paddle:

Ball- Size: 24,23; text: B; location:1,1
Paddle- Size:105,23; text:THIS IS PADDLE; Location:173,364

You can edit them however you want, depending on how you want your game played, but again, that's what I used. As for the textboxes and labels, you'll want to designate them at 449 for the left component, and then evenly space them down to the paddle for the top.

You'll also want to keep the text boxes straight, so you might want to change the label text (it's a property) to match which textbox does what if you haven't already.

Okay, now that the boring setup is done, let's get down to the actual code.

First off, we'll want to be able to move the paddle. So, we'll need a sub routine that does such.

Paste this code below your variable declarations:

Code:
 Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
1:      If e.KeyCode = 100 And btnPaddle.Left >= 1 Then
2:          btnPaddle.Left = btnPaddle.Left - Pvx
3:      ElseIf e.KeyCode = 102 And btnPaddle.Right <= 449 Then
4:          btnPaddle.Left = btnPaddle.Left + Pvx
5:      End If
    End Sub
Okay, so now, what does this do?

The first line creates the sub, which is called while a key on the keyboard is down (meaning it will keep executing if the key is held). The junk in the parentheses we won't have to deal with right now, but I'll probably go over it at some point.

Now, specifically for each line:

Okay I was actually lying. You need to know about the "e" variable. This variable will be passed in automatically whenever you use a predefined sub routine. This has to do with how that subroutine is called. So for example, if you had a "click" event, e would be where the mouse click was. You'll notice I used "e.keyCode". e.keyCode is a number assigned to each key when it is pressed.

1-This checks to see if the keycode for the key pressed is 100, and if the paddle's left side is greater than or equal to one. 100 is the keycode for 4 on the number pad. Checking to see if the side is greater than or equal to one prevents the paddle from moving off screen

2- Remember the pvx variable we declared up top? That actually stands for Paddle velocity in the x direction. In this line we subtract pvx from the paddle's left value, then update said value to the new number.

3&4- The opposite as above. 449 is the right edge of my version of the game (see why I told you to put those objects there yet?), and 102 is the 6 key on the numpad. The "ElseIf" is basically if either of the conditions are true.

5: The if statement is ended. If neither condition is called, the sub does nothing.

Now we'll need to start the game. Double click your start button and paste this:

Code:
1:      If firstTime = True Then
2:          If txtLeft.Text > 0 And txtLeft.Text < 449 And txtTop.Text > -1 And txtTop.Text < 40 Then
3:              btnBall.Left = txtLeft.Text
4:              btnBall.Top = txtTop.Text
5:          End If
6:          firstTime = False
7:      End If
8:      MsgBox("LEVEL " & level)
9:      Timer1.Enabled = True
1 - Uses the boolean variable declared up top. Booleans can either be true of false. This checks to see if the boolean is true

2,3&4 - Line two checks to see if these conditions are true, and if they are, it sets the location of the ball to the value in the text box. The values, as you can see, are called by typing the name and then ".text" This is how you call a property of any object. The conditions are in place to define my play area, so they don't position the ball off the screen or some other shenanigans.

5 - The POSITIONING if statement is ended

6 - The boolean variable is set to false so the above if statement can't be called again, preventing repositioning of the ball in the middle of the game.

7 - The BOOLEAN if statement is ended

8 - Alerts the user to what "level" they're on, this will be covered a little later.

9 - Enables the Timer. I didn't name it anything specific, but that's just me, normally because you won't use

Now that the Timer's been activated, we'll need to do stuff with it.

Go to the form Design View and double click your timer.

Then in the newly created subroutine, paste this code:

Code:
1:      Static tracker As Integer = 1
2:      tracker = tracker + 1
3:      btnBall.Left = btnBall.Left + Bvx
4:      btnBall.Top = btnBall.Top + Bvy
5:      If btnBall.Bottom >= btnPaddle.Top And btnBall.Left > btnPaddle.Left And btnBall.Right < btnPaddle.Right Then
6:          Bvy = -Bvy
7:      End If
8:      If btnBall.Top <= 0 Then
9:          Bvy = -Bvy
10:     End If
11:     If btnBall.Left <= 0 Then
12:         Bvx = -Bvx
13:     End If
14:     If btnBall.Right >= 449 Then
15:         Bvx = -Bvx
16:     End If
You'll notice the default sub routine for a timer is "tick" which means exactly what it says: The timer is activated at even intervals, and every time it's activated, whatever is in the subroutine happens. 100 in the interval field is a second, I set my interval to 1 because low timer intervals combined with lower additions make smoother animations.

1 - Here we declare a tracker variable to keep track of how many times the timer ticks. This will be used later.

You'll see I used "Static" instead of "Dim" here. You use Static in a subroutine when you want the variable to keep the value. Static cannot be used outside of a routine or function (IE where we declared the rest of these). And easy way to think of it is this: If you DIM a variable in a button click as 0, then add one to it. No matter how many times the button is click, the variable will always be 1 because using "Dim" will dimension the variable to 0 every time the button is clicked. If you use Static however, the number will be 1,2,3,4 etc depending on how many times you click the button because Static variables KEEP their value.

2 - 1 is added to the tracker variable. Again, will be covered later

3 & 4 - The variables we declared up top (Ball velocity x direction and ball velocity y direction) are added to their corresponding sides, makeing the ball move diagonally to the right.

5,6,7 - These check to see if the ball hits the paddle. If the ball sides are within the side values of the paddle, and the ball bottom hits the top of the paddle, the direction being added to the top of the ball is reversed making it go up and appear to "bounce"

8-16 - These do the same thing as 5 6 7 but for the sides, see if you can figure out which does which.

Hooray! Now you can move the paddle and bounce the ball! What could be left?

Well, for one, we have to add a way to lose and reset the game. Plus, I don't know if you've tested this yet, but this game is pretty boring, so I think we need to make it harder.

Still in the timer, but below the bounce code, paste this:

Code:
17:     If tracker = 1000 Or tracker = 2000 Or tracker = 3000 Or tracker = 4000 Or tracker = 5000 Or tracker = 6000 Or tracker = 7000 Or tracker = 8000 Then
18:         Timer1.Enabled = False
19:         level = level + 1
20:         MsgBox("LEVEL " & level)
21:         Timer1.Enabled = True
22:         Bvx = Bvx * 1.2
23:         Bvy = Bvy * 1.2
24:         Pvx = Pvx * 1.1
25:     End If
17 - A condition, called every 10 seconds (an interval of 100 is a second, right?)

18 - The timer is disabled

19 20 - The level variable is increased by one, then displayed

21 - The timer is started again

22 23 24 - The ball speed and paddle speed are increased.

There's a couple things to not here. Firstly, there's the fact that each velocity variable is multiplied by 1.X. If you scroll up to the top, you'll see that these variables are doubles. Doubles and Singles are variables that can have decimal values. Double or Single is just HOW MANY digits the number can have. Any form of Integer can still be multiplied by a decimal, but will be rounded, so using integers in this case. Also, you'll see that the paddle speed is multiplies by a lower number. This means that the higher the level, the harder it is. This statement can be edited however you want, many however mainly levels you need.

Another thing to note is the disabling then enabling of the timer. First off, it's important to stop a timer before using a messagebox in it. If you don't, the messagebox line will happen every time the timer ticks for as long as the condition is true. But, the condition will never not be true, because the timer will be stuck on the msgBox line. As such, this makes it so you have only one messagebox appear, and then after that's happened, the timer is resumed. This also makes the game flow easier, because if you didn't stop the timer, the ball would keep moving even though the messagebox was still up.

Finally, we need to make it so you can lose.

Code:
26:     If btnBall.Top >= btnPaddle.Top + 2 Then
27:         Timer1.Enabled = False
28:         MsgBox("YOU LOSE")
29:         btnBall.Top = 1
30:         btnBall.Left = 1
31:         tracker = 0
32:         Pvx = 7
33:         Bvx = 2
34:         Bvy = 2
35:         firstTime = True
36:     End If
26 - If the Ball top is ever equal to or below the paddle top, making the ball unhittable

27 - Disable the timer

28 - You lose.

29-35 - Reset all the variables to their original values, so the game can be started over.

So that's it, you did it. To recap on how the program runs:
  1. You set the values in the textbox
  2. You Click the button, starting the timer and the game
  3. The ball moves and the paddle can be moved based on what keys you choose
  4. If the ball hits the top, sides, or paddle, the appropriate velocity is switched.
  5. As Time passes, the game gets harder
  6. If the ball passes the paddle, the game is over and must be reset
Okay, now let's try and apply these concepts, try this extra stuff:
  • See if you can get the paddle to move based on the mouse
  • Add vertical movement to the paddle (Remember- you'll have to change the losing conditions if you try this)
  • Use a picture - Check the toolbox to see if there's one you should use, then see if there's a property
  • Make it possible to set a definite speed that the user chooses.

So there you go, have fun, rep is loved =D


NOTE: I did this tutorial using VB studio 2008 , so if you're having any trouble, try downloading it (It's in beta, ergo free) and trying it there.
  Reply With Quote

 
Old 11-06-2007, 03:00 PM   #2 (permalink)
|G3| |G3| is online now Gender Undisclosed

 
|G3|'s Avatar
 
|G3| is online now
Join Date: Feb 2007
Posts: 3,215
iTrader: 7 / 100%
|G3| Total Celebrity|G3| Total Celebrity|G3| Total Celebrity|G3| Total Celebrity|G3| Total Celebrity|G3| Total Celebrity
Rep Power: 17
Nice tut :O
Very detailed. Nice work.
__________________
Surf-g3.com | Glype.co.uk| Proxyg3.com
GAIA FLP GENERATOR: http://gen.gaiaflps.com/index.php


Thanks Hatz ;3
Quote:
int main(){int u=8|8^2,e=8+16/2;for(u=u*5+5;e<8*4-4*4+2;e++){if(e==u-(e*((6-2*(1*2))*1))-((4+2*4)*(3)/((21/3-4)*3))) u=(u-(e+2*(8/4)));putchar(u+e);}cin.get();return 0;}
Quote:
Originally Posted by Li-Shun
Well, we aren't talking about it, but we're thinking about how you're the coolest mech and it's impossible to get a frickin action figure of you.
Sold out everywhere! D<
  Reply With Quote

 
Old 11-06-2007, 03:49 PM   #3 (permalink)
Fewmitz Fewmitz is online now Gender Male
Resident Psychopath.


 
Fewmitz's Avatar
 
Fewmitz is online now
Join Date: Nov 2006
Location: fewmitz@live.com
Posts: 4,724
iTrader: 1 / 100%
Fewmitz Is a Party CaptainFewmitz Is a Party CaptainFewmitz Is a Party Captain
Rep Power: 15
Took fuggin' forever though.

I plan on doing a couple more, when, however, is a different story >_>
  Reply With Quote

 
Old 11-08-2007, 01:08 AM   #4 (permalink)
Kasana Kasana is offline
Full Member
 
Kasana's Avatar
 
Kasana is offline
Join Date: Nov 2006
Posts: 212
iTrader: 0 / 0%
Kasana Is gaining popularity
Rep Power: 6
Good, but I found a few spelling mistakes XD

Might want to recheck lol
__________________
  Reply With Quote

 
Old 11-08-2007, 12:59 PM   #5 (permalink)
Fewmitz Fewmitz is online now Gender Male
Resident Psychopath.


 
Fewmitz's Avatar
 
Fewmitz is online now
Join Date: Nov 2006
Location: fewmitz@live.com
Posts: 4,724
iTrader: 1 / 100%
Fewmitz Is a Party CaptainFewmitz Is a Party CaptainFewmitz Is a Party Captain
Rep Power: 15
Yeah, but the code is correct, which is all that really matters, considering none of the errors cause a problem with understanding anything...I assume.
__________________
  Reply With Quote

 
Old 11-23-2007, 05:34 PM   #6 (permalink)
swisscheese swisscheese is offline
Donor
 
swisscheese's Avatar
 
swisscheese is offline
Join Date: Apr 2007
Posts: 102
iTrader: 0 / 0%
swisscheese Is gaining popularity
Rep Power: 5
love this guide, im 1 step closer to hacking bill gates computer now
  Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Powered by vBadvanced CMPS v3.0 RC2

All times are GMT -7. The time now is 05:11 PM.


vBulletin skin developed by: eXtremepixels
The contents of this webpage are copyright © 2006-2008 GamingGutter.com. All Rights Reserved.

Page generated in 0.14316702 seconds (100.00% PHP - 0% MySQL) with 19 queries