XNA 做一个水果机的游戏

简介: XNA下在电脑里也有一年了,不过只是刚刚下载来时看看,前两天突然想做个小游戏,于是又重新安装了XNA的3.0CTP 看了看,比C++/DirectX的流程自然是简单许多,做出来的游戏也很是流畅,不必去担心API也不用去想复杂的效果实现细节 只要将算法搞好,坐标搞对,就成了。

XNA下在电脑里也有一年了,不过只是刚刚下载来时看看,前两天突然想做个小游戏,于是又重新安装了XNA的3.0CTP


看了看,比C++/DirectX的流程自然是简单许多,做出来的游戏也很是流畅,不必去担心API也不用去想复杂的效果实现细节

只要将算法搞好,坐标搞对,就成了。

 

总体来看XNA还是不错

下载地址

 http://www.microsoft.com/downloads/details.aspx?FamilyId=DF4AF56A-58A7-474C-BFD0-7CF8ED3036A3&displaylang=en

首先我们来建立一个最基本的XNA程序

安装完成后,应该在"新建,项目" 的菜单中就会多出XNA Studio 3.0的项目点“新建Windows Game”新建即可

 

一个全新的项目,基本包含一个启动类即Programe.cs

内容是

static   class  Program {
        
static   void  Main( string [] args) {
            
using  (SheepYangGame game  =   new  SheepYangGame()) {
                game.Run();
            }
        }
    }

 意为启动SheepYangGame(我自己定义的游戏)这个游戏类

 

namespace  SheepYangWin {
    
public   class  SheepYangGame : Microsoft.Xna.Framework.Game {
                SpriteBatch spriteBatch;
// 一个精灵(sprite)组(毛叫精灵呢,其实游戏中是个可视的东西就叫精灵,物品 ,NPC 角色)
                GraphicsDeviceManager graphics; // 图形设备管理器
         protected   override   void  Initialize() {
            
// 初始化方法
             base .Initialize();
        }
                
protected   override   void  LoadContent() {
// 载入Content的方法,什么叫Content呢,所有资源 ,比如图片,字体,粒子效果模型啥的
            spriteBatch  =   new  SpriteBatch(GraphicsDevice);
        }
        
public  SheepYangGame() { // 构造函数,在这里也能进行一些初始化
            graphics  =   new  GraphicsDeviceManager( this );
            Content.RootDirectory 
=   " Content " ;
        }
        
protected   override   void  UnloadContent() { // 这个就不用说了吧
            
//  TODO: Unload any non ContentManager content here
        }
        
protected   override   void  Update(GameTime gameTime) {
// 游戏定时触发的事件,其实就是游戏的消息响应函数,按键判断什么的都在这里
             if  (GamePad.GetState(PlayerIndex.One).Buttons.Back  ==  ButtonState.Pressed)
                
this .Exit();
            
base .Update(gameTime);
        }
                
protected   override   void  Draw(GameTime gameTime) {
// 绘制函数,每个Update执行后都应该执行这个
            graphics.GraphicsDevice.Clear( new  Color( 0x29 0x84 0xad 50 )); // 这个是我 喜欢的背景色

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            spriteBatch.End();


            
this .graphics.GraphicsDevice.Present();

            
base .Draw(gameTime);
        }
    }

 

 算了,原打算分开写,现在一起写在一篇 中吧

下面定义一些Model

 分数的类:

 

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif Code
using System;
using System.Collections.Generic;

using System.Text;

namespace SheepYangWin {
    
/// <summary>
    
/// 分类的类
    
/// </summary>
    public class Score {
        
public int Value {
            
get;
            
set;
        }
        
public Score() {
            Value 
= 0;
        }
        
/// <summary>
        
/// 得分
        
/// </summary>
        
/// <param name="x"></param>
        public void Add(int x) {//得分
            this.Value += x;
        }
        
/// <summary>
        
/// 失分
        
/// </summary>
        
/// <param name="x"></param>
        public void Remove(int x) { this.Value -= x; }

    }
}

 

对应的水果的类(上面一圈的小方块)

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif Code
using System;
using System.Collections.Generic;

using System.Text;
using System.Collections;
using Microsoft.Xna.Framework;


namespace SheepYangWin {
    
public class Elem {
        
/// <summary>
        
/// 几倍
        
/// </summary>
        public int Multiple { getset; }
        
/// <summary>压下的分
        
/// 
        
/// </summary>
        public Score UsingScore { getset; }
        
/// <summary>
        
/// 起点坐标
        
/// </summary>
        public Point Position { getset; }

        
public bool IsBig { getset; }
        
        
public Elem(int x, Score y,bool i,Point p) {
            
this.Multiple = x;
            
this.UsingScore = y;
            
this.Position = p;
            
this.IsBig = i;
        }


    }
}

 

上面一圈的类,我用一个队列模拟一个循环链表

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif Code
using System;
using System.Collections.Generic;

using System.Text;
using System.Collections;

namespace SheepYangWin {
    
/// <summary>
    
/// 一圈的类
    
/// </summary>
    public class QueueList {
    
        
public Queue<Elem> List { getset; }
        
public QueueList(params Elem[] x) {
            List 
= new Queue<Elem>();
            
foreach (Elem e in x)
                List.Enqueue(e);
        }
        
public Elem Next() {
            Elem e 
= List.Dequeue();
            List.Enqueue(e);
            
return e;
        }
    }
}

 


好了,其它的类大家可以看看源码,现在我说一下显示的问题,这个应该是游戏的核心,但也是最没有变化的部分

拿背景图片为例

说明一下图片的显示

