Delphi achieves greedy snake games

zhaozj2021-02-16  42

Borland's development tool Delphi6.0 is powerful, I now introduce you to the use of Delphi to make mobile games and eat snakes. First, open the New menu, create a new FORM1, name the Caption property to the snake, open the System tab, add a PaintBox control in the form, the size of yourself, add two Timer Controls in the form, Timer1 Control the snake), Timer2 (control food), add a TmainMenu control, which has three topplates: open, level, and about. Open the following and divide it into new (name: = new2), stop (name: = stop1) and the Away. Level below one (name: = one1) Pushing in this class, the number of levels can have its own decision. About About below. Secondly, enter the code area, open the "Open" menu, click New, enter the code editing area, find where the variable definition is set, add the following code:

x, y: array [1..100] of integer; // snaken: integer; sum: integer; fx: integer; // Directora, B: Integer; SS1, SS2, SS3, SS4: Integer; // Control Snake .

Find Procedure TForm1.new2Click (Sender: TOBJECT); if the code is changed to: procedure tform1.new2click (sender: TOBJECT); vari: integer; beginfor i: = 1 to 10 dobeginx [i]: = 10 i * 10; y [I]: = 260; PaintBox1.canvas.pen.color: = CLSILVER; PaintBox1.canvas.brush.color: = CLGreen; PaintBox1.canvas.Rectangle (x [i], y [i], x [i] 10, y [i] 10); end; time; time; timer2.enabled: = true; fx: = 4; // means initial, direction to right; end;

Double-click Timer1, if the code is changed to: Procedure TFORM1.TIMER1TIMER (Sender: TObject); vari: integer; beginsum: = 0; if (fx = 4) THEN // Diretor is right; beginpaintbox1.canvas.pen.color: = CLSilver; PaintBox1.canvas.brush.color: = CLSilver; PaintBox1.canvas.Rectangle (x [1], y [1], x [1] 10, y [1] 10); for i: = 1 to 9 doBeginx [I]: = x [i 1]; y [I]: = y [i 1]; end; paintbox1.canvas.brush.color: = CLGreen; PaintBox1.canvas.Rectangle (x [9] 10, y [9], x [9] 20, y [9] 10); x [10]: = x [9] 10; y [10]: = y [9] ;; END; IF (fx = 2) THEN / / DIRETOR IS DOWN; BeginPaintBox1.canvas.pen.Color: = CLSILVER; PaintBox1.canvas.brush.color: = CLSILVER; PaintBox1.canvas.Rectangle (x [1], y [1] , x [1] 10, y [1] 10); for i: = 1 to 9 dobeginx [i]: = x [i 1]; y [i]: = y [i 1]; end Paintbox1.canvas.brush.color: = CLGreen; PaintBox1.canvas.Rectangle (x [9] 10, y [9], x [9] 20, y [9] 10); x [10]: = x [9]; y [10]: = y [9] 10; end; if (fx = 1) THEN // Diretor IS Up; Beginif (x [1] = x [2]).. Pen.Color: = CLSilver; PaintBox1.canvas.brush.color: = CLSILVER; PaintBox1.canvas .Rectangle (x [2], y [2] 10, x [2] 10, y [2] 20); end; paintbox1.canvas.pen.color: = CLSilver; PaintBox1.canvas.brush.color : = CLSILVER; PaintBox1.canvas.Rectangle (x [1], y [1], x [1] 10, y [1] 10); for i: = 1 to 9 dobeginx [i]: = x [ i 1]; y [i]: = y [i 1]; end; paintbox1.canvas.brush.color: = clgreen; paintbox1.canvas.rectangle (x [9] 10, y [9], x [9] 20, y [9] 10); x [10]: = x [9]; y [10]: = y [9] - 10; end; if (fx = 3) THEN // Diretor IS left; beginpaintbox1.canvas.pen.color: = clsilver; PaintBox1.canvas.brush.color: =

