![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2005
Location: UK
Posts: 13
Rep Power: 0
![]() |
Pong AI
Hi, I'm new to Delphi (I have to learn it as part of my course). I've tried my hand at making a basic Pong-like game, but I can't get the AI player to work.
Currently I have a bar which the player can control with the arrow keys. The other bar uses a timer which is supposed to update predictions as to where the ball will collide and cause the bar to move to that position. unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, StdCtrls, ExtCtrls;
type
TMain = class(TForm)
MainMenu: TMainMenu;
FileMenu: TMenuItem;
Exit: TMenuItem;
Player: TLabel;
Bar: TShape;
Ball: TShape;
Timer: TTimer;
Comp: TShape;
AI: TTimer;
procedure ExitClick(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure TimerTimer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure AITimer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Main: TMain;
implementation
{$R *.DFM}
var ForwardX, ForwardY: Boolean;
var Score, Target, BallMove, BarMove, StartPos: Integer;
procedure TMain.ExitClick(Sender: TObject);
begin
Close;
end;
procedure TMain.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
const Up=38; Down=40;
begin
if (Key = Up) and ((Bar.Top - BarMove) >= StartPos) then
Bar.Top := Bar.Top - BarMove;
if (Key = Down) and ((Bar.Top + Bar.Height + BarMove) <= Main.ClientHeight) then
Bar.Top := Bar.Top + BarMove;
end;
procedure TMain.TimerTimer(Sender: TObject);
begin
if Comp.Top < (Target - (Comp.Height div 2)) then
Comp.Top := Comp.Top + BarMove
else
if Comp.Top > (Target + (Comp.Height div 2)) then
Comp.Top := Comp.Top - BarMove;
if (Ball.Left + Ball.Width) >= Main.ClientWidth then
ForwardX := False
else
if Ball.Left < 0 then
begin
ForwardX := True;
Score := Score - 1;
Player.Caption := 'Player Score: ' + IntToStr(Score);
end;
if (Ball.Left > 0) and (Ball.Left <= Bar.Width)
and ((Ball.Top + Ball.Height) <= (Bar.Top + Bar.Height))
and (Ball.Top >= Bar.Top) then
begin
ForwardX := True;
Score := Score + 1;
Player.Caption := 'Player Score: ' + IntToStr(Score);
end;
if (Ball.Top + Ball.Height) >= Main.ClientHeight then
ForwardY := False
else
if Ball.Top <= StartPos then
ForwardY := True;
if ForwardX = True then
Ball.Left := Ball.Left + BallMove
else
if ForwardX = False then
Ball.Left := Ball.Left - BallMove;
if ForwardY = True then
Ball.Top := Ball.Top + BallMove
else
if ForwardY = False then
Ball.Top := Ball.Top - BallMove;
end;
procedure TMain.FormCreate(Sender: TObject);
begin
ForwardX := True;
ForwardY := True;
Score := 0;
BarMove := 20;
BallMove := 10;
StartPos := 40;
Target := Comp.Top;
end;
procedure TMain.AITimer(Sender: TObject);
var GapX, GapY, CrashX, CrashY: Integer;
begin
if ForwardX = False then;
if ForwardY = True then
GapY := Main.ClientHeight - (Ball.Top + Ball.Height)
else
GapY := Ball.Top - StartPos;
CrashX := Ball.Left + Ball.Width + GapY;
GapX := Main.ClientWidth - CrashX;
CrashY := Main.ClientHeight - GapX;
Target := CrashY
end;
end.P.S. Damn I can't upload the project files as a zip because there too big. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Aug 2005
Location: UK
Posts: 13
Rep Power: 0
![]() |
NVM, I've got it working now
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|