 先搞一个背景图片,命名为Backs.png

如下图

 

Copy到Content文件夹中,在工程 中对Content右键,添加已经存在的文件,选中Backs.png加入

这样Backs.png就已经被当做资源载入了,但是要将他载入游戏还要以下几步

 

  1.  声明一个Texture2D 类型的对象 background;Texture2D这个类就可以看做是2D的图形精灵的类
  2. 在LoadContent() 中将background与图片对应,写
    protected   override   void  LoadContent() {
                background 
    =  Content.Load < Texture2D > ( " backs " );
                spriteBatch 
    =   new  SpriteBatch(GraphicsDevice);
            }
  3. 将之画在窗体上
         protected   override   void  Draw(GameTime gameTime) {
                graphics.GraphicsDevice.Clear(
    new  Color( 0x29 0x84 0xad 50 ));
                spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
                spriteBatch.Draw(background, 
    new  Vector2( 0 , 0 ), Color.White); // 这句,这个Vector2是个2D的向量,表示的是图形左上在窗体上的坐标
                spriteBatch.End();
                
    this .graphics.GraphicsDevice.Present();
                
    base .Draw(gameTime);
            }
  4. 这样就将图形显示在游戏上了

再说一下文字的显示

这个要稍微复杂一点,比如我要显示总分(RightScore.Value里保存)

  1. 对Content右键,新建,一个SpriteFont(译为精灵字体。。。不通,反正就是字体的配置文件)注意一定要有,不然就无法显示,新建好名为SpriteFont1.spritefont的文件其内容如下
    img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif Code
    <?xml version="1.0" encoding="utf-8"?>
    <!--
    这个就是配置文件
    -->
    <XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
      
    <Asset Type="Graphics:FontDescription">

        
    <!--
    字体
        
    -->
        
    <FontName>新宋体</FontName>

        
    <!--
    字号
        
    -->
        
    <Size>24</Size>
        
    <Spacing>0</Spacing>
        
    <UseKerning>true</UseKerning>
        
    <Style>Regular</Style>
        
    <!--
    字符规则,编码从几到几这里只是英文和数字什么的
        
    -->
        
    <CharacterRegions>
          
    <CharacterRegion>
            
    <Start>&#32;</Start>
            
    <End>&#126;</End>
          
    </CharacterRegion>
        
    </CharacterRegions>
      
    </Asset>
    </XnaContent>
  2. 声明一对象SpriteFont ScoreFont;与Textture差不多,是用来显示文字的,当然,也要在LoadContent下(我只写了一句,其实是要和上面的放在一起):
    protected   override   void  LoadContent() {
                ScoreFont 
    =  Content.Load < SpriteFont > ( " SpriteFont1 " );
                spriteBatch 
    =   new  SpriteBatch(GraphicsDevice);
            }
  3. Draw里也要(当然Textture的那 句也是还在,只是这里没有写)
    img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif Code
        protected override void Draw(GameTime gameTime) {
                graphics.GraphicsDevice.Clear(
    new Color(0x290x840xad50));
                spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
                spriteBatch.DrawString(ScoreFont, RightScore.Value.ToString(), 
    new Vector2(1641), Color.White);
                spriteBatch.End();
                
    this.graphics.GraphicsDevice.Present();
                
    base.Draw(gameTime);
            }

 于是就产生了以下效果

 

 

 

也就是文字与图片的显示了

 

键盘控制,看看我的代码吧,写的累了还要赶车

 http://files.cnblogs.com/chsword/SheepYangWin.rar



目录
相关文章
|
图形学
Unity3D教程:回合制游戏实现
一、场景布置 首先是简单的场景布置,在3D部分很简单。就几个简单的基本组件,在一个Plane上面放2个Cube,并且上不同颜色的纯色Material。唯一需要大家注意的是,请将两个Cube改好名,以免到时候编程不知道哪个跟哪个。
1652 0

热门文章

最新文章