Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / I NEED AI HELP

Author
Message
kevin lor 12
20
Years of Service
User Offline
Joined: 8th Jun 2004
Location:
Posted: 11th Jun 2004 23:13
I HAVE A PONG GAME AND I NEED TO A.I CODE TO GET THE BAT TO MOVE THAT I AM NOT CONTROLLING SO THE COMPUTER PLAYS AGENST ME...
HERES THE CODE IM USING
kevin lor 12
20
Years of Service
User Offline
Joined: 8th Jun 2004
Location:
Posted: 11th Jun 2004 23:13
[/code]rem SCORING
if ballx#>6 then player2score#=player2score#+1:ballx#=0:ballz#=0:balla#=270
if ballx#<-6 then player1score#=player1score#+1:ballx#=0:ballz#=0:balla#=90
set cursor 300,50:print player2score#
set cursor 320,50:print "-"
set cursor 340,50:print player1score#
rem BALL MOVEMENT
ballx#=newxvalue(ballx#,balla#,0.3):ballz#=newzvalue(ballz#,balla#,0.3)
rem PLAYER 1 PADDLE MOVEMENT
IF upkey()=1 and player1pos#<3.5 then player1pos#=player1pos#+0.5
IF downkey()=1 and player1pos#>-3.5 then player1pos#=player1pos#-0.5
if upkey()=1 and ballx#>4 and ballx#<4.5 then balla#=balla#+8
if downkey()=1 and ballx#>4 and ballx#<4.5 then balla#=balla#-8
rem PLAYER 2 PADDLE MOVEMENT
IF keystate(17)=1 and player2pos#<3.5 then player2pos#=player2pos#+0.5
IF keystate(31)=1 and player2pos#>-3.5 then player2pos#=player2pos#-0.5
if keystate(17)=1 and ballx#<-4 and ballx#>-4.5 then balla#=balla#+8
if keystate(31)=1 and ballx#<-4 and ballx#>-4.5 then balla#=balla#-8
rem BOUNCING
if ballx#>4 and ballx#<4.5 and ABS(player1pos#-ballz#)<1.5 then balla#=360-balla#
if ballx#<-4 and ballx#>-4.5 and ABS(player2pos#-ballz#)<1.5 then balla#=360-balla#
if ballz#>4 or ballz#<-4 then balla#=180-balla#
rem MAKE SURE balla# IS LESS THAN 360 AND MORE THAN 0
balla#=wrapvalue(balla#)
rem POSITION OBJECTS
position object 1,5,0,player1pos#:position object 2,-5,0,player2pos#
position object 3,ballx#,0,ballz#:yrotate object 3,balla#
position camera 0,10,-10:point camera 0,0,0
sync
loop
[code]
HERE
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 11th Jun 2004 23:50 Edited at: 11th Jun 2004 23:52
you could just get the computer to track the ball position and occasionaly get it to miss by making it move slow eg:-(psuedocode)

if rnd(10)=0
speed=0.9
else
speed=1
endif

if ballvert>batvert
batvert=batvert+speed
endif

if ballvert<batvert
batvert=batvert-speed
endif

how it works...as long as the bat is at a different position to the ball then the program will adjust the bats position to get nearer to the balls height, so it will be at the same height as the ball when it gets there, to stop it being infallible (boring to play against) we create a random number from 0 to 10, and if it`s 0 (on average 1/11th of the time, then we make the bat move slower by 0.1, so if the bat has a long way to go then it will miss the ball since it will get there late, and if it`s a short distance it will be a close thing if it gets it or not since the total error will be smaller, hope that explains things.

Mentor.

PC1: P4 hyperthreading 3ghz, 1gig mem, 2x160gig hd`s, Nvidia FX5900 gfx, 6 way surround sound, PC2: AMD 1.2ghz, 512mb ram, FX5200 ultra gfx, stereo 16 bit soundblaster, ups.
kevin lor 12
20
Years of Service
User Offline
Joined: 8th Jun 2004
Location:
Posted: 12th Jun 2004 00:44 Edited at: 12th Jun 2004 01:17
ok im sorry to bug you but that makes no sence to me.... do you think you could use an example so i could visialize it?
zircher
21
Years of Service
User Offline
Joined: 27th Dec 2002
Location: Oklahoma
Posted: 12th Jun 2004 10:14 Edited at: 12th Jun 2004 10:23
Mentor has the right idea. What you need to do is have one of the player paddles track the ball, but use the speed variable to introduce some error into the tracking so it is not a perfect player.

You need to add some logic to your player 2 paddle control. Something like this...



DISCLAIMER: You may need to tweak the logic or the constant values to get the results you desire. The purpose of this code fragment is to show you how to organize that logic and what to test for.
--
TAZ

[Yes, I can be a grumpy old man. But, I try to be helpful more than tactless.]

Posting tip 1: You first post here was correct. Anyone can click on the source button and see your code is a seperate window. You second post was redundant and you had the code tags backwards.
( [ c o d e ] and [ / c o d e ] not [ / c o d e ] and [ c o d e ] )

Posting tip 2: The code tags are handy when you want to display formated text where spacing is important. It is good for ASCII art, tables, and other forms of text as well as source code fragments.

History did not begin with PONG. -- Greg Costikyan

Game Beavers

Login to post a reply

Server time is: 2024-09-22 12:23:00
Your offset time is: 2024-09-22 12:23:00