Autor Beitrag
Debo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 26



BeitragVerfasst: So 30.07.06 14:58 
hallo,

wie fast jeder einmal hier versuche ich mich als delphi-anfänger (2 Jahre Informatik in der Schule) an dem guten alten pong.

Habe das Spielfeld schon hinbekommen und auch den Ball (eigentlich mehr Rechteck, was aber auch nicht schlimm ist) geschaffen.

Doch so wie ich den Ball übers Spielfeld wandern lasse ist es mit sicherheit keine gute lösung, deswegen würde ich gern wissen, wie ich es besser machen sollte. Am besten so, dass ich nicht alles komplett neu machen muss ;)

Ausserdem habe ich immer ein Flimmern auf meinem Image, was ich mit einem Buffer vermeiden wollte (klappt aber nicht - warum?) und wo wir grad dabei sind - warum flimmert es überhaupt?

Habe versucht klar und strukturiert zu programmieren, ich hoffe es ist gut verständlich.

Danke schonmal!

Achja, der code ;)

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:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    Procedure Spielfeldgenerator();
    Procedure Ballgenerator(x0,y0,x,y : Integer);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var Spielfeld, Buffer, Ball : TBitmap;
    BallPos : TPoint;
    x0,y0,x,y : Integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
Spielfeld:=TBitmap.Create;
Spielfeld.Height:=350;
Spielfeld.Width:=450;

Buffer:=TBitmap.Create;
Buffer.Height:=350;
Buffer.Width:=450;

Ball:=TBitmap.Create;
Ball.Height:=10;
Ball.Width:=10;

x0:=240;  //Startposition des Balls, später -> letzte Ballposition
y0:=175;
x:=50;
y:=50;

Spielfeldgenerator;
Ballgenerator(x0,y0,x,y);
end;

Procedure TForm1.Spielfeldgenerator();
begin
with Spielfeld.Canvas do
    begin
      Pen.Color:=clBlack; //Alles schwarz
      Brush.Color:=clBlack;
      Rectangle(0,0,450,350);

      Pen.Color:=clWhite; //Mittellinie Weiß
      Brush.Color:=clWhite;
      Rectangle(223,1,227,349);

      Pen.Color:=clWhite; //Seite-Oben Weiß
      Rectangle(1,1,449,5);

      Pen.Color:=clWhite; //Seite-Unten Weiß
      Rectangle(1,349,449,345);

      Pen.Color:=clWhite; //Seite-Links Weiß
      Rectangle(1,1,5,349);

      Pen.Color:=clWhite; //Seite-Rechts Weiß
      Rectangle(445,1,449,349);
    end;

    Buffer:=Spielfeld;

    Image1.Picture.Bitmap:=Buffer;
end;

Procedure TForm1.Ballgenerator(x0,y0,x,y : Integer);
begin
  BallPos.X:=x0;
  BallPos.Y:=y0;
  with Ball.Canvas do
    begin
        pen.Color:=clWhite;
        brush.Color:=clWhite;
        Rectangle(0,0,Ball.Width,Ball.Height);
        Buffer.Canvas.Draw(BallPos.X,BallPos.Y,Ball);
        Image1.Picture.Bitmap:=Buffer;
    end;

   BallPos.X:=x;
   BallPos.Y:=y;

  with Ball.Canvas do
    begin
        pen.Color:=clBlack;
        brush.Color:=clBlack;
        Rectangle(0,0,Ball.Width,Ball.Height);
        Buffer.Canvas.Draw(BallPos.X,BallPos.Y,Ball);
        Image1.Picture.Bitmap:=Buffer;
    end;


end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
x:=x0;
y:=y0;
x0:=x+10;
y0:=y+10;
Ballgenerator(x0,y0,x,y);
end;

end.
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: So 30.07.06 15:00 
Wegen dem Flackern: Form1.DoubleBuffered := true
Probiere das mal.
Debo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 26



BeitragVerfasst: So 30.07.06 15:06 
Danke! Flackert nicht mehr - ist mein Buffer jetzt noch nötig?
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: So 30.07.06 15:26 
Schätze nein.
Debo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 26



BeitragVerfasst: Mo 31.07.06 19:40 
und vielleicht jemand noch ne bessere idee, wie man das mit dem ball realisieren kann?

meine bisherige lösung:

-ein bitmap 10x10
-und dann mal ich das Bitmap immer an die Position x0,y0
-im timer veränder ich dann x0,y0 -> somit die position
-damit die stelle, wo der Ball war, wieder schwarz wird, lass ich einen zweiten schwrazen Ball hinterher wandern

blöde idee, da ich so keinen flüssigen Ball hinbekomme...
oern
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 142

Linux, Win XP
D7
BeitragVerfasst: Mo 31.07.06 21:42 
Benutze ne Grafik API z.B. OpenGL

_________________
Ich hab eine Signatur
Debo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 26



BeitragVerfasst: Do 03.08.06 12:15 
okay, jetzt hab ich das ganze mit opengl geschrieben. Kollisionen klappen, Schläger bewegen sich, alles wunderbar!

Jedoch ist das Spiel manchmal schnell und manchmal langsam, je nachdem was ich so im Hintergrud laufen habe. Ich denke mal es liegt an den Frames, dass dann unterschiedlich viele erzeugt werden, wie jedoch bekomme ich eine Einheitliche Spielgeschwindigkeit. Hab da mal was von gehört, weiss aber nicht mehr genau wie es heisst: Unter "Timebase Movement" hab ich nichts gefunden, könnt ihr mir mal kurz mit nem Link helfen?

Danke schonmal!

achja: OpenGL ruult ;)
jakobwenzel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1889
Erhaltene Danke: 1

XP home, ubuntu
BDS 2006 Prof
BeitragVerfasst: Do 03.08.06 12:17 

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.