C# 如何使父脚本中更改的布尔值影响子脚本? 对于一个学校项目,我需要制作一个Gradius克隆,我需要拥有大部分相同的东西,并添加我自己的小天赋,以获得更高的分数。但我遇到了一个问题,我无法让我的脚本正常工作。 我想要的是: 如果玩家按回车键请求,我的游戏需要能够暂停。这需要暂停我的玩家脚本、2个敌人脚本以及敌人的繁殖者脚本。 关于更多细节,我将向您介绍每个脚本的功能,因为我认为它对解决方案非常重要。 Actor.cs=此脚本是母脚本,包含子脚本要继承的基本信息 Player.cs=顾名思义,这是控制宇宙飞船并能够射击的玩家 FanDebuy.cs=是目前最简单的敌人,只是从右向左飞行 RugalEnemy.cs=是一个试图到达球员位置并跟随他到处的敌人 Eneyspawner.cs=此脚本处理游戏中敌人的繁殖,并能够根据秒数创建模式
我的脚本如下:C# 如何使父脚本中更改的布尔值影响子脚本? 对于一个学校项目,我需要制作一个Gradius克隆,我需要拥有大部分相同的东西,并添加我自己的小天赋,以获得更高的分数。但我遇到了一个问题,我无法让我的脚本正常工作。 我想要的是: 如果玩家按回车键请求,我的游戏需要能够暂停。这需要暂停我的玩家脚本、2个敌人脚本以及敌人的繁殖者脚本。 关于更多细节,我将向您介绍每个脚本的功能,因为我认为它对解决方案非常重要。 Actor.cs=此脚本是母脚本,包含子脚本要继承的基本信息 Player.cs=顾名思义,这是控制宇宙飞船并能够射击的玩家 FanDebuy.cs=是目前最简单的敌人,只是从右向左飞行 RugalEnemy.cs=是一个试图到达球员位置并跟随他到处的敌人 Eneyspawner.cs=此脚本处理游戏中敌人的繁殖,并能够根据秒数创建模式,c#,windows,inheritance,boolean,C#,Windows,Inheritance,Boolean,我的脚本如下: Actor.cs using System; using System.Collections; using System.IO; using System.Windows.Forms.VisualStyles; namespace GameEngine { public class Actor : GameObject { //Paused public bool isPaused = false; publ
Actor.cs
using System;
using System.Collections;
using System.IO;
using System.Windows.Forms.VisualStyles;
namespace GameEngine
{
public class Actor : GameObject
{
//Paused
public bool isPaused = false;
public float PauseTimePassed;
//Bitmaps
protected Bitmap bitmap;
//Gradius reference
protected Gradius game;
//info for the Actors position
public float PosX = 320;
public float PosY = 240;
protected float Width = 50;
protected float Height = 30;
//info for the Actors speed
protected float XSpeed;
protected float YSpeed;
protected float Accel;
protected float Friction;
public float flySpeed = 100f;
public float maxFlySpeed = 200f;
public Actor()
{
GameStart();
}
public Actor(Gradius gameref)
{
game = gameref;
GameStart();
}
public override void GameStart()
{
}
public override void Update()
{
if (GAME_ENGINE.GetKeyDown(Key.Enter) && isPaused == false)
{
isPaused = true;
}
else if (GAME_ENGINE.GetKeyDown(Key.Enter) && isPaused == true)
{
isPaused = false;
}
if (isPaused == true)
{
PauseTimePassed += GAME_ENGINE.GetDeltaTime();
Console.WriteLine(Convert.ToString(PauseTimePassed));
}
}
public override void Paint()
{
if (bitmap != null)
{
DrawBitmap();
}
else
{
DrawShape();
}
}
public void DrawShape()
{
GAME_ENGINE.SetColor(255, 255, 255);
GAME_ENGINE.FillRectangle(PosX, PosY, Width, Height);
}
public void DrawBitmap()
{
GAME_ENGINE.DrawBitmap(bitmap, PosX, PosY);
}
public void SetPosition(float x, float y)
{
PosX = x;
PosY = y;
}
public Vector2f GetPosition()
{
return new Vector2f(PosX, PosY);
}
}
}
Player.csusing System;
using System.Windows.Forms;
namespace GameEngine
{
public class Player : Actor
{
//info for the Player about the Borders of the game
private int screenBoundriesX1 = 0;
private int screenBoundriesX2 = 590;
private int screenBoundriesY1 = 0;
private int screenBoundriesY2 = 450;
//Bullet info
public bool isShooting = false;
private int OffsetX = 20;
private int OffsetY = 10;
public override void GameStart()
{
bitmap = new Bitmap("VicViper.png");
Width = 50;
Height = 30;
Accel = 100f;
Friction = 0.87f;
}
public override void Update()
{
float deltaTime = GAME_ENGINE.GetDeltaTime();
//-----Player-----
if (GAME_ENGINE.GetKey(Key.D) && isPaused == false)
{
XSpeed += Accel;
}
else if (GAME_ENGINE.GetKey(Key.A) && isPaused == false)
{
XSpeed -= Accel;
}
if (GAME_ENGINE.GetKey(Key.W) && isPaused == false)
{
YSpeed -= Accel;
}
else if (GAME_ENGINE.GetKey(Key.S) && isPaused == false)
{
YSpeed += Accel;
}
if (isPaused == false)
{
XSpeed = XSpeed * Friction;
YSpeed = YSpeed * Friction;
PosX += XSpeed * deltaTime;
PosY += YSpeed * deltaTime;
}
if (PosX > screenBoundriesX2)
{
PosX = screenBoundriesX2;
}
else if (PosX < screenBoundriesX1)
{
PosX = screenBoundriesX1;
}
if (PosY < screenBoundriesY1)
{
PosY = screenBoundriesY1;
}
else if (PosY > screenBoundriesY2)
{
PosY = screenBoundriesY2;
}
//-----Bullet-----
//-----De lijnen aan code kunnen wellicht via Actor.cs opgegeven worden voor Player.cs-----
if (GAME_ENGINE.GetKeyDown(Key.Space) && isShooting == false && isPaused == false)
{
Bullet bullet = new Bullet();
bullet.SetPosition(PosX + OffsetX, PosY + OffsetY);
isShooting = true;
if (isShooting == true)
{
isShooting = false;
}
}
}
}
}
使用系统;
使用System.Windows.Forms;
命名空间游戏引擎
{
公开课选手:演员
{
//关于游戏边界的玩家信息
私有int screenBoundriesX1=0;
私有int屏幕边界x2=590;
私有整数屏幕边界1=0;
私有int屏幕边界2=450;
//子弹信息
公共bool isShooting=假;
私人国际贸易抵销额x=20;
专用整数偏移=10;
公共覆盖无效GameStart()
{
位图=新位图(“VicViper.png”);
宽度=50;
高度=30;
加速度=100f;
摩擦力=0.87f;
}
公共覆盖无效更新()
{
float deltaTime=GAME_ENGINE.GetDeltaTime();
//-----玩家-----
if(GAME_ENGINE.GetKey(Key.D)和&isPaused==false)
{
XSpeed+=加速度;
}
else if(GAME_ENGINE.GetKey(Key.A)和&isPaused==false)
{
XSpeed-=加速度;
}
if(GAME_ENGINE.GetKey(Key.W)和&isPaused==false)
{
YSpeed-=加速度;
}
else if(GAME_ENGINE.GetKey(Key.S)&&isPaused==false)
{
YSpeed+=加速度;
}
如果(isPaused==false)
{
XSpeed=XSpeed*摩擦力;
Y速度=Y速度*摩擦力;
PosX+=XSpeed*deltaTime;
PosY+=YSpeed*deltaTime;
}
如果(PosX>screenBoundriesX2)
{
PosX=屏幕边界x2;
}
else if(PosXscreenBoundriesY2)
{
PosY=屏幕边界2;
}
//-----子弹头-----
//-----通过Actor.cs opgegegeven worden voor Player.cs执行代码kunnen wellicht-----
if(GAME_ENGINE.GetKeyDown(Key.Space)&&isShooting==false&&isPaused==false)
{
子弹=新子弹();
bullet.SetPosition(PosX+OffsetX,PosY+OffsetY);
isShooting=真;
如果(isShooting==true)
{
isShooting=假;
}
}
}
}
}
fan敌方.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameEngine
{
public class FanEnemy : Actor
{
private float DespawnPos = 10;
private float SpeedX = 100f;
public override void GameStart()
{
bitmap = new Bitmap("FanEnemy.png");
Width = 30;
Height = 20;
}
public override void Update()
{
if (isPaused == false)
{
PosX -= SpeedX * GAME_ENGINE.GetDeltaTime();
}
if (PosX <= DespawnPos)
{
this.Dispose();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameEngine
{
public class RugalEnemy : Actor
{
private float PlayerPosX;
private float PlayerPosY;
private float DespawnPos = 10;
public RugalEnemy(Gradius gameref) : base(gameref)
{
}
public override void GameStart()
{
bitmap = new Bitmap("VicViper.png");
Width = 30;
Height = 20;
flySpeed = 100f;
PosX = 640;
PosY = 10;
}
public override void Update()
{
Player p = game.GetPlayer();
PlayerPosX = p.GetPosition().X;
PlayerPosY = p.GetPosition().Y;
//PosX -= flySpeed * GAME_ENGINE.GetDeltaTime();
if (PosX >= PlayerPosX && isPaused == false)
{
PosX -= flySpeed * GAME_ENGINE.GetDeltaTime();
}
else if (PosX <= PlayerPosX && isPaused == false)
{
PosX += flySpeed * GAME_ENGINE.GetDeltaTime();
}
if (PosY <= PlayerPosY && isPaused == false)
{
PosY += flySpeed * GAME_ENGINE.GetDeltaTime();
}
else if (PosY >= PlayerPosY && isPaused == false)
{
PosY -= flySpeed * GAME_ENGINE.GetDeltaTime();
}
if (PosX <= DespawnPos)
{
this.Dispose();
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间游戏引擎
{
公开课:演员
{
私人浮存资产剥离POS=10;
私人浮动速度x=100f;
公共覆盖无效GameStart()
{
位图=新位图(“fanDefey.png”);
宽度=30;
高度=20;
}
公共覆盖无效更新()
{
如果(isPaused==false)
{
PosX-=SpeedX*游戏引擎.GetDeltaTime();
}
if(PosX=PlayerPosX&&isPaused==false)
{
PosX-=flySpeed*GAME_ENGINE.GetDeltaTime();
}
elseif(PosX)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace GameEngine
{
public struct SpawnListItem
{
public float SpawnTime;
public EnemyType type;
public float X;
public float Y;
public SpawnListItem(float time, EnemyType t, float x, float y)
{
SpawnTime = time;
type = t;
X = x;
Y = y;
}
}
public class EnemySpawner : GameObject
{
private float Time;
private Gradius game;
private List<SpawnListItem> spawnList;
public EnemySpawner(Gradius gameref)
{
game = gameref;
GameStart();
}
public override void GameStart()
{
spawnList = new List<SpawnListItem>();
spawnList.Add(new SpawnListItem(1f, EnemyType.Rugal, 640, 10));
spawnList.Add(new SpawnListItem(2f, EnemyType.Fan, 630, 20));
spawnList.Add(new SpawnListItem(4f, EnemyType.Fan, 620, 30));
}
public override void Update()
{
Time += GAME_ENGINE.GetDeltaTime();
if (spawnList.Count == 0)
{
return;
}
if (Time >= spawnList[0].SpawnTime)
{
if (spawnList[0].type == EnemyType.Fan)
{
FanEnemy fEnemy = new FanEnemy();
fEnemy.SetPosition(spawnList[0].X, spawnList[0].Y);
//Kan ik de enum koppelen aan het script van de enemy?
spawnList.RemoveAt(0);
}
else if (spawnList[0].type == EnemyType.Rugal)
{
RugalEnemy rEnemy = new RugalEnemy(game);
rEnemy.SetPosition(spawnList[0].X, spawnList[0].Y);
//hetzelfde geval met de positie
spawnList.RemoveAt(0);
}
}
}
}
}