5 Replies - 1309 Views - Last Post: 26 November 2013 - 07:15 AM Rate Topic: -----

#1 Karusegawa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-November 13

did a space invaders game and the laser shoots but doesn't move

Posted 25 November 2013 - 06:25 PM

Just what you read folks. I am currently doing a Space Invaders game in Visual Studio for my Programming Class
and so far:
  • The user's sprite moves (with the Arrow Keys)
  • Shoot a laser with the Space Key.


But the problem is that once you shoot, you don't see the bullet move. You only see it a second later on the top of the window.

Hope someone can help, here's my code so you can grasp a little better my problem:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        bool Fuego = false;
        int lasTimer = 8;
        int lasTirado;

        public Form1()
        {
            InitializeComponent();
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //Here is where I draw the laser and its movement
        public void laserFire()
        {
            if (balaTimer.Enabled == true)
            {
                
                PictureBox laser = new PictureBox();
                laser.BackColor = Color.White;
                laser.Width = 5;
                laser.Height = 10;
                this.Controls.Add(laser);
                lasTirado = pbNave.Location.X;
                laser.Location = new Point(pbNave.Location.X, lasTirado);
                
            }
        }
        //This are the controls
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Left)
            {
               pbNave.Left -= 10;
            }
            

            else if (e.KeyCode == Keys.Right)
            {
                pbNave.Left += 10;
            }
            
            else if (e.KeyCode == Keys.Space)
            {
                Fuego = true;

                if (Fuego == true)
                {
                    laserFire();
                    balaTimer.Enabled = true;
                }
            }
        }
            //And this is where I "move" the laser
            private void balaTimer_Tick(object sender, EventArgs e)
            {
                lasTirado = lasTirado - 10;

                if (lasTimer == 0)
                {
                    balaTimer.Enabled = false;
                }
                else 
                {
                    lasTimer = lasTimer - 1; 
                }
            }
        }
    }




Thank you for your time and patience. :D/>

Is This A Good Question/Topic? 0
  • +

Replies To: did a space invaders game and the laser shoots but doesn't move

#2 Rhino1111  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 259
  • Joined: 28-August 13

Re: did a space invaders game and the laser shoots but doesn't move

Posted 25 November 2013 - 06:41 PM

I just want to start off by saying this is structured horribly, imo. You should of have a GameLoop() method you call at the end of form_load event, and in GameLoop() call Update() and Draw() methods, where you handle the logic for drawing/updating anything game related(preferably calling .Update and .Draw on custom classes and having the actual logic inside there).

as for your problem, in the timer tick your not moving your picturebox at all. When you fire a laser you set the 'lasTirado' variable to the ships x location. You set the bullets location to your ships X and X axis, when the last argument in the Point(int, int) should be the ships Y position.

When you decrement "lasTirado" in your timer tick method, you're just changing the variables value, not the picturebox's location(representing the laser). You've added that to the form's control collection. A problem arises if you attempted to loop through this to find your bullets, because your ship is also a picturebox and you can't distinguish if it's a laser or ship. You're better off holding your lasers in a custom collection, such as a List<Picturebox>(). You can then access your lasers easily and change its X location to simulate movement.
Was This Post Helpful? 1
  • +
  • -

#3 Karusegawa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-November 13

Re: did a space invaders game and the laser shoots but doesn't move

Posted 25 November 2013 - 07:43 PM

View PostRhino1111, on 25 November 2013 - 06:41 PM, said:

I just want to start off by saying this is structured horribly, imo.


Thank you for your honesty on that one. :D
I know it's terribly structured, and your advise was reaaaaaally helpful.
Also, I think I should let you know I just started to learn C# (just need to let that out of my system).
Once again, thank you very much :D
Was This Post Helpful? 0
  • +
  • -

#4 Rhino1111  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 259
  • Joined: 28-August 13

Re: did a space invaders game and the laser shoots but doesn't move

Posted 25 November 2013 - 08:54 PM

I'm glad you took it well and didn't get discouraged, as I didn't mean it in a bad way at all. I'm critical like that with myself too when I look at my old code :)/> . It's not entirely your fault though, the way the Windows Forms design is set up, as a beginner it seems logical to plug everything into the Form class, with all those event hookups and what not. Plus without studying OOP concepts a little bit, knowing how to structure things so it's well designed isn't really clear at all(still learning about good design principles myself).

The windows forms apps I wrote a month or so ago are an atrocity, I didn't actually start applying OOP (Object-Oriented Programming) Principles to my coding until I started working with XNA, which I highly suggest you look into (It's a managed DirectX wrapper that makes developing games with C# much easier and more efficient than your standard GDI+ in Windows Forms.)

Also, glad I could help :)/>. I'm still relatively new to programming myself :P .

This post has been edited by Rhino1111: 25 November 2013 - 08:54 PM

Was This Post Helpful? 0
  • +
  • -

#5 Karusegawa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-November 13

Re: did a space invaders game and the laser shoots but doesn't move

Posted 25 November 2013 - 10:52 PM

I'm going to learn how to apply OOPs (lol?) next semester, but up until now I'm stuck with this P:
I was planning to look into XNA, but the professor specified that he wanted us to experience the (no joke) distasteful wrath of using Windows Forms.
Was This Post Helpful? 0
  • +
  • -

#6 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: did a space invaders game and the laser shoots but doesn't move

Posted 26 November 2013 - 07:15 AM

If your going into game programming, I'd agree with your professor. You can still peek at XNA. It uses Update() Draw() loops like was suggested, but handles those calls under the hood. It can give you an idea of how to implement them yourself.

Studying OOP next semester doesn't prevent you from learning some now on your free time.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1