I’m new to programming and have a question

Hey guys,

I'd like to learn programming and decided to start with C# a week ago. I bought a book for beginners and read half of it already.

For better understanding I always try to make some small programs. Right now I'm working on a moving Square. However, my problem is that I have 2 panels right now. I want the moving panel (panel1) to change the color of the stationary panel (panel2) when touching it. But on the second touch I want it to turn back to it's old color.

Is it possible to do something "every other time"?

Heres is my code:

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 Test { public partial class Form1 : Form { public void kollision() { if (panel1.Bounds.IntersectsWith(panel2.Bounds)) { panel2.BackColor = Color.Green; } else { panel2.BackColor = Color.Yellow; } } public Form1() { InitializeComponent(); } private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.A) { timerLeft.Enabled = true; // TRUE timerRight.Enabled = false; timerUp.Enabled = false; timerDown.Enabled = false; } if (e.KeyCode == Keys.D) { timerLeft.Enabled = false; timerRight.Enabled = true; // TRUE timerUp.Enabled = false; timerDown.Enabled = false; } if (e.KeyCode == Keys.W) { timerLeft.Enabled = false; timerRight.Enabled = false; timerUp.Enabled = true; // TRUE timerDown.Enabled = false; } if (e.KeyCode == Keys.S) { timerLeft.Enabled = false; timerRight.Enabled = false; timerUp.Enabled = false; timerDown.Enabled = true; // TRUE } } private void timerRight_Tick(object sender, EventArgs e) { if (panel1.Location.X < 492) panel1.Location = new Point(panel1.Location.X + 3, panel1.Location.Y); kollision(); } private void timerLeft_Tick(object sender, EventArgs e) { if(panel1.Location.X > 0) panel1.Location = new Point(panel1.Location.X - 3, panel1.Location.Y); kollision(); } private void timerUp_Tick(object sender, EventArgs e) { if(panel1.Location.Y > 0) panel1.Location = new Point(panel1.Location.X, panel1.Location.Y - 3); kollision(); } private void timerDown_Tick(object sender, EventArgs e) { if (panel1.Location.Y < 470) panel1.Location = new Point(panel1.Location.X, panel1.Location.Y + 3); kollision(); } private void Form1_Load(object sender, EventArgs e) { } } } 

The problem is that the colors keep flickering when going through the second panel. I hope some more experienced users can help me with that.

The method "kollision()" is where I want to tell the program what to do on collision.

by Klausar via /r/csharp

Leave a Reply