Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Trang 1 của 5 123 ... CuốiCuối
Kết quả 1 đến 10 của 46
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Tạo game bóng nẩy đơn giản cho Windows Phone 7

    <để thấy được hình ảnh cần đăng nhập>

    mục tiêu là làm 1 game bóng nẩy ,chạm vào thành là bật lại. Game chạy trên hệ điều hành window phone 7.

    preview :



    1.chuẩn bị
    -kiến thức về lập trình C#
    -kiến thức về XNA framework
    -Visual studio 2010
    - window phone tool develop, cài cho VS2010 ,download ở trang chủ Microsoft

    2. tạo project
    -sau khi cài window phone tool cho VS2010 ,bộ tool này đã bao gồm emulator,XNA,silvelight. sau khi cài chúng sẽ được bổ sung vào menu new project của VS2010
    -tạo project window phone game



    thêm 1 số tài nguyên cần thiết



    -images: add bình thường
    -font : bạn chuột phải chọn add->new item->chọn spritefont->điền tên ->OK
    bạn có thể mở file *.font để thay đổi kích cỡ,kiểu font

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    3. Code

    Tạo đối tượng ball.cs


    Mã:
    using System;using System.Collections.Generic;using System.Linq;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.GamerServices;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Input.Touch;using Microsoft.Xna.Framework.Media;  namespace Zstar_Ball_Game{    public class Ball    {        private GraphicsDeviceManager graphics;        private Texture2D texture;//sprite bóng        private Vector2 location;//vị trí của bóng        private Point size = new Point(48, 48);//kích thước của bóng        private SpriteFont font;//font để vẽ chữ        private Vector2 direction;//hướng chuyển động của bóng        private float h = 0.5f;//bước chuyển động        public Ball(GraphicsDeviceManager g, Random rand)        {            this.graphics = g;            this.location = new Vector2(rand.Next(200), rand.Next(200));//random ngẫu nhiên vị trí bóng            this.direction = new Vector2(rand.Next(1, 3), rand.Next(1, 3));//random ngẫu nhiên hướng chuyển động            this.h = rand.Next(1, 5) * rand.Next(1, 3) * 0.5f;//random ngẫu nhiên bước chuyển động        }        public void LoadContent(ContentManager content, string path)        {            this.texture = content.Load<Texture2D>(path);            this.font = content.Load<SpriteFont>("Font/SpriteFont1");        }        public void Update()        {            MouseState mouse = Mouse.GetState();            if (mouse.LeftButton == ButtonState.Pressed)            {                this.Press = true;            }            else this.Press = false;            if (this.Press)            {                if (mouse.X > 0 && mouse.Y > 0 && mouse.X + size.X <= this.graphics.PreferredBackBufferWidth && mouse.Y + size.Y <= this.graphics.PreferredBackBufferHeight)                    this.Location = new Vector2(mouse.X, mouse.Y);            }            //di chuyển            this.location += this.direction * h;            if (!outside())            {                if (location.X * location.Y < 0) this.location = new Vector2();            }        }        public bool outside()        {            if (this.Location.X < 0)            {                this.direction = new Vector2(-this.direction.X, this.direction.Y);                return true;            }            if (this.Location.Y < 0)            {                this.direction = new Vector2(this.direction.X, -this.direction.Y); return true;            }            if (this.Location.X + this.size.X > this.graphics.PreferredBackBufferWidth)            {                this.direction = new Vector2(-this.direction.X, this.direction.Y); return true;            }             if (this.Location.Y + this.size.Y > this.graphics.PreferredBackBufferHeight)            {                this.direction = new Vector2(this.direction.X, -this.direction.Y);                return true;            } return false;        }        public void Draw(SpriteBatch spriteBatch)        {            spriteBatch.Draw(this.texture, this.location, Color.White);            //spriteBatch.DrawString(this.font, this.location.ToString(), this.location + new Vector2(0, -20), Color.White);        }        public Vector2 Location        {            get { return this.location; }            set { this.location = value; }        }        public bool Press        {            get;            set;        }     }}
    tạo đôi tượng BallManager.cs
    mục đích của lớp này là quản lí bóng cho dễ,có thể thêm nhiều bóng rất dễ dàng


    Mã:
    using System;using System.Collections.Generic;using System.Linq;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.GamerServices;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Input.Touch;using Microsoft.Xna.Framework.Media; namespace Zstar_Ball_Game{    public class BallManager    {        private Random rand = new Random();        private Ball[] balls;        private int num = 6;        private string[] paths = new string[] { "Images/ball4", "Images/ball2", "Images/ball3" };        public BallManager(GraphicsDeviceManager graphics, Random rand)        {            balls = new Ball[num];            for (int i = 0; i < balls.Length; i++)            {                balls[i] = new Ball(graphics, rand);            }        }        public void LoadContent(ContentManager content)        {            for (int i = 0; i < balls.Length; i++)            {                balls[i].LoadContent(content, paths[rand.Next(paths.Length)]);            }        }        public void Draw(SpriteBatch spriteBatch)        {            for (int i = 0; i < balls.Length; i++)            {                balls[i].Draw(spriteBatch);            }        }        public void Update()        {            for (int i = 0; i < balls.Length; i++)            {                balls[i].Update();            }        }    }}
    code ở Game1.cs

    Mã:
    using System;using System.Collections.Generic;using System.Linq;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.GamerServices;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Input.Touch;using Microsoft.Xna.Framework.Media; namespace Zstar_Ball_Game{    /// <summary>    /// This is the main type for your game    /// </summary>    public class Game1 : Microsoft.Xna.Framework.Game    {        private GraphicsDeviceManager graphics;        private SpriteBatch spriteBatch;        private BallManager ballManager;        private SpriteFont font;        private string s = "Hello Zstar !
    Welcome to Window phone OS Emulator";        private Random rand = new Random();        public Game1()        {            graphics = new GraphicsDeviceManager(this);            Content.RootDirectory = "Content";            this.ballManager = new BallManager(graphics, rand);            s += "
    Screen : (" + this.graphics.PreferredBackBufferWidth + "," + this.graphics.PreferredBackBufferHeight + ")";                      TargetElapsedTime = TimeSpan.FromTicks(333333);        }          protected override void Initialize()        {                       base.Initialize();        }         protected override void LoadContent()        {                       spriteBatch = new SpriteBatch(GraphicsDevice);            this.ballManager.LoadContent(Content);            this.font = Content.Load<SpriteFont>("Font/SpriteFont1");        }         protected override void UnloadContent()        {         }         protected override void Update(GameTime gameTime)        {                       if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)                this.Exit();            this.ballManager.Update();                                base.Update(gameTime);        }          protected override void Draw(GameTime gameTime)        {            GraphicsDevice.Clear(Color.DarkCyan);             // TODO: Add your drawing code here            spriteBatch.Begin();            this.ballManager.Draw(spriteBatch);            spriteBatch.DrawString(font, s, new Vector2(), Color.Orange);            spriteBatch.End();            base.Draw(gameTime);        }           }}
    sau đó F5, chạy thử ta được kết quả :
    video demo : http://www.youtube.com/watch?v=Ji5sOrayM_M


  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    như vậy là chúng ta có 3 lớp Ball.cs,BallManager.cs,Game1.cs

    sau đây là phần giải thích chi tiết

    1. hàm update trong ball.cs
    -ý của tôi là khi nhấn "chuột trái" (chuột trái ở emulator trên computer tương đương nhấn tay vào màn hình touch trên window phone 7) thì các bóng sẽ được set lại vị trí tại chuột


    Mã:
    public void Update()        {            MouseState mouse = Mouse.GetState();            if (mouse.LeftButton == ButtonState.Pressed)            {                this.Press = true;            }            else this.Press = false;            if (this.Press)            {                if (mouse.X > 0 && mouse.Y > 0 && mouse.X + size.X <= this.graphics.PreferredBackBufferWidth && mouse.Y + size.Y <= this.graphics.PreferredBackBufferHeight)                    this.Location = new Vector2(mouse.X, mouse.Y);            }            //di chuyển            this.location += this.direction * h;            if (!outside())            {                if (location.X * location.Y < 0) this.location = new Vector2();            }        }
    this.graphics.PreferredBackBufferWidth
    this.graphics.PreferredBackBufferHeight


    xác định kích thước của màn hình window phone, lệnh if dài loằng ngoằng ở trên có nghĩa là nếu dí chuột vào phạm vi màn hình thì mới set lai vị trí bóng
    vì bóng có kích thước , nên xuất hiện lượng size trong đoạn if trên

    2. hàm outside trong ball.cs

    hàm này xác định xem bóng có còn ở trong khung màn hình hay không ?


    Mã:
    public bool outside()        {            if (this.Location.X < 0)            {                this.direction = new Vector2(-this.direction.X, this.direction.Y);                return true;            }            if (this.Location.Y < 0)            {                this.direction = new Vector2(this.direction.X, -this.direction.Y); return true;            }            if (this.Location.X + this.size.X > this.graphics.PreferredBackBufferWidth)            {                this.direction = new Vector2(-this.direction.X, this.direction.Y); return true;            }             if (this.Location.Y + this.size.Y > this.graphics.PreferredBackBufferHeight)            {                this.direction = new Vector2(this.direction.X, -this.direction.Y);                return true;            } return false;        }
    mấy lệnh trong if sẽ quyết định đến sự bật trở lại của bóng, tôi sẽ giải thích kĩ hơn trong hình sau :


    vector màu đỏ là vector hướng chuyển động
    đó là lí do tại sao tôi code trong hàm if như thế [IMG]images/smilies/biggrin.png[/IMG]

    3.lớp ballmanager.cs

    lớp này thì đơn giản rồi, tôi tạo 1 mảng đối tượng ball(6 quả), viết hàm update chung,draw chung,load content chung
    vì thế bạn thấy game1.cs code đơn giản hơn, ít rối rắm

  4. #4
    kết luận :
    -mình ko có thời gian viết chi tiết, ai thắc mắc cứ hỏi
    -có thể đọc thêm các tut xna trước mà tôi đã viết
    -window phone OS dùng silverlight,XNA để phát triển ứng dụng,game

    - nếu run mà game ko hiện ra thì làm như sau

    Project demo:

    chúc vui vẻ

  5. #5
    Ngày tham gia
    Dec 2015
    Bài viết
    0
    ò cho mình xin mấy cái hình ball đi, mình thấy hay đó

  6. #6
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    tài nguyên có trong project mình upload ở trên

    tool thì đây

  7. #7
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    cho mình xin tài nguyên với cái setup window phone tool develop luôn đi
    thank bạn rồi đó

  8. #8
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    nghiền viết game qá nhg mà máy kô có card màn hình kô cài đc xna ~~! buồn qá đi

  9. #9
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi shockgun
    nghiền viết game qá nhg mà máy kô có card màn hình kô cài đc xna ~~! buồn qá đi
    trước mình dùng máy bàn , VGA share RAM vẫn chạy đc XNA 3.1

  10. #10
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi zstar
    trước mình dùng máy bàn , VGA share RAM vẫn chạy đc XNA 3.1
    máy của em penIV 2.8 ram 1g Vga onboard (ram shared 128mb) chạy bản 2.0 lẫn 3.0 và 3.1 đầu ra kết qả như này này [IMG]images/smilies/21.gif[/IMG]



    máy đã cài direct 9.0c rùi

 

 
Trang 1 của 5 123 ... CuốiCuối

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •