游戏第一步 XNA游戏2
上次写完了游戏第一步 XNA游戏1,之后那天在传2的时候就地震了,断网了,晕的好死,N久都没睡好了,难民啊……
呵呵,还是先把代码贴出来:
上次写完了游戏第一步 XNA游戏1,之后那天在传2的时候就地震了,断网了,晕的好死,N久都没睡好了,难民啊……
呵呵,还是先把代码贴出来:
1
public enum BombType2

{ StoneBomb,PlaneBomb}3

4
public struct Bomb5

{6
public Vector2 posi;7
public BombType type;8
public bool isActive;9
public int bmpnum;10

11
public void update()12

{13
bmpnum++;14
}15
}16

17
public class BombCtrl : DrawableGameComponent18

{19
DesSpace curGame;20
Bomb[] bomb = new Bomb[5];21
Texture2D bomb1;22
Texture2D bomb2;23
double timelost=0;24

25
public Texture2D Bomb126

{27

get
{ return bomb1; }28

set
{ bomb1 = value; }29
}30

31
public Texture2D Bomb232

{33

get
{ return bomb2; }34

set
{ bomb2 = value; }35
}36

37
public BombCtrl(Game game): base(game)38

{39
curGame = (DesSpace)game;40
}41

42
public override void Update(GameTime gameTime)43

{44
double ts = gameTime.ElapsedGameTime.TotalSeconds;45
timelost += ts;46
for (int i = 0; i < 5; i++)47

{48
if (bomb[i].isActive && timelost>0.05)49

{50
bomb[i].update();51
timelost = 0;52
}53
}54

55
base.Update(gameTime);56
}57

58
public override void Draw(GameTime gameTime)59

{60
//double tim = gameTime.ElapsedRealTime.TotalSeconds;61
//timelost += tim;62
Rectangle DrRect=new Rectangle(0,0,0,0);63
for (int i = 0; i < 5; i++)64

{ 65
if (bomb[i].isActive)66

{67
if (bomb[i].type == BombType.StoneBomb )68

{69
if (bomb[i].bmpnum == 0)70
DrRect = new Rectangle(0, 0, 30, 30);71
if (bomb[i].bmpnum == 1)72
DrRect = new Rectangle(0, 30, 30, 30);73
if (bomb[i].bmpnum == 3)74
DrRect = new Rectangle(0, 60, 38, 30);75
if (bomb[i].bmpnum == 4)76
DrRect = new Rectangle(0, 98, 32, 30);77
if (bomb[i].bmpnum == 5)78
DrRect = new Rectangle(130, 0, 30, 30);79
if (bomb[i].bmpnum == 6)80
DrRect = new Rectangle(160, 0, 30, 30);81
if (bomb[i].bmpnum == 7)82
DrRect = new Rectangle(195, 0, 20, 30);83

84
curGame.SpriteBatch.Draw(Bomb1, bomb[i].posi, DrRect, Color.White);85

86
if (bomb[i].bmpnum > 7)87
bomb[i].isActive = false;88
}89
else90

{91
if (bomb[i].bmpnum == 0)92
DrRect = new Rectangle(0, 0, 200, 120);93
if (bomb[i].bmpnum == 1)94
DrRect = new Rectangle(0, 200, 200, 120);95
if (bomb[i].bmpnum == 3)96
DrRect = new Rectangle(0, 400, 200, 120);97
if (bomb[i].bmpnum == 4)98
DrRect = new Rectangle(0, 800, 200, 120);99
if (bomb[i].bmpnum == 5)100
DrRect = new Rectangle(200, 0, 200, 120);101
if (bomb[i].bmpnum == 6)102
DrRect = new Rectangle(200, 200, 200, 120);103

104
curGame.SpriteBatch.Draw(Bomb2, bomb[i].posi-new Vector2(100,60), DrRect, Color.White);105

106
if (bomb[i].bmpnum > 6)107
bomb[i].isActive = false;108
}109
} 110
}111
//timelost = 0;112

113
base.Draw(gameTime);114
}115

116
public void StartBom(Vector2 Posi, BombType type)117

{118
for (int i = 0; i < 5; i++)119

{120
if (!bomb[i].isActive)121

{122
bomb[i].posi = Posi;123
bomb[i].type = type;124
bomb[i].bmpnum = 0;125
bomb[i].isActive = true;126
break;127
}128
}129
}130
}
1
public struct Stone2

{3
public Vector2 position;4
public Vector2 direction;5
public float speed;6
public bool isActive;7
public Color[] StoneCol;8
//public Matrix RotationMatrix;9

10
public void Update()11

{12
position.X += (float)speed*direction.X;13
position.Y += (float)(direction.Y);14

15
if (position.Y > 600)16
isActive = false;17

18
if (position.X < 0)19
position.X += 820;20
if (position.X > 800)21
position.X -= 820;22
}23
}24

25
public class StoneCtrl : DrawableGameComponent26

