[Windows Mobile]使用 OpenNETCF.Media.WaveAudio 录音与播放

使用 OpenNETCF.Media.WaveAudio 录音与播放


1. 简介

本来想写个贪食蛇游戏,在 Windows Mobile 中可以玩,不过因为要准备点部落聚会的简报,结果没时间写,只好变成撞墙蛇了

2. 方法

在程序的功能上,就是当蛇撞到屏幕的边边时,游戏结束,程序流程如下

(1) 取得屏幕分辨率,才能到蛇什么时候撞墙死掉,可参考先前的文章 [Windows Mobile]旋转屏幕方向、取得屏幕分辨率与工作区域大小

(2) 声明全域变量用来记录座标

(3) 点选 Menu 上的 Start 按钮,使 Timer 动作,并且跟据使用者点选的方向更新座标,并判断是否撞墙了

(4) 在表单 Paint 事件中,绘制蛇出来

程序

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SmartDeviceProject8
{
    public partial class Form1 : Form
    {
        const int snakeSize = 10;
        int w = Screen.PrimaryScreen.WorkingArea.Width / snakeSize;
        int h = Screen.PrimaryScreen.WorkingArea.Height / snakeSize;
        bool[,] point = new bool[Screen.PrimaryScreen.WorkingArea.Width / snakeSize, Screen.PrimaryScreen.WorkingArea.Height / snakeSize];
        string direction = "Right";  // 声明全域变量 direction,放置 snake 的行进方向

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            point[0, 0] = true;  // 设定 snake 初始位置
        }

        private void miStart_Click(object sender, EventArgs e)
        {
            this.timer1.Interval = 100;  // 设定 timer1 的触发时间为 0.1
            this.timer1.Enabled = true;  // 开启 timer1
        }

        // 表单 Paint 刷新事件
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen pen = new Pen(Color.Red, 2);  // 设定 Pen Color: Red,width: 2
            int x = 0; int y = 0;
            while (y < h)
            {
                bool skip = false;
                if (point[x, y] == true)
                {
                    g.DrawRectangle(pen, x * snakeSize, y * snakeSize, snakeSize, snakeSize);
                }
                if (x >= w-1)
                {
                    x = 0; y++; skip = true;
                }

                if (x < w-1 && skip == false)
                {
                    x++;
                }
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                int x = 0; int y = 0;
                while (y < h)
                {
                    bool skip = false;
                    if (direction == "Right")  // 当方向为 Right 时
                    { if (point[x, y] == true) { point[x, y] = false; point[x + 1, y] = true; break; } }
                    if (direction == "Left")  // 当方向为 Left 时
                    { if (point[x, y] == true) { point[x, y] = false; point[x - 1, y] = true; break; } }
                    if (direction == "Down") // 当方向为 Down 时
                    { if (point[x, y] == true) { point[x, y] = false; point[x, y + 1] = true; break; } }
                    if (direction == "Up") // 当方向为 Up 时
                    { if (point[x, y] == true) { point[x, y] = false; point[x, y - 1] = true; break; } }
                    if (x >= w-1) { x = 0; y++; skip = true; }
                    if (x < w-1 && skip == false) { x++; }
                }
                Invalidate();  // 重新绘制画面
            }
            catch
            {
                MessageBox.Show("Game Over");
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // 将使用者点选按键给direction
            direction = e.KeyData.ToString();
        }
    }
}

3. 执行结果




原文:大专栏  [Windows Mobile]使用 OpenNETCF.Media.WaveAudio 录音与播放


猜你喜欢

转载自www.cnblogs.com/petewell/p/11465347.html