Results 1 to 3 of 3

Thread: Help with Asteroids Game! (positioning and repainting graphics)

  1. #1
    Spooferproof is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    May 2012
    Posts
    2
    Reputation
    0

    Question Help with Asteroids Game! (positioning and repainting graphics)

    Hey everybody! I'm new here as ive found some other of my questions answered through google search (such a pro searcher) Anyways I'm writing a Asteroids game in VB as a final project. I need some help with logic or how i can do some stuff.

    As of right now, I have a ship that can move directions and can move forward and backwards from those directions. This is done with a single PictureBox that changes the image on keypress(A for west, D for East, W for North, X for South and the others inbetween) and then whenever the "Up" key is pressed, it checked the tag on the imagebox and moves accordingly (same with down and backtrack).

    My next task at hand is lasers. I originally had the same idea to have a single picture box that changes images and direction according to the Ship's direction. And upon my thinking, i came to the conclusion that this won't work as each time I press the space bar (what ill use to shoot) it will simply revert the picture box to the ship and resend the laser.

    How can I make an old laser stay on screen while i shoot a new one? I had the idea of simply making multiple picture boxes, say 10, and call it "ammunition" but how can i "Roll" through ammuniction with a single keystroke? Like on multiple presses of "spacebar" it goes to the next ammunition, and eventually checkes if I have any left. or reloading ammunition? I have many ideas but i simpley don't know the logic behind the code.

    Thanks for your help guys, If you want i can keep this updated to show my progress over the next two weeks ill be working on this

    Here is the Code so far
    -Disclaimer- I'm new to VB and i'm sure this is very inefficiently done, try to work with me plz =D
    *******If someone can tell me how to make a spoiler i will edit*********

    Public Class Form1
    Dim AsteroidSize As Integer
    Dim AsteroidX(10) As Integer
    Dim AsteroidY(10) As Integer
    Dim ShipSize As Integer
    Dim ShipX As Integer
    Dim ShipY As Integer
    Dim laserX As Integer
    Dim laserY As Integer
    Dim MyGraphics As Graphics
    Dim MyRandom As New Random
    Dim BlankBrush As Brush


    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    MyGraphics.Dispose()
    BlankBrush.Dispose()
    End Sub


    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    'Erase Ship at old location
    MyGraphics.FillRectangle(BlankBrush, ShipX, ShipY, ShipSize, ShipSize)
    'Check for up key
    If e.KeyCode = Keys.Up Then
    'Checks which direction for momvement
    If picShip.Tag = "North" Then
    ShipY = ShipY - 7
    ElseIf picShip.Tag = "NorthEast" Then
    ShipY = ShipY - 7
    ShipX = ShipX + 7
    ElseIf picShip.Tag = "East" Then
    ShipX = ShipX + 7
    ElseIf picShip.Tag = "SouthEast" Then
    ShipX = ShipX + 7
    ShipY = ShipY + 7
    ElseIf picShip.Tag = "South" Then
    ShipY = ShipY + 7
    ElseIf picShip.Tag = "SouthWest" Then
    ShipY = ShipY + 7
    ShipX = ShipX - 7
    ElseIf picShip.Tag = "West" Then
    ShipX = ShipX - 7
    ElseIf picShip.Tag = "NorthWest" Then
    ShipX = ShipX - 7
    ShipY = ShipY - 7
    End If
    'Check for down key
    ElseIf e.KeyCode = Keys.Down Then
    If picShip.Tag = "North" Then
    ShipY = ShipY + 3
    ElseIf picShip.Tag = "NorthEast" Then
    ShipY = ShipY + 3
    ShipX = ShipX - 3
    ElseIf picShip.Tag = "East" Then
    ShipX = ShipX - 3
    ElseIf picShip.Tag = "SouthEast" Then
    ShipX = ShipX - 3
    ShipY = ShipY - 3
    ElseIf picShip.Tag = "South" Then
    ShipY = ShipY - 3
    ElseIf picShip.Tag = "SouthWest" Then
    ShipY = ShipY - 3
    ShipX = ShipX + 3
    ElseIf picShip.Tag = "West" Then
    ShipX = ShipX + 3
    ElseIf picShip.Tag = "NorthWest" Then
    ShipX = ShipX + 3
    ShipY = ShipY + 3
    End If
    'Changes directions
    ElseIf e.KeyCode = Keys.E Then
    picShip.Image = WindowsApplication1.My.Resources.Resources.ShipNorthEast
    picShip.Tag = "NorthEast"
    ElseIf e.KeyCode = Keys.D Then
    picShip.Image = WindowsApplication1.My.Resources.Resources.ShipEast
    picShip.Tag = "East"
    ElseIf e.KeyCode = Keys.C Then
    picShip.Image = WindowsApplication1.My.Resources.Resources.ShipSouthEast_copy
    picShip.Tag = "SouthEast"
    ElseIf e.KeyCode = Keys.X Then
    picShip.Image = WindowsApplication1.My.Resources.Resources.ShipSouth
    picShip.Tag = "South"
    ElseIf e.KeyCode = Keys.Z Then
    picShip.Image = WindowsApplication1.My.Resources.Resources.ShipSouthWest
    picShip.Tag = "SouthWest"
    ElseIf e.KeyCode = Keys.A Then
    picShip.Image = WindowsApplication1.My.Resources.Resources.ShipWest
    picShip.Tag = "West"
    ElseIf e.KeyCode = Keys.Q Then
    picShip.Image = WindowsApplication1.My.Resources.Resources.ShipNorthWest
    picShip.Tag = "NorthWest"
    ElseIf e.KeyCode = Keys.W Then
    picShip.Image = WindowsApplication1.My.Resources.Resources.ShipNorth
    picShip.Tag = "North"
    'Shoot laser. 1st check direction of ship, then shoot laser
    ElseIf e.KeyCode = Keys.Space Then
    If picShip.Tag = "North" Or picShip.Tag = "South" Then
    picLaser.Image = WindowsApplication1.My.Resources.Resources.laserNorth
    ElseIf picShip.Tag = "NorthEast" Or picShip.Tag = "SouthWest" Then
    picLaser.Image = WindowsApplication1.My.Resources.Resources.laserNorthEast
    ElseIf picShip.Tag = "East" Or picShip.Tag = "West" Then
    picLaser.Image = WindowsApplication1.My.Resources.Resources.laserEast
    ElseIf picShip.Tag = "NorthWest" Or picShip.Tag = "SouthEast" Then
    picLaser.Image = WindowsApplication1.My.Resources.Resources.laserSouthEast
    End If

    End If

    'position ship
    MyGraphics.DrawImage(picShip.Image, ShipX, ShipY, ShipSize, ShipSize)
    End Sub


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim X As Integer
    Dim I As Integer
    Dim Y As Integer
    'Asteroids spread across the panel with 20 pixels borders
    AsteroidSize = Int((pnlAsteroids.Width - 6 * 20) / 5)
    X = 10
    For I = 1 To 10
    AsteroidX(I) = X
    X = X + AsteroidSize + 20
    Next
    Y = 10
    For I = 1 To 10
    AsteroidY(I) = Y
    Y = Y + AsteroidSize + 20
    Next
    ShipSize = Int(AsteroidSize / 2)
    MyGraphics = pnlAsteroids.CreateGraphics
    BlankBrush = New SolidBrush(pnlAsteroids.BackColor)
    'Give form focus
    Me.Focus()
    End Sub

    End Class
    Last edited by Paszt; 05-16-2012 at 3:40 PM. Reason: Added xcode blocks: [xcode=vb]CODE HERE[/xcode] or use the vb button in the editor's toolbar.

  2. #2
    Spooferproof is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    May 2012
    Posts
    2
    Reputation
    0
    *Bump* Could i get some help plz? ive been working a lot on the project and could edit the quote with new code. I have finished a working system of asteroids that fly around the screen from all directions. Still need help with the lasers shooting

  3. #3
    Paszt's Avatar
    Paszt is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Raleigh, NC - USA
    Posts
    1,502
    Reputation
    308
    I don't mean to sound rude, but there are many things wrong with your code and I would advise starting over. For one, you never want to cache the graphics object for reuse (as you do with the myGraphics variable in the Form1_Load procedure). From the documentation for the Control.CreateGraphics Method (System.Windows.Forms):

    The Graphics object that you retrieve through the CreateGraphics method should not normally be retained after the current Windows message has been processed, because anything painted with that object will be erased with the next WM_PAINT message. Therefore you cannot cache the Graphics object for reuse, except to use non-visual methods like Graphics.MeasureString. Instead, you must call CreateGraphics every time that you want to use the Graphics object, and then call Dispose when you are finished using it.
    Here is a good tutorial on building a pong game from coding4fun: Upgrade Your Game: Tiny Tennis (Visual Basic) | Coding4Fun Articles | Channel 9


    If you want to search for other tutorials, use the search terms: vb.net sprite game tutorial

    And please, no Crossposting. I have deleted your other thread.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking