Игра. Проблемы с плавным движением объектов - C#
Формулировка задачи:
господа а на C# нельзя пример у меня форма должна вращаться увеличиваясь в размере а она при этом мерцает и DoubleBuffered не помогает
Решение задачи: «Игра. Проблемы с плавным движением объектов»
textual
Листинг программы
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 WindowsFormsApplication1
{
public partial class MainForm : Form
{
Point _cursorPosition;
Point _pictPos;
Bitmap _bgrBitmap;
Bitmap _frgBitmap;
Graphics _gr;
//Graphics _bufGraphics; //Тестил вместо BufferedGraphics _bufGraphics;
BufferedGraphics _bufGraphics;
public MainForm()
{
InitializeComponent();
FormBorderStyle = (FormBorderStyle.None);
Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Location = new Point(0, 0);
TopMost = true;
_cursorPosition = Cursor.Position;
Cursor.Hide();
_pictPos = new Point(0, 0);
_bgrBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
//1 раз отрисовываем фон на _bgrBitmap (как бы текстурируем всё изображение частями маленького изображения)
RenderBackground(Properties.Resources.Огонь_темный, _bgrBitmap);
_frgBitmap = Properties.Resources.Метал;
InitializeGraphics();
Application.Idle += new EventHandler(Application_Idle);
System.Timers.Timer timerCoord = new System.Timers.Timer();
timerCoord.Interval = 20;
timerCoord.Elapsed += new System.Timers.ElapsedEventHandler(CoordinateChangerTimer_Elapsed);// += new EventHandler(CoordinateChangerTimer_Tick);
timerCoord.Start();
}
void CoordinateChangerTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
_pictPos = new Point(_pictPos.X + 1, _pictPos.Y + 1);
}
private void RenderBackground(Bitmap bmp, Bitmap bgrBitmap)
{
using (Graphics gr = Graphics.FromImage(bgrBitmap))
{
int quntityX = bgrBitmap.Width / bmp.Size.Width;
int quntityY = (int)Math.Ceiling((double)bgrBitmap.Height / (double)bmp.Size.Height);
for (int x = 0; x < quntityX; x++)
for (int y = 0; y < quntityY; y++)
gr.DrawImage(bmp, x * bmp.Size.Width, y * bmp.Size.Height);
}
}
void Application_Idle(object sender, EventArgs e)
{
if (Cursor.Position != _cursorPosition)
{
Close();
return;
}
RenderScene();
}
private void RenderScene()
{
_bufGraphics.Graphics.Clear(BackColor);
_bufGraphics.Graphics.DrawImage(_bgrBitmap, 0, 0);
_bufGraphics.Graphics.DrawImage(_frgBitmap, _pictPos);
_bufGraphics.Render();
}
private void InitializeGraphics()
{
_gr = CreateGraphics();
_bufGraphics = BufferedGraphicsManager.Current.Allocate(_gr, ClientRectangle);
}
//private void RenderScene()
//{
// _bufGraphics.Clear(BackColor);
// _bufGraphics.DrawImage(_bgrBitmap, 0, 0);
// _bufGraphics.DrawImage(_frgBitmap, _pictPos);
// _gr.DrawImage(_bufBmp, 0, 0);
//}
//Bitmap _bufBmp;
//private void InitializeGraphics()
//{
// _bufBmp = new Bitmap(ClientSize.Width, ClientSize.Height);
// _gr = CreateGraphics();
// _bufGraphics = Graphics.FromImage(_bufBmp);
//}
}
}