0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cscの作法 その634

0
Posted at

概要

cscの作法、調べてみた。
tetris書いてみた。

写真

image.png

サンプルコード

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


public struct BlockData {
	public int[][,] Shape;
	public Color Color;
	public Color Highlight;
	public Color Shadow;
	public BlockData(int[][,] shape, Color color, Color highlight, Color shadow) {
		Shape = shape;
		Color = color;
		Highlight = highlight;
		Shadow = shadow;
	}
}

public class Tetris {
	public int StageWidth = 10;
	public int StageHeight = 20;
	private int stageCanvasWidth;
	private int stageCanvasHeight;
	private int nextCanvasWidth;
	private int nextCanvasHeight;
	private float cellSize;
	private float stageLeftPadding;
	private float stageTopPadding;
	private BlockData[] blocks;
	private int?[,] virtualStage;
	private int? currentBlock;
	private int? nextBlock;
	private int blockX;
	private int blockY;
	private int blockAngle;
	private int deletedLines;
	private System.Timers.Timer mainTimer;
	private Random random = new Random();
	public event Action RequestRepaint;
	public event Action<string> GameOver;
	public event Action<int> LinesChanged;
	public Tetris(int stageWidth, int stageHeight, int nextWidth, int nextHeight) {
		this.stageCanvasWidth = stageWidth;
		this.stageCanvasHeight = stageHeight;
		this.nextCanvasWidth = nextWidth;
		this.nextCanvasHeight = nextHeight;
		float cellWidth = (float) stageCanvasWidth / StageWidth;
		float cellHeight = (float) stageCanvasHeight / StageHeight;
		this.cellSize = cellWidth < cellHeight ? cellWidth : cellHeight;
		this.stageLeftPadding = (stageCanvasWidth - this.cellSize * StageWidth) / 2;
		this.stageTopPadding = (stageCanvasHeight - this.cellSize * StageHeight) / 2;
		this.blocks = CreateBlocks();
		this.deletedLines = 0;
		mainTimer = new System.Timers.Timer(500);
		mainTimer.Elapsed += OnTimerElapsed;
		mainTimer.AutoReset = true;
	}
	private BlockData[] CreateBlocks() {
		return new BlockData[] {
			new BlockData(new int[][,] {
				new int[,] {{-1, 0}, {0, 0}, {1, 0}, {2, 0}},
				new int[,] {{0, -1}, {0, 0}, {0, 1}, {0, 2}},
				new int[,] {{-1, 0}, {0, 0}, {1, 0}, {2, 0}},
				new int[,] {{0, -1}, {0, 0}, {0, 1}, {0, 2}}
			}, Color.FromArgb(0, 255, 255), Color.FromArgb(255, 255, 255), Color.FromArgb(0, 128, 128)),
			new BlockData(new int[][,] {
				new int[,] {{0, 0}, {1, 0}, {0, 1}, {1, 1}},
				new int[,] {{0, 0}, {1, 0}, {0, 1}, {1, 1}},
				new int[,] {{0, 0}, {1, 0}, {0, 1}, {1, 1}},
				new int[,] {{0, 0}, {1, 0}, {0, 1}, {1, 1}}
			}, Color.FromArgb(255, 255, 0), Color.FromArgb(255, 255, 255), Color.FromArgb(128, 128, 0)),

			new BlockData(new int[][,] {
				new int[,] {{0, 0}, {1, 0}, {-1, 1}, {0, 1}},
				new int[,] {{-1, -1}, {-1, 0}, {0, 0}, {0, 1}},
				new int[,] {{0, 0}, {1, 0}, {-1, 1}, {0, 1}},
				new int[,] {{-1, -1}, {-1, 0}, {0, 0}, {0, 1}}
			}, Color.FromArgb(0, 255, 0), Color.FromArgb(255, 255, 255), Color.FromArgb(0, 128, 0)),
			new BlockData(new int[][,] {
				new int[,] {{-1, 0}, {0, 0}, {0, 1}, {1, 1}},
				new int[,] {{0, -1}, {-1, 0}, {0, 0}, {-1, 1}},
				new int[,] {{-1, 0}, {0, 0}, {0, 1}, {1, 1}},
				new int[,] {{0, -1}, {-1, 0}, {0, 0}, {-1, 1}}
			}, Color.FromArgb(255, 0, 0), Color.FromArgb(255, 255, 255), Color.FromArgb(128, 0, 0)),
			new BlockData(new int[][,] {
				new int[,] {{-1, -1}, {-1, 0}, {0, 0}, {1, 0}},
				new int[,] {{0, -1}, {1, -1}, {0, 0}, {0, 1}},
				new int[,] {{-1, 0}, {0, 0}, {1, 0}, {1, 1}},
				new int[,] {{0, -1}, {0, 0}, {-1, 1}, {0, 1}}
			}, Color.FromArgb(0, 0, 255), Color.FromArgb(255, 255, 255), Color.FromArgb(0, 0, 128)),
			new BlockData(new int[][,] {
				new int[,] {{1, -1}, {-1, 0}, {0, 0}, {1, 0}},
				new int[,] {{0, -1}, {0, 0}, {0, 1}, {1, 1}},
				new int[,] {{-1, 0}, {0, 0}, {1, 0}, {-1, 1}},
				new int[,] {{-1, -1}, {0, -1}, {0, 0}, {0, 1}}
			}, Color.FromArgb(255, 165, 0), Color.FromArgb(255, 255, 255), Color.FromArgb(128, 82, 0)),
			new BlockData(new int[][,] {
				new int[,] {{0, -1}, {-1, 0}, {0, 0}, {1, 0}},
				new int[,] {{0, -1}, {0, 0}, {1, 0}, {0, 1}},
				new int[,] {{-1, 0}, {0, 0}, {1, 0}, {0, 1}},
				new int[,] {{0, -1}, {-1, 0}, {0, 0}, {0, 1}}
			}, Color.FromArgb(255, 0, 255), Color.FromArgb(255, 255, 255), Color.FromArgb(128, 0, 128))
		};
	}
	public void StartGame() {
		virtualStage = new int?[StageWidth, StageHeight];
		currentBlock = null;
		nextBlock = GetRandomBlock();
		mainTimer.Start();
		MainLoop();
	}
	private void OnTimerElapsed(object sender, ElapsedEventArgs e) {
		MainLoop();
	}
	private void MainLoop() {
		lock (this)
		{
			if (currentBlock == null)
			{
				if (!CreateNewBlock())
				{
					mainTimer.Stop();
					return;
				}
			}
			else
			{
				FallBlock();
			}
		}
		RequestRepaint.Invoke();
	}
	private bool CreateNewBlock() {
		currentBlock = nextBlock;
		nextBlock = GetRandomBlock();
		blockX = StageWidth / 2 - 2;
		blockY = 0;
		blockAngle = 0;
		if (!CheckBlockMove(blockX, blockY, currentBlock.Value, blockAngle))
		{
			mainTimer.Stop();
			GameOver.Invoke("GAME OVER");
			return false;
		}
		return true;
	}
	private int GetRandomBlock() {
		return random.Next(0, 7);
	}
	private void FallBlock() {
		if (CheckBlockMove(blockX, blockY + 1, currentBlock.Value, blockAngle))
		{
			blockY++;
		}
		else
		{
			FixBlock(blockX, blockY, currentBlock.Value, blockAngle);
			currentBlock = null;
		}
	}
	private bool CheckBlockMove(int x, int y, int type, int angle) {
		var shape = blocks[type].Shape[angle];
		for (int i = 0; i < shape.GetLength(0); i++)
		{
			int cellX = x + shape[i, 0];
			int cellY = y + shape[i, 1];
			if (cellX < 0 || cellX > StageWidth - 1)
				return false;
			if (cellY > StageHeight - 1)
				return false;
			if (cellY >= 0 && virtualStage[cellX, cellY] != null)
				return false;
		}
		return true;
	}
	private void FixBlock(int x, int y, int type, int angle) {
		var shape = blocks[type].Shape[angle];
		for (int i = 0; i < shape.GetLength(0); i++)
		{
			int cellX = x + shape[i, 0];
			int cellY = y + shape[i, 1];
			if (cellY >= 0)
			{
				virtualStage[cellX, cellY] = type;
			}
		}
		for (int currentY = StageHeight - 1; currentY >= 0; )
		{
			bool filled = true;
			for (int currentX = 0; currentX < StageWidth; currentX++)
			{
				if (virtualStage[currentX, currentY] == null)
				{
					filled = false;
					break;
				}
			}
			if (filled)
			{
				for (int y2 = currentY; y2 > 0; y2--)
				{
					for (int currentX = 0; currentX < StageWidth; currentX++)
					{
						virtualStage[currentX, y2] = virtualStage[currentX, y2 - 1];
					}
				}
				for (int currentX = 0; currentX < StageWidth; currentX++)
				{
					virtualStage[currentX, 0] = null;
				}
				deletedLines++;
				LinesChanged.Invoke(deletedLines);
			}
			else
			{
				currentY--;
			}
		}
	}
	public void MoveLeft() {
		Move( -1, 0, 0);
	}
	public void MoveRight() {
		Move(1, 0, 0);
	}
	public void Fall() {
		Move(0, 1, 0);
	}
	public void Rotate() {
		Move(0, 0, 1);
	}
	private void Move(int dx, int dy, int dAngle) {
		lock (this)
		{
			if (currentBlock == null)
				return;
			int nextAngle = blockAngle;
			if (dAngle != 0)
			{
				nextAngle = (blockAngle + dAngle) % blocks[currentBlock.Value].Shape.Length;
			}
			if (CheckBlockMove(blockX + dx, blockY + dy, currentBlock.Value, nextAngle))
			{
				blockX += dx;
				blockY += dy;
				blockAngle = nextAngle;
				RequestRepaint.Invoke();
			}
			else if (dy > 0)
			{
				FixBlock(blockX, blockY, currentBlock.Value, blockAngle);
				currentBlock = null;
				RequestRepaint.Invoke();
			}
		}
	}
	public void DrawStage(Graphics g) {
		g.Clear(Color.Black);
		if (virtualStage != null)
		{
			for (int x = 0; x < StageWidth; x++)
			{
				for (int y = 0; y < StageHeight; y++)
				{
					if (virtualStage[x, y] != null)
					{
						DrawCell(g, stageLeftPadding + (x * cellSize), stageTopPadding + (y * cellSize), cellSize, virtualStage[x, y].Value);
					}
				}
			}
		}
		if (currentBlock != null)
		{
			DrawBlock(g, stageLeftPadding + blockX * cellSize, stageTopPadding + blockY * cellSize, currentBlock.Value, blockAngle);
		}
	}
	public void DrawNextBlock(Graphics g) {
		g.Clear(Color.Black);
		if (nextBlock != null)
		{
			DrawBlock(g, cellSize * 2, cellSize, nextBlock.Value, 0);
		}
	}
	private void DrawBlock(Graphics g, float x, float y, int type, int angle) {
		var shape = blocks[type].Shape[angle];
		for (int i = 0; i < shape.GetLength(0); i++)
		{
			DrawCell(g, x + (shape[i, 0] * cellSize), y + (shape[i, 1] * cellSize), cellSize, type);
		}
	}
	private void DrawCell(Graphics g, float cellX, float cellY, float cellSize, int type) {
		var block = blocks[type];
		float adjX = cellX + 0.5f;
		float adjY = cellY + 0.5f;
		float adjSize = cellSize - 1f;
		using (var brush = new SolidBrush(block.Color))
		{
			g.FillRectangle(brush, adjX, adjY, adjSize, adjSize);
		}
		using (var pen = new Pen(block.Highlight))
		{
			g.DrawLine(pen, adjX, adjY + adjSize, adjX, adjY);
			g.DrawLine(pen, adjX, adjY, adjX + adjSize, adjY);
		}
		using (var pen = new Pen(block.Shadow))
		{
			g.DrawLine(pen, adjX, adjY + adjSize, adjX + adjSize, adjY + adjSize);
			g.DrawLine(pen, adjX + adjSize, adjY + adjSize, adjX + adjSize, adjY);
		}
	}
}



