silverlight2 游戏 1 你能坚持多少秒
2009-05-15 12:08:26 来源:WEB开发网本文示例源代码或素材下载
这是开始学习silverlight的第一个游戏
游戏截图:
click onto the red block with jump over it:
鼠标移动到红色部分。
move the red block avoiding crash with the blue ones
拖动红块,避免碰到蓝色部分。
don't touch the black edge.
不要碰到黑色区域。
if you can reach more than 18 seconds, you are a genius.
如果你能达到18秒以上,你算得上是个天才。
1、游戏管理类
GameManager.cs
1using System;
2using System.Net;
3using System.Windows;
4using System.Windows.Controls;
5using System.Windows.Documents;
6using System.Windows.Ink;
7using System.Windows.Input;
8using System.Windows.Media;
9using System.Windows.Media.Animation;
10using System.Windows.Shapes;
11using System.Windows.Threading;
12using System.Collections.Generic;
13
14namespace Movebox
15{
16 public class GameManager
17 {
18 /**//// <summary>
19 /// 游戏状态(Ready, Play, Pause, Over)
20 /// </summary>
21 ///
22 public GameStatus GameStatus { get; set; }
23
24 /**//// <summary>
25 /// 移动速度
26 /// </summary>
27 public double MoveSpeed { get; set; }
28
29 private double initSpeed = 13; // 初始速率(毫秒)
30
31 private DispatcherTimer timer;
32
33 public List<Enemy> enemies; //敌方
34
35 private Canvas layoutRoot;
36 private Owner owner; //玩家
37
38 public GameManager(Canvas _layoutRoot, Owner _owner)
39 {
40 enemies = new List<Enemy>();
41 this.layoutRoot = _layoutRoot;
42 this.owner = _owner;
43
44 Enemy enemy1 = new Enemy();
45 enemy1.X = 70; //初始化位置
46 enemy1.Y = 70;
47 enemy1.ContainerWidth = (int)_layoutRoot.Width;
48 enemy1.ContainerHeight = (int)_layoutRoot.Height;
49 enemies.Add(enemy1);
50
51 Enemy enemy2 = new Enemy();
52 enemy2.X = 270; //初始化位置
53 enemy2.Y = 60;
54 enemy2.Dimension(60, 50);
55 enemy2.ContainerWidth = (int)_layoutRoot.Width;
56 enemy2.ContainerHeight = (int)_layoutRoot.Height;
57 enemies.Add(enemy2);
58
59 Enemy enemy3 = new Enemy();
60 enemy3.X = 70; //初始化位置
61 enemy3.Y = 320;
62 enemy3.Dimension(30, 60);
63 enemy3.ContainerWidth = (int)_layoutRoot.Width;
64 enemy3.ContainerHeight = (int)_layoutRoot.Height;
65 enemies.Add(enemy3);
66
67 Enemy enemy4 = new Enemy();
68 enemy4.X = 300; //初始化位置
69 enemy4.Y = 330;
70 enemy4.Dimension(100, 20);
71 enemy4.ContainerWidth = (int)_layoutRoot.Width;
72 enemy4.ContainerHeight = (int)_layoutRoot.Height;
73 enemies.Add(enemy4);
74
75 timer = new DispatcherTimer();
76 timer.Interval = TimeSpan.FromMilliseconds(initSpeed);
77 timer.Tick += new EventHandler(timer_Tick);
78
79 GameStatus = GameStatus.Ready;
80
81 }
82
83 /**//// <summary>
84 /// 开始游戏(启动计时器)
85 /// </summary>
86 public void Play()
87 {
88 GameStatus = GameStatus.Play;
89 timer.Start();
90
91 }
92
93 /**//// <summary>
94 /// 暂停游戏(停止计时器)
95 /// </summary>
96 public void Pause()
97 {
98 timer.Stop();
99 GameStatus = GameStatus.Pause;
100
101 }
102
103 /**//// <summary>
104 /// 开始运行
105 /// </summary>
106 /// <param name="sender"></param>
107 /// <param name="e"></param>
108 private void timer_Tick(object sender, EventArgs e)
109 {
110 OnGrameRun(null);
111 foreach (Enemy enemy in enemies)
112 MoveEnemy(enemy);
113 }
114
115 /**//// <summary>
116 /// 移动敌方
117 /// </summary>
118 public void MoveEnemy(Enemy enemy)
119 {
120 if (GameStatus != GameStatus.Play) return;
121
122 if (!IsBoundary(enemy))
123 {
124 enemy.Move = true;
125 enemy.Speed = MoveSpeed;
126 }
127 else
128 {
129 OnGameOver(null);
130 this.Pause();
131 }
132 }
133
134 /**//// <summary>
135 /// 是否敌方与玩家撞击
136 /// </summary>
137 /// <param name="x"></param>
138 /// <param name="y"></param>
139 /// <param name="xw"></param>
140 /// <param name="yh"></param>
141 /// <returns></returns>
142 private bool IsBoundaryForOwner(double x, double y, double xw, double yh)
143 {
144 bool bCollision = false;
145 double getpointx = owner.X - Convert.ToInt32(x + xw);
146 double getpointy = owner.Y - Convert.ToInt32(y + yh);
147 if ((getpointy <= 0 && y <= (owner.Y + owner.Height)) && (getpointx <= 0 && x <= (owner.X + owner.Width)))
148 {
149 bCollision = true;
150 }
151 return bCollision;
152 }
153
154 /**//// <summary>
155 /// 是否撞击
156 /// </summary>
157 /// <returns></returns>
158 private bool IsBoundary(Enemy enemy)
159 {
160 return IsBoundaryForOwner(enemy.X, enemy.Y, enemy.Width, enemy.Height);
161 }
162
163
164 public event EventHandler GameOver;
165 public event EventHandler GrameRun;
166
167 private void OnGrameRun(EventArgs e)
168 {
169 EventHandler handler = GrameRun;
170 if (handler != null)
171 handler(this, e);
172 }
173
174 /**//// <summary>
175 /// 游戏结束后所调用的方法,并触发游戏结束事件
176 /// </summary>
177 /// <param name="e"></param>
178 private void OnGameOver(EventArgs e)
179 {
180 GameStatus = GameStatus.Over;
181 timer.Interval = TimeSpan.FromMilliseconds(initSpeed);
182 timer.Stop();
183
184 EventHandler handler = GameOver;
185 if (handler != null)
186 handler(this, e);
187 }
188
189 }
190}
191
Tags:silverlight 游戏 坚持
编辑录入:爽爽 [复制链接] [打 印]- ››silverlight全屏显示图片
- ››Silverlight MVVM 模式(一) 切近实战
- ››Silverlight for Windows Phone 7开发系列(1):...
- ››Silverlight for Windows Phone 7开发系列(2):...
- ››Silverlight for Windows Phone 7开发系列(3):...
- ››Silverlight for Windows Phone 7开发系列(4):...
- ››游戏定位成牵引Android发展的火车头
- ››Silverlight for Symbian
- ››游戏场景烘焙贴图制作
- ››游戏植物大战僵尸 iPhone 版9天销售额超100万美元...
- ››坚持功能完善 美图秀秀2.1.7发布
- ››游戏开发包 iPhone cocos2d 初体验
更多精彩
赞助商链接