CLSilver; PaintBox1.canvas.Rectangle (x [1], y [1], x [1] 10, y [1] 10); for i: = 1 to 9 dobeginx [i]: = x [i 1]; y [i]: = y [i 1]; end; paintbox1.canvas.brush.color: = clgreen; paintbox1.canvas.rectangle (x [9] 10, y [9], x [9 ] 20, y [9] 10); x [10]: = x [9] - 10; y [10]: = y [9]; end; if (x [10] <= SS3 10) THENBEGINTIMER1.ENABED: = false; timer2.enabled: = false; form2.show; end; if (x [10]> = ss4 - 10) thenbegintimer1.enabled: = false; formr2.Show; End; if (y [10] <= SS1 10) Thenbegintimer1.enabled: = false; timer2.enabled: = false; form2.show; end; if (y [10]> = ss2 - 10) thenbegintimer1.enabled: = False; timer2.enabled: = false; form2.show; end; if ((x [10] = a) and (y [10] = b)) ThenbeginPaintBox1.canvas.brush.color: = CLSILVER; PaintBox1.canvas .Rectangle (A, B, A 10, B 10); Timer2.enabled: = true; end; end; then, double-click on the blank of the form, if the code is: Procedure TFORM1.FormCreate (Sender: TOBJECT) BeginsS1: = PaintBox1.top; ss2: = PaintBox1.Height SS1; SS3: = PaintBox1.Left; SS4: = PaintBox1.Width SS3; N: = 0; END; selected in Object Inspector Form1 EVENT page, double click onkeydown, if the code is: // get user input; Procedure TFORM1.FORMKEYDOWN (Sender: TOBJECT; var Key: Word; Shift: tshiftstate); begin // vk_left = 37; // vk_up = 38 ; // vk_right = 39; // vk_down = 40; IF ((Key = 37) AND (fx <> 4)) THENFX: = 3; IF ((key = 38) and (fx <> 2)) thenfx: = 1; IF ((key = 39) and (fx <> 3)) THENFX: = 4; IF ((Key = 40) and (fx <> 1)) THENFX: = 2; End; to this, users can control Snake's movement.

Below, we introduce the generation of food, double-click Timer2, add the code: Procedure TFORM1.TIMER2TIMER (Sender: TOBJECT); becoma: = random (501); B: = Random (301); PaintBox1.canvas.brush.color: = CLYELLOW; PaintBox1.canvas.Rectangle (A, B, A 10, B 10); IF ((a> 10) AND (a <501) AND (B> 10) AND (B <301)). Enabled: = false; endelsebeginpaintbox1.canvas.brush.color: = CLSILVER; PaintBox1.canvas.Rectangle (A, B, A 10, B 10); Timer2.enabled: = true; end; end; ok, completion . Let me do it below. procedure TForm1.Stop1Click (Sender: TObject); beginif n = 0 thenbeginStop1.Caption: = 'Continue'; Timer1.Enabled: = False; n: = 1; endelsebeginStop1.Caption: = 'Stop'; Timer1.Enabled: = True ; n: = 0; end; end; procedure TForm1.Away1Click (Sender: TObject); beginclose (); end; procedure TForm1.One1Click (Sender: TObject); begintimer1.Interval: = 400; end; procedure TForm1.two1Click ( Sender: TOBJECT); Begintimer1.Iinterval: = 150;

procedure TForm1.three1Click (Sender: TObject); begintimer1.Interval: = 95; end; procedure TForm1.Four1Click (Sender: TObject); begintimer1.Interval: = 50; end; then, in a new Form, Form2, Name: = Add a Label, label1. Write your own information. In the code editing area, select Unit1, add uese unit2 below {$ r * .dfm}; click About, add the code to: procedure tform1.aboutme1click (sender: TOBJECT); beginfrmabout.showModal (); end; All completed, a total of 273 lines of code, but there is a mistake. Please contact me to inform me.

转载请注明原文地址:https://www.9cbs.com/read-24937.html

New Post(0)