cscの作法 その60


概要

cscの作法、調べてみた。
ゲームやってみた。

参考にしたページ

写真

サンプルコード

using System;
using System.Drawing;
using System.Windows.Forms;

namespace _neko
{
	public partial class Form1 : Form {
		PictureBox pictureBox1;
		Panel panel1;
		String str;
		public bool clear_flag = false;
		public bool tree_flag = false;
		const int box = 100;
		const int disp_box = 4;
		const int disp_x = box * disp_box;
		const int disp_y = box * disp_box;
		const int max_x = disp_x - box;
		const int max_y = disp_y - box;
		int neko_x = max_x / 2;
		int neko_y = max_y / 2;
		int[,] map_normal = new int[,] {
			{ 1,0,0,2},
			{ 0,0,1,0},
			{ 0,0,0,0},
			{ 1,0,0,1}
		};
		Bitmap bmp_player_f = new Bitmap(@"neko_front.png");
		Bitmap bmp_player_b = new Bitmap(@"neko_back.png");
		Bitmap bmp_player_l = new Bitmap(@"neko_left.png");
		Bitmap bmp_player_r = new Bitmap(@"neko_right.png");
		Bitmap bmp_bg_kusa = new Bitmap(@"kusa.png");
		Bitmap bmp_bg_ki = new Bitmap(@"ki.png");
		Bitmap bmp_bg_sakana = new Bitmap(@"sakana.png");
		Bitmap bmp;
		public Form1() {
			Text = "neko";
			ClientSize = new Size(450, 450);
			pictureBox1 = new PictureBox();
			pictureBox1.Width = 400;
			pictureBox1.Height = 400;
			panel1 = new Panel();
			panel1.Width = 400;
			panel1.Height = 400;
			Controls.AddRange(new Control[] {
				panel1
			});
			pictureBox1.Parent = panel1;
			bmp_player_f.MakeTransparent(bmp_player_f.GetPixel(0, 0));
			bmp_player_b.MakeTransparent(bmp_player_b.GetPixel(0, 0));
			bmp_player_l.MakeTransparent(bmp_player_l.GetPixel(0, 0));
			bmp_player_r.MakeTransparent(bmp_player_r.GetPixel(0, 0));
			pictureBox1.BackColor = (Color.Transparent);
			bmp = bmp_player_f;
			this.KeyDown += new KeyEventHandler(Form1_KeyDown);
			panel1.Paint += panel1_Paint;
			pictureBox1.Paint += pictureBox1_Paint;
			Timer timer1 = new Timer();
			timer1.Interval = 100;
			timer1.Tick += new EventHandler(timer1_Tick);
			timer1.Start();
		}
		private void timer1_Tick(object sender, EventArgs e) {
			int diff_x = 0;
			int diff_y = 0;
			const int step = 10;
			bmp = bmp_player_f;
			switch (str)
			{
			case "LEFT\r":
				bmp = bmp_player_l;
				diff_x = -step;
			break;
			case "DOWN\r":
				bmp = bmp_player_f;
				diff_y = step;
			break;
			case "UP\r":
				bmp = bmp_player_b;
				diff_y = -step;
			break;
			case "RIGHT\r":
				bmp = bmp_player_r;
				diff_x = step;
			break;
			default:
			break;
			}
			int new_x = neko_x + diff_x;
			int new_y = neko_y + diff_y;
			if ((0 < new_x && new_x < max_x) && (0 < new_y && new_y < max_y))
			{
				if (check_zone(new_x + 50, new_y + 80) == 0)
				{
					neko_x = new_x;
					neko_y = new_y;
				}
				if (check_zone(new_x + 50, new_y + 80) == 2)
				{
					if (clear_flag == false)
					{
						clear_flag = true;
					}
				}
				else
				{
					if (clear_flag == true)
					{
						clear_flag = false;
					}
				}
			}
			pictureBox1.Invalidate();
		}
		private void panel1_Paint(object sender, PaintEventArgs e) {
			Image currentImage = bmp_bg_kusa;
			for (int i = 0; i < 4; i++)
			{
				for (int j = 0; j < 4; j++)
				{
					switch (map_normal[i, j])
					{
					case 0:
						currentImage = bmp_bg_kusa;
					break;
					case 1:
						currentImage = bmp_bg_ki;
					break;
					case 2:
						currentImage = bmp_bg_sakana;
					break;
					}
					e.Graphics.DrawImage(currentImage, j * 100, i * 100, currentImage.Width, currentImage.Height);
				}
			}
		}
		private void pictureBox1_Paint(object sender, PaintEventArgs e) {
			Graphics g = e.Graphics;
			g.DrawImage(bmp, neko_x, neko_y);
			if ( clear_flag == true)
			{
				Font fnt = new Font("Bauhaus 93", 36);
				g.DrawString("Congratulations!", fnt, Brushes.Black, 0, 200);
			}
		}
		public int check_zone(int pos_x, int pos_y) {
			 return (map_normal[pos_y / 100, pos_x / 100]);
		}
		private void Form1_KeyDown(object sender, KeyEventArgs e) {
			switch(e.KeyData)
			{
			case Keys.Left:
				str = "LEFT\r";
			break;
			case Keys.Right:
				str = "RIGHT\r";
			break;
			case Keys.Up:
				str = "UP\r";
			break;
			case Keys.Down:
				str = "DOWN\r";
			break;
			default:
			break;
			}
		}
		private void Form1_KeyUp(object sender, KeyEventArgs e) {
			str = "NONE\n";
		}
		[STAThread]
		static void Main(string[] args) {
			Application.Run(new Form1());
		}
	}
}





以上。