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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| unit Main;
interface
uses
Math,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ImgList, ExtCtrls;
type
TForm1 = class(TForm)
Timer1: TTimer;
Timer2: TTimer;
ImageList1: TImageList;
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
private
{ Déclarations privées }
FFond : TBitmap;
FDessin: TBitmap;
sprite:TImage;
public
{ Déclarations publiques }
end;
var Form1: TForm1;
level:Array [1..15] of String;
tiles:Array [1..500] of TImage;
wish_direction,une_action:String;
anim:Integer;
implementation
{$R *.dfm}
function deplacement_joueur():Boolean;
var n,x,y:Integer;
xx,yy:Double;
begin
x:=Round(Form1.sprite.Left/60)+1;
y:=Round(Form1.sprite.Top/60)+1;
xx:=Form1.sprite.Left/60+1;
yy:=Form1.sprite.Top/60+1;
if une_action='' then une_action:=wish_direction;
if (x=xx) and (y=yy) then
begin
if (wish_direction='left') and (une_action<>'left') then if (level[y][x-1]<>'1') then une_action:='left';
if (wish_direction='right') and (une_action<>'right') then if (level[y][x+1]<>'1') then une_action:='right';
if (wish_direction='up') and (une_action<>'up') then if (level[y-1][x]<>'1') then une_action:='up';
if (wish_direction='down') and (une_action<>'down') then if (level[y+1][x]<>'1') then une_action:='down';
end;
x:=Ceil(Form1.sprite.Left/60)+1;
if (une_action='left') then if (level[y][x-1]<>'1') then
begin
for n := 1 to 4 do
begin
if anim>=4 then anim:=0;
if anim=0 then Form1.ImageList1.GetBitmap (anim, Form1.sprite.Picture.Bitmap);
if anim=1 then Form1.ImageList1.GetBitmap (anim, Form1.sprite.Picture.Bitmap);
if anim=2 then Form1.ImageList1.GetBitmap (anim, Form1.sprite.Picture.Bitmap);
if anim=3 then Form1.ImageList1.GetBitmap (anim-1, Form1.sprite.Picture.Bitmap);
Form1.sprite.Left:=Form1.sprite.Left-1; InvalidateRect(Form1.Handle, nil, False);
end;
exit;
end else une_action:='';
x:=Trunc(Form1.sprite.Left/60)+1;
if (une_action='right') then if (level[y][x+1]<>'1') then
begin
for n := 1 to 4 do
begin
if anim>=4 then anim:=0;
if anim=0 then Form1.ImageList1.GetBitmap (3, Form1.sprite.Picture.Bitmap);
if anim=1 then Form1.ImageList1.GetBitmap (4, Form1.sprite.Picture.Bitmap);
if anim=2 then Form1.ImageList1.GetBitmap (5, Form1.sprite.Picture.Bitmap);
if anim=3 then Form1.ImageList1.GetBitmap (4, Form1.sprite.Picture.Bitmap);
Form1.sprite.Left:=Form1.sprite.Left+1; InvalidateRect(Form1.Handle, nil, False);
end;
exit;
end else une_action:='';
y:=Ceil(Form1.sprite.Top/60)+1;
if (une_action='up') then if (level[y-1][x]<>'1') then
begin
for n := 1 to 4 do
begin
if anim>=4 then anim:=0;
if anim=0 then Form1.ImageList1.GetBitmap (anim, Form1.sprite.Picture.Bitmap);
if anim=1 then Form1.ImageList1.GetBitmap (anim, Form1.sprite.Picture.Bitmap);
if anim=2 then Form1.ImageList1.GetBitmap (anim, Form1.sprite.Picture.Bitmap);
if anim=3 then Form1.ImageList1.GetBitmap (anim-1, Form1.sprite.Picture.Bitmap);
Form1.sprite.Top:=Form1.sprite.Top-1; InvalidateRect(Form1.Handle, nil, False);
end;
exit;
end else une_action:='';
y:=Trunc(Form1.sprite.Top/60)+1;
if (une_action='down') then if (level[y+1][x]<>'1') then
begin
for n := 1 to 4 do
begin
if anim>=4 then anim:=0;
if anim=0 then Form1.ImageList1.GetBitmap (3, Form1.sprite.Picture.Bitmap);
if anim=1 then Form1.ImageList1.GetBitmap (4, Form1.sprite.Picture.Bitmap);
if anim=2 then Form1.ImageList1.GetBitmap (5, Form1.sprite.Picture.Bitmap);
if anim=3 then Form1.ImageList1.GetBitmap (4, Form1.sprite.Picture.Bitmap);
Form1.sprite.Top:=Form1.sprite.Top+1; InvalidateRect(Form1.Handle, nil, False);
end;
exit;
end else une_action:='';
une_action:=wish_direction;
une_action:=wish_direction;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
hasard,i, j: Integer;
Appdir:Variant;
begin
sprite:=TImage.Create(nil);
//// MAP DU JEU (les 1 c'est les bords, l'espace vide où on peut circuler)
/// le S est le point de départ du PACMAN, les autres lettres des futurs fantômes
/// les - sont les endroits où il n'y a pas de pillule.
level[1]:='11111111111111111111';
level[2]:='1 1';
level[3]:='1 1 111 1111 111 1 1';
level[4]:='1 1 1 1111 1 1 1';
level[5]:='1 1 1 1111 1 1 1';
level[6]:='1 1 1 Y 1 1 1';
level[7]:='1 11 11T11 111 1';
level[8]:='1 1 1UG-1 1 1';
level[9]:='1 1111 111111111 1 1';
level[10]:='1 1 ----- 1 1';
level[11]:='1 1 1 1-111-1 1 1 1';
level[12]:='1 1 1 1--S--1 1 1 1';
level[13]:='1 1 1 1 111 1 1 1 1';
level[14]:='1 1';
level[15]:='11111111111111111111';
for i := 1 to 300 do //Pour les tiles, créez des images BMP de dimensions 60*60, j'ai pris perso des murs en legos, un peu comme dans pacman arranged de namco
begin
tiles[i]:=Timage.Create(nil);
tiles[i].Width:=60;
tiles[i].Height:=60;
tiles[i].Stretch:=True;
end;
// Le décor, c'est là où on va placer les Tiles (ou la map) du jeu
FFond := TBitmap.Create;
FFond.Width := ClientWidth;
FFond.Height := ClientHeight;
// Dessin hors écran (double buffer)
FDessin := TBitmap.Create;
for j := 1 to 15 do //15 lignes, 20 colonnes (la map est lisible directement sur le code, ce qui permet avant de lancer le jeu de "voir" ce qu'il va s'afficher à l'écran
for i := 1 to 20 do
begin
if level[j][i]='1' then //Il existe 4 tiles différentes pour faire les murs, c'est pour faire joli. On pourrait en prendre qu'une...
begin
hasard:=random(4);
tiles[j*20-20+i].Picture.LoadFromFile(Appdir+'assets\tilewall_'+IntToStr(hasard+1)+'.bmp');
end;
if (level[j][i]<>'1') then tiles[j*20-20+i].Picture.LoadFromFile(Appdir+'assets\tilecorridor_1.bmp');
tiles[j*20-20+i].Left:=i*60-60;
tiles[j*20-20+i].Top:=j*60-60;
tiles[j*20-20+i].Visible:=True;
Form1.FFond.Canvas.Draw(tiles[j*20-20+i].Left,tiles[j*20-20+i].Top,tiles[j*20-20+i].Picture.Bitmap);
if level[j][i]='S' then
begin
Form1.sprite.Picture.LoadFromFile(Appdir+'pacmanleft1.bmp');
sprite.Left:=i*60-60;
sprite.Top:=j*60-60;
sprite.Transparent:=True;
end;
end;
end;
procedure TForm1.FormPaint(Sender: TObject); //Cette procédure met à jour l'affiche à l'écran, il faut cliquer dans l'inspecteur de la Form -> event -> On Paint et ça crée cette procédure
var i,j:Integer;
begin
FDessin.Assign(FFond); // copier le décor dans le dessin hors écran, équivalent à un doublebuffer, mais au moins ça rame pas! (donc c'est beaucoup mieux!)
Form1.FDessin.Canvas.Draw(sprite.Left,sprite.Top,sprite.Picture.Bitmap); // Ajouter le personnage sur le dessin hors écran
Canvas.Draw(0 , 0, FDessin);// Dessiner le tout à l'écran (transfère le dessin hors écran sur la form aux coordonnées 0,0
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var i:Integer;
begin
if GetKeyState(VK_RIGHT) and $80=$80 then begin wish_direction:='right'; if une_action='left' then une_action:='right'; end;
if GetKeyState(VK_LEFT) and $80=$80 then begin wish_direction:='left'; if une_action='right' then une_action:='left'; end;
if GetKeyState(VK_UP) and $80=$80 then begin wish_direction:='up'; if une_action='down' then une_action:='up'; end;
if GetKeyState(VK_DOWN) and $80=$80 then begin wish_direction:='down'; if une_action='up' then une_action:='down'; end;
deplacement_joueur();
end;
procedure TForm1.Timer2Timer(Sender: TObject);
begin
anim:=anim+1;
end;
end. |
Partager