10 Replies - 927 Views - Last Post: 09 July 2014 - 07:07 AM Rate Topic: -----

#1 Matejd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-July 14

C# Platform Game Help

Posted 09 July 2014 - 06:24 AM

Hello!
I am doing a simple C# platform game and i have a little problem.
When i jump on the first platform it is great, but when i jump on the second platform it gets laggy.
Please download the .txt file in Attachments and see my mistake. Tell me if i am doing nonsense stuff because i am a beginner.

Thanks,
Matej

Attached File(s)


Is This A Good Question/Topic? 0
  • +

Replies To: C# Platform Game Help

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: C# Platform Game Help

Posted 09 July 2014 - 06:28 AM

Please post the code. It will make it easier for people who want to help to be able to access your code. Remember with today's mobile world, some people are viewing this site using phones and tablets. It is easier to browse a page and have the code right there, than it is to have to go through the extra steps of doing a download and opening the file.
Was This Post Helpful? 1
  • +
  • -

#3 Matejd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-July 14

Re: C# Platform Game Help

Posted 09 July 2014 - 06:30 AM

View PostMatejd, on 09 July 2014 - 06:24 AM, said:

Hello!
I am doing a simple C# platform game and i have a little problem.
When i jump on the first platform it is great, but when i jump on the second platform it gets laggy.
Please download the .txt file in Attachments and see my mistake. Tell me if i am doing nonsense stuff because i am a beginner.

Thanks,
Matej


