Přidat otázku mezi oblíbenéZasílat nové odpovědi e-mailem Delphi - jak naprogramovat hru "Hada"?

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

type
    TForm1 = class(TForm)
        snaketimer: TTimer; // Updates the old
// direction and
// draws the snake
        procedure snaketimerTimer(Sender: TObject);
        procedure FormKeyPress(Sender: TObject; var Key: Char); // Handles key input
        procedure FormPaint(Sender: TObject); // Draws the form
// and resets the
// game
    PRIVATE
    PUBLIC
    end;

var
    Form1: TForm1;
    score : Integer;

procedure ResetGame; // Resets the snake and score
function HitWall : Boolean; // returns true if snake hits the wall
function HitFood : Boolean; // Returns true if snake hits food
procedure DrawSnake; // Draws the snake, increase points if
// snake hit food,
procedure PlaceFood;
procedure DrawPlayfield; // Draws the playfield
// (the area which the snake navigates on)
procedure EndGame; // Stops the snake timer and free memory
// allocated by the snake array
implementation

{$R *.dfm}

const
    snakewidth = 10; // specify how wide the snake is
    snakegrow : Integer = 2; // specify how fast snake grows
    snakecolor : tcolor = clBlack;
    foodcolor : tcolor = clRed;
    pfcolor : tcolor = clWhite; // playfield color
    pfdimention : tpoint = (x:200; y:200); // playfield width and hight
    pfposition : tpoint = (x:5; y:5); // playfield position on form
    dup = 0;
    ddown = 1;
    dleft = 2;
    dright = 3;
    dnone = 4;
    speed : Integer = 100; // snake speed higher value
// equals slower snake
var
    snake : array of tpoint; // dynamic array holding snake
// coordinates
    snakelength : Integer; // length of snake
    food : tpoint; // food position
    direction : dup..dnone;
    olddirection : dup..dnone;
// HitWall: returns true if snake hits the wall
function HitWall : Boolean;
var
    i : Integer;
begin
    for i := 0 to snakelength - 1 do
    begin
        if (snake[i].x < pfposition.X) Or
            (snake[i].x + snakewidth > pfposition.X + pfdimention.X) Or
            (snake[i].y < pfposition.y) Or
            (snake[i].y + snakewidth > pfposition.y + pfdimention.y) then
        begin
            hitwall := True;
            exit;
        end;
    end;
    hitwall := False;
end;
// HitFood: Returns true if snake hits food
function HitFood : Boolean;
var
    i : Integer;
begin
    for i := 0 to snakelength - 1 do
    begin
        if (snake[i].x = food.x) And (snake[i].y = food.y) then
        begin
            hitfood := True;
            exit;
        end;
    end;
    hitfood := False;
end;
// PlaceFood: Draws the food on the playfield on a random location
procedure PlaceFood;
begin
    food.x := random(pfdimention.x - snakewidth - pfposition.x) + pfposition.x;
    food.y := random(pfdimention.y - snakewidth - pfposition.y) + pfposition.y;
    while (pfdimention.x - (food.x - pfposition.x)) Mod snakewidth <> 0 do
    begin
        inc(food.x)
    end;
    while (pfdimention.y - (food.y - pfposition.y)) Mod snakewidth <> 0 do
    begin
        inc(food.y)
    end;
    if HitFood then
    begin
        PlaceFood
    end;
    Form1.Canvas.Brush.Color := foodcolor;
    Form1.Canvas.Pen.Color := snakecolor;
    Form1.Canvas.Ellipse(food.x + 1, food.y + 1,
        food.x + snakewidth - 2, food.y + snakewidth - 2);
end;

Reakce na odpověď

1 Zadajte svou přezdívku:
2 Napište svou odpověď:
3 Pokud chcete dostat ban, zadejte libovolný text:

Zpět do poradny