c# - 无法在winforms C#中恢复以前的图纸
问题描述
所以我正在尝试制作俄罗斯方块游戏,但形状不会留下来。第一个形状工作正常,但是当第二个形状出现时,第一个形状消失了。
我尝试像在代码中一样在 Form1 的 Invalidate() 中设置整个区域,但没有帮助。即使是简单的 Invalidate() 也不起作用。
任何指导都会有很大帮助。
namespace Tetris
{
public partial class Form1 : Form
{
int value = 0;
Timer timer;
string s;
Point p;
int widthOfShape = 0; int heightOfShape = 0;
Graphics g;
Point sizeOfPanel;
Dictionary<int, string> shapes = new Dictionary<int, string>();
public Form1()
{
InitializeComponent();
shapes.Add(1, "Rectangle");
shapes.Add(2, "Square");
p.X = 0;
p.Y = 0;
timer = new Timer();
timer.Interval = 800;
timer.Tick += new EventHandler(Timer_Tick);
timer.Enabled = true;
timer.Start();
SomeRandom();
sizeOfPanel = new Point(488, 337);
}
public void SomeRandom()
{
s = ChooseOneShape();
switch (s)
{
case "Rectangle":
widthOfShape = 80; heightOfShape = 20;
this.Invalidate(new Rectangle(0, 0, 500, 400));
break;
case "Square":
widthOfShape = 40; heightOfShape = 40;
break;
}
}
private void Timer_Tick(object sender, EventArgs e)
{
if (p.X + widthOfShape < sizeOfPanel.X && p.Y + heightOfShape < sizeOfPanel.Y)
{
p.X += 30;
p.Y += 30;
this.Invalidate(new Rectangle(0, 0, 500, 400));
}
else {
p.X = 0;p.Y = 0;s = string.Empty;
SomeRandom(); }
}
public string ChooseOneShape()
{
Random r = new Random();
int index = r.Next(1, shapes.Count+1);
return shapes[index];
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Red, p.X, p.Y, widthOfShape, heightOfShape);
}
}
}
解决方案
我按照我认为应该完成的方式修改了您的代码。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Tetris
{
public partial class Form1 : Form
{
const int X = 488;
const int Y = 337;
Graphics g;
Point sizeOfPanel;
Timer timer;
TetrisShape currentShape;
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}
public void Form1_Load(object sender, EventArgs e)
{
new TetrisShape("Rectangle");
new TetrisShape("Square");
currentShape = TetrisShape.SomeRandom();
panel1.Controls.Add(currentShape);
timer = new Timer();
timer.Interval = 800;
timer.Tick += new EventHandler(Timer_Tick);
timer.Enabled = true;
timer.Start();
sizeOfPanel = new Point(X, Y);
panel1.Height = Y;
panel1.Width = X;
}
private void Timer_Tick(object sender, EventArgs e)
{
if (currentShape.p.X + currentShape.Width < sizeOfPanel.X && currentShape.p.Y + currentShape.Height < sizeOfPanel.Y)
{
currentShape.Top += 30;
panel1.Refresh();
}
else
{
currentShape.p = new Point(0, 0);
currentShape = TetrisShape.SomeRandom();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Red, currentShape.ClientRectangle);
}
}
public class TetrisShape : Control
{
static Dictionary<int, TetrisShape> shapes = new Dictionary<int, TetrisShape>();
static Random rand = new Random();
static string[] shapeTypes = { "Rectangle", "Square" };
public int number { get; set; }
public string name { get; set; }
public Point p { get; set; }
public static int maxNumber;
public TetrisShape(string name)
{
this.name = name;
p = new Point(0, 0);
this.BackColor = Color.Red;
switch (name)
{
case "Rectangle":
this.Invalidate(new Rectangle());
Width = 80;
Height = 20;
number = ++maxNumber;
shapes.Add(number, this);
break;
case "Square":
this.Invalidate(new Rectangle());
Width = 40;
Height = 40;
break;
}
}
public static TetrisShape SomeRandom()
{
int shapeNumber = rand.Next(TetrisShape.shapeTypes.Length);
return new TetrisShape(TetrisShape.shapeTypes[shapeNumber]);
}
public TetrisShape ChooseOneShape()
{
int index = rand.Next(1, shapes.Count + 1);
return shapes[index];
}
}
}
推荐阅读
- excel - 如何通过 Laravel 从 Excel 中的特定单元格地址获取值
- python - 如何将.npy(numpy)文件转换为.png文件,其中.npy数组形状(30,256,256)?
- rsa - 如何从部分暴露的私钥、公钥和纯文本密码样本中确定 RSA 的完整私钥(d)?
- vba - 使用 VS2017 自定义 Excel UI/功能区?
- java - 如何在打印日志时始终避免打印记录器主类名称?
- javascript - 如何使图像在div中保持纵横比
- jquery - 如何在 fb:login-button 上应用自定义 CSS?
- android - 为什么我的代码没有读取接收方聊天?
- javascript - 从父母到孩子的反应传递
- javascript - 为什么在 Firefox 中通过 JavaScript 将背景恢复为透明会失败?