角色在我的C#游戏中走得很慢

时间:2014-06-18 04:16:29

标签: c# performance draw

问题是我无法弄清楚为什么我的角色在绘制图像时移动缓慢。所有计时器都设置为1个间隔,从不更改。任何帮助将不胜感激。这是整个项目:

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 Rice_Boy_Tester_2
{
  public partial class Form1 : Form
  {
    bool iggy = false;
    bool left2 = false;
    bool right2 = false;
    bool Up2 = false;
    bool Down2 = false;
    bool Check2 = false;

    bool left = false;
    bool right = false;
    bool Up = false;
    bool Down = false;
    bool Check = false;

    public Form1() {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e) {
        // Empty block
    }


    private void Refresh_Tick(object sender, EventArgs e) {
        this.Refresh();

    }

    private void PriceBoyWalk_Tick(object sender, EventArgs e) {
        if (left) // Goes Left
            Player.Left -= 1;

        if (Player.Left < 170 & Check == false) {
            // Checks how far away player is from form
            left = false;
            Up = true;
        }
        if (Up & Player.Left < 170) { // Goes Up
            Player.Top -= 1;
            Check = true;
        }

        if (Player.Top < 100 & Check) {
            Up = false;
            Down = true;

        }

        if (right) // Goes Right
            Player.Left += 1;

        if (Down) // Goes Down
            Player.Top += 1;

        if (Player.Top + 150 > this.ClientSize.Height) {
            Check = false;
            Down = false;
            right = true;
        }


        if (Player.Left + 150 > this.ClientSize.Width)
            right = false;

    }
    private void B1_Click(object sender, EventArgs e) {
        this.Paint += new PaintEventHandler(form1_Pad1_Rice);
        RiceBoyWalkGif.Enabled = true;
        left = true;
        left2 = true;
        RiceBoyWalk.Enabled = true;

    }

    private void timer1_Tick(object sender, EventArgs e) {
        if (left2) {
            Player.Image = Image.FromFile("Rice-Boy-Walking-Left-Bouncing.gif"); // Animates RiceBoyWalkingLeft
            left2 = false;

        }
        if (Player.Left < 170 & Check2 == false) {
            // Checks how far away the player is from form
            left2 = false;
            Up2 = true;

        }
        if (Up2 & Player.Left < 170) { // Goes Up
            this.Player.Size = new System.Drawing.Size(36, 76); // Changes size of the picture box to maintain quality 
            Player.Image = Image.FromFile("Rice-Boy-Walking-Up-Bouncing.gif"); // Animates RiceBoyWalkingUp
            Check2 = true;
            Up2 = false;

        }

        if (Player.Top < 101 & Check2) {
            // Player.Top < 101 must be +1 greater than the RiceBoyWalkTimer
            Up2 = false;
            Down2 = true;

        }

        if (right2) {
            this.Player.Size = new System.Drawing.Size(53, 77); // Changes size of the picture box to maintain quality 
            Player.Image = Image.FromFile("Rice-Boy-Walking-Right-Bouncing.gif"); // Animates RiceBoyWalkingRight
            right2 = false;

        }

        if (Down2) { // Goes Down
            Player.Image = Image.FromFile("Rice-Boy-Walking-Down.gif");
            Down2 = false;

        }

        if (Player.Top + 150 > this.ClientSize.Height) {
            iggy = true; // Shows that riceboy is approaching the starting point
            Check2 = false;
            Down2 = false;
            right2 = true;
        }

        if (Player.Left + 150 > this.ClientSize.Width & iggy) {
            right2 = false;
            Player.Image = Properties.Resources.Rice_Boy_Standing_Left;
        }

    }
    private void form1_Pad1_Rice(object sender, System.Windows.Forms.PaintEventArgs e) {
        e.Graphics.DrawImage(Properties.Resources.Corn_Cobs, 120, 58, 50, 50);
        // Draws Corn cobs Character goes slower when corn is drawing
        // (x(-left), y(-Up), W, H) 
    }

1 个答案:

答案 0 :(得分:4)

从这开始:

  

Player.Image = Image.FromFile(&#34; Rice-Boy-Walking-Down.gif&#34;);

(以及其他加载例程)。

每一个嘀嗒声?严重?

在初始化期间加载一次,将它们存储在变量中,重用图像。曾经玩过电脑游戏吗?它们并不是在试图每帧都加载所有图形资产时丢弃你的光盘。

光盘访问速度很慢。图像解码很慢。我怀疑你在程序运行时改变了图像。