Ich mache ein kleines Programm, bei dem zwei Rechtecke um eine Rennwagenspur fahren. Wenn ich das Programm laufe, läuft alles wie geplant und ich kann die Rechtecke mit den Pfeiltasten für die eine und A, S, D, W für die andere um die Spur bewegen. Das Problem ist, dass wenn ich eins mit den Pfeiltasten bewege und ich versuche, D zu drücken, um das andere Rechteck gleichzeitig nach rechts zu bewegen, stoppt dasjenige, das mit den Pfeiltasten bewegt wird. Ziel ist es, dass sie sich gleichzeitig bewegen können. Was soll ich machen?C#: Zwei Rechtecke gleichzeitig mit verschiedenen Tasten bewegen
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Race_Game
{
public partial class Form1 : Form
{
private int x1 = 24;
private int y1 = 16;
private int size1 = 115;
private int size2 = 50;
private Rectangle _rect1;
private int x2 = 24;
private int y2 = 74;
private int size3 = 115;
private int size4 = 50;
private Rectangle _rect2;
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Paint_1(object sender, PaintEventArgs e)
{
_rect1 = new Rectangle(x1, y1, size1, size2);
e.Graphics.FillRectangle(Brushes.Red, _rect1);
_rect2 = new Rectangle(x2, y2, size3, size4);
e.Graphics.FillRectangle(Brushes.Black, _rect2);
}
private void pictureBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Right)
{
x1 += 15;
}
if (e.KeyData == Keys.Left)
{
x1 -= 15;
}
if (e.KeyData == Keys.Up)
{
y1 -= 15;
}
if (e.KeyData == Keys.Down)
{
y1 += 15;
}
if (e.KeyData == Keys.D)
{
x2 += 15;
}
if (e.KeyData == Keys.A)
{
x2 -= 15;
}
if (e.KeyData == Keys.W)
{
y2 -= 15;
}
if (e.KeyData == Keys.S)
{
y2 += 15;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
}
}
Visual Studio generiert, Design, Code:
namespace Race_Game
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// pictureBox1
//
this.pictureBox1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("pictureBox1.BackgroundImage")));
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(1944, 1066);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint_1);
this.pictureBox1.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.pictureBox1_PreviewKeyDown);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1916, 1053);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint_1);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.PictureBox pictureBox1;
}
}
Dies ist ein häufiges Designproblem. Die Frage sollte offen bleiben. – ja72
'private Rectangle _rect1 = new Rectangle();' wird nicht benötigt, weil 'Rectangle' ein Werttyp ist und auch beim Paint-Ereignis überschrieben wird. – ja72
Danke für den Tipp, aber es hat das Problem nicht gelöst:/@ ja72 – jholsch29