namespace TetrisGame
{
	public partial class Form1: Form {
		private Tetris tetris;
		private PictureBox pbStage;
		private PictureBox pbNext;
		private Label lblScore;
		public Form1() {
			InitializeComponent();
			InitializeTetrisGame();
		}
		private void InitializeComponent() {
			this.Text = "Tetris - C#";
			this.Size = new Size(500, 700);
			this.StartPosition = FormStartPosition.CenterScreen;
			this.FormBorderStyle = FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.KeyPreview = true;
			pbStage = new PictureBox();
			pbStage.Location = new Point(20, 20);
			pbStage.Size = new Size(300, 600);
			pbStage.BackColor = Color.Black;
			pbStage.Paint += PbStage_Paint;
			pbNext = new PictureBox();
			pbNext.Location = new Point(340, 20);
			pbNext.Size = new Size(120, 120);
			pbNext.BackColor = Color.Black;
			pbNext.Paint += PbNext_Paint;
			lblScore = new Label();
			lblScore.Location = new Point(340, 160);
			lblScore.Size = new Size(120, 40);
			lblScore.Font = new Font("Arial", 12, FontStyle.Bold);
			lblScore.Text = "Lines: 0";
			this.Controls.Add(pbStage);
			this.Controls.Add(pbNext);
			this.Controls.Add(lblScore);
			this.KeyDown += Form1_KeyDown;
		}
		private void InitializeTetrisGame() {
			tetris = new Tetris(pbStage.Width, pbStage.Height, pbNext.Width, pbNext.Height);
			tetris.RequestRepaint += Tetris_RequestRepaint;
			tetris.LinesChanged += Tetris_LinesChanged;
			tetris.GameOver += Tetris_GameOver;
			tetris.StartGame();
		}
		private void Form1_KeyDown(object sender, KeyEventArgs e) {
			switch (e.KeyCode)
			{
			case Keys.Left:
				tetris.MoveLeft();
			break;
			case Keys.Right:
				tetris.MoveRight();
			break;
			case Keys.Down:
				tetris.Fall();
			break;
			case Keys.Up:
			case Keys.Space:
				tetris.Rotate();
			break;
			}
		}
		private void Tetris_RequestRepaint() {
			if (this.InvokeRequired)
			{
				this.Invoke(new Action(Tetris_RequestRepaint));
				return;
			}
			pbStage.Invalidate();
			pbNext.Invalidate();
		}
		private void Tetris_LinesChanged(int lines) {
			if (this.InvokeRequired)
			{
				this.Invoke(new Action<int>(Tetris_LinesChanged), lines);
				return;
			}
			lblScore.Text = string.Format("Lines: {0}", lines);
		}
		private void Tetris_GameOver(string message) {
			if (this.InvokeRequired)
			{
				this.Invoke(new Action<string>(Tetris_GameOver), message);
				return;
			}
			MessageBox.Show(message, "Game Over", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}
		private void PbStage_Paint(object sender, PaintEventArgs e) {
			tetris.DrawStage(e.Graphics);
		}
		private void PbNext_Paint(object sender, PaintEventArgs e) {
			tetris.DrawNextBlock(e.Graphics);
		}
		[STAThread]
		public static void Main() {
			Application.Run(new Form1());
		}
	}
}




以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?