PDA

View Full Version : گفتگو: مشکل در کد بازی پینگ پنگ



c_doost
سه شنبه 26 اردیبهشت 1391, 20:19 عصر
کدی که می گذارم یک بازی مثل پینگ پونگ که از 3 تا pictuer Box استفاده کردم
این اولش یک بازی یک طرفه بود که من خواستم کاملترشم بکنم که با دو راکت به صورت دو نفره کار کنم
یک راکت که همون پیکچر باکس باشه با موس و یکی دیگه با بالا و پایین کیبورد کنترل میشه
یه مشکل تو برکشت توپ دارم که حتی وقتی به راکت سمت چپم برخورد میکنه توپ رو بر نمیگردونه

برای اینکه وقتی به راکت سمت چپم برخورد میکنه توپ رو بر گردونه چیکار کنم ؟

من کد و خود پروژه را میزارم لطفا کمکم کنید



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 game
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int speed =4;
int result;
bool top, left ;

private void Form1_Load(object sender, EventArgs e)
{
Random rand = new Random();
pictureBox1.Location = new Point(20, rand.Next(this.Height));
top = left = true;
timer1.Enabled = true;

}

private void timer1_Tick(object sender, EventArgs e)
{ // in ghesmat marboot b Control shart baraye bargasht top b samt chap
if (pictureBox1.Left > pictureBox2.Left) { timer1.Enabled = false; MessageBox.Show("شما باختید...امتیاز " + result.ToString()); result = 0; }
if (pictureBox1.Left + pictureBox1.Width >= pictureBox2.Left &&
pictureBox1.Left + pictureBox1.Width <= pictureBox2.Left + pictureBox2.Width
&& pictureBox1.Top + pictureBox1.Height >= pictureBox2.Top &&
pictureBox1.Top + pictureBox1.Height <= pictureBox2.Top + pictureBox2.Height + pictureBox1.Height)


{
left = false;
result += 1;
this.Text = result.ToString();
}

// in ghesmatiye k moshkel dare ta...
if (pictureBox1.Left < pictureBox3.Left) { timer1.Enabled = false; MessageBox.Show("شما باختید..ggggg.امتیاز " + result.ToString()); result = 0; }
if (pictureBox1.Left + pictureBox1.Width>= pictureBox3.Left &&
pictureBox1.Left + pictureBox1.Width <= pictureBox3.Left + pictureBox3.Width
&& pictureBox1.Top + pictureBox1.Height >= pictureBox3.Top &&
pictureBox1.Top + pictureBox1.Height <= pictureBox3.Top + pictureBox3.Height + pictureBox1.Height)
{
left = true;
result += 1;
this.Text = result.ToString();
}
// .... ta inja

if (left) pictureBox1.Left += speed; else pictureBox1.Left -= speed;// braye harkat b samt chap y rast b andaze meghdar moteghayer speed
if (top) pictureBox1.Top += speed; else pictureBox1.Top -= speed;
if (pictureBox1.Top >= this.Height - 50) top = false; // in 2 khat marbot b top k dar sorat inke bishtar az ertefa form ya kamtar az an beshe b samte mokhalef bargarde
if (pictureBox1.Top <= 0) top = true;
// if (pictureBox1.Left <= pictureBox3.Left) left = true;

//this.Text = speed.ToString();\
// pictureBox3.Top = pictureBox1.Top;






/* if (left) pictureBox1.Left += speed; else pictureBox1.Left -= speed;
if (top) pictureBox1.Top += speed; else pictureBox1.Top -= speed;
if (pictureBox1.Top >= this.Height - 50) top = false;
if (pictureBox1.Top <= 0) top = true;
if (pictureBox1.Left <= 0) left = false;*/
}


private void Form1_MouseMove(object sender, MouseEventArgs e)
{
pictureBox2.Top = e.Y; // کنترل کردن راکت سمت راست
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up) // کنترل کردن راکت سمت چپ
pictureBox3.Top -= 15;
else if (e.KeyCode == Keys.Down)
pictureBox3.Top += 15;
}
}
}




با تشکر

c_doost
پنج شنبه 28 اردیبهشت 1391, 12:46 عصر
دوستان این کد 14 بار دانلود شده یعنی کسی نمیتونه کمکم کنه؟؟؟

c_doost
جمعه 29 اردیبهشت 1391, 20:29 عصر
هیچکی نیست کمکم کنههه؟

Mahmoud.Afrad
جمعه 29 اردیبهشت 1391, 21:25 عصر
به این صورت اصلاح کنید:


int speed = 3;
int result;
bool top, left;

private void Form1_Load(object sender, EventArgs e)
{
Random rand = new Random();
pictureBox1.Location = new Point(20, rand.Next(this.Height));
top = left = true;
timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{ // in ghesmat marboot b Control shart baraye bargasht top b samt chap
if (pictureBox1.Left > pictureBox2.Left)
{
timer1.Enabled = false;
MessageBox.Show("شما باختید...امتیاز " + result.ToString());
result = 0;
}

if (pictureBox1.Right >= pictureBox2.Left && pictureBox1.Right <= pictureBox2.Right && pictureBox1.Bottom >= pictureBox2.Top && pictureBox1.Bottom <= pictureBox2.Bottom + pictureBox1.Height)
{
left = false;
result += 1;
this.Text = result.ToString();
}

if (pictureBox1.Left < pictureBox3.Left)
{
timer1.Enabled = false;
MessageBox.Show("شما باختید..ggggg.امتیاز " + result.ToString());
result = 0;
}

// in ghesmat marboot b Control shart baraye bargasht top b samt rast
if (pictureBox1.Left <= pictureBox3.Right && pictureBox1.Left >= pictureBox3.Left && pictureBox1.Bottom >= pictureBox3.Top && pictureBox1.Bottom <= pictureBox3.Bottom)
{
left = true;
result += 1;
this.Text = result.ToString();
}

if (left)
pictureBox1.Left += speed;
else
pictureBox1.Left -= speed;// braye harkat b samt chap y rast b andaze meghdar moteghayer speed
if (top)
pictureBox1.Top += speed;
else
pictureBox1.Top -= speed;
if (pictureBox1.Top >= this.Height - 50)
top = false; // in 2 khat marbot b top k dar sorat inke bishtar az ertefa form ya kamtar az an beshe b samte mokhalef bargarde
if (pictureBox1.Top <= 0)
top = true;
// if (pictureBox1.Left <= pictureBox3.Left) left = true;

//this.Text = speed.ToString();\
// pictureBox3.Top = pictureBox1.Top;


/* if (left) pictureBox1.Left += speed; else pictureBox1.Left -= speed;
if (top) pictureBox1.Top += speed; else pictureBox1.Top -= speed;
if (pictureBox1.Top >= this.Height - 50) top = false;
if (pictureBox1.Top <= 0) top = true;
if (pictureBox1.Left <= 0) left = false;*/
}


private void Form1_MouseMove(object sender, MouseEventArgs e)
{
pictureBox2.Top = e.Y; // کنترل کردن راکت سمت راست
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up) // کنترل کردن راکت سمت چپ
pictureBox3.Top -= 15;
else if (e.KeyCode == Keys.Down)
pictureBox3.Top += 15;
}