Could someone help me? I really don't know what to do. I don't want to restart my project even if there is little code inside it don't ask me why. Maybe because Im lazy. I think it is probably problems with the varibels I guess. As I said I don't know.
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 Project_Zomboid
{
public partial class Form1 : Form
{
int positiony = 0;
int positionx = 0;
public Form1()
{
InitializeComponent();
player.Controls.Add(ak47gun);
ak47gun.Location = new Point(0, 0);
ak47gun.BackColor = Color.Transparent;
Startup();
player.Location = new Point(positionx, positiony);
}
void Startup()
{
if (true == true)
{
}
}
private void keypress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.W)
{
positiony = 1;
}
else if(e.KeyCode == Keys.S)
{
positiony -= 1;
}
else if (e.KeyCode == Keys.D)
{
positionx = 1;
}
else if( e.KeyCode == Keys.A)
{
positionx -= 1;
}
}
}
}
CodePudding user response:
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 Project_Zomboid
{
public partial class Form1 : Form
{
int positiony = 0;
int positionx = 0;
Thread gameLoop;
public Form1()
{
InitializeComponent();
player.Controls.Add(ak47gun);
ak47gun.Location = new Point(0, 0);
ak47gun.BackColor = Color.Transparent;
gameLoop = new Thread(new ThreadStart(Startup));
gameLoop.Start();
}
public void Startup()
{
while(true)
{
player.Location = new Point(positionx, positiony);
Thread.Sleep(128);
}
}
private void keypress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.W)
{
positiony = 1;
}
else if(e.KeyCode == Keys.S)
{
positiony -= 1;
}
else if (e.KeyCode == Keys.D)
{
positionx = 1;
}
else if( e.KeyCode == Keys.A)
{
positionx -= 1;
}
}
}
}
I think that will get your image moving around the screen but, I would seriously suggest if you're interested in making games you should consider watching a lot of tutorials online on youtube. This movement isn't time sensitive so your picture will move as fast as the cpu can add to the x or y position. Which should be really fast. The sleep in the thread is to make sure your picture isn't always updating. I would first suggest a tutorial that explains delta time and velocity.
There are also a TON of free example games online that can give you a great example of how a lot of this is done.
Something else to note is if you don't want a game loop/thread update the player.Location = new Point(positionx, positiony) inside the keypress method after updating the integer.