Here is the code:
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 My_Game
{
    public partial class Form1 : Form
    {

        bool right;
        bool left;
        bool jump;
        int G = 30;
        int Force;
        int index = 0;

        public Form1()
        {
            InitializeComponent();

          
            player.Top = screen.Height - player.Height;
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
     
            
            if (e.KeyCode == Keys.Right) 
            { 
                right = true;
            }
           
            if (e.KeyCode == Keys.Left) 
            { 
                left = true;
            }
           
            if (e.KeyCode == Keys.Escape)
            { 
                this.Close(); 
            }

            
            if(jump != true)
            {

                if(e.KeyCode == Keys.Space)
                {
                    jump = true;
                    Force = G;
                    player.Image = Image.FromFile("jump.png");
                }

            }

        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            index++;

            //Gif Replay
            if (right == true && index % 15 == 0)
            {
                player.Image = Image.FromFile("walk_r.gif");
            }

            if (left == true && index % 15 == 0)
            {
                player.Image = Image.FromFile("walk_l.gif");
            }


            //Side Collision
            if (player.Right > block.Left && player.Left < block.Right - player.Width && player.Bottom < block.Bottom && player.Bottom > block.Top)
            {

                right = false;

            }

            if (player.Left < block.Right && player.Right > block.Left + player.Width && player.Bottom < block.Bottom && player.Bottom > block.Top)
            {

                left = false;

            }

            if (right == true) { player.Left += 3; }
            if (left == true) { player.Left -= 3; }

            if (jump == true)
            {
                player.Top -= Force;
                Force -= 1;

            }

            if (player.Top + player.Height >= screen.Height)
            {
                player.Top = screen.Height - player.Height;
                if (jump == true)
                {
                    player.Image = Image.FromFile("stand.png");


                }
                jump = false;


            }
            else
            {
                player.Top += 5;


            }
            //Side Collision 2
            if (player.Right > block2.Left && player.Left < block2.Right - player.Width && player.Bottom < block2.Bottom && player.Bottom > block2.Top)
            {

                right = false;

            }

            if (player.Left < block2.Right && player.Right > block2.Left + player.Width && player.Bottom < block2.Bottom && player.Bottom > block2.Top)
            {

                left = false;

            }

            if (right == true) { player.Left += 3; }
            if (left == true) { player.Left -= 3; }

            if (jump == true)
            {
                player.Top -= Force;
                Force -= 1;

            }

            if (player.Top + player.Height >= screen.Height)
            {
                player.Top = screen.Height - player.Height;
                if (jump == true)
                {
                    player.Image = Image.FromFile("stand.png");


                }
                jump = false;


            }
            else
            {
                player.Top += 5;


                //Top Collision
                if (player.Left + player.Width > block.Left && player.Left + player.Width < block.Left + block.Width + player.Width && player.Top + player.Height >= block.Top && player.Top < block.Top)
                {
                    jump = false;
                    Force = 0;
                    player.Top = block.Location.Y - player.Height;
                }
                //Top Collision2
                if (player.Left + player.Width > block2.Left && player.Left + player.Width < block2.Left + block2.Width + player.Width && player.Top + player.Height >= block2.Top && player.Top < block2.Top)
                {
                    jump = false;
                    Force = 0;
                    player.Top = block2.Location.Y - player.Height;
                }


                //Head Colision
                if (player.Left + player.Width > block.Left && player.Left + player.Width < block.Left + block.Width + player.Width && player.Top - block.Bottom <= 10 && player.Top - block.Top > -10)
                {
                    Force = -1;
                    jump = false;

                }
                //Head Colision 2
                if (player.Left + player.Width > block2.Left && player.Left + player.Width < block2.Left + block2.Width + player.Width && player.Top - block2.Bottom <= 10 && player.Top - block2.Top > -10)
                {
                    Force = -1;
                    jump = false;

                }

            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Right) 
            {
                right = false; 
                player.Image = Image.FromFile("stand.png"); 
            }
            
            if (e.KeyCode == Keys.Left)
            { 
                left = false;
                player.Image = Image.FromFile("stand.png");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Bounds = Screen.PrimaryScreen.Bounds;
        }

     
    }
}

Attached File(s)


This post has been edited by Skydiver: 09 July 2014 - 06:35 AM
Reason for edit:: Put code in code tags. Learn to do this yourself.

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: C# Platform Game Help

Posted 09 July 2014 - 06:38 AM

Remember that every time you call Image.FromFile() you are loading up a brand new instance and eating up resources.
Was This Post Helpful? 3
  • +
  • -

#5 Matejd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-July 14

Re: C# Platform Game Help

Posted 09 July 2014 - 06:42 AM

View PostSkydiver, on 09 July 2014 - 06:38 AM, said:

Remember that every time you call Image.FromFile() you are loading up a brand new instance and eating up resources.

And what should I change?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: C# Platform Game Help

Posted 09 July 2014 - 06:50 AM

Load the images once and cache them away. I personally would cache them away in a dictionary, but if you run Lutz's Reflector or JetBrains dotPeek() on the .NET framework code, you'll notice that tendency is to cache images away in well named variables. Using the variables avoids the overhead of the dictionary.
Was This Post Helpful? 2
  • +
  • -

#7 Matejd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-July 14

Re: C# Platform Game Help

Posted 09 July 2014 - 06:57 AM

Sorry @Skydiver but im not that good i cant understand what are you saying.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: C# Platform Game Help

Posted 09 July 2014 - 07:01 AM

Load your images once, then reuse them.

Don't base your game on screen height and primary screen. As a multi-monitor user I hate it when developers presume I want to run their program in full-screen mode on my primary monitor (which is rotated 90 making it tall and narrow (1080wide by 1920tall). I should be able to size the game area and put it on monitor 3 if I want.

Don't do it in 1980's WinForms unless you are aiming at Win98 users. If you want to be able to make a program that can easily move from Window PC, to a Windows Tablet, to a Windows Phone you want to be using WPF and Visual Studio 2013. The latest updates to Visual Studio 2013 are aimed directly at this kind of cross-device development.
http://www.visualstu...form-support-vs
Was This Post Helpful? 0
  • +
  • -

#9 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,539
  • Joined: 08-April 09

Re: C# Platform Game Help

Posted 09 July 2014 - 07:02 AM

It looks to me you bit more than you can chew.
If you don't understand what he is saying then stop and learn first, i think you are getting ahead of yourself.
Was This Post Helpful? 3
  • +
  • -

#10 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: C# Platform Game Help

Posted 09 July 2014 - 07:02 AM

I'd suggest not using Image.FromFile at all. There's a pretty well-known side effect that it can lock the image file during loading, and not release the file lock when you'd expect it to.

There's another option called Image.FromStream. With that, you'll want to create a FileStream to your image file, then use that to load the image. It will release the file lock correctly.

Here's an example:

http://stackoverflow...en-release-lock

You can easily wrap that up in a function that takes a filename and returns an Image.
Was This Post Helpful? 4
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: C# Platform Game Help

Posted 09 July 2014 - 07:07 AM

In pseudo code:
class MyCustomCheckBox : IDisposable
{
    Image _checkedImage;
    Image _uncheckedImage;

    MyCustomCheckBox()
    {
        // cache the images
        _checkedImage = Image.FromFile(path to my checked image resource file);
        _uncheckedImage = Image.FromFile(path to my unchecked image resource file);
    }

    // code to implement IDisposable pattern skipped for clarity.

    void Paint()
    {
        if (IsChecked)
            draw using _checkedImage
        else
            draw using _uncheckedImage
    }
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1