Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 12th, 2005, 11:27 AM   #1
Markpy
Newbie
 
Markpy's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 13
Rep Power: 0 Markpy is on a distinguished road
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.
Markpy is offline   Reply With Quote
Old Oct 13th, 2005, 5:50 AM   #2
Markpy
Newbie
 
Markpy's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 13
Rep Power: 0 Markpy is on a distinguished road
NVM, I've got it working now
Markpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:37 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC