Is this too complicated to do?
Is this too complicated to do?
Is this too complicated to do?
Is this too complicated to do?
Is this too complicated to do?
Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do?
Is this too complicated to do? Is this too complicated to do?
Is this too complicated to do?
Go Back  Xtreme Visual Basic Talk > > > Is this too complicated to do?


Reply
 
Thread Tools Display Modes
  #21  
Old 06-10-2011, 12:44 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default


My apologies passel it was PlausiblyDamp who posted the “Windows Application 1” above that I would like the comments for.

There is some good code in there I would like to try get my head around it. Atmaweapon tried to explain Classes in another thread, but I am still struggling to come to grips with it. I notice the application PlausiblyDamp wrote calls some external classes and perhaps comments might help cement it for me a little better, and explain the fascinating code he wrote for the non class related code.

My interest in games has increased lately (wrote my fist complete game Tic-Tac-Toe the other day in next to no time) and I am now interested in new functions like “Point” that I have never used. I offered 45Minutes some advice on this current game in another thread here Making a picture box move right then left but it was only to move a single picture box.

Clearly 45Minutes has moved beyond the very basic advice I offered previously, and now I would like to try bringing myself up to where he is at now, and that is moving a group of aliens across the screen.

Edit: Looks like I have already used Point before... terrible memory

Last edited by CodeCruncher; 06-10-2011 at 12:52 AM.
Reply With Quote
  #22  
Old 06-11-2011, 10:09 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by CodeCruncher View Post
My apologies passel it was PlausiblyDamp who posted the “Windows Application 1” above that I would like the comments for.

There is some good code in there I would like to try get my head around it. Atmaweapon tried to explain Classes in another thread, but I am still struggling to come to grips with it. I notice the application PlausiblyDamp wrote calls some external classes and perhaps comments might help cement it for me a little better, and explain the fascinating code he wrote for the non class related code.

My interest in games has increased lately (wrote my fist complete game Tic-Tac-Toe the other day in next to no time) and I am now interested in new functions like “Point” that I have never used. I offered 45Minutes some advice on this current game in another thread here Making a picture box move right then left but it was only to move a single picture box.

Clearly 45Minutes has moved beyond the very basic advice I offered previously, and now I would like to try bringing myself up to where he is at now, and that is moving a group of aliens across the screen.

Edit: Looks like I have already used Point before... terrible memory
Yes, Thank you code cruncher, I'm trying to do more complex things now, but I need more time to understand how to do them. Trying to make arrays work with loops to produce heaps of aliens performing the same function. I might know another way to do this.
Reply With Quote
  #23  
Old 06-12-2011, 03:54 AM
PlausiblyDamp's Avatar
PlausiblyDampIs this too complicated to do? PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Just to give a little more detail about the sample I posted earlier....

The main class is the Sprite class, this effectively represents a thing that can draw itself at a location on screen, track it's own position and movement speed and update it's own location on request.

If we break it down into smaller (commented) chunks we can see what is going on a bit better.
The Sprite class currently only tracks four things, the image to draw, the size of the image, it's current position and how much it moves each update cycle - these are all held in three class level variables.
Code:
'Current location of the sprite
Public Property CurrentLocation As Point
'How much we move per update cycle
Public Property Movement As Point

'Image the sprite will display
Private image As Image
'Size of the image we display - used in collision checking rather than obtaining it from the image each time
Private imageSize As Size
When we need to create an instance of this class we can use it's constructor to provide the image to draw
Code:
'Constructor - simply takes an image to use
Public Sub New(pic As Image)
    image = pic
    imageSize = image.Size
End Sub
this constructor accepts an image and stores this in the image property and also gets the size of the image so we can use it for detecting when we have hit the sides of the picture box we are being drawn on. This is called with code similar to
Code:
Dim s as New Sprite(My.Resources.logo)
The drawing itself is simply
Code:
'Draw ourselves on the Graphics object we are given
Public Sub Draw(g As Graphics)
    g.DrawImageUnscaled(image, CurrentLocation)
End Sub
which takes a Graphics object and we simply draw ourselves onto this graphics object at our current location.

The main logic of how a sprite moves / what happens when it hits the edge of the picture box is contained in the UpdatePosition method
Code:
'This method will update the sprites location, and if it has reached the limit for an axis
'then will simply reverse direction for that axis
Public Sub UpdatePosition()
    CurrentLocation += Movement 'change current location to be our old position + distance we move by

    If CurrentLocation.X < 0 OrElse (CurrentLocation.X + imageSize.Width) > World.WorldSize.Width Then
        Movement = New Point(Movement.X * -1, Movement.Y)
    End If

    If CurrentLocation.Y < 0 OrElse (CurrentLocation.Y + imageSize.Height) > World.WorldSize.Height Then
        Movement = New Point(Movement.X, Movement.Y * -1)
    End If
End Sub
This method simply causes the CurrentLocation of the Sprite to be updated based on the Movement property - this effectively updates the Sprite's X and Y co-ordinates. We also then check if we have reached the edge of the picturebox and if so we reverse the appropriate direction (e.g. if we have reached the far right we change our Movement so we are now going to the left etc.)

This now gives us a self contained (if rather simplistic) Sprite class that can now track where on screen it should be, update it's position, track it's own movement speed and store an image to display as well. In a more complex system we could use an array of images and cycle through them to provide animation or other feedback or also detect collision with other objects not just with the edges of the display area by extending this logic.

If we look at the form itself we are simply setting a couple of performance options in the constructor
Code:
Sub New()

' This call is required by the designer.
InitializeComponent()

'performance optimisations
SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
End Sub
and we also declare a couple of other variables
Code:
Dim rng As New Random
Dim things As New List(Of Sprite) 'list of sprites we are managing
rng is an instance of the Random class, this is used to generate random numbers and things is a list of Sprites we are going to draw on screen. In the form load we populate this list with 11 randomly generated Sprite instances with the following code
Code:
'create a list of 11 sprites
For i As Integer = 0 To 10
    Dim s As Sprite

    'set randomly to one of two images
    If rng.Next(10) > 5 Then
        s = New Sprite(My.Resources.logo)
    Else
        s = New Sprite(My.Resources.Untitled)
    End If

    'give each a random start location
    s.CurrentLocation = New Point(rng.Next(300), rng.Next(300))
    'and a random speed / direction
    s.Movement = New Point(rng.Next(-10, 10), rng.Next(-10, 10))
    things.Add(s)
Next
The form also contains a timer which when it fires causes all the Sprite's to update themselves
Code:
'every tick will update the game's status and force a redraw of the screen
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    For Each thing In things
        thing.UpdatePosition()
    Next

    PictureBox1.Invalidate()
End Sub
the final line PictureBox1.Invalidate() will inform Windows that the picturebox needs to redraw it's contents - this will cause the Paint event to be fired and it is in this event we get all the Sprites to draw themselves to the screen
Code:
'When asked to repaint the picturebox we get each sprite to draw itself at 
'it's current location.
Private Sub PictureBox1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    Dim g As Graphics = e.Graphics
    g.Clear(Color.Gray)
    For Each thing In things
        thing.Draw(g)
    Next
End Sub
Hope that helps to clear some of the code up a bit.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #24  
Old 06-15-2011, 07:36 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

45 Minutes what PlausiblyDamp has provided does much of what you are chasing; it just needs to be reworked.

Instead of using random direction I’m sure it could be reworked to provide left and right movement and when it hits the boundary drop by some amount. (like what I gave you but this has a bit of class about it)

Using that in conjunction with a looping array and I think you would have all the elements you need for the game.

PlausiblyDamp... After going through the code a number of times I worked out some of it, but there are still some things I don’t understand.

Code:
Public Property CurrentLocation As Point
Public Property Movement As Point
Private image As Image
Private imageSize As Size
I have never used a Property before... I know this is some kind of declaration but how is the Property different to say something like Public CurrentLocation as Point?

I’m guessing Image and Size are reserved words in order to use them in a declaration like that?

Code:
Public Sub New(pic As Image)
    image = pic
    imageSize = image.Size
End Sub
Couldn’t work out what you did here... At the top you declared “Private image as Image”

But in the Sub you have (pic as Image), now “pic” doesn’t seem to be declared anywhere, but is used without anything being put in it. Then it value is attributed to image (Image) = pic.

Code:
    imageSize = image.Size
I get this bit... it is just the property values of image being put in imageSize

Code:
Dim s as New Sprite(My.Resources.logo)
Fairly straight forward taking the properties of the image and storing it in “s”

Code:
Public Sub Draw(g As Graphics)
    g.DrawImageUnscaled(image, CurrentLocation)
End Sub
Again “g” doesn’t seem to be declared anywhere (unless “(g As Graphics)” is a declaration)

Code:
g.DrawImageUnscaled(image, CurrentLocation)
Wild guess that this draws the image in the current location, not sure what “Unscaled” does.

Code:
        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
No idea but for the time being understand they improve the graphics.

Code:
Dim things As New List(Of Sprite) 'list of sprites we are managing
A List is like an Array? and it fills the List with Sprite objects?

Sorry to ask so many questions but this is all new stuff.
Reply With Quote
  #25  
Old 06-15-2011, 08:24 AM
PlausiblyDamp's Avatar
PlausiblyDampIs this too complicated to do? PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Quote:
Originally Posted by CodeCruncher View Post
PlausiblyDamp... After going through the code a number of times I worked out some of it, but there are still some things I don’t understand.

Code:
Public Property CurrentLocation As Point
Public Property Movement As Point
Private image As Image
Private imageSize As Size
I have never used a Property before... I know this is some kind of declaration but how is the Property different to say something like Public CurrentLocation as Point?
A property behaves a lot like a public variable, behind the scenes it is implemented as two methods (a get and a set) - one of the things new in VB is the ability to use a shorthand syntax instead of the more long winded syntax previously required. Under 2008 or earlier the code for the CurrentLocation would have looked more like...
Code:
private _currentLocation as Point
    Public Property CurrentLocation As Point
        Get
            return _currentLocation
        End Get
        Set(value As Point)
            currentLocation = value
        End Set
    End Property
