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: