游戏第一步 XNA游戏1
快要毕业了,为了这后能找份游戏相关的工作(当然是程序员,我可不会C++),于是有了我要做的游戏,过年的时候我看过一些XNA的例子代码,但由于没有安装环境调试不起,终于我要大胆的做出我的第一个游戏……(素材是从其它游戏里找的)
先看下效果:
感觉是不好看,但必竟是我写的第一个,
然后是类和结构:
部分类:
1
namespace DesSpace
2
{
3
4
public class DesSpace : Game
5
{
6
const int screenWidth =800;
7
const int screenHeight = 600;
8
9
GraphicsDeviceManager graphics;
10
SpriteBatch spriteBatch;
11
KeyboardState kes;
12
public BulletCtrl bull;
13
public Back back; //背景部件
14
public Plane plane;
15
public StoneCtrl stone;
16
public BombCtrl bomb;
17
public Score GameScore;
18
public bool isActive=true; //游戏失败
19
public int level=1;
20
21
22
public SpriteBatch SpriteBatch
23
{
24
get { return spriteBatch; }
25
}
26
27
28
public DesSpace()
29
{
30
graphics = new GraphicsDeviceManager(this);
31
Content.RootDirectory = "Content";
32
33
graphics.PreferredBackBufferHeight = screenHeight;
34
graphics.PreferredBackBufferWidth = screenWidth;
35
this.Window.Title = "游戏制作:Desmend";
36
37
Components.Add(new GamerServicesComponent(this));
38
back = new Back(this);
39
stone = new StoneCtrl(this);
40
plane = new Plane(this);
41
bull = new BulletCtrl(this);
42
bomb = new BombCtrl(this);
43
GameScore.atticstone = 0;
44
GameScore.deathtimes = 0;
45
GameScore.sendbullet = 500;
46
47
}
48
49
50
protected override void LoadContent()
51
{
52
Random rnd = new Random();
53
spriteBatch = new SpriteBatch(GraphicsDevice);
54
back.BackGround1 = Content.Load<Texture2D>("Textures/BackGround1");
55
back.BackGround2 = Content.Load<Texture2D>("Textures/BackGround2");
56
back.BackGround3 = Content.Load<Texture2D>("Textures/BackGround3");
57
back.SpeadFont = Content.Load<SpriteFont>("Font/Speed");
58
59
plane.PlanTex = Content.Load<Texture2D>("Textures/Plane");
60
61
bull.BullTex = Content.Load<Texture2D>("Textures/Bullet");
62
stone.StoneTex = Content.Load<Texture2D>("Textures/Stone");
63
64
bomb.Bomb2 = Content.Load<Texture2D>("Textures/PlaneBomb");
65
bomb.Bomb1 = Content.Load<Texture2D>("Textures/StoneBomb");
66
67
}
68
69
protected override void Update(GameTime gameTime)
70
{
71
KeyboardState key=Keyboard.GetState();
72
if (key.IsKeyDown(Keys.Escape))
73
Exit();
74
if (GameScore.atticstone > 800)
75
level = 2;
76
if (GameScore.atticstone > 3000)
77
level = 3;
78
stone.Update(gameTime);
79
back.Update(gameTime);
80
bull.Update(gameTime);
81
plane.Update(gameTime);
82
bomb.Update(gameTime);
83
84
if (GameScore.deathtimes > 5)
85
{
86
isActive = false;
87
}
88
base.Update(gameTime);
89
}
90
91
92
protected override void Draw(GameTime gameTime)
93
{
94
graphics.GraphicsDevice.Clear(Color.Black);
95
spriteBatch.Begin();
96
back.Draw(gameTime);
97
if (isActive)
98
{
99
bull.Draw(gameTime);
100
plane.Draw(gameTime);
101
stone.Draw(gameTime);
102
bomb.Draw(gameTime);
103
spriteBatch.DrawString(back.SpeadFont, "Life:" + (5 - GameScore.deathtimes), new Vector2(700, 5), Color.Red);
104
spriteBatch.DrawString(back.SpeadFont, "Level:" + level, new Vector2(700, 25), Color.Red);
105
spriteBatch.DrawString(back.SpeadFont, "Score:" + GameScore.atticstone, new Vector2(700, 50), Color.Yellow);
106
}
107
else
108
{
109
spriteBatch.DrawString(back.SpeadFont, "Game Over!", new Vector2(350, 280), Color.White);
110
}
111
spriteBatch.End();
112
base.Draw(gameTime);
113
}
114
}
115
}
116

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

1
namespace DesSpace
2
{
3
4
public class Back : Microsoft.Xna.Framework.DrawableGameComponent
5
{
6
Rectangle BackRect1;
7
Rectangle BackRect2;
8
9
private DesSpace curGame = null;
10
private int DrawTimes=1;
11
private int spead = 3;
12
private float acceleration = 0.0f;
13
private KeyboardState kes;
14
15
public Texture2D BackGround1; //背景材质1
16
public Texture2D BackGround2; //背景材质1
17
public Texture2D BackGround3; //背景材质1
18
public SpriteFont SpeadFont;
19
public int Speed //速度
20
{
21
get
22
{ return spead; }
23
set
24
{ spead = value; }
25
}
26
public float Acceleration //加速度
27
{
28
get { return acceleration; }
29
set { acceleration = value; }
30
}
31
32
public Back(Game game): base(game)
33
{
34
curGame = (DesSpace)game;
35
BackRect1 = new Rectangle(0,0,800,600);
36
BackRect2 = new Rectangle(0,-600,800,600);
37
}
38
39
public override void Update(GameTime gameTime)
40
{
41
kes = Keyboard.GetState();
42
InputHandel(); //按键控制
43
44
spead +=Convert.ToInt32( acceleration); //速度改变
45
BackRect1.Y +=spead;
46
if (DrawTimes % 2 == 0)
47
{
48
BackRect2.Y = BackRect1.Y + 600;
49
}
50
else
51
{
52
BackRect2.Y = BackRect1.Y - 600;
53
}
54
55
if (BackRect1.Y == 600)
56
{
57
DrawTimes++;
58
BackRect1.Y = -600;
59
}
60
if (BackRect1.Y == 0)
61
{
62
DrawTimes++;
63
BackRect2.Y = -600;
64
}
65
66
base.Update(gameTime);
67
}
68
69
public override void Draw(GameTime gameTime)
70
{
71
Texture2D texture=null;
72
if (curGame.level == 1)
73
texture = BackGround1;
74
if (curGame.level == 2)
75
texture = BackGround2;
76
if (curGame.level == 3)
77
texture = BackGround3;
78
curGame.SpriteBatch.Draw(texture, BackRect1, Color.White);
79
curGame.SpriteBatch.Draw(texture, BackRect2, Color.White);
80
curGame.SpriteBatch.DrawString(SpeadFont, string.Format("Speed:{0}", spead.ToString()), new Vector2((float)550, (float)10), Color.White);
81
base.Draw(gameTime);
82
}
83
84
/// <summary>
85
/// 键盘控制
86
/// </summary>
87
private void InputHandel()
88
{
89
if (kes.IsKeyDown(Keys.Escape))
90
curGame.Exit();
91
if (kes.IsKeyDown(Keys.W))
92
acceleration += 0.001f;
93
if (kes.IsKeyDown(Keys.S))
94
acceleration -= 0.001f;
95
}
96
}
97
}

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

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端