想想我会拍摄一个俄罗斯方块克隆,但我遇到了一个问题。我有一个Tetromino
类,其中有一个Draw()
方法,以便在将每个部分绘制到屏幕时更容易。然而,它没有正常工作,它似乎什么都没有,除非我删除graphics.Clear(Color.Black)
行,当然,这会导致一个混乱的结果(因为绘图表面不会在游戏的每次迭代中被清除环)。我将图形对象作为参数传递给我的方法。我错过了什么吗?另外,我使用自定义Timer类来提高准确性,但我认为这不重要。
Game.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Tetris
{
public partial class Game : Form
{
private Timer timer;
private Graphics graphics;
private List<Tetromino> tetrominos;
private static readonly Random random = new Random();
private const int SCREEN_WIDTH = 300;
private const int SCREEN_HEIGHT = 600;
public Game()
{
InitializeComponent();
timer = new Timer();
this.Text = "Tetris";
this.DoubleBuffered = true;
this.ClientSize = new Size(SCREEN_WIDTH, SCREEN_HEIGHT);
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.StartPosition = FormStartPosition.CenterScreen;
this.MaximizeBox = false;
graphics = this.CreateGraphics();
tetrominos = new List<Tetromino>();
}
public void GameLoop()
{
// This is here just for testing.
int tetromino = random.Next(0, 6);
tetrominos.Add(new Tetromino((Tetromino.Tetrominos)tetromino));
while (this.Created)
{
timer.Reset();
GameUpdate();
GameDraw();
Application.DoEvents();
while (timer.GetTicks() < 33) ;
}
}
private void GameUpdate()
{
for (int i = 0; i < tetrominos.Count; i++)
tetrominos[i].Offset(0, 5);
}
private void GameDraw()
{
graphics.Clear(Color.Black);
for (int i = 0; i < tetrominos.Count; i++)
tetrominos[i].Draw(graphics);
}
}
}
Tetromino.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace Tetris
{
class Tetromino
{
private List<Rectangle> _tetromino;
private Brush brush;
public Tetromino(Tetrominos tetromino)
{
_tetromino = new List<Rectangle>();
switch (tetromino)
{
//---[][][][]---
case Tetrominos.I:
brush = Brushes.Cyan;
_tetromino.Add(new Rectangle(90, -60, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[0].X + 30, -60, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[1].X + 30, -60, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[2].X + 30, -60, 30, 30));
break;
//----[][]----
//----[][]----
case Tetrominos.O:
brush = Brushes.Yellow;
_tetromino.Add(new Rectangle(120, -60, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[0].X + 30, -60, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[0].X, -30, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[0].X + 30, -30, 30, 30));
break;
//---- [] -----
//---[][][]----
case Tetrominos.T:
brush = Brushes.Purple;
_tetromino.Add(new Rectangle(120, -60, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[0].X - 30, -30, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[1].X + 30, -30, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[2].X + 30, -30, 30, 30));
break;
//---- [][]----
//---[][] -----
case Tetrominos.S:
brush = Brushes.Green;
_tetromino.Add(new Rectangle(120, -60, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[0].X + 30, -60, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[0].X - 30, -30, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[0].X, -30, 30, 30));
break;
//---[][] -----
//---- [][]----
case Tetrominos.Z:
brush = Brushes.Red;
_tetromino.Add(new Rectangle(90, -60, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[0].X + 30, -60, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[0].X + 30, -30, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[2].X + 30, -30, 30, 30));
break;
//---[] ------
//---[][][]----
case Tetrominos.J:
brush = Brushes.Blue;
_tetromino.Add(new Rectangle(90, -60, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[0].X, -30, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[0].X + 30, -30, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[2].X + 30, -30, 30, 30));
break;
//----- []----
//---[][][]----
case Tetrominos.L:
brush = Brushes.Orange;
_tetromino.Add(new Rectangle(150, -60, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[0].X - 60, -30, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[1].X + 30, -30, 30, 30));
_tetromino.Add(new Rectangle(_tetromino[2].X + 30, -30, 30, 30));
break;
}
}
public void Offset(int x, int y)
{
for (int i = 0; i < _tetromino.Count; i++)
_tetromino[i] = new Rectangle(_tetromino[i].X + x, _tetromino[i].Y + y, 30, 30);
}
public void Draw(Graphics graphics)
{
for (int i = 0; i < _tetromino.Count; i++)
graphics.FillRectangle(brush, _tetromino[i]);
}
public void Rotate()
{
// TO-DO: Implement
}
public enum Tetrominos { I, O, T, S, Z, J, L }
}
}
Timer.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace Tetris
{
class Timer
{
[DllImport("kernel32.dll")]
private static extern long GetTickCount();
private long startTick;
public Timer() { Reset(); }
public void Reset() { startTick = 0; }
public long GetTicks() => GetTickCount() - startTick;
}
}
答案 0 :(得分:0)
我添加了我的Timer类供你查看,它只是打我!错误在于startTick = 0
,应该是startTick = GetTickCount()
。我可能错了。哎呀,我觉得处于疲惫状态的编码会对你做这样的事情。