در اين بخش مي‌توانيد در مورد تمامي مباحث گرافيک و طراحي گرافيکي به بحث بپردازيد
Captain II

Captain II



نماد کاربر
پست ها

412

تشکر کرده: 0 مرتبه
تشکر شده: 0 مرتبه
تاريخ عضويت

سه شنبه 22 فروردین 1385 10:46

آرشيو سپاس: 64 مرتبه در 33 پست

تغيير در اپلت

توسط njmh » دوشنبه 27 خرداد 1387 21:59

سلام میخواستم توی این اپلت تغییری ایجاد کنم که یه مسیر حرکت براش تعریف شه و در صورت برخورد به دیواره های مسیر بازی متوقف شه
میشه راهنمایی کنید به من؟
import java.awt.*; // A few imports
import java.awt.event.*;
import java.applet.*;

public class dotAttack extends Applet implements MouseMotionListener, ActionListener{  // Make our class an Applet and let it use MouseMotionListener and ActionListener

     private Thread t = new Thread(); // a declaration for timing

     private static Button easyBut; //easy medium and hard button declarations
     private static Button medBut;
     private static Button hardBut;

     private int speed = 10; // the speed our dot moves

     private int ourX; // our current X and Y coordinate
     private int ourY;

     private int enemyX = 50; // the dot's current X and Y coordinate
     private int enemyY = 50;

     private int points = 0; // the amount of points we have

     public void actionPerformed(ActionEvent evt){ // when ever we click a button, this function is triggered
          if(evt.getSource() == easyBut) // if we click the easy button make the dot move slow speed
               speed = 15;

          if(evt.getSource() == medBut) // if we click the medium button make the dot move medium speed
               speed = 10;

          if(evt.getSource() == hardBut) // if we click the hard button make the dot move fast
               speed = 5;
//NOTE: putting this on hard earns points faster than on easy.
     }

     public void init(){

          addMouseMotionListener(this); //Make our applet check for movement of the mouse

          t.start(); // starting our timer

          easyBut = new Button("Easy"); //telling the easy, medium, and hard buttons what the captions should be
          medBut = new Button("Medium");
          hardBut = new Button("Hard");

          easyBut.addActionListener(this); // make the buttons wait to be clicked annd when they are cklicked, trigger the actionPerformed function
          medBut.addActionListener(this);
          hardBut.addActionListener(this);

          add(easyBut); //add the buttons to our form
          add(medBut);
          add(hardBut);
     }

     public void mouseMoved(MouseEvent e){ // when our applet senses mouse movement it triggers this function
          ourX = e.getX(); //storing the current X value of the mouse into a variable
          ourY = e.getY(); //storing the current Y value of the mouse into a variable
     }

     public void mouseDragged(MouseEvent e){ //when our applet senses the mouse being clicked and then dragged
          ourX = e.getX(); // storing the current X value of the mouse into a variable
          ourY = e.getY(); // storing the current Y value of the mouse into a variable
     }

     public void paint (Graphics g){//this is a standard function on most applets

          g.drawLine(0, 0, 500, 0); // drawing lines around the edges of the applet so we can try to avoid letting the dot hit the wall
          g.drawLine(500, 0, 500, 500);
          g.drawLine(500, 500, 0, 500);
          g.drawLine(0, 500, 0, 0);
            
                g.drawRect(50,100,150,50);
              
          g.drawRect(enemyX, enemyY, 2, 2); //drawing and filling the dot that chases us
          g.fillRect(enemyX, enemyY, 2, 2);

          g.drawString(points + " Points", 20, 60); //draw out points

          try{ //try whats in the curly braces
               t.sleep(speed); //wait for either 5, 10, or 15 milliseconds (Depends on difficulty level)
          } catch(InterruptedException ex){ //for some reason, we get an exception with the above line, this fixes the problem

          }
          touch(g); // a function i made that checks to see if our mouse is touching the dot
          whichDir(); // check to see which direction to move the dot so that it follows the mouse
          points++; // give us 1 point :D NOTE: this line is the same as 'points = points + 1;' Just shorter
     }

     public void whichDir(){ // our function that checks to see which way we should move the dot to follow the mouse
          if(ourX > enemyX) // if our mouse's X coord is higher than the enemy's, than move the enemy 1 spot closer in that direction
               enemyX++;

          if(ourX < enemyX) // if our mouse's X coord is lower than the enemy's, than move the enemy 1 spot closer in that direction
               enemyX--;

          if(ourY > enemyY) //if our mouse's Y coord is higher than the enemy's, than move the enemy 1 spot closer in that direction
               enemyY++;

          if(ourY < enemyY)//if our mouse's Y coord is lower than the enemy's, than move the enemy 1 spot closer in that direction
               enemyY--;

          repaint(); // trigger the paint function again(See how it is an endless loop?)

     }

     public void touch(Graphics g){ //our function to check if out mouse and the dot are touching
          if(ourX == enemyX && ourY == enemyY){ // if our x coord is the same as the enemy's and our Y coord is the same as the enemy's then do the stuff in the curly braces
               g.drawString("You Lost!", 50, 40); // draw on our applet that we lost
               
               points = 0; // reset our points back to 0
               try{ // same as the paint method
                    t.sleep(5000); // wait 5 second for the player to get ready to start again
               } catch(InterruptedException ex){

               }
          }

     }

}
مرا دوست بدار،اندكي ولي طولاني!

Captain II

Captain II



نماد کاربر
پست ها

412

تشکر کرده: 0 مرتبه
تشکر شده: 0 مرتبه
تاريخ عضويت

سه شنبه 22 فروردین 1385 10:46

آرشيو سپاس: 64 مرتبه در 33 پست

توسط njmh » چهارشنبه 29 خرداد 1387 23:21

تو رو خدا به من کمک کنيد
اينو ميدونم که بايد دونه دونه پيکسل مسيرها رو توي يه آرايه بفرستم براش ولي خوب چه جوري؟
مرا دوست بدار،اندكي ولي طولاني!

Captain

Captain



no avatar
پست ها

2736

تشکر کرده: 1473 مرتبه
تشکر شده: 306 مرتبه
تاريخ عضويت

چهارشنبه 18 بهمن 1385 12:46

محل سکونت

شیراز

آرشيو سپاس: 6007 مرتبه در 1644 پست

توسط Present » پنج شنبه 30 خرداد 1387 00:49

والا من زیاد سر در نمیارم ولی فکر کنم اگه از تو سایت زیر بپرسی جوابت رابدن
http://dotnetsource.com/fa/forum
پروانه نیستم که به یک شعله جان دهم / شمعم که سوزم و دودی نیاورم

گــــــــــــــــاهی تــــــــــــــاوان شیــــــــــر بودن قـــفس اســـت ...
امــــــــا شـــــغال هــــا در شـــــهــــر آزاد مـــــیگـــــــردنــــــــــد ... !

به زودی تعمیرکار لب تاپ می شم :-)


چه کسي حاضر است ؟

کاربران حاضر در اين انجمن: بدون كاربران آنلاين و 0 مهمان