天天看點

Silverlight C# 遊戲開發:Flyer06小小的改進讓遊戲更有趣

今天這套主題,僅僅是通過改進讓遊戲更加有趣,遊戲中的細節永遠是耐人尋味,有的遊戲團隊為此付諸努力甚至成為整個項目的成功關鍵。

我們将在本次完成加血、背景、更加完美的碰撞,讓遊戲變得更加有趣。

首先改進碰撞範圍,對于主角來說,并不需要完全的碰撞,而隻有身體的部分,是以将Rect設定的小一點,例如Width = 32, Height = 36

<a target="_blank" href="http://blog.51cto.com/attachment/201111/161754429.jpg"></a>

我們再加上一個食物類,讓遊戲中有加血的方法,具體效果如下:

<a target="_blank" href="http://blog.51cto.com/attachment/201111/161807502.jpg"></a>

食物類的代碼如下:

代碼

public class ClassFood : Canvas

{

private Rectangle _rectangle = new Rectangle()

{ Width = 32, Height = 32, Stroke = new SolidColorBrush(Colors.Green) };

Image Food;

ImageSource[] FoodFrames = new ImageSource[4];

public ClassFood()

Food = new Image();

BitmapImage bitmap = new BitmapImage(new Uri(@"Src/food.png", UriKind.Relative));

bitmap.ImageOpened += new EventHandler&lt;RoutedEventArgs&gt;(bitmap_ImageOpened);

Food.Source = bitmap;

this.Children.Add(Food);

this.Children.Add(_rectangle);

}

private static int _framecont = 0;

void bitmap_ImageOpened(object sender, RoutedEventArgs e)

Food.Source = sender as BitmapImage;

for (int i = 0; i &lt; 4; i++)

WriteableBitmap wb = new WriteableBitmap(32, 32);

wb.Render(Food, new TranslateTransform() { X = -32 * i, Y = 0 });

wb.Invalidate();

FoodFrames[i] = (ImageSource)wb;

Food.Source = FoodFrames[_framecont];

_framecont++;

if (_framecont &gt;= 4)

_framecont = 0;

/// &lt;summary&gt;

/// 移動速度

/// &lt;/summary&gt;

public double Speed = 1;

public double X

get { return Canvas.GetLeft(this); }

set { Canvas.SetLeft(this, value); }

public double Y

get { return Canvas.GetTop(this); }

set { Canvas.SetTop(this, value); }

public Rect MyRect

get

return new Rect(X, Y, _rectangle.Width, _rectangle.Height);

可以看出,這個部分的代碼和固體類有很接近的地方,是以我們也同樣的寫一個組,來管理和建立整個食物動畫

public class ClassFoodGroup :Canvas

Random _random = new Random((int)DateTime.Now.Ticks);

public ClassFoodGroup(int max)

int segment = (int)(MainPage.ScreenWidth - 64) / max * 2;

for (int i = 0; i &lt; max; i++)

ClassFood food = new ClassFood();

this.Children.Add(food);

food.X = _random.Next(segment * i, segment * (i + 1));

food.Y = _random.Next((int)MainPage.ScreenHeight - 64) + MainPage.ScreenHeight;

DispatcherTimer dispatcherTimer = new DispatcherTimer();

dispatcherTimer.Tick += new EventHandler(TickGameFrameLoop);

dispatcherTimer.Interval = TimeSpan.FromMilliseconds(10); //重複間隔

dispatcherTimer.Start();

private void TickGameFrameLoop(object sender, EventArgs e)

foreach (UIElement s in this.Children)

if (s is ClassFood)

ClassFood cloud = (s as ClassFood);

if (cloud.Y &gt;= -32)

cloud.Y -= cloud.Speed;

else

cloud.Y = _random.Next(400 - 64) + 400;

然後加入到遊戲的主循環當中,進行檢測判定:

foreach (ClassFood food in foodgroup.Children)

Rect rt = food.MyRect;

rt.Intersect(herorect);

if (!double.IsInfinity(rt.Height) &amp;&amp; !double.IsInfinity(rt.Width))

flyerlife.Add(10);

food.Y = -32;

break;

好了,F5運作一下,看看效果,相信你會覺得有點意思了,當然了,其他部分的代碼需要工程支援,還是先下載下傳代碼直接看吧:)

還有一些小細節需要增加,比如背景,背景天空的加入可以讓遊戲更加豐滿,但是背景是有一定的要求的,它肯定不可能一直在上升,于是我們搞了一個背景類來處理内部邏輯。

public class ClassBackGroup:Canvas

//天空的高度,在這裡順便了解一下在Silverlight裡取得圖檔的寬高

private int skyHeight = 1000;

public ClassBackGroup()

Image _Image = new Image();

BitmapImage bitmap = new BitmapImage(new Uri(@"Src/sky.jpg", UriKind.Relative)) ;

_Image.Source = bitmap;

this.Children.Add(_Image);

dispatcherTimer.Tick += new EventHandler(TickBackGroupLogic);

dispatcherTimer.Interval = TimeSpan.FromMilliseconds(40); //重複間隔

//在這個完成事件中可以取得bitmap的高度,同樣寬度也可以用類似的方式

skyHeight = (sender as BitmapSource).PixelHeight;

public void TickBackGroupLogic(object sender, EventArgs e)

//如果移動大于螢幕減去天空的高度,那麼就不能再動了

if (Y&lt; 400 - skyHeight)

Y -= Speed;

public double Speed = 0.5;

最後做一點點的修改,将輔助的線條全部隐蔽,運作效果就是如下了:

<a target="_blank" href="http://blog.51cto.com/attachment/201111/161829883.jpg"></a>

基本上已經出來一個遊戲的大概形狀,在這個基礎上,可以做一些修改就變成了更加豐富的遊戲,預計在Flyer07就結束這套純用代碼實作的Silverlight小遊戲。

<a href="http://files.cnblogs.com/nowpaper/SL_FlyerGame5.rar">本Flyer06的源代碼在這裡下載下傳。</a>

本文轉自nowpaper 51CTO部落格,原文連結:http://blog.51cto.com/nowpaper/712583