So here is the basic code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
protected Random random;
public Form1()
{
InitializeComponent();
random = new Random();
}
private void Form1_Load(object sender, EventArgs e)
{ }
private void button1_Click(object sender, EventArgs e)
{
bool button1Clicked = true;
if (button1Clicked == true) { ITIpanel.Visible = true; }
}
private void ITIpanel_Paint(object sender, PaintEventArgs e)
{
ITItimer.Enabled = true;
}
private void ITItimer_Tick(object sender, EventArgs e)
{
double rand = random.NextDouble();
if (rand < .50d) { bluestimPanel.Visible = true; }
else if (rand > .5d) { redstimPanel.Visible = true; }
ITItimer.Enabled = false;
}
private void bluestimPanel_Paint(object sender, PaintEventArgs e)
{
Trialtimer.Enabled = true;
}
private void redstimPanel_Paint(object sender, PaintEventArgs e)
{
Trialtimer.Enabled = true;
}
private void Trialtimer_Tick(object sender, EventArgs e)
{
bluestimPanel.Visible = false;
redstimPanel.Visible = false;
Trialtimer.Enabled = false;
ITIpanel.Visible = true;
}
}
}
As you can see, the program itself is fairly straight forward. At the tick of the ITItimer the red or blue panels occurs at random. I want to modify this such that if the ITItimer ticks a total of 10 times, the red and blue panels will both have occurred 5 times each.
I have been researching this for a week or so and have yet to find a solution. Any ideas on how I could best accomplish this?
I actually got the following to work:
double rand = random.NextDouble();
if (rand < .50d && blue < 5) { bluestimPanel.Visible = true; }
else if (blue == 5) { redstimPanel.Visible = true; }
if (rand > .5d && red < 5) { redstimPanel.Visible = true; }
else if (red == 5) { bluestimPanel.Visible = true; }
if (red >= 5 && blue >= 5) { panel1.Visible = true; }
It isn't exactly the prettiest thing in the world. But it gets the job done.
Random numbers using most normal library routines are a low-quality source of pseudorandomness. If this is for a randomized scientific study, this will be a flaw in your protocol design.
The approach that I would recommend would be to consider this a method of randomly arranging a session of at least N trials, where there are X trial types.
The below is pseudocode for illustration of the concept.
Let MinimumTrials be N MOD X + X
Let SessionList be a List<Trial>
For Each TrialType
add X instances of that trial type to SessionList
Shuffle(SessionList)
Then your session engine can call the individual Trials as it walks through the SessionList to have an even distribution of possible trial orders. Note that Shuffle is an operation which requires a certain degree of finesse to get right, searching on SO is a good starting point for that.
Related
My question is very simple.
My Label1 should move continuously from left to right.
(Start from the left part of the form and go to the right part of the form. When it reaches the right part of the Form, go to the left and so on.) I have 2 timers(one for right and one for left)
I have the following code, it starts at the left and goes to the right, but when it reaches the right of the Form, it doesn't return.
Can anyone help me?
enum Position
{
Left,Right
}
System.Windows.Forms.Timer timerfirst = new System.Windows.Forms.Timer();
private int _x;
private int _y;
private Position _objPosition;
public Form1()
{
InitializeComponent();
if (label1.Location == new Point(0,180))
{
_objPosition = Position.Right;
}
if (label1.Location == new Point(690,0))
{
_objPosition = Position.Left;
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Label1.SetBounds(_x, _y, 0, 180);
}
private void tmrMoving_Tick(object sender, EventArgs e)
{
tmrMoving.Start();
if (_objPosition == Position.Right && _x < 710)
{
_x +=10;
}
if(_x == 710)
{
tmrMoving.Stop();
}
Invalidate();
}
private void tmrMoving2_Tick(object sender, EventArgs e)
{
tmrMoving2.Start();
if (_objPosition == Position.Right && _x > 690)
{
_x -= 10;
}
if(_x == 1)
{
tmrMoving2.Stop();
}
Invalidate();
}
Thanks.
I think you've overcomplicated things. Let's just have one timer, a step number of pixels that we sometimes flip negative, and some time later flip back to positive. We can move the label by repeatedly setting its Left:
public partial class Form1 : Form
{
private int _step = 10;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (label1.Right > this.Width) _step = -10;
if (label1.Left < 0) _step = 10;
label1.Left += _step;
}
}
You might want to fine tune it, but the basic motion is there
I am trying to create a game in Visual studios using C# where you have rows of sliding boxes that you must press a key in certain locations one after the other, kinda like stacker, before the time runs out.
I am using this as the basis but unsure how to adjust the speed among making it work in the first place since the code apparently runs with no issues but I only got it where the button just gives you a message box.
https://www.youtube.com/watch?v=O78n7apXjG8
Just asking for tips on where to go.
https://files.catbox.moe/x3wx22.zip
additional info for clarification
https://catbox.moe/c/fakpxe
// Project by 124
// Redid:
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;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
private int _ticks;
private object textBox1;
public Form1()
{
InitializeComponent();
timer1.Start();
}
//This should be the part that moves the button back and forth but it's not working
int Left = 140;
private void timer1_tick(object sender, EventArgs e)
{
Left += 10;
if (Left > 430)
{
Left = -138;
Left += Left;
if (Left > 140 )
{
} else
{
button1.Left = Left;
}
}
else
button1.Left = Left;
{
button1.Visible = false;
button2.Visible = true;
}
{
_ticks++;
this.Text = _ticks.ToString();
if (_ticks == 9)
{
this.Text = "Game Over";
timer1.Stop();
}
}
}
private void button1_Click(object sender, EventArgs e)
//since the movement button function is not working so is this one where it's supposed to give you the win but the win is there regardless
{
if (_ticks < 9) ;
{
MessageBox.Show("good job you beat the time");
MessageBox.Show("You are a winner");
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Restart();
Environment.Exit(0);
}
private void button3_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.ShowDialog();
}
}
}
Adjust the speed among making it work:
Left += Convert.ToInt32(label1.Text);
Control speed +1:
private void Button4_Click(object sender, EventArgs e) {
label1.Text = (Convert.ToInt32(label1.Text) + 1).ToString();
}
Control speed -1:
private void Button5_Click(object sender, EventArgs e) {
label1.Text = (Convert.ToInt32(label1.Text) - 1).ToString();
}
Updated:
Write at the top:
You can control the speed and position of the button by modifying the values of'V0' and'a'.
//current position
Left += 20* _ticks+(a* _ticks* _ticks)/2;//s = v0·t + a·t²/2
Set the initial time value outside the timer
private double _ticks = 0;
Set the initial position to 0.
double Left = 0;
Increase by 1 every second. Because internal is 0.2, add 0.2 every time.
_ticks+=0.2;
Modified timer1_tick:
private void timer1_tick(object sender, EventArgs e) {
//Set acceleration
double a = 0.5;
//Increase by 1 every second. Because internal is 0.2, add 0.2 every time.
_ticks += 0.2;
this.Text = _ticks.ToString();
//Time to stop at 9s
if (_ticks == 9) {
this.Text = "Game Over";
timer1.Stop();
MessageBox.Show("Game Over");
button1.Visible = false;
button2.Visible = true;
}
//current position
Left += 20 * _ticks + (a * _ticks * _ticks) / 2;//s = v0·t + a·t²/2
//When the total distance exceeds the set width on the interface, perform operations such as subtraction until the current position is less than the set value.
rtn:
if (Left > 300) {
Left -= 300;
if (Left > 300) {
goto rtn;//Use goto: Perform an iterative operation.
} else {
button1.Left = Convert.ToInt32(Left);//Cast double data type to int
}
} else
button1.Left = Convert.ToInt32(Left);
}
Modified button_Click: Don’t add ‘;’ after ‘if’, it will be useless.
private void button1_Click(object sender, EventArgs e) {
if (_ticks < 9) //Don’t add ‘;’ after ‘if’, it will be useless.
{
timer1.Stop();
button1.Visible = false;
button2.Visible = true;
MessageBox.Show("good job you beat the time");
MessageBox.Show("You are a winner");
}
}
Output:
I want to make an AI using PictureBox that can roam randomly without messing its movements like for example:
PictureBox must execute the movement to go Right, if the Timer runs out it the next movement is going Down, like just how random it would roam.
I'd thought I might figured it out to Hard code it. However might took long, also once the Timer Stops, it won't Restart again. idk why.
Here's a Picture of my Game so you would have some ideas about it.
Here's the code also
private void Game_Load(object sender, EventArgs e)
{
E_Right.Start();
if(Upcount == 0)
{
E_Right.Start();
}
}
private void E_Right_Tick(object sender, EventArgs e)
{
if(Rightcount > 0)
{
EnemyTank.Left += 5;
EnemyTank.BackgroundImage = Properties.Resources.EnemyTank_RIGHT_v_2;
Rightcount = Rightcount - 1;
}
if (Rightcount == 0)
{
E_Right.Stop();
E_Down.Start();
}
}
private void E_Up_Tick(object sender, EventArgs e)
{
if (Upcount > 0)
{
EnemyTank.Top -= 5;
EnemyTank.BackgroundImage = Properties.Resources.EnemyTank_TOP_v_1;
Upcount = Upcount - 1;
}
if (Upcount == 0)
{
E_Up.Stop();
E_Right.Start();
}
}
private void E_Down_Tick(object sender, EventArgs e)
{
if (Downcount > 0)
{
EnemyTank.Top += 5;
EnemyTank.BackgroundImage = Properties.Resources.EnemyTank_DOWN_v_1;
Downcount = Downcount - 1;
}
if (Downcount == 0)
{
E_Down.Stop();
E_Left.Start();
}
}
private void E_Left_Tick(object sender, EventArgs e)
{
if (Leftcount > 0)
{
EnemyTank.Left -= 5;
EnemyTank.BackgroundImage = Properties.Resources.EnemyTank_LEFT_v_1;
Leftcount = Leftcount - 1;
}
if (Leftcount == 0)
{
E_Left.Stop();
E_Up.Start();
}
}
Well just assume that the PictureBox is in Location(0,0). If you see a PictureBox an image of a Tank nevermind that. That goes for the MainTank of the user will be using.
You could do it with just one timer:
int moveTo = 0; // Move while this is not 0
int speed = 3;
Random rand = new Random();
// Keep track of whether the tank is moving up/down or left/right
enum MoveOrientation { Vertical, Horizontal };
MoveOrientation orientation = MoveOrientation.Vertical;
void ChooseNextPosition()
{
// Negative numbers move left or down
// Positive move right or up
do {
moveTo = rand.Next(-5, 5);
} while (moveTo == 0); // Avoid 0
}
private void Game_Load(object sender, EventArgs e) {
ChooseNextPosition();
timer1.Start();
}
private void timer1Tick(object sender, EventArgs e) {
if (orientation == MoveOrientation.Horizontal)
{
EnemyTank.Left += moveTo * speed;
}
else
{
EnemyTank.Top += moveTo * speed;
}
moveTo -= moveTo < 0 ? -1 : 1;
if (moveTo == 0)
{
// Switch orientation.
// If currently moving horizontal, next move is vertical
// And vice versa
orientation = orientation == MoveOrientation.Horizontal ? MoveOrientation.Vertical : MoveOrientation.Horizontal;
// Get new target
ChooseNextPosition();
}
}
I have a list
static List<Participants> soop = ParticipantRepository.GetAllParticipants();
It has some 800 items. Then there's a label and a timer. At timer_tick, I want to display one of the items randomly. Here's the code for that event
private void timer1_Tick(object sender, EventArgs e) {
foreach (var participants in soop)
{
a = participants.RollNumber;
label1.Text = a;
break;
}
counter++;
if (counter == 200) {
timer1.Stop();
pictureBox5.Visible = false;
counter = 0;
}
}
I have not been able to achieve the random functionality so far because only one RollNumber is being displayed and then the timer takes its time and runs out. What am I doing wrong?
I would suggest using the random class.
Random randomGen = new Random();
private void timer1_Tick(object sender, EventArgs e)
{
var i = randomGen.Next(0, soop.Count);
label1.Text = soop[i].RollNumber;
counter++;
if (counter == 200)
{
timer1.Stop();
pictureBox5.Visible = false;
counter = 0;
}
}
At each tick the timer1_Tick is called, so your foreach loop starts back from the beginning and you end up displaying the first item every time. Instead, you can store the index of the last item you displayed. You already have counter so let's use it:
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = soop[counter % soop.Count].RollNumber;
counter++;
if (counter == 200) {
timer1.Stop();
pictureBox5.Visible = false;
counter = 0;
}
}
I dont know if I'm doing this correctly but I have a grid and I loop through the grid to see if the items matched. If they do I want to make the row flash every 3 seconds. Right now what I have in my code just pretty much highlight the row but no flashing. Can anyone help take a look?
public static void CheckRow(int item, DataGridViewRow row)
{
List<int> col = new List<int>();
//call to db and add to col
foreach (var item in col)
{
if (item == col.Item)
{
currentRow = row;
Timer t = new Timer();
t.Interval = 3000;
t.Tick += new System.EventHandler(Highlight);
t.Start();
}
}
}
private static void Highlight(object sender, EventArgs e)
{
currentRow.DefaultCellStyle.BackColor = Color.Brown;
}
Wouldn't you need to change the color again (to the original) to have a flashing effect?
You should use Threading. Look at the code :)
bool go = false; //for changing cell color
int count = 10; //to stop timer (blinking)
public blinkForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
Thread a = new Thread(blink);
a.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
if (dataGridView1.Columns.Count == 0)
{
//generate new columns for DataGridView
dataGridView1.Columns.Add("user", "User");
dataGridView1.Columns.Add("pcStatus", "PC Status");
dataGridView1.Columns.Add("service", "Servis");
//generate new rows for DataGridView
dataGridView1.Rows.Add("Ali", "PC007", "chrome.exe");
dataGridView1.Rows.Add("Vusal", "PC010", "photoshop.exe");
dataGridView1.Rows.Add("Rahim", "PC015", "chrome.exe");
}
}
private void blink(object o)
{
while (count > 0)
{
while (!go)
{
//change color for binking
dataGridView1.Rows[0].Cells["service"].Style.BackColor = Color.Tomato;
go = true;
//stop for 0.5 second
Thread.Sleep(500);
}
while (go)
{
//change color for binking
dataGridView1.Rows[0].Cells["service"].Style.BackColor = Color.LimeGreen;
go = false;
//stop for 0.5 second
Thread.Sleep(500);
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
count--;
if (count == 0)
{
//stop blinking after 10 second
timer1.Stop();
}
}
Perhaps this, no?
private static void Highlight(object sender, EventArgs e)
{
currentRow.DefaultCellStyle.BackColor = Color.Brown;
System.Threading.Thread.Sleep(2000);
currentRow.DefaultCellStyle.BackColor = Color.White;
}