Как сделать движение шара бесконечным ? - C#
Формулировка задачи:
Код отталкивания шара от стенок прямоугольника присутсвует.
sing 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 Form1 : Form
{
BufferedGraphics _bufGraphics;
public Form1()
{
InitializeComponent();
InitializeGraphics();
}
public void InitializeGraphics()
{
BufferedGraphicsContext c = new BufferedGraphicsContext();
_bufGraphics = c.Allocate(CreateGraphics(), ClientRectangle);
c.MaximumBuffer = ClientRectangle.Size;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
for (int y = 475; y > 0; --y)
{
//Рисуем в буфере, чтобы не мигало изображение
_bufGraphics.Graphics.Clear(Color.Blue);
_bufGraphics.Graphics.DrawRectangle(new Pen(Color.Red, 8), 250, 100, 400, 400);
_bufGraphics.Graphics.DrawEllipse(new Pen(Color.Yellow, 15), 290, y, 15, 15);
_bufGraphics.Render();
if (y == 113)
{
for (int y_2 = 113; y_2 < 475; ++y_2)
{
_bufGraphics.Graphics.Clear(Color.Blue);
_bufGraphics.Graphics.DrawRectangle(new Pen(Color.Red, 8), 250, 100, 400, 400);
_bufGraphics.Graphics.DrawEllipse(new Pen(Color.Yellow, 15), 290, y_2, 15, 15);
_bufGraphics.Render();
}
}
}
}
}
}Решение задачи: «Как сделать движение шара бесконечным ?»
textual
Листинг программы
private void Form1_Paint(object sender, PaintEventArgs e)
{
// Чисто теоретически что-то вродеКод C#
int delta = -1;
for (int y = 475; y > 0; y+=delta)
{
if (y == 113)
delta = 1;
else if (y == 475)
delta = -1;
//Рисуем в буфере, чтобы не мигало изображение
_bufGraphics.Graphics.Clear(Color.Blue);
_bufGraphics.Graphics.DrawRectangle(new Pen(Color.Red, 8), 250, 100, 400, 400);
_bufGraphics.Graphics.DrawEllipse(new Pen(Color.Yellow, 15), 290, y, 15, 15);
_bufGraphics.Graphics.DrawRectangle(new Pen(Color.Red, 8), 250, 100, 400, 400);
_bufGraphics.Graphics.DrawEllipse(new Pen(Color.Purple, 15), 580, y, 15, 15);
_bufGraphics.Render();
}
}
//Привет Москве!Красивый город!Я бывал.