Легенда
Регистрация: 01.10.2006
Сообщений: 3,705
Написано 296 полезных сообщений (для 568 пользователей)
|
Обойти - реально?
Пробовал, пробовал, но так и не сумел избежать ругани дебаггера. Прошу помощи у вас. Вот код:
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System.Collections;
#endregion
namespace Spacer
{
///<summary>
/// This is the main type for your game
///</summary>
///
publicclassGame : Microsoft.Xna.Framework.Game
{
staticGraphicsDeviceManager graphics;
staticContentManager content;
staticSpriteBatch SBatch;
staticTexture2D Background;
staticKeyboardState KState;
staticPlayer Igrok = newPlayer();
staticTexture2D PulaImage;
staticint Time = 10;
staticStack<Bullet> Bulllist = newStack<Bullet>();
SpriteFont SFont;
classPlayer
{
publicTexture2D Entity;
publicdouble X = 640.0, Y = 512.0; publicdouble Angle = 90;
publicdouble Xspeed = 0, Yspeed = 0;
publicdouble Acceleration = 0.1, friction = 0.03;
publicdouble MaxSpd = 4.0;
publicdouble TurnSpd = 0, TurnMax = 0.08, TurnAccel = 0.04, TurnFrict = 0.03;
KeyboardState KState;
publicvoid Update ( )
{
KState = Keyboard.GetState ( );
if ( KState.IsKeyDown ( Keys.Up ) )
{
Yspeed += Math.Sin ( Angle ) * Acceleration;
Xspeed += Math.Cos ( Angle ) * Acceleration;
}
if ( KState.IsKeyDown ( Keys.Down ) )
{
Yspeed -= Math.Sin ( Angle ) * Acceleration;
Xspeed -= Math.Cos ( Angle ) * Acceleration;
}
double SpdVector = Math.Sqrt ( ( Yspeed * Yspeed ) + ( Xspeed * Xspeed ) );
if ( SpdVector > 0 )
{
Xspeed -= ( Xspeed / SpdVector ) * friction;
Yspeed -= ( Yspeed / SpdVector ) * friction;
}
if ( SpdVector > MaxSpd )
{
Xspeed += ( Xspeed / SpdVector ) * ( MaxSpd - SpdVector );
Yspeed += ( Yspeed / SpdVector ) * ( MaxSpd - SpdVector );
}
if ( SpdVector == 0 )
{
Xspeed = 0;
Yspeed = 0;
}
// TODO: Add your update logic here
X += Xspeed;
Y += Yspeed;
if ( KState.IsKeyDown ( Keys.Left ) )
{
TurnSpd -= TurnAccel;
}
if ( KState.IsKeyDown ( Keys.Right ) )
{
TurnSpd += TurnAccel;
}
if ( TurnSpd > TurnMax ) TurnSpd = TurnMax;
if ( TurnSpd < -TurnMax ) TurnSpd = -TurnMax;
Angle += TurnSpd;
if ( Angle < 0 ) Angle += 360;
if ( Angle > 360 ) Angle -= 360;
if ( TurnSpd > TurnFrict ) TurnSpd -= TurnFrict;
if ( TurnSpd < -TurnFrict ) TurnSpd += TurnFrict;
if ( ( TurnSpd < TurnFrict ) && ( TurnSpd > -TurnFrict ) ) TurnSpd = 0;
}
}
classBullet
{
double x, y, angle;
double xspeed, yspeed;
publicfloat alpha = 1.0f;
publicTexture2D entity = PulaImage;
int speed = 15;
// Должна ли удаляццо пулйо
publicbool ShouldBeDeleted
{
get { return alpha <= 0; }
}
public Bullet ( double x, double y, double angle )
{
this.x = x;
this.y = y;
this.angle = angle;
this.xspeed = Math.Cos ( this.angle ) * this.speed + this.xspeed;
this.yspeed = Math.Sin ( this.angle ) * this.speed + this.yspeed;
Bulllist.Push ( this ); // Впринципе тоже не правильно, но ошибки быть не должно
}
publicvoid Update ( )
{
x += xspeed;
y += yspeed;
alpha -= 0.01f;
}
publicvoid Draw ( )
{
SBatch.Draw ( entity, newRectangle ( ( int ) x, ( int ) y, ( int ) ( entity.Width * 0.9 ), ( int ) ( entity.Height * 0.9 ) ),
null, Color.White, ( float ) angle, newVector2 ( ( float ) entity.Width * 0.5f, ( float ) entity.Height * 0.5f ),
SpriteEffects.None, 0 );
}
}
public Game ( )
{
graphics = newGraphicsDeviceManager ( this );
content = newContentManager ( Services );
}
///<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>
protectedoverridevoid Initialize ( )
{
// TODO: Add your initialization logic here
graphics.PreferredBackBufferHeight = 800;
graphics.PreferredBackBufferWidth = 600;
//graphics.IsFullScreen = true;
graphics.ApplyChanges ( );
IsFixedTimeStep = false;
base.Initialize ( );
}
///<summary>
/// Load your graphics content. If loadAllContent is true, you should
/// load content from both ResourceManagementMode pools. Otherwise, just
/// load ResourceManagementMode.Manual content.
///</summary>
///<param name="loadAllContent">Which type of content to load.</param>
///
protectedoverridevoid LoadGraphicsContent ( bool loadAllContent )
{
if ( loadAllContent )
{
// TODO: Load any ResourceManagementMode.Automatic content
Background = content.Load<Texture2D> ( "Images\\earth" );
Igrok.Entity = content.Load<Texture2D> ( "Images\\player" );
PulaImage = content.Load<Texture2D> ( "Images\\Pula" );
SFont = content.Load<SpriteFont> ( "Font" );
SBatch = newSpriteBatch ( graphics.GraphicsDevice );
}
// TODO: Load any ResourceManagementMode.Manual content
}
///<summary>
/// Unload your graphics content. If unloadAllContent is true, you should
/// unload content from both ResourceManagementMode pools. Otherwise, just
/// unload ResourceManagementMode.Manual content. Manual content will get
/// Disposed by the GraphicsDevice during a Reset.
///</summary>
///<param name="unloadAllContent">Which type of content to unload.</param>
protectedoverridevoid UnloadGraphicsContent ( bool unloadAllContent )
{
if ( unloadAllContent )
{
// TODO: Unload any ResourceManagementMode.Automatic content
content.Unload ( );
}
// TODO: Unload any ResourceManagementMode.Manual content
}
///<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>
protectedoverridevoid Update ( GameTime gameTime )
{
KState = Keyboard.GetState ( );
// Allows the game to exit
foreach ( Bullet i in Bulllist )
{
if ( i.ShouldBeDeleted ) continue; // Пока подождать...
i.Update ( );
}
if ( KState.IsKeyDown ( Keys.Escape ) ) { this.Exit ( ); }
Igrok.Update ( );
if ( KState.IsKeyDown ( Keys.Space ) ) {
if ( Time<=0)
{
Bullet i = newBullet ( Igrok.X, Igrok.Y, Igrok.Angle );
Time = 10;
}
}
Time--;
base.Update ( gameTime );
}
///<summary>
/// This is called when the game should draw itself.
///</summary>
///<param name="gameTime">Provides a snapshot of timing values.</param>
protectedoverridevoid Draw ( GameTime gameTime )
{
graphics.GraphicsDevice.Clear ( Color.Black );
SBatch.Begin ( );
SBatch.Draw ( Background, newRectangle ( 369, 229, 541, 565 ), Color.White );
foreach ( Bullet i in Bulllist ) i.Draw ( );
SBatch.Draw ( Igrok.Entity, newRectangle ( (int)Igrok.X, (int)Igrok.Y,
(int)Igrok.Entity.Width, (int)Igrok.Entity.Height), null, Color.White, (float)Igrok.Angle,
newVector2 ( Igrok.Entity.Width / 2, Igrok.Entity.Height/2 ), SpriteEffects.None,0 );
SBatch.DrawString ( SFont, "Kolvo - " + Bulllist.Count, newVector2 ( 15, 15 ), Color.White );
SBatch.End ( );
// TODO: Add your drawing code here
base.Draw ( gameTime );
}
}
}
Вся проблема в том, что я никак не могу придумать как еще сделать проверку. Мне нужно чтоб при нажатии пробел - создавалась новая пуля (в конструкторе она добавляется в стек(стек, мне подсказали, ибо лучше с ним работать, чем со списком)). Ну и чтобы как то обновлять их я сделал метод. А как удалить со стека, чтоб еще и программа жива осталась я незнаю, может вы знаете? Подскажите пожайлуста, а то уже третий день эту проблему не могу решить
|