12 Replies - 949 Views - Last Post: 17 June 2021 - 09:05 PM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 980
  • Joined: 15-December 14

Help with modi123_1 game tutorial

Posted 25 May 2021 - 07:37 PM

I'm doing my best doing, following and creating modi123_1 game tutorial unsuccessfully. I am converting it from VB to C# using an online website.

I have so many questions and I hope you guys can help me. I usually just use key down and key up to handle the movement of the player in a game on form1. I understand why you should use a class instead of having everything bundled up together in form1. So I am trying to do it his way as I am inexperienced and he isn't. Ill show the whole code below in case you need to see the whole thing.

This is the original tutorial
https://www.dreaminc...-basics-part-1/

In the Entity class visual studios has a lot of things with a red underline. Ill go line by line and show what the error message says to hopefully get some help.

This is the line of code
private System.Drawing.Point pointLocation = null/* TODO Change to default(_) if this is not a reference type */; // -- 
where on the map is the user.
This is the error message.
cs0037 cannot convert null to a point because its a non nullable value point

This is the next error I am getting
    public string Print()
    {
        return string.Format("{0}: x={1}, y={2}", _sID, _pointLocation.X, _pointLocation.Y);
    }

Next to print I am getting this error message
cs0116 A namespace cannot directly contain members such as fields or methods.

_sID
CS0103 The name _sID, _pointLocation, _pointLocationdoes not exist in the current context

public abstract bool ProcessState();
A namespace cannot directly contain members such as fields or methods.

This is what I have in that class for the game.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;

namespace MODIGAME
{
    class Entity
    {
        public enum MovementDirection
        {
            NONE = 0,
            UP = 1,
            RIGHT = 2,
            DOWN = 3,
            LEFT = 4
        }
        protected System.Drawing.Bitmap _bitmapImage = null/* TODO Change to default(_) if this is not a reference type */; // -- the image to be drawn.
        private System.Drawing.Point pointLocation = null/* TODO Change to default(_) if this is not a reference type */; // -- where on the map is the user.
        protected string _sID = string.Empty; // -- ids are usually important
        protected Int32 _lMovementRate = 0; // -- how fast the location changes when a move command is issued.
        protected Int32 _lScreenWidth = 0; // -- used to determine the boundaries so the location is still visible.
        public Int32 _lScreenHeight { get; set; } = 0; // -- used to determine the boundaries so the location is still visible.

        public Int32 ScreenHeight
        {
            set
            {
                _lScreenHeight = value;
            }
        }

        public Int32 ScreenWidth
        {
            set
            {
                _lScreenWidth = value;
            }
        }

        protected Point PointLocation { get => pointLocation; set => pointLocation = value; }
    }
    //
    public string Print()
    {
        return string.Format("{0}: x={1}, y={2}", _sID, _pointLocation.X, _pointLocation.Y);
    }

    // -- Everything must be drawn!
    public abstract void Paint(Graphics e);

    // -- Everything must be provided a consistant entry point to process that state for that given time slice.
    public abstract bool ProcessState();
}


Is This A Good Question/Topic? 0
  • +

Replies To: Help with modi123_1 game tutorial

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 16302
  • View blog
  • Posts: 64,852
  • Joined: 12-June 08

Re: Help with modi123_1 game tutorial

Posted 25 May 2021 - 09:03 PM

Look at the code. It should be immediately clear what you need to do.

1. Add semi colons to lines 21 and 22.
2. If 'Point' cannot be defaulted to null, then remove the assignment to null.
3. Look where your namespace scope ends. Notice you have methods outside of the ending }. Move that ending curly brace to include those.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 16302
  • View blog
  • Posts: 64,852
  • Joined: 12-June 08

Re: Help with modi123_1 game tutorial

Posted 25 May 2021 - 10:12 PM

The tutorial has the github repo link for the VB.NET and C# translation of that project.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6951
  • View blog
  • Posts: 28,689
  • Joined: 12-December 12

Re: Help with modi123_1 game tutorial

Posted 26 May 2021 - 12:39 AM

Your print method is outside of the class definition. It is a member of a class and cannot appear directly within a namespace.
Was This Post Helpful? 0
  • +
  • -

#5 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 980
  • Joined: 15-December 14

Re: Help with modi123_1 game tutorial

Posted 31 May 2021 - 10:47 PM

modi123
Yep I saw what I did wrong and I also found the translation into C#

andrewsw
Yeah I realised that mistake and corrected it.

Ok, so I made some progress and wanted to make the player shoot their weapon at the enemy. I made a progressbar and set it to zero and in the unser class I gave the player 10 hp. To make it work in form1 I had to change the health from private to public.
public Int32 _lHealth = 10;// Player hp