{27
Texture2D stonetex;28
Rectangle stonerect;29
DesSpace curGame;30
Stone[] ston;31
float fTotalElapsedTime;32

33
public Texture2D StoneTex34

{35

set
{ stonetex = value; }36

get
{ return stonetex; }37
}38
public Stone[] Ston39

{40

get
{ return ston; }41

set
{ ston = value; }42
}43
public Rectangle StoneRect44

{45

set
{ stonerect = value; }46

get
{ return stonerect; }47
}48

49
public StoneCtrl(Game game): base(game)50

{51
curGame = (DesSpace)game;52
ston = new Stone[15];53
ResetStone();54
}55

56

57
public override void Update(GameTime gameTime)58

{59
float timeDelta = (float)gameTime.ElapsedGameTime.TotalSeconds;60
fTotalElapsedTime += timeDelta;61

62
float XStart, YStart, XDirect, YDirect;63
Random rnd = new Random();64

65
for (int i = 0; i < 15; i++)66

{67
ston[i].StoneCol = new Color[stonetex.Width * stonetex.Height];68
//stonetex.GetData(ston[i].StoneCol);69

70
if (curGame.plane.IsActive && Intersect.IntersectPixels(new Rectangle((int)ston[i].position.X, (int)ston[i].position.Y, stonetex.Width, stonetex.Height), new Rectangle((int)curGame.plane.PlanPosi.X, (int)curGame.plane.PlanPosi.Y, curGame.plane.PlanTex.Width, curGame.plane.PlanTex.Height)))71

{72
curGame.bomb.StartBom(curGame.plane.PlanPosi,BombType.PlaneBomb);73
ston[i].isActive = false;74
curGame.plane.IsActive = false;75
curGame.GameScore.deathtimes++;76
}77
ston[i].Update();78
}79

80
for (int i = 0; i < 15; i++)81

{82
if (!ston[i].isActive)83

{84
double angle = rnd.NextDouble() * MathHelper.PiOver4/2;85
switch (rnd.Next(4))86

{ 87
case 0:88
XStart = 120f;89
YStart = -50f;90
XDirect = -(float)Math.Sign(angle);91
break;92
case 1:93
XStart = 380f;94
YStart = -50f;95
XDirect = (float)Math.Sign(angle);96
break;97
case 2:98
XStart =460f;99
YStart = -50f;100
XDirect = -(float)Math.Sign(angle);101
break;102
case 3:103
XStart = 630f;104
YStart = -50f;105
XDirect =(float)Math.Sign(angle);106
break;107
default:108
XStart = 720f;109
YStart = -50f;110
XDirect = -(float)Math.Sign(angle);111
break;112
}113
YDirect = (float)Math.Cos(angle);114

115
ston[i].position = new Vector2(XStart, YStart);116
ston[i].direction = new Vector2(XDirect, YDirect);117
ston[i].speed = 1f+(float)rnd.NextDouble() * 1.5F;118
ston[i].isActive = true;119

120
break;121
}122
}123

124
fTotalElapsedTime = 0;125
base.Update(gameTime);126
}127

128
public override void Draw(GameTime gameTime)129

{130
Random rnd=new Random();131
for (int i = 0; i < 15; i++)132

{133
if (ston[i].isActive)134
curGame.SpriteBatch.Draw(stonetex, ston[i].position, Color.White);135
}136
base.Draw(gameTime);137
}138

139
private void ResetStone()140

{141
float XStart, YStart, XDirect, YDirect;142

143
Random rnd = new Random();144
for (int i = 0; i < 15; i++)145

{146
double angle = rnd.NextDouble() * MathHelper.PiOver2;147
switch (rnd.Next(4))148

{149
case 0:150
XStart = 50f;151
YStart = -50f;152
XDirect = -(float)Math.Sign(angle);153
break;154
case 1:155
XStart =230f;156
YStart = -50f;157
XDirect = (float)Math.Sign(angle);158
break;159
case 2:160
XStart = 420f;161
YStart = -50f;162
XDirect = -(float)Math.Sign(angle);163
break;164
case 3:165
XStart = 660f;166
YStart = -50f;167
XDirect = (float)Math.Sign(angle);168
break;169
default:170
XStart = 780f;171
YStart = -50f;172
XDirect = -(float)Math.Sign(angle);173
break;174

175
}176
YDirect = (float)Math.Cos(angle);177

178
ston[i].position = new Vector2(XStart, YStart);179
ston[i].direction = new Vector2(XDirect, YDirect);180
ston[i].speed = 1f + (float)rnd.NextDouble() * 1.5F;181
ston[i].isActive = true;182
}183
}184
}
1
public struct Score2

{3
public int sendbullet;4
public int deathtimes;5
public int atticstone;6
}
1
public class Plane : DrawableGameComponent2

