Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using SlimDX;
- using SlimDX.DirectInput;
- namespace JoystickApplication
- {
- public partial class Form1 : Form
- {
- DirectInput Input = new DirectInput();
- SlimDX.DirectInput.Joystick stick;
- Joystick[] sticks;
- Joystick stick1;
- JoystickState state;
- int Xaxis;
- int Yaxis;
- int posY;
- int posX;
- bool[] buttons;
- SolidBrush brush;
- SolidBrush brush2;
- int score;
- //int licznik=600;
- int seconds=30;
- int temp = 20;
- //bool isCatched = false;
- int squareX;
- int squareY;
- Random rand = new Random();
- public Form1()
- {
- InitializeComponent();
- sticks=GetSticks();
- stick1 = sticks[0];
- UpdateStatus(stick1);
- timer.Enabled = true;
- posY = (int)panel1.Height / 2;
- posX = (int)panel1.Width / 2;
- brush = new SolidBrush(Color.Red);
- brush2 = new SolidBrush(Color.Black);
- timer.Interval = 50;
- gameTimer.Interval = 50;
- RandSquare();
- gameTimer.Start();
- score = 0;
- //FindButton();
- }
- public Joystick[] GetSticks()
- {
- List<SlimDX.DirectInput.Joystick> sticks = new List<SlimDX.DirectInput.Joystick>(); // Creates the list of joysticks connected to the computer via USB.
- foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
- {
- // Creates a joystick for each game device
- try
- {
- stick = new SlimDX.DirectInput.Joystick(Input, device.InstanceGuid);
- stick.Acquire();
- foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
- {
- if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
- stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
- }
- // Adds how ever many joysticks are connected to the computer into the sticks list.
- sticks.Add(stick);
- }
- catch (DirectInputException)
- {
- }
- }
- Console.WriteLine(sticks.Count);
- return sticks.ToArray();
- }
- public void UpdateStatus(Joystick js)
- {
- js.Poll();
- state = js.GetCurrentState();
- buttons = state.GetButtons();
- Xaxis = state.X;
- Yaxis = state.Y;
- }
- public void FindButton()
- {
- int length = buttons.Length;
- for(int i=0;i < length; i++)
- {
- if (buttons[i])
- {
- // textBox1.Text="Button " + i + " pressed";
- break;
- }
- }
- }
- private void timer_Tick(object sender, EventArgs e)
- {
- UpdateStatus(stick1);
- if (buttons[0])
- {
- brush = new SolidBrush(Color.Green);
- if (posX<=squareX+29 && posX>=squareX-29 && posY<=squareY+29 && posY>=squareY-29)
- {
- RandSquare();
- score++;
- label2.Text = score.ToString();
- }
- }
- else
- {
- brush = new SolidBrush(Color.Red);
- }
- moveSquare();
- //FindButton();
- if (posX <= 10)
- {
- posX = 5;
- }
- if (posY <= 10)
- {
- posY = 5;
- }
- if (posX >= panel1.Width-20)
- {
- posX = panel1.Width - 15;
- }
- if (posY >= panel1.Height-20)
- {
- posY = panel1.Height - 15;
- }
- panel1.Invalidate();
- }
- private void panel1_Paint(object sender, PaintEventArgs e)
- {
- paintSquare(squareX, squareY, brush2, 30,10);
- paintSquare(posX, posY, brush, 20,5);
- }
- private void paintSquare(int posX, int posY, SolidBrush sb, int size, int difference)
- {
- Graphics g = panel1.CreateGraphics();
- Pen p = new Pen(Color.Black);
- g.DrawRectangle(p, posX - difference, posY - difference, size, size);
- g.FillRectangle(sb, posX - difference, posY - difference, size, size);
- }
- private void moveSquare()
- {
- if (Xaxis > 60)
- {
- posX -= 10;
- }
- if (Xaxis < -60)
- {
- posX += 10;
- }
- if (Yaxis > 60)
- {
- posY -= 10;
- }
- if (Yaxis < -60)
- {
- posY += 10;
- }
- }
- private void RandSquare()
- {
- squareX = rand.Next(10, panel1.Width - 10);
- squareY = rand.Next(10, panel1.Height - 10);
- }
- private void gameTimer_Tick(object sender, EventArgs e)
- {
- temp--;
- if (temp <= 0 && seconds>0)
- {
- seconds--;
- label5.Text = seconds.ToString();
- temp = 20;
- }
- if (seconds <= 0)
- {
- timer.Stop();
- }
- }
- //button backcolor
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement