Autor Beitrag
Teekeks
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 211
Erhaltene Danke: 23



BeitragVerfasst: Mi 25.02.09 19:44 
hi!
Ich habe ein Problem.
Das um was es hier geht soll mal ein kleines Spiel werden.
Ich habe als Spielfeld bis jetzt unterschiedlich Farbige Felder.
OnMouseDown soll das Feld auf das ich gerade Klicke andersfarbig gemacht werden.
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var xx,yy:integer;
begin
  for xx:=1 to 60 do
    for yy:=1 to 60 do
    begin
      if (x in [(Feld[xx,yy].Pos.X)..(Feld[xx,yy].Pos.X)+14]) and (y in [(Feld[xx,yy].Pos.Y)..(Feld[xx,yy].Pos.Y)+14]) then
      begin
        if Button=mbLeft then Feld[xx,yy].Land:=Wasser else
        if Button=mbMiddle then Feld[xx,yy].Land:=Gebirge else
        Feld[xx,yy].Land:=Wueste;
       // Showmessage(inttostr(x)+' '+inttostr(Feld[xx,yy].Pos.X)+'  '+inttostr(y)+' '+inttostr(Feld[xx,yy].Pos.y));
      end;
    end;
  paintact;
end;             
{...}
procedure TForm1.paintact;
var x,y:integer;
begin
  for x:=1 to 60 do
    for y:=1 to 60 do
    begin
      case Feld[x,y].Land of
        Wasser:  Canvas.Color:=clblue;
        Wueste:  Canvas.Color:=clyellow;
        Gebirge: Canvas.Color:=clgray;
      end;
      Canvas.Rectangle(Feld[x,y].Pos.X,Feld[x,y].Pos.Y,Feld[x,y].Pos.X+14,Feld[x,y].Pos.Y+14);
    end;
end;

Sobald ich jetzt auf ein Rechteck in der Oberen Linken ecke klicke, werden Fast Alle Felder andersfarbig...
Wisst ihr warum und wie ich den Fehler beheben kann?

gruß Teekeks


Moderiert von user profile iconNarses: Topic aus Delphi Language (Object-Pascal) / CLX verschoben am Do 26.02.2009 um 09:19
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mi 25.02.09 19:56 
Ich geh mal davon aus, du willst, dass nur dein Rectangle in der jeweiligen Farbe ist. Dann musst du Canvas.Brush.Color zuweißen - wenn mich jetzt nicht alles täuscht.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: Mi 25.02.09 19:58 
Hi :)

Du wechselst die Zeichenfarbe. Natürlich wird danach mit deiner neuen Farbe weitergezeichnet.

:arrow: Lösung: die Farbe nach dem Zeichnen des Rechtecks wieder zurücksetzen. Am bestren legst du dazu eine lokale Variable vom Typ TColor an, in der du den alten Wert zwischenspeicherst, dann ist das alles etwas variabler.

E: Ich sehe gerade: Du benutzt das Canvas des Formulars, das ist leider möglich aber keine so gute Idee. Zieh' am besten eine Komponente der TPaintbox-Klasse auf dein Formular und suche mal hier im Forum danach, das Problem tritt eigentlich ständig auf.

mfG,

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)


Zuletzt bearbeitet von Hidden am Mi 25.02.09 20:00, insgesamt 1-mal bearbeitet
Teekeks Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 211
Erhaltene Danke: 23



BeitragVerfasst: Mi 25.02.09 19:59 
Hmm. Ändert nichts. das interesante ist ja das wenn ich z.b. unten rechts klicke garnichts gemacht wird...
ps.: ich aktualisiere ja so wie so bei jedem neuen rechteck die Farbe...
pps.: ok. mach ich mal...
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mi 25.02.09 20:02 
Lieg vermutlich nicht daran, du könntest aber deine If-Abfrage so lösen:

ausblenden Delphi-Quelltext
1:
if PtInRect(Point(X, Y), Rect(...X, ...Y, ...X + 14, ...Y + 14)) then					


Ist vielleicht etwas eleganter als die Prüfung nach in.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: Mi 25.02.09 20:05 
Ich würde die "14" im obigen Code übrigens durch eine Konstante ersetzen, dann kannst du alles sehr viel leichter anpassen :)

PS: Hast du das Array mal überprüft, steht danach auch in allen Feldern dasselbe, oder ist nur die Farbausgabe falsch? Wenn es am Array liegt, muss wohl die markierte Abfrage fehlschlagen :gruebel:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
for xx:=1 to 60 do begin
  for yy:=1 to 60 do begin
    if (x in [(Feld[xx,yy].Pos.X)..(Feld[xx,yy].Pos.X)+14]) and (y in [(Feld[xx,yy].Pos.Y)..(Feld[xx,yy].Pos.Y)+14]) then begin
      if Button=mbLeft then Feld[xx,yy].Land:=Wasser else
      if Button=mbMiddle then Feld[xx,yy].Land:=Gebirge else
      Feld[xx,yy].Land:=Wueste;
      // Showmessage(inttostr(x)+' '+inttostr(Feld[xx,yy].Pos.X)+'  '+inttostr(y)+' '+inttostr(Feld[xx,yy].Pos.y));
    end;
  end;
  paintact;
end;


mfG,

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)


Zuletzt bearbeitet von Hidden am Mi 25.02.09 20:11, insgesamt 2-mal bearbeitet
Teekeks Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 211
Erhaltene Danke: 23



BeitragVerfasst: Mi 25.02.09 20:58 
Aus welcher Unit ist Ptinrect(); ?
ps.: ich benutze Lazarus.
pps.: Es liegt nicht am Array. Da steht der richtige Wert jeweils drinn...

---Moderiert von user profile iconNarses: Beiträge zusammengefasst---

Also. ich habs jetzt mit meiner alten Version der Auswertung, der paintBox und dem Farbe merken gemacht.
Es ist schon besser. Aber ich habe immernoch das Problem das, sobald ich z.B. das Feld ganz oben Links klicke werden andere (in geometrischer Form) auch Farbig (siehe Bild).
Woran mag das liegen?
ps.: mein Aktueller Quelltext:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var xx,yy:integer;
begin
  for xx:=1 to 60 do
    for yy:=1 to 60 do
    begin
      if (x in [(Feld[xx,yy].Pos.X)..(Feld[xx,yy].Pos.X)+14]) and (y in [(Feld[xx,yy].Pos.Y)..(Feld[xx,yy].Pos.Y)+14]) then
      begin
        if Button=mbLeft then Feld[xx,yy].Land:=Wasser else
        if Button=mbMiddle then Feld[xx,yy].Land:=Gebirge else
        Feld[xx,yy].Land:=Wueste;
      end;
    end;
  paintact;
end;


procedure TForm1.Timer1Timer(Sender: TObject);
var x,y:integer;      //für die Initialisierung...
begin
  for x:=1 to 60 do
    for y:=1 to 60 do
    begin
      PaintBox1.Canvas.Brush.Color:=clgreen;
      paintBox1.Canvas.Rectangle(x*15,Y*15,X*15+14,Y*15+14);
      Feld[x,y].Pos.X:=x*15;
      Feld[x,y].Pos.Y:=y*15;
      Feld[x,y].Einheit.Typ:=kein;
      Feld[x,y].Einheit.Manschaft:=keine;
      Feld[x,y].Land:=Gras;
    end;
    Timer1.Enabled:=false;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
  paintact;
end;

procedure TForm1.paintact;
var x,y:integer;
    clor:Tcolor;
begin
  for x:=1 to 60 do
    for y:=1 to 60 do
    begin
      clor:=paintbox1.Canvas.Brush.Color;
      case Feld[x,y].Land of
        Wasser:  PaintBox1.Canvas.Brush.Color:=clblue;
        Wueste:  PaintBox1.Canvas.Brush.Color:=clyellow;
        Gebirge: PaintBox1.Canvas.Brush.Color:=clgray;
      end;
      PaintBox1.Canvas.Rectangle(Feld[x,y].Pos.X,Feld[x,y].Pos.Y,Feld[x,y].Pos.X+14,Feld[x,y].Pos.Y+14);
      paintbox1.Canvas.Brush.Color:=clor;
    end;
end;
Einloggen, um Attachments anzusehen!
Teekeks Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 211
Erhaltene Danke: 23



BeitragVerfasst: Do 26.02.09 18:33 
*push* Hat wirklich keiner eine Idee?

wäre schade...

gruß Teekeks
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 26.02.09 18:54 
Es wurde doch schon verschiedentlich auf die if-Abfrage hingewiesen... Warum prüfst du nicht ganz normal?

Du prüfst in einem Set (x in []), nur das kann nur 256 Zustände haben, und deshalb wiederholt sich das alle 256, ganz einfach wegen einem Überlauf des Bereichs eines Sets.

Probiere es doch mal so:
ausblenden Delphi-Quelltext
1:
2:
      if (Feld[xx,yy].Pos.X <= x) and (x <= Feld[xx,yy].Pos.X + 14)
        and (Feld[xx,yy].Pos.Y <= y) and (y <= Feld[xx,yy].Pos.Y + 14) then
Teekeks Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 211
Erhaltene Danke: 23



BeitragVerfasst: Do 26.02.09 19:00 
Cool! danke, jetzt funktioniert es...

gruß Teekeks