I have a question for you, do I create two functions to handle the combat> 1 to handle when the player is hit, loses health and dies. Then a second when the enemy is hit, loses health and dies or put them both together in one function?

This is what I have 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;
using System.Diagnostics;

namespace MODI1GAME
{
    public partial class Form1 : Form
    {
        private bool _IsDebug = false;// '-- turn on if you want to see information in the debug window about the object's state.
        private Timer _timer = null;//'-- the 'game engine'.
        private User _oUser;//'-- our user class
        private Enemy _oEnemy;//'-- An enemy class that follows a way point that is hard coded.

        public Form1()
        {
            InitializeComponent();
            //'-- Setup the display to not flicker and procss the graphics smoothly.
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            Height = 500;
            Width = 500;

            //'-- create our user to be drawn.
            _oUser = new User();
            _oUser.ScreenHeight = Height;// '-- to prevent moving off the screen!
            _oUser.ScreenWidth = Width;//'-- to prevent moving off the screen!
            if (_IsDebug) Debug.WriteLine(_oUser.Print());

            //'-- create our user to be drawn.
            _oEnemy = new Enemy();
            _oEnemy.ScreenHeight = Height;// '-- to prevent moving off the screen!
            _oEnemy.ScreenWidth = Width;//'-- to prevent moving off the screen!
            if (_IsDebug) Debug.WriteLine(_oEnemy.Print());
            /*
            //'-- setup our timer as the engine to process.
            _timer = new Timer();
            _timer.Interval = 100;
            _timer.Start();
            _timer.Tick += _timer_Tick;
            */
            playerlife.visible = true;//player progress bar
            enemylife.visible = true;//enemy progress bar
        }

        //'-- the "game engine".
        private void _timer_Tick(object sender, System.EventArgs e)
        {
            //'-- When the engine ticks proces the input into something our user class can do.
            _oUser.ProcessState();
            if (_IsDebug) Debug.WriteLine(_oUser.Print());

            _oEnemy.ProcessState();
            if (_IsDebug) Debug.WriteLine(_oEnemy.Print());

            //'-- Redraw the scene
            Refresh();
        }

        //    '-- The take the form's input and set that action to our user object's next action on deck.
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // '-- To avoid the issue of having a stack of key presses if the user holds the key down only accept one 
            //'-- key stroke until it is processed and the graphics updated.
            if (_oUser.NextAction == Entity.MovementDirection.NONE)
            {
                switch (e.KeyCode)
                {
                    case System.Windows.Forms.Keys.Up:
                    case System.Windows.Forms.Keys.W:
                        _oUser.NextAction = Entity.MovementDirection.UP;
                        break;
                    case System.Windows.Forms.Keys.Down:
                    case System.Windows.Forms.Keys.S:
                        _oUser.NextAction = Entity.MovementDirection.DOWN;
                        break;

                    case System.Windows.Forms.Keys.Right:
                    case System.Windows.Forms.Keys.D:
                        _oUser.NextAction = Entity.MovementDirection.RIGHT;
                        break;

                    case System.Windows.Forms.Keys.Left:
                    case System.Windows.Forms.Keys.A:
                        _oUser.NextAction = Entity.MovementDirection.LEFT;
                        break;

                }
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            _oEnemy.Paint(e.Graphics);

            //'-- Pain the changes to the screen.
            _oUser.Paint(e.Graphics);
        }

        void Combat()
        {
            foreach (Control j in this.Controls)
            {
                //shot is what I called the enemy picturebox ammo and so is missile
                if (j is PictureBox && (j.Tag == "shot" || j.Tag == "missile"))
                {
                    foreach (Control i in Controls)
                    {
                        if (i is PictureBox && i.Tag == "player")//player is what I called the tag blue triangle
                        {
                            if (j.Bounds.IntersectsWith(i.Bounds))
                            {
                                if (_oUser._lHealth == 0)
                                {
                                    PictureBox bullet = new PictureBox();
                                    bullet.SizeMode = PictureBoxSizeMode.AutoSize;
                                    bullet.Image = Properties.Resources.Shoot;
                                    bullet.BackColor = System.Drawing.Color.Transparent;
                                    ((PictureBox)j).Image = Properties.Resources.explosion;
                                }
                                else
                                    _oUser._lHealth -= 1;
                            }
                        }
                    }
                }
            }
        }
    }
}



Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 16302
  • View blog
  • Posts: 64,852
  • Joined: 12-June 08

Re: Help with modi123_1 game tutorial

Posted 01 June 2021 - 07:05 AM

