gpt4 book ai didi

c# - 游戏滞后(初学者)

转载 作者:太空宇宙 更新时间:2023-11-03 18:40:58 25 4
gpt4 key购买 nike

我正在创建一个小行星在屏幕上生成并向下移动的游戏。在游戏的更新方法中,我使用随机数来偶尔产生小行星。当我启动它时,它在前 5 秒内开始滞后。我可以看到这一点,因为分数计数器(每刻都上升)开始以 30 为间隔开始。此外,小行星的图像甚至没有显示。

这是游戏对象类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace thedodger
{
public abstract class gameObject
{
public static Texture2D texture;
public Rectangle rectangle;

public abstract void Draw(SpriteBatch spriteBatch);
public abstract void Update(GameTime gameTime);
}
}

这里是小行星类;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace thedodger
{
public class Asteroid : gameObject
{
Random rand = new Random(1);
int yPos = -10;

public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Begin();
spriteBatch.Draw(texture, new Rectangle(rand.Next(32,400), yPos,32,32),Color.White);
spriteBatch.End();

}

public override void Update(GameTime gameTime)
{
yPos--;


}
}
}

这是 game1 类:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace thedodger
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
int scorevalue = 0;
SpriteFont font;
player player1 = new player();
List<gameObject> objectList = new List<gameObject>();
Random rand = new Random(1);
Asteroid asteroid = new Asteroid();


public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here

base.Initialize();
}

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
font = Content.Load<SpriteFont>("font");
//player1.image = Content.Load<Texture2D>("EnemyShip005.png");
gameObject.texture = Content.Load<Texture2D>("asteroid");

// TODO: use this.Content to load your game content here
}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
scorevalue++;

if (rand.Next(0, 8) == 2)
{
for (int i = 0; i < 30; i++)
{

objectList.Add(asteroid);


}
}

foreach (Asteroid asteroid in objectList)
{
asteroid.Update(gameTime);
asteroid.Draw(spriteBatch);
}



// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);
}

/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.DrawString(font, "Score: " + scorevalue, new Vector2(5, 5), Color.White);
spriteBatch.End();



base.Draw(gameTime);
}
}
}

所有帮助将不胜感激。对不起代码。我很难设置它。也请帮忙。

最佳答案

正如 Tobias 所说,您可能应该将它放在 Game Dev 站点上,并且您似乎只实例化了一个 Asteroid。在 Game1 对象中,您声明并实例化了一个 Asteroid 对象。然后在 Update 方法中将其重复添加到 objectList。为了得到我认为你想要的,你应该改变

Asteroid asteroid = new Asteroid();

Asteroid asteroid;

然后改变

for (int i = 0; i < 30; i++)
{
objectList.Add(asteroid);
}

for (int i = 0; i < 30; i++)
{
asteroid = new Asteroid();
objectList.Add(asteroid);
}

在您的原始代码中,您将 asteroid 声明并实例化为特定的 Asteroid,但之后永远不会更改它。因此,在整个程序中,asteroid 都指向 Asteroid 的一个特定实例。然后,您将 asteroid 重复添加到 objectList。因此,在每一帧之后,将向 objectList 以及 Update()Draw 添加 30 个对同一 asteroid 对象的新引用() 方法在 asteroid 上被调用,以便在 objectList 中引用它。因此,在 30 帧之后,如果以 30FPS 运行一秒钟,objectList 中将有多达 900 个对同一 asteroid 对象的引用,并且在第 30 帧 asteroidUpdate()Draw() 方法被调用多达 900 次。我很确定这是你滞后的根源。执行上面给出的更正将导致 objectList 填充多达 900 个不同 Asteroid,但肯定也会遇到延迟。您还需要做的是在任何给定时间对屏幕上的小行星数量添加限制(objectList 的长度只能是 x 数量)和/或降低每次创建的小行星数量时间。我会建议类似

for (int i = 0; i < 5; i++)
{
if (objectList.Count < 50) // Maximum asteroids on screen
{
asteroid = new Asteroid();
objectList.Add(asteroid);
}
}

在一段时间内只会产生 5 颗新小行星,在任何给定时间最多会产生 50 颗。如果您添加了摧毁小行星的功能,则必须记住将它们从 objectList 中移除。

编辑 - 正如 Neomex 所说,您的绘图也是一个问题。我建议在此处查看 GDC 2008 关于 XNA 框架性能的演讲:http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=6082它非常简要地介绍了一些绘图和计算性能以及您应该/不应该做的事情。

EDIT2- 实际上,由于您的随机概率,最多 900 个对象引用和调用,而不是完整的 900 个。

关于c# - 游戏滞后(初学者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8957086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com