PDA

View Full Version : حرکت یک عکس در جهت های مختلف



rerzaz
شنبه 16 خرداد 1394, 15:31 عصر
سلام خسته نباشید
من یک پیکچر باکس دارم می خوام وقتی فرم لود شد این پیکچر باکس در جهات مختلف حرکت کنه مثلا بره سمت راست بعتد بیاد پاین بره سمت چپ و.. چکار باید کنم

winner1
شنبه 16 خرداد 1394, 15:35 عصر
خوب باید location رو تغیر بدی مثلا

int x=10;
int y=20;
myPictureBox.Location=new Point(x,y);

khokhan
شنبه 16 خرداد 1394, 17:12 عصر
سلام خسته نباشید
من یک پیکچر باکس دارم می خوام وقتی فرم لود شد این پیکچر باکس در جهات مختلف حرکت کنه مثلا بره سمت راست بعتد بیاد پاین بره سمت چپ و.. چکار باید کنم

به عبارتی می شهbouncing ball animation:لبخند:


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;
using System.Drawing.Drawing2D;
using System.Media;

namespace bouncing_ball1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private const int BallWidth = 50;
private const int BallHeight = 50;
private int BallX, BallY; // Position.
private int BallVx, BallVy; // Velocity.
private void Form1_Load(object sender, EventArgs e)
{
Random rnd = new Random();
BallVx = rnd.Next(1, 4);
BallVy = rnd.Next(1, 4);
BallX = rnd.Next(0, ClientSize.Width - BallWidth);
BallY = rnd.Next(0, ClientSize.Height - BallHeight);

// Use double buffering to reduce flicker.
this.SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer,
true);
this.UpdateStyles();
}

private void timer1_Tick(object sender, EventArgs e)
{
BallX += BallVx;
if (BallX < 0)
{
BallVx = -BallVx;
Boing();
}
else if (BallX + BallWidth > ClientSize.Width)
{
BallVx = -BallVx;
Boing();
}

BallY += BallVy;
if (BallY < 0)
{
BallVy = -BallVy;
Boing();
}
else if (BallY + BallHeight > ClientSize.Height)
{
BallVy = -BallVy;
Boing();
}

Refresh();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.Clear(BackColor);
e.Graphics.FillEllipse(Brushes.Blue, BallX, BallY,
BallWidth, BallHeight);
e.Graphics.DrawEllipse(Pens.Black, BallX, BallY,
BallWidth, BallHeight);
}
private void Boing()
{
using (SoundPlayer player = new SoundPlayer(
Properties.Resources.boing))
{
player.Play();
}
}
}
}