Sounds like a common function that should be in the base parent class as well as a 'health' variable.

Though it may do well (depending on where you are going with this all) to have an intermediary abstract class that holds that, which inherits from entity, and then the player/enemy inherit from that intermediary.
Was This Post Helpful? 0
  • +
  • -

#7 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 980
  • Joined: 15-December 14

Re: Help with modi123_1 game tutorial

Posted 15 June 2021 - 11:03 PM

Ok, Ive checked line by line of the tutorial and have deleted and recreated the program and just to be sure copied everything directly from the examples. The game runs without any errors but I still can't get the red triangle or the blue square to appear. I put both in Resources.resx and they should both appear on the screen, right? Only other option is to put 2 picturebox on the screen and name one red_triangle but that beats the whole purpose of using bitmap. Could someone please tell me what I am doing wrong? I am truly out of ideas.

I'm also getting a message box saying parameter is not valid.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 16302
  • View blog
  • Posts: 64,852
  • Joined: 12-June 08

Re: Help with modi123_1 game tutorial

Posted 16 June 2021 - 06:45 AM

Did you download the project from github. I mean actually download it and not just copy paste?
Was This Post Helpful? 0
  • +
  • -

#9 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 980
  • Joined: 15-December 14

Re: Help with modi123_1 game tutorial

Posted 16 June 2021 - 08:40 AM

Yes, I went to your github openeed the file for c sharp and copied pasted everything on each form to my visual studio. Originally I just rewrote what you had there.
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 16302
  • View blog
  • Posts: 64,852
  • Joined: 12-June 08

Re: Help with modi123_1 game tutorial

Posted 16 June 2021 - 08:44 AM

Download the whole zip and run it, and report back what's up.
Was This Post Helpful? 0
  • +
  • -

#11 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 980
  • Joined: 15-December 14

Re: Help with modi123_1 game tutorial

Posted 17 June 2021 - 10:38 AM

Ok So, I did what you told me to do and the program now works. I have a few new questions for you.

1. Why did it not work when I copied what you had in your Github but it worked when I downloaded the file?
2. When I am ready to make my own game how do I avoid similar issues with the sprites not appearing? Do I need to download your file and change things to what Ill need in my game?
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 16302
  • View blog
  • Posts: 64,852
  • Joined: 12-June 08

Re: Help with modi123_1 game tutorial

Posted 17 June 2021 - 11:06 AM

I have no idea what you copied or not so it could be a myriad of things.

My ballpark guess is you didn't include them images in the project, and set them to copy to the bin folder, but honestly I don't know what you have done.
Was This Post Helpful? 0
  • +
  • -

#13 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 980
  • Joined: 15-December 14

Re: Help with modi123_1 game tutorial

Posted 17 June 2021 - 09:05 PM

Youre right I missed a few steps when I went through the tutorial. Thank you for your help, I really do appreciate it.

I have a few new questions for you. I wanted the square to shoot a laser or projectile at red triangle. So my question is when I go to Form1_KeyDown and go to the switch statement, this is where I am not sure what to do. Am I on the right track?
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // '-- To avoid the issue of having a stack of key presses if the user holds the key down only accept one 
            //'-- key stroke until it is processed and the graphics updated.
            if (_oUser.NextAction == Entity.MovementDirection.NONE)
            {
                switch (e.KeyCode)
                {
                    case System.Windows.Forms.Keys.Up:
                    case System.Windows.Forms.Keys.W:
                        _oUser.NextAction = Entity.MovementDirection.UP;
                        break;
                    case System.Windows.Forms.Keys.Down:
                    case System.Windows.Forms.Keys.S:
                        _oUser.NextAction = Entity.MovementDirection.DOWN;
                        break;

                    case System.Windows.Forms.Keys.Right:
                    case System.Windows.Forms.Keys.D:
                        _oUser.NextAction = Entity.MovementDirection.RIGHT;
                        break;

                    case System.Windows.Forms.Keys.Left:
                    case System.Windows.Forms.Keys.A:
                        _oUser.NextAction = Entity.MovementDirection.LEFT;
                        break;
                    case System.Windows.Forms.Keys.A:
                        _oUser.NextAction = Entity. //Not sure what to put here
                }
            }
        }


I have 3 options and none of them seem right. I have movementdirection, reference equals and equals. Logically, I know its not moving the square so the first one is out. So its one of the last two, but I couldn't find any references using my googlfu to help me figure out what to put there.

My plan is after I get the player to shoot a laser, I was going to add a sound effect and make it pause after each shot. So the sound doesn't overlap when the player shoots multiple times.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1