6 Replies - 485 Views - Last Post: 02 July 2020 - 11:30 AM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 869
  • Joined: 15-December 14

Shooting game questions

Posted 29 June 2020 - 10:51 PM

I learn better when its a fun project, so I decided to try and follow a game tutorial on youtube. The guy giving the tutorial can't be heard so I dissected what each line does and tried to look up online things I wasn't sure what they meant or did. I am stuck and I would like to ask for help with some questions.

These are my questions.
1. I read on different websites explain of what is the function 'this' and I still don't get it. Would someone please explain it to me as basically as possible? I just can't seem to get it to sink in.

2. I understand what picturebox is and I read the explanation of why you use it when you're drawing something. Is it better to use PictureBox bullet = new PictureBox(); instead of paint when you want to draw something in C#?

3. I understand what a foreach loop is, but I cannot find anywhere what the function Control does in C#

4. In the function move enemy he has a variable called PictureBox enemyObj. What is that, what does it do and can someone give me an example of how its used?

5. The video included has the guy explaining the tutorial has the player shooting very slowly and I wanted to see if I could make the player rapid fire like in R type. I was able to get it to do that but I can't figure out how to make them disappear after the enemy is shot.

6. I have images I used for the game which have a different coloured back ground. When I click on the image and choose transparent it still has the original back ground. How do I change the back ground of an image I used with picturebox? That way I can change the colour to whatever colour and the background of the image I used will also change to the back ground of forum1.

This is the link to the tutorial

https://www.youtube....8ggEv4w&t=1757s

This is the game I am working on along with the modifications I've done so far.



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 Macross_mod1
{

    public partial class Form1 : Form
    {
        bool shot = false;// this will check if the player has shot any rounds left
        bool goup;         
        bool godown;       
        bool goleft;
        bool goright;
        int playerSpeed = 7; // this interger will control how fast the player moves  
        int playerhp = 10; //hp of player
        int damage = 1;//damage player takes when shot
        int heal = 3;//player heals when touches special box
        int enemyshoot = 1;//player shoots
        public Form1()
        {
            InitializeComponent();
        }
        private void makeBullet()
        {
            PictureBox bullet = new PictureBox();
            bullet.BackColor = System.Drawing.Color.Yellow;
            bullet.Height = 5;
            bullet.Width = 10;
            bullet.Left = Player.Left + Player.Width;
            bullet.Top = Player.Top + Player.Height / 2;
            bullet.Tag = "bullet";
            this.Controls.Add(bullet);
            

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            over.Visible = false;
            restart.Visible = false;
            enemy1.Left = 1000;
            enemy2.Left = 3000;
            enemy3.Left = 4000;
            enemy4.Left = 3000;
            enemy5.Left = 2000;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                goup = true;
            }
            if (e.KeyCode == Keys.Down)
            {
                godown = true;
            }
            if (e.KeyCode == Keys.Left)
            {                
                goleft = true;
            }
            if (e.KeyCode == Keys.Right)
            {              
                goright = true;
            }
            if (e.KeyCode == Keys.Space && shot == false)
            {
                System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"C:\users\albert\Documents\blaster.wav");//blaster
                player.Play();
                makeBullet();
                shot = true;
            }
        }
        private void gametimer_Tick(object sender, EventArgs e)
        {
            if (goup)
            {
                if (Player.Top > 20)
                    Player.Top -= playerSpeed;
            }
            if (godown)
            {
                if (Player.Top < 512)
                    Player.Top += playerSpeed;
            }
            if (goleft)
            {
                if (Player.Left > 10)
                    Player.Left -= playerSpeed;
            }
            if (goright)
            {
                if (Player.Left < 700)
                    Player.Left += playerSpeed;
            }
            foreach (Control X in this.Controls)
            {
                // if X is a picture box object AND it has a tag of bullet                 
                // then we will follow the instructions within                 
                if (X is PictureBox && X.Tag == "bullet")
                {                    // move x towards the right of the screen                    
                    X.Left += 15;
                    // if x has left the screen towards the right                     
                    // x's location is greater than 900 pixels from the screen                     
                    if (X.Left > 900)
                    {
                        // then remove x from display                         
                        this.Controls.Remove(X);
                        // dispose the x from the application                         
                        // we use the dispose method so it doesn't leak memory later on                         
                        X.Dispose();
                    }
                }
                //
                if (X.Bounds.IntersectsWith(enemy1.Bounds))
                {                      
                    this.Controls.Remove(X);
                    // dispose the bullet from the program                         
                    X.Dispose();
                    
                }

                //
                // below we will check if X collides with the UFO object       +
                /*
                if (X.Bounds.IntersectsWith(enemy1.Bounds) || (X.Bounds.IntersectsWith(enemy2.Bounds) || (X.Bounds.IntersectsWith(enemy3.Bounds) || (X.Bounds.IntersectsWith(enemy4.Bounds) || (X.Bounds.IntersectsWith(enemy5.Bounds))
                {                    
                    this.Controls.Remove(X);
                    // dispose the bullet from the program                         
                    X.Dispose();
                }

                //
                */
            }
        }
        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {              
                goup = false;
            }
            if (e.KeyCode == Keys.Down)
            {                
                godown = false;
            }
            if (e.KeyCode == Keys.Left)
            {              
                goleft = false;
            }
            if (e.KeyCode == Keys.Right)
            {                
                goright = false;
            }
            if (shot == true)
            {               
                shot = false;
            }
        }

        private void enemytimer_Tick(object sender, EventArgs e)
        {
            Moveenemy(enemy1);
            Moveenemy(enemy2);
            Moveenemy(enemy3);
            Moveenemy(enemy4);
            Moveenemy(enemy5);
        }
        Random rnd = new Random();
        void Moveenemy(PictureBox enemyObj)
        {
            if (enemyObj.Left >= 0)
                enemyObj.Left += -10;

            else
            {
                enemyObj.Location = new Point(1000, rnd.Next(100, 400));
            }


        }
    }
}


