Данный Код был взят из видео, что можно найти на Ю ТУБЕ, по названию:
Tutoriel C# XNA - Chapitre 1 - Les bases
Tutoriel C# XNA - Chapitre 2 - Animation de sprite
и чуть модифицирован под мой спрайт и мои нужды.
Реализованы движения анимации влево и вправо.
Если будут какие-то идеи пишите в ЛС или если админи позволят, то пишите прямо в тему.
Game1.cs
C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
| 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 AnimationSprite
{
public class Game1 : Microsoft.Xna.Framework.Game
{
// VARIABLES
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
GameMain Main;
// MAIN
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.IsMouseVisible = true;
graphics.IsFullScreen = false;
}
// INITIALIZE
protected override void Initialize()
{
base.Initialize();
}
// LOAD CONTENT
protected override void LoadContent()
{
Resources.LoadContent(Content);
spriteBatch = new SpriteBatch(GraphicsDevice);
Main = new GameMain();
}
// UNLOAD CONTENT
protected override void UnloadContent()
{
}
// UPDATE & DRAW
protected override void Update(GameTime gameTime)
{
Main.Update(Mouse.GetState(), Keyboard.GetState());
debugVAR.Update(Mouse.GetState(), Keyboard.GetState());
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
Main.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
} |
|
GameMain.cs
C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace AnimationSprite
{
class GameMain
{
// FIELDS
Player LocalPlayer;
// КОНСТРУКТОР
public GameMain()
{
LocalPlayer = new Player();
}
// METHODS
// UPDATE & DRAW
public void Update(MouseState mouse, KeyboardState keybord)
{
LocalPlayer.Update(mouse, keybord);
}
public void Draw(SpriteBatch spriteBatch)
{
LocalPlayer.Draw(spriteBatch);
}
}
} |
|
Resorces.cs
C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace AnimationSprite
{
class Resources
{
// STATIC FIELDS
public static Texture2D Player, Mario;
// LOAD CONTENT
public static void LoadContent(ContentManager Content)
{
Player = Content.Load<Texture2D>("Player");
Mario = Content.Load<Texture2D>("SpriteMarion");
}
}
} |
|
Player.cs
C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace AnimationSprite
{
public enum Direction
{
J, S, A, D
};
class Player
{
// FIELDS
Rectangle Hitbox;
Direction Direction;
SpriteEffects Effects;
int Speed = 5;
bool Animation = false;
public int FrameColumn;
// КОНСТРУКТОР
public Player()
{
this.Hitbox = new Rectangle(0, 0, 42, 42);
this.FrameColumn = 0;
}
// METHODS
// Анимация прыжка Марио вверх
public void AnimateJ()
{
}
// Анимация Стрельбы Марио
public void AnimateS()
{
}
// Анимация движения Марио в правую сторону
// Кадры: [1] - [8]
public void AnimateDA()
{
if (this.Animation)
{
this.FrameColumn++;
if (this.FrameColumn > 8)
{
this.FrameColumn = 1;
}
}
else
{
this.FrameColumn = 1;
}
}
// UPDATE & DRAW
public void Update(MouseState mouse, KeyboardState keybord)
{
if(keybord.IsKeyDown(Keys.J))
{
this.Animation = true;
this.Hitbox.Y -= this.Speed;
this.Direction = Direction.J;
}
else if(keybord.IsKeyDown(Keys.S))
{
this.Hitbox.Y += this.Speed;
this.Direction = Direction.S;
this.AnimateS();
}
else if(keybord.IsKeyDown(Keys.A))
{
this.Animation = true;
this.Hitbox.X -= this.Speed;
this.Direction = Direction.A;
}
else if(keybord.IsKeyDown(Keys.D))
{
this.Animation = true;
this.Hitbox.X += this.Speed;
this.Direction = Direction.D;
}
if(keybord.IsKeyUp(Keys.J) && keybord.IsKeyUp(Keys.S)
&& keybord.IsKeyUp(Keys.A) && keybord.IsKeyUp(Keys.D))
{
// Добавить анимацию Простоя
this.Animation = false;
}
switch (this.Direction)
{
case Direction.J: this.FrameColumn = 3;
this.Effects = SpriteEffects.None;
break;
case Direction.S: this.FrameColumn = 2;
this.Effects = SpriteEffects.None;
break;
case Direction.A: this.AnimateDA();
this.Effects = SpriteEffects.FlipHorizontally;
break;
case Direction.D: this.AnimateDA();
this.Effects = SpriteEffects.None;
break;
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(
Resources.Mario,
this.Hitbox,
new Rectangle((this.FrameColumn - 1) * 42, 0, 42, 42),
Color.White, 0f, new Vector2(0,0), this.Effects, 0f );
}
}
} |
|