The code I used just takes the drudgery out of it by letting the compiler worry about the details.

Quote:
Originally Posted by CodeCruncher View Post
I’m guessing Image and Size are reserved words in order to use them in a declaration like that?
They are part of the .Net framework- specifically http://msdn.microsoft.com/en-us/libr...wing.size.aspx and http://msdn.microsoft.com/en-us/libr...ing.point.aspx

Quote:
Originally Posted by CodeCruncher View Post
Code:
Public Sub New(pic As Image)
    image = pic
    imageSize = image.Size
End Sub
Couldn’t work out what you did here... At the top you declared “Private image as Image”

But in the Sub you have (pic as Image), now “pic” doesn’t seem to be declared anywhere, but is used without anything being put in it. Then it value is attributed to image (Image) = pic.
The Sub New is what is known as a constructor - this is called when you create a new instance of an object, pic is simply a named parameter that allows us to pass an image into the object when we create it.

I am storing the image in a class level variable called image so it is available to the entire class.

Quote:
Originally Posted by CodeCruncher View Post
Code:
    imageSize = image.Size
I get this bit... it is just the property values of image being put in imageSize
Spot on.

Quote:
Originally Posted by CodeCruncher View Post
Code:
Dim s as New Sprite(My.Resources.logo)
Fairly straight forward taking the properties of the image and storing it in “s”
This is creating a new instance of the Sprite class and passing an image into the constructor, see above for the explanation of Sub New.

Quote:
Originally Posted by CodeCruncher View Post
Code:
Public Sub Draw(g As Graphics)
    g.DrawImageUnscaled(image, CurrentLocation)
End Sub
Again “g” doesn’t seem to be declared anywhere (unless “(g As Graphics)” is a declaration)

Code:
g.DrawImageUnscaled(image, CurrentLocation)
Wild guess that this draws the image in the current location, not sure what “Unscaled” does.
g is a parameter passed to the Sub, this is a Graphics object - effectively the surface we are drawing onto. The actual value for g will be supplied in the Paint event for the picturebox on form1

Quote:
Originally Posted by CodeCruncher View Post
Code:
        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
No idea but for the time being understand they improve the graphics.
They are just performance tweaks.

Quote:
Originally Posted by CodeCruncher View Post
Code:
Dim things As New List(Of Sprite) 'list of sprites we are managing
A List is like an Array? and it fills the List with Sprite objects?
A List(Of ...) has a lot in common with an array however it isn't a fixed size and is easier to work with in most situations http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

Quote:
Originally Posted by CodeCruncher View Post
Sorry to ask so many questions but this is all new stuff.
Not a problem, it is why we are here
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #26  
Old 06-16-2011, 06:04 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Thumbs up

Thanks PlausiblyDamp that made it easier to understand what is happening.

Here you go 45 Minutes my 5-10 minute mod of PD's Form1_Load event

As I started coding I realised you don't need an array to store all your rows of aliens, PD's list holds them just fine.

Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim blnAlt As Boolean
        Dim iLocX As Integer
        Dim iLocY As Integer

        For i1 As Integer = 0 To 1 'Move next row of aliens right
            iLocX += 100
            For i2 As Integer = 0 To 1 'Move next alien down
                iLocY += 100
                Dim s As Sprite

                If blnAlt = True Then 'alternate the images
                    s = New Sprite(My.Resources.logo)
                    blnAlt = False
                Else
                    s = New Sprite(My.Resources.Untitled)
                    blnAlt = True
                End If
                s.Location = New Point(iLocX, iLocY) 'Set various starting postions
                s.Movement = New Point(10, 0) 'Move horizontally
                things.Add(s)
            Next
            iLocY = 0 'Reset ready for next pass
        Next

    End Sub

Last edited by CodeCruncher; 06-16-2011 at 06:22 AM.
Reply With Quote
  #27  
Old 06-16-2011, 06:07 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

I still left you plenty of work to do, once you run the mod you will see that all sprites try to hit the wall individually, you need to tweak this bit to turn them all together and drop by x amount.

If you should shoot all of the aliens except one that one should trigger the change of direction, or if some aliens are bigger than others they should touch first.

Code:
    Public Sub UpdatePosition()

        Location += Movement
        If Location.X < 0 OrElse (Location.X + imageSize.Width) > World.WorldSize.Width Then
            Movement = New Point(Movement.X * -1, Movement.Y)
        Else
            Movement = New Point(Movement.X, Movement.Y)
        End If

    End Sub

Last edited by CodeCruncher; 06-16-2011 at 06:34 AM.
Reply With Quote
Reply


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 Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Is this too complicated to do?
Is this too complicated to do?
Is this too complicated to do? Is this too complicated to do?
Is this too complicated to do?
Is this too complicated to do?
Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do? Is this too complicated to do?
Is this too complicated to do?
Is this too complicated to do?
 
Is this too complicated to do?
Is this too complicated to do?
 
-->