This post has been edited by albert003: 29 June 2020 - 10:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Shooting game questions

#2 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Shooting game questions

Posted 29 June 2020 - 11:29 PM

Quote

I learn better when its a fun project

I disagree

Quote

1. I read on different websites explain of what is the function 'this' and I still don't get it.

3. I understand what a foreach loop is, but I cannot find anywhere what the function Control does in C#

You are asking fairly basic/fundamental questions which state that you have not spent enough time studying before embarking on projects.

(Yes, ONCE the fundamentals are covered a fun project would be good.)

[I also wouldn't want to, personally, follow a course if I couldn't hear the tutor, unless there were clear subtitles.]
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15792
  • View blog
  • Posts: 63,279
  • Joined: 12-June 08

Re: Shooting game questions

Posted 30 June 2020 - 06:50 AM

#1 - check the docs. It's used to make sure the compiler knows to use the 'current instance'. Most commonly used when differentiating between parameters and variables that both have the same name.

    class foo
    {
        private int lval = 0;
        public foo(int lval)
        {
            this.lval = lval;
        }
    }


https://docs.microso...e/keywords/this
https://www.tutorial...eyword-in-Chash

#2 - Picturebox is a GUI object. It has a lot of trappings and extra bits that are not suited well for "games". I tend to have an object that defines a rectangle, image location, and other "game things", and then use the 'paint' event to make this hit the screen. Pretty sure I covered that in an old VB.NET "game" tutorial here.

#3 - Not sure what you are referring to. Control as in the winforms class?
https://docs.microso...iew=netcore-3.1

#4 - Presumably that is the picturebox object of some 'enemy' on the screen'. Most likely looking to handle collision or something else.

#5 - I do not know what you are asking there or what you want to disappear.

#6 - transparency is weird when trying bash around winform GUI controls. You need to make sure your image is one that handles transparency. That's typically PNG or TIF exclusively.

Background changing would be as simple as having a secondary image with what ever color, swap it out when needed, and revert back when not.
Was This Post Helpful? 0
  • +
  • -

#4 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 869
  • Joined: 15-December 14

Re: Shooting game questions

Posted 01 July 2020 - 07:10 AM

andrewsw You are absolutely right, I haven't. But to be honest I got to chapter 5 or 6 in a c++ book, then switched to C# and looked up what I learned in C++ to its equivalent in C#. I thought with that knowledge I would be able to follow a tutorial to help me learn more. Its like when I was learning python, I would learn a few chapters and then try to make a program with the things I have learned.



modi123_1 Would you please give me the link to the vb game tutorial? I couldn't find it. Basically what I am trying to do is when the player shoots an enemy it disappears.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15792
  • View blog
  • Posts: 63,279
  • Joined: 12-June 08

Re: Shooting game questions

Posted 01 July 2020 - 07:11 AM

https://www.dreaminc...-basics-part-1/
Was This Post Helpful? 0
  • +
  • -

#6 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1032
  • View blog
  • Posts: 3,962
  • Joined: 13-June 14

Re: Shooting game questions

Posted 02 July 2020 - 09:01 AM

View Postalbert003, on 30 June 2020 - 06:51 AM, said:

3. I understand what a foreach loop is, but I cannot find anywhere what the function Control does in C#


I don't know why you'd assume Control is a function. It looks more like a type (a class or whatever else you have in C#) to me.
Was This Post Helpful? 0
  • +
  • -

#7 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 869
  • Joined: 15-December 14

Re: Shooting game questions

Posted 02 July 2020 - 11:30 AM

modi123_1
I will go through your tutorial and hopefully be a much better programmer.


ndc85430
Thats the way the guy who made the tutorial demonstrated how to make his game.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1