I am currently working on a platformer in C#,VS 2015
The problem i have encountered is:when i press the left/right arrow,the background moves behind the player in the opposite direction,and this happens using a timer(which cannot be set to a value lower than 1 milisecond).
Because the timer is limited to 1 milisecond,the background moves laggy,and i would like to solve this :D
Here is the code
public Form1()
{
InitializeComponent();
}
Bitmap back;
Bitmap bobright;
Bitmap bobleft;
Bitmap bobimage;
Bitmap exit;
Point boblocation = Point.Empty;
Point exitlocation = Point.Empty;
float offSet = 0, vit;
int acc_secunde, acc_secunde_max = 30;
bool left = false, right = false;
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
public void Game()
{
int imageNumber = panel1.Width / back.Width + 3;
using (Bitmap frame = new Bitmap(panel1.Width, panel1.Height))
{
using (Graphics graph = Graphics.FromImage(frame))
{
for (int i = 0; i < imageNumber; i++)
{
graph.DrawImage(back, offSet/2 % back.Width + i * back.Width - back.Width, 0);
}
graph.DrawImage(bobimage, boblocation);
graph.DrawImage(exit,exitlocation);
}
using (Graphics graph = panel1.CreateGraphics())
{
graph.DrawImage(frame, Point.Empty);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
back = Properties.Resources.back;
bobleft = Properties.Resources.bobleft;
bobright = Properties.Resources.bobright;
bobimage = bobright;
exit = Properties.Resources.Exit;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
boblocation.X = panel1.Width / 2 - bobimage.Width / 2;
boblocation.Y = 210;
exitlocation.X = panel1.Width - exit.Width - 10;
exitlocation.Y = 5;
if (left)
{
offSet = offSet + acc_secunde;
bobimage = bobleft;
}
if(right)
{
bobimage = bobright;
offSet = offSet - acc_secunde;
}
Game();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Left)
{
acceleratie.Start();
left = true;
}
if(e.KeyCode == Keys.Right)
{
acceleratie.Start();
right = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Left)
{
acceleratie.Stop();
deacceleratie.Start();
}
if(e.KeyCode == Keys.Right)
{
acceleratie.Stop();
deacceleratie.Start();
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
}
private void deacceleratie_Tick(object sender, EventArgs e)
{
if(acc_secunde > 0)
acc_secunde = acc_secunde - acc_secunde_max / 3;
if (acc_secunde <= 0)
{
deacceleratie.Stop();
if (right == true)
right = false;
if (left == true)
left = false;
acc_secunde = 0;
}
}
private void acceleratie_Tick(object sender, EventArgs e)
{
if(acc_secunde< acc_secunde_max)
acc_secunde+=2;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
Point mousePt = new Point(e.X, e.Y);
if (mousePt.X > panel1.Width - exit.Width - 10 && mousePt.X < panel1.Width - 10 && mousePt.Y > panel1.Top + 10 && mousePt.Y < panel1.Top + 10 + exit.Height)
Application.Exit();
}
}
}
Related
I am a novice in programming and i need Your advice, please.
I was tasked with writing a programm that will be counting number of items needed according to length of a track. This track we need to draw by ourselves, but I don't want to make this application full screen, so I need a Zooming function and for a life of me I can't get how should i do it.
Please help!
And here's the code.
namespace RFIDGovno
{
public partial class Form1 : Form
{
List<Line> lines = new List<Line>();
List<Dot> dots = new List<Dot>();
bool isVert;
Point sPlace;
Point ePlace;
bool downed=false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
Point temp = e.Location;
if (downed)
{
if (!(Math.Abs(e.X - sPlace.X) > Math.Abs(e.Y - sPlace.Y)))
{
temp.X = sPlace.X;
}
else
{
temp.Y = sPlace.Y;
}
downed = false;
ePlace = temp;
dots.Add(new Dot(ePlace));
lines.Add(new Line(sPlace,ePlace,isVert, isVert ? Math.Abs(sPlace.Y-ePlace.Y) : Math.Abs(sPlace.X - ePlace.X)));
}else
{
if (!dots.Any<Dot>())
{
downed = true;
sPlace = e.Location;
dots.Add(new Dot(sPlace));
}
else if(dots.Where(t => t.fuse).Any<Dot>())
{
downed = true;
sPlace = dots.Where(t => t.fuse).First().place;
}
}
drawing(null);
}
private void Zooming()
{
}
private void drawing(Point? mouse)
{
Graphics g = pictureBox1.CreateGraphics();
Bitmap im = new Bitmap(pictureBox1.Width-1, pictureBox1.Height-1);
Graphics q = Graphics.FromImage(im);
q.Clear(Color.White);
lines.ForEach(line =>
{
q.DrawLine(new Pen(Brushes.Black, 2), line.pStart, line.pEnd);
q.DrawString(line.length.ToString(), new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Black, line.isVert ? line.pStart.X : line.pStart.X + ((line.pEnd.X - line.pStart.X) / 2) - 5, line.isVert ? line.pStart.Y + ((line.pEnd.Y - line.pStart.Y) / 2) - 5 : line.pStart.Y);
});
if (downed)
{
q.DrawLine(new Pen(Brushes.Black, 2), sPlace, mouse ?? sPlace);
if ((isVert)&&(mouse.HasValue))
{
q.DrawString(Math.Abs(sPlace.Y-mouse.Value.Y).ToString(), new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Black, sPlace);
}
else if ((isVert==false) && (mouse.HasValue))
{
q.DrawString(Math.Abs(sPlace.X - mouse.Value.X).ToString(), new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Black, sPlace);
}
}
dots.ForEach(dot =>
{
q.DrawEllipse(new Pen(Brushes.Black, 2),dot.place.X-1, dot.place.Y - 1, 2, 2);
if (dot.fuse)
{
q.DrawEllipse(new Pen(Brushes.Red, 2), dot.place.X - 2, dot.place.Y - 2, 4, 4);
}
});
g.DrawImage(im, 1, 1);
q.Dispose();
im.Dispose();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
Point temp = e.Location;
if (downed)
{
if (!(Math.Abs(e.X - sPlace.X) > Math.Abs(e.Y - sPlace.Y)))
{
temp.X = sPlace.X;
isVert = true;
}
else
{
temp.Y = sPlace.Y;
isVert = false;
}
}
else
{
dots.ForEach(t => t.fuse = false);
List<Dot> tDots = dots.Where(dt => Math.Abs(e.Location.X - dt.place.X) < 20 && Math.Abs(e.Location.Y - dt.place.Y) < 20).ToList();
if (tDots.Any<Dot>())
{
double min = 0;
min = tDots.Min(t => distance(t.place, e.Location));
tDots.Where(t => distance(t.place, e.Location) == min).First().fuse = true;
}
}
drawing(temp);
}
private double distance(Point p1, Point p2)
{
double dist=0;
dist = (Math.Sqrt(Math.Abs(p1.X-p2.X)+Math.Abs(p1.Y-p2.Y)));
return dist;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void tabPage1_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void tabPage2_Click(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
int between = 0;
string curr = comboBox1.SelectedItem.ToString();
if (curr == "Short")
{
between = 1250;
}
else if (curr == "Medium")
{
between = 1875;
}
else
{
between = 3750;
}
double wut = (double.Parse(textBox1.Text) / between * 300 + double.Parse(textBox3.Text) + double.Parse(textBox2.Text) * 2);
string answer = Convert.ToString(Math.Ceiling(wut));
label4.Text = answer;
}
}
public class Line
{
public Point pEnd;
public Point pStart;
public bool isVert;
public int length;
public Line(Point pStart, Point pEnd, bool isVert,int length)
{
this.pStart = pStart;
this.pEnd = pEnd;
this.isVert = isVert;
this.length = length;
}
}
public class Dot
{
public bool fuse;
public Point place;
public Dot(Point place)
{
this.place = place;
}
}
}
Stack Overflow users. In a C# VS 2010 Windows Form project I have a problem regarding control flicker when dragging a user created control around on a tab page during run time. I used the following code:
private void control_MouseMove(object sender, MouseEventArgs e)
{
if (isDragged)
{
Point newPoint = ((Control)sender).PointToScreen(new Point(e.X,
e.Y));
newPoint.Offset(ptOffset);
((Control)sender).Location = newPoint;
((Control)sender).Refresh();
}
}
private void control_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragged = true;
Point ptStartPosition = ((Control)sender).PointToScreen(new
Point(e.X, e.Y));
ptOffset = new Point();
ptOffset.X = ((Control)sender).Location.X - ptStartPosition.X;
ptOffset.Y = ((Control)sender).Location.Y - ptStartPosition.Y;
}
else
{
isDragged = false;
}
}
private void control_MouseUp(object sender, MouseEventArgs e)
{
((Control)sender).Refresh();
isDragged = false;
}
private void createButton_PB_Click(object sender, EventArgs e)
{
int ctrlExists = 0;
string btnName = btnName_TB.Text;
foreach (Button button in tabControl1.SelectedTab.Controls)
{
if (button.Text == btnName)
{
ctrlExists = 1;
}
}
if (btnName_TB.Text != "" && ctrlExists == 0)
{
Button newButton = new Button();
newButton.Name = btnName.Replace(" ", String.Empty);
newButton.Name += "u";
newButton.Text = btnName;
tabControl1.SelectedTab.Controls.Add(newButton);
newButton.Left = 10;
newButton.Top = 420;
lastBtnClicked = newButton;
}
SetupClickEvents(tabControl1.SelectedTab);
}
So, the problem is that I can add a button and drag it around in run time. But, when I add another Button and drag it around...after I've done that, and go back to trying to drag the first button, that button flickers and acts as if it is trying to move all over the place. Sometimes it disappears. I feel like this has something to do with the fact that the controls are inside a tab page. Perhaps I am not properly calculating the "newPoint" variable. Any ideas guys?
Ok, so I found a few fundamental flaws related to the button creation and the events that were being added at the time of creation. I made some considerable changes and the issue seems to have gone away. Following is the updated code.
private void control_MouseMove(object sender, MouseEventArgs e)
{
if (isDragged)
{
Point newPoint = ((Control)sender).PointToScreen(new Point(e.X,
e.Y));
newPoint.Offset(ptOffset);
((Control)sender).Location = newPoint;
((Control)sender).Refresh();
}
}
private void control_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && checkBox1.Checked)
{
isDragged = true;
((Control)sender).MouseMove += new
MouseEventHandler(control_MouseMove);
Point ptStartPosition = ((Control)sender).PointToScreen(new
Point(e.X, e.Y));
ptOffset = new Point();
ptOffset.X = ((Control)sender).Location.X - ptStartPosition.X;
ptOffset.Y = ((Control)sender).Location.Y - ptStartPosition.Y;
}
else
{
isDragged = false;
}
}
private void control_MouseUp(object sender, MouseEventArgs e)
{
((Control)sender).MouseMove -= control_MouseMove;
((Control)sender).Refresh();
isDragged = false;
}
private void SetupClickEvents(Control control)
{
control.Click += new EventHandler(StoreLastClick);
control.MouseDown += new MouseEventHandler(control_MouseDown);
//control.MouseMove += new MouseEventHandler(control_MouseMove);
control.MouseUp += new MouseEventHandler(control_MouseUp);
}
private void createButton_PB_Click(object sender, EventArgs e)
{
ctrlExists = 0;
string btnName = btnName_TB.Text;
foreach (Button button in tabControl1.SelectedTab.Controls)
{
if (button.Name == btnName)
{
ctrlExists = 1;
}
}
if (btnName_TB.Text != "" && ctrlExists == 0)
{
Button newButton = new Button();
newButton.Name = btnName.Replace(" ", String.Empty);
newButton.Text = btnName;
tabControl1.SelectedTab.Controls.Add(newButton);
newButton.Left = 10;
newButton.Top = 420;
SetupClickEvents(newButton);
}
}
private void deleteButton_PB_Click(object sender, EventArgs e)
{
ctrlExists = 0;
if (lastCtrlClicked != null)
{
string btnName = lastCtrlClicked.Name;
foreach (Button button in tabControl1.SelectedTab.Controls)
{
if (button.Name == btnName)
{
ctrlExists = 1;
}
}
}
if (ctrlExists == 1 && lastCtrlClicked != null)
{
tabControl1.SelectedTab.Controls.Remove(lastCtrlClicked);
lastCtrlClicked.Dispose();
ctrlExists = 0;
}
lastCtrlClicked = null;
}
I'm trying to re-create flappybird in Visual studio 2015 c# for a little school project. But for some reason i get this error that i really can't fix. I'm following an tutorial on how to create flappybird, but the one making the tutorial is writing in VB.net Heres the YT Link and under that my code I'm trying to make.
https://www.youtube.com/watch?v=tnjdMbdEzMo
public partial class Form10 : Form
{
int gravity = 1;
int yspeed = 0;
PictureBox[,] Pipe;
public Form10()
{
InitializeComponent();
}
private void gameTimer_Tick(object sender, EventArgs e)
{
int i;
this.yspeed += this.gravity;
bird.Top += this.yspeed;
}
private void inGameKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
this.yspeed = -15;
}
}
private void pausePlayToolStripMenuItem_Click(object sender, EventArgs e)
{
if (gameTimer.Enabled == true)
{
gameTimer.Enabled = false;
}
else
{
if (gameTimer.Enabled == false)
{
gameTimer.Enabled = true;
}
}
}
private void restartToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void startGame_Click(object sender, EventArgs e)
{
if (gameTimer.Enabled == false)
{
gameTimer.Enabled = true;
startGame.Enabled = false;
}
}
private void CreatePipes(int number)
{
int i = 0;
for (i = 0; (i <= number); i++)
{
var temp = new PictureBox();
this.Controls.Add(temp);
temp.Width = 50;
temp.Height = 370;
temp.BorderStyle = BorderStyle.FixedSingle;
temp.BackColor = Color.Red;
temp.Top = 50;
temp.Left = (2 * 200) + 300;
Pipe(i) = temp;
Pipe(i).Visable = true;
}
}
private void Form10_Load(object sender, EventArgs e)
{
gameTimer.Enabled = true;
CreatePipes(1);
}
}
}
The problem you are seeing is in the lines
Pipe(i) = temp;
Pipe(i).Visable = true;
If you are trying to access Pipe as an array, the syntax is Pipe[i], although pipe is a 2d array so it should be Pipe[i,j] Where j is something else.
Also you have misspelled Visible.
using System;
using System.Drawing;
using System.IO.Ports;
using System.Windows.Forms;
namespace ek_zıplama
{
public partial class Form1 : Form
{
public enum Directions
{
right,
left,
up,
}
private Directions car_direction;
public SerialPort myPort;
int G = 15;
int force;
bool jump;
public string DATA;
public Form1()
{
InitializeComponent();
myPort = new SerialPort();
myPort.BaudRate = 9600;
myPort.PortName = "COM6";
myPort.Open();
jump = false;
}
private void Form1_Load(object sender, EventArgs e)
{
moves();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (jump)
{
car.Top -= force;
force -= 1;
}
//using block to stay in same position when car is stopped
if (car.Left + car.Width - 1 > block.Left && car.Left + car.Width + 5 < block.Left + block.Width + car.Width
&& car.Top + car.Height >= block.Top && car.Top < block.Top)
{
car.Top = ekran.Height - block.Height - car.Height;
force = 0;
jump = false;
}
}
private void moves()
{
if (label2.Text == "10111" && car_direction != Directions.right)
{
car.Location = new Point(car.Location.X + 130, car.Location.Y);
car_direction = Directions.right;
}
if (label2.Text == "01111" && car_direction != Directions.left)
{
car.Location = new Point(car.Location.X - 130, car.Location.Y);
car_direction = Directions.left;
}
if (!jump && label2.Text == "11011")
{
jump = true;
force = G;
}
}
private void timer3_Tick(object sender, EventArgs e)
{
DATA = myPort.ReadExisting();
label2.Text = DATA;
}
}
}
What I botch about that in move function.
I tried to create a function like
*if DATA is 01111 then my car turns left
*if DATA is 10111 then my car turns right
*if DATA is 11011 then my car jumps
Before I changed,car was controlled with keyboards.Everything was same but "move"function.And it was working.What were instead od "move" function is:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right&& car_direction != Directions.right)
{
car.Location = new Point(car.Location.X + 130, car.Location.Y);
car_direction = Directions.right;
}
if (e.KeyCode == Keys.Left && car_direction != Directions.left)
{
car.Location = new Point(car.Location.X - 130, car.Location.Y);
car_direction = Directions.left;
}
if (!jump && e.KeyCode == Keys.Up)
{
jump = true;
force = G;
}
}
Hope you get me :)
I'm quite sure it's not going to work if you only call it once in your Form_Load event. Since you maybe want your car to move each time the data in the label has changed, you should just make a reference to the function you created. That means, that you should just put moves(); in private void timer3_Tick(object sender, EventArgs e):
private void timer3_Tick(object sender, EventArgs e)
{
DATA = myPort.ReadExisting();
label2.Text = DATA;
moves();
}
In this way, each time the timer ticks, your car is going to move according to the new data in the label.
When I was doing something with Arduino I used something like this:
private void timer_Tick(object sender, EventArgs e)
{
if (serialPort != null && serialPort.IsOpen && serialPort.BytesToRead > 0)
{
data_as_string += serialPort.ReadLine();
}
}
I'm not sure how DATA is sent, but you can send single lines from Arduino using Serial.write("your line here");. Receiving and parsing data from here is very simple, as you could just read lines 01111, 10111, etc. and use any logic to manipulate the car. I hope this helps.
I made a script for 1 form with 2 pictureboxes, until here everything is fine.
If you execute the code below, than you can see you can move the picturebox1 and also drop it inside picturebox2. Now i would like that the dropped picturebox1 can be resized, rotated and moved around inside picturebox2 (once executed by client).
I have looked around but can not find the answers to this problem. Any help i would appreciate, Thank you
Here is the code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int x = 0;
int y = 0;
bool drag = false;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
x = e.X;
y = e.Y;
drag = true;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (drag)
{
//position new get
pictureBox1.Top += e.Y - y;
pictureBox1.Left += e.X - x;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
drag = false;
}
private void pictureBox2_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void pictureBox2_DragDrop(object sender, DragEventArgs e)
{
pictureBox2.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox2.AllowDrop = true;
}
}
}
In order to resize the pictureboxes you can define the following methods:
private void IncreaseSize(PictureBox p,int dt)
{
Size size = p.Size;
size.Height = size.Height + dt;
size.Width=size.Width + dt;
p.Size = size;
}
private void DecreaseSize(PictureBox p, int dt)
{
Size size = p.Size;
size.Height = size.Height - dt;
size.Width = size.Width - dt;
p.Size = size;
}
These methods can be called to events that you decide in your main form e.g:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
IncreaseSize(pictureBox1,5);
}
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
DecreaseSize(pictureBox2, 10);
}