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 Keyboard
{
public partial class Move : Form
{
public Move()
{
InitializeComponent();
}
Graphics gr;
BufferedGraphics bg;
BufferedGraphicsContext bufferedGraphicsContext;
Pen Black = new Pen(Color.Black, 4);
Pen Red = new Pen(Color.Red, 5);
RectangleF player1 = new RectangleF(575.0F, 20.0F, 60.0F, 100.0F);
RectangleF player2 = new RectangleF(50.0F, 500.0F, 90.0F, 20.0F);
Rectangle bar = new Rectangle(400, 40, 70, 100);
void Move_SizeChanged(object sender, EventArgs e)
{
InitializeComponent();
Size = new Size(800, 680);
StartPosition = FormStartPosition.CenterScreen;
KeyDown += new KeyEventHandler(Move_KeyDown);
Paint += new PaintEventHandler(Move_Paint);
SizeChanged += new EventHandler(Move_SizeChanged);
bufferedGraphicsContext = BufferedGraphicsManager.Current;
InitializeGraphics();
}
private void Move_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
player1.Location = new PointF(player1.Left - 10, player1.Top);
if (e.KeyCode == Keys.Right)
player1.Location = new PointF(player1.Left + 10, player1.Top);
if (e.KeyCode == Keys.Up)
player1.Location = new PointF(player1.Left, player1.Top - 10);
if (e.KeyCode == Keys.Down)
player1.Location = new PointF(player1.Left, player1.Top + 10);
if (e.KeyCode == Keys.A)
player2.Location = new PointF(player2.Left - 10, player2.Top);
if (e.KeyCode == Keys.D)
player2.Location = new PointF(player2.Left + 10, player2.Top);
if (e.KeyCode == Keys.W)
player2.Location = new PointF(player2.Left, player2.Top - 10);
if (e.KeyCode == Keys.S)
player2.Location = new PointF(player2.Left, player2.Top + 10);
if (player1.IntersectsWith(player2) || player2.IntersectsWith(player1))
{
MessageBox.Show;
Environment.Exit(0);
}
if (player2.Top > 535 || player2.Top < 0 || player2.Right > 780 || player2.Right < 85)
{
player2.Location = new PointF(player2.Location.X - 15, player2.Location.Y);
}
if (player1.Top > 535 || player1.Top < 0 || player1.Right > 780 || player1.Right < 50)
{
player1.Location = new PointF(player1.Location.X - 15, player1.Location.Y);
}
if (player1.IntersectsWith(bar))
{
player1.Location = new PointF(player1.Location.X + 10, player1.Location.Y);
player1.Location = new PointF(player1.Location.X, player1.Location.Y + 10);
}
if (player1.IntersectsWith(bar))
{
player1.Location = new PointF(player1.Location.X - 20, player1.Location.Y);
player1.Location = new PointF(player1.Location.X, player1.Location.Y - 10);
}
if (player2.IntersectsWith(bar))
{
player2.Location = new PointF(player2.Location.X + 10, player2.Location.Y);
player2.Location = new PointF(player2.Location.X, player2.Location.Y + 10);
}
if (player2.IntersectsWith(bar))
{
player2.Location = new PointF(player2.Location.X - 20, player2.Location.Y);
player2.Location = new PointF(player2.Location.X, player2.Location.Y - 10);
}
Refresh();
}
private void Move_Paint(object sender, PaintEventArgs e)
{
DrawToBuffer();
}
private void DrawToBuffer()
{
bg.Graphics.Clear(BackColor); //Проблема с этой строкой. Выдает System.NullReferenceException: "Ссылка на объект не указывает на экземпляр объекта"
bg.Graphics.DrawEllipse(Black, player1);
bg.Graphics.DrawEllipse(Black, player2);
SolidBrush red = new SolidBrush(Color.Red);
SolidBrush black = new SolidBrush(Color.Black);
bg.Graphics.DrawRectangle(Red, bar);
bg.Graphics.FillRectangle(red, bar);
bg.Graphics.FillEllipse(black, player1);
bg.Graphics.FillEllipse(black, player2);
bg.Render();
}
private void InitializeGraphics()
{
gr = CreateGraphics();
bg = bufferedGraphicsContext.Allocate(gr, ClientRectangle);
}
}
}
Asked
Active
Viewed 153 times
-2
Катя Камм
- 35
-
На какой хоть строке выдаёт ошибку? – A K May 05 '19 at 09:41
-
Точку останова поставьте в месте, где у вас проблема, и поглядите. – tym32167 May 05 '19 at 09:43
-
Точка останова не помогает решить проблему – Катя Камм May 05 '19 at 09:50
1 Answers
0
private void DrawToBuffer()
{
if(bg==null)return;
bg.Graphics.Clear(BackColor); //Проблема с этой строкой. Выдает System.NullReferenceException: "Ссылка на объект не указывает на экземпляр объекта"
bg.Graphics.DrawEllipse(Black, player1);
bg.Graphics.DrawEllipse(Black, player2);
SolidBrush red = new SolidBrush(Color.Red);
SolidBrush black = new SolidBrush(Color.Black);
bg.Graphics.DrawRectangle(Red, bar);
bg.Graphics.FillRectangle(red, bar);
bg.Graphics.FillEllipse(black, player1);
bg.Graphics.FillEllipse(black, player2);
bg.Render();
}
defTrue
- 16