{3
private DesSpace curGame;4
private Texture2D plantex;5
private Vector2 planposi;6
private KeyboardState Kes;7
private bool isActive;8

9
public bool IsActive10

{11

get
{ return isActive; }12

set
{ isActive = value; }13
}14
public Vector2 PlanPosi15

{16

get
{ return planposi; }17
}18
public Texture2D PlanTex19

{20

get
{ return plantex; }21

set
{ plantex = value; }22
}23

24
public Plane(Game game) : base(game)25

{26
curGame = (DesSpace)game;27
isActive = true;28
planposi = new Vector2((float)400, (float)500-(float)20);29
}30
31
public override void Update(GameTime gameTime)32

{33
Kes = Keyboard.GetState();34

35
if (!isActive && Kes.IsKeyDown(Keys.R))36

{37
planposi = new Vector2((float)400, (float)500 - (float)20);38
isActive = true;39
curGame.bull.IsActive = true;40
}41
else42

{43
if (Kes.IsKeyDown(Keys.Right) && planposi.X < 800f - plantex.Width / 2)44
planposi.X += 3.5f;45

46
if (Kes.IsKeyDown(Keys.Left) && planposi.X > 0.0f - plantex.Width / 2)47
planposi.X -= 3.5f;48

49
if (Kes.IsKeyDown(Keys.Up) && planposi.Y > 0.0f)50
planposi.Y -= 3f;51
if (Kes.IsKeyDown(Keys.Down) && planposi.Y < 600.0f - plantex.Height / 2)52
planposi.Y += 3f;53
}54

55
base.Update(gameTime);56
}57

58
public override void Draw(GameTime gameTime)59

{60
if (isActive)61
curGame.SpriteBatch.Draw(plantex, planposi, Color.White);62

63
base.Draw(gameTime);64
}65
}
1
static class Intersect2

{3
public static bool IntersectPixels(Rectangle rectangleA, Rectangle rectangleB)4

{5
return rectangleA.Intersects(rectangleB);6

7
}8
}
1
struct Bullet //子弹2

{3
public float bullspeed; //子弹速度4
public Vector2 Posi;5
public bool isActive;6
public Color[] BullCol;7

8
public void update()9

{10
Posi -= new Vector2(0.0f, bullspeed);11
if (Posi.Y <= 0)12
isActive = false;13
}14
} 15

16
public class BulletCtrl :DrawableGameComponent17

{18
private DesSpace curGame;19
private Texture2D bulltex;20
private bool isActive;21

22
KeyboardState lastkeys = Keyboard.GetState();23

24
Bullet[] bull = new Bullet[15];25

26
public Texture2D BullTex27

{28

set
{ bulltex = value; }29
}30
public bool IsActive31

{32

get
{ return isActive; }33

set
{ isActive = value; }34
}35
public BulletCtrl(Game game): base(game)36

{37
curGame = (DesSpace)game;38
isActive = true;39
}40

41
private void InputCtrl()42

{43
KeyboardState kes = Keyboard.GetState();44

45
if (kes.IsKeyDown(Keys.Space) && lastkeys.IsKeyUp(Keys.Space) && curGame.plane.IsActive)46

{47
curGame.GameScore.atticstone--;48
for (int i=0; i < 15; i++)49

{50
if (!bull[i].isActive)51

{52
bull[i].Posi = curGame.plane.PlanPosi + new Vector2(31,0);53
if(curGame.level==1)54
bull[i].bullspeed= 5.0f;55
if (curGame.level == 2)56
bull[i].bullspeed = 8.0f;57
if (curGame.level == 3)58
bull[i].bullspeed = 12.0f;59
bull[i].isActive = true;60
break;61
}62
}63
}64
lastkeys = kes;65
}66

67
public override void Update(GameTime gameTime)68

{69
InputCtrl();70

71
for (int i = 0; i < 15; i++)72

{73
if (bull[i].isActive)74
bull[i].update();75
//冲突检测76
for (int j = 0; j < 15; j++)77

{78
bull[i].BullCol = new Color[bulltex.Height * bulltex.Width];79
bulltex.GetData(bull[i].BullCol);80

81

82
if (bull[i].isActive && Intersect.IntersectPixels(new Rectangle((int)bull[i].Posi.X, (int)bull[i].Posi.Y, 8, 8), new Rectangle((int)curGame.stone.Ston[j].position.X, (int)curGame.stone.Ston[j].position.Y, curGame.stone.StoneTex.Width, curGame.stone.StoneTex.Height)))83

{84
bull[i].isActive = false;85
curGame.stone.Ston[j].isActive = false;86
curGame.GameScore.atticstone+=5;87
curGame.bomb.StartBom(bull[i].Posi, BombType.StoneBomb);88
}89
}90
}91

92
93

94
base.Update(gameTime);95
}96

97
public override void Draw(GameTime gameTime)98

{99
for (int i = 0; i < 15; i++)100

{101
if (bull[i].isActive)102

{103
if (curGame.level == 1)104
curGame.SpriteBatch.Draw(bulltex, bull[i].Posi, new Rectangle(0, 0, 8, 8), Color.White);105
if (curGame.level == 2)106
curGame.SpriteBatch.Draw(bulltex, bull[i].Posi - new Vector2(3, 0), new Rectangle(0, 0, 16, 8), Color.White);107
if (curGame.level == 3)108
curGame.SpriteBatch.Draw(bulltex, bull[i].Posi-new Vector2(5,0), new Rectangle(0, 0, 24, 8), Color.White);109
}110
111
}112
base.Draw(gameTime);113
}114


浙公网安备 33010602011771号