Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using SlimDX;
  11. using SlimDX.DirectInput;
  12.  
  13. namespace JoystickApplication
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         DirectInput Input = new DirectInput();
  18.         SlimDX.DirectInput.Joystick stick;
  19.         Joystick[] sticks;
  20.         Joystick stick1;
  21.  
  22.         JoystickState state;
  23.  
  24.         int Xaxis;
  25.         int Yaxis;
  26.  
  27.         int posY;
  28.         int posX;
  29.         bool[] buttons;
  30.         SolidBrush brush;
  31.         SolidBrush brush2;
  32.  
  33.         int score;
  34.         //int licznik=600;
  35.         int seconds=30;
  36.         int temp = 20;
  37.  
  38.         //bool isCatched = false;
  39.         int squareX;
  40.         int squareY;
  41.         Random rand = new Random();
  42.  
  43.         public Form1()
  44.         {
  45.             InitializeComponent();
  46.             sticks=GetSticks();
  47.             stick1 = sticks[0];
  48.             UpdateStatus(stick1);
  49.             timer.Enabled = true;
  50.             posY = (int)panel1.Height / 2;
  51.             posX = (int)panel1.Width / 2;
  52.             brush = new SolidBrush(Color.Red);
  53.             brush2 = new SolidBrush(Color.Black);
  54.             timer.Interval = 50;
  55.             gameTimer.Interval = 50;
  56.             RandSquare();
  57.             gameTimer.Start();
  58.            
  59.             score = 0;
  60.             //FindButton();
  61.         }
  62.  
  63.         public Joystick[] GetSticks()
  64.         {
  65.             List<SlimDX.DirectInput.Joystick> sticks = new List<SlimDX.DirectInput.Joystick>(); // Creates the list of joysticks connected to the computer via USB.
  66.  
  67.             foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
  68.             {
  69.                 // Creates a joystick for each game device
  70.                 try
  71.                 {
  72.                     stick = new SlimDX.DirectInput.Joystick(Input, device.InstanceGuid);
  73.                     stick.Acquire();
  74.  
  75.                     foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
  76.                     {
  77.                         if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
  78.                             stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
  79.                     }
  80.  
  81.                     // Adds how ever many joysticks are connected to the computer into the sticks list.
  82.                     sticks.Add(stick);
  83.                 }
  84.  
  85.                 catch (DirectInputException)
  86.                 {
  87.                 }
  88.             }
  89.             Console.WriteLine(sticks.Count);
  90.             return sticks.ToArray();
  91.         }
  92.  
  93.         public void UpdateStatus(Joystick js)
  94.         {
  95.             js.Poll();
  96.             state = js.GetCurrentState();
  97.             buttons = state.GetButtons();
  98.             Xaxis = state.X;
  99.             Yaxis = state.Y;
  100.         }
  101.  
  102.         public void FindButton()
  103.         {
  104.                 int length = buttons.Length;
  105.  
  106.                 for(int i=0;i < length; i++)
  107.                 {
  108.                     if (buttons[i])
  109.                     {
  110.                        // textBox1.Text="Button " + i + " pressed";
  111.                         break;
  112.                     }
  113.                 }
  114.            
  115.         }
  116.  
  117.         private void timer_Tick(object sender, EventArgs e)
  118.         {
  119.             UpdateStatus(stick1);
  120.            
  121.            
  122.            
  123.            
  124.             if (buttons[0])
  125.             {
  126.                 brush = new SolidBrush(Color.Green);
  127.                 if (posX<=squareX+29 && posX>=squareX-29 && posY<=squareY+29 && posY>=squareY-29)
  128.                 {
  129.                     RandSquare();
  130.                     score++;
  131.                     label2.Text = score.ToString();
  132.                 }
  133.             }
  134.             else              
  135.             {
  136.                 brush = new SolidBrush(Color.Red);
  137.             }
  138.  
  139.             moveSquare();
  140.  
  141.             //FindButton();
  142.             if (posX <= 10)
  143.             {
  144.                 posX = 5;
  145.             }
  146.             if (posY <= 10)
  147.             {
  148.                 posY = 5;
  149.             }
  150.             if (posX >= panel1.Width-20)
  151.             {
  152.                 posX = panel1.Width - 15;
  153.             }
  154.             if (posY >= panel1.Height-20)
  155.             {
  156.                 posY = panel1.Height - 15;
  157.             }
  158.            
  159.  
  160.            
  161.             panel1.Invalidate();
  162.         }
  163.  
  164.         private void panel1_Paint(object sender, PaintEventArgs e)
  165.         {
  166.            
  167.             paintSquare(squareX, squareY, brush2, 30,10);
  168.             paintSquare(posX, posY, brush, 20,5);
  169.  
  170.         }
  171.  
  172.         private void paintSquare(int posX, int posY, SolidBrush sb, int size, int difference)
  173.         {
  174.             Graphics g = panel1.CreateGraphics();
  175.             Pen p = new Pen(Color.Black);
  176.             g.DrawRectangle(p, posX - difference, posY - difference, size, size);
  177.             g.FillRectangle(sb, posX - difference, posY - difference, size, size);
  178.         }
  179.  
  180.         private void moveSquare()
  181.         {
  182.             if (Xaxis > 60)
  183.             {
  184.                 posX -= 10;
  185.             }
  186.             if (Xaxis < -60)
  187.             {
  188.                 posX += 10;
  189.             }
  190.             if (Yaxis > 60)
  191.             {
  192.                 posY -= 10;
  193.             }
  194.             if (Yaxis < -60)
  195.             {
  196.                 posY += 10;
  197.             }
  198.         }
  199.  
  200.         private void RandSquare()
  201.         {
  202.             squareX = rand.Next(10, panel1.Width - 10);
  203.             squareY = rand.Next(10, panel1.Height - 10);
  204.         }
  205.  
  206.         private void gameTimer_Tick(object sender, EventArgs e)
  207.         {
  208.             temp--;
  209.  
  210.             if (temp <= 0 && seconds>0)
  211.             {
  212.                 seconds--;
  213.                 label5.Text = seconds.ToString();
  214.                 temp = 20;
  215.  
  216.             }
  217.  
  218.             if (seconds <= 0)
  219.             {
  220.                 timer.Stop();
  221.             }
  222.         }
  223.  
  224.         //button backcolor
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement