I'm having 2 troubles with the following code. First the AI bat will move faster than the speed I have specified. And second it always hits the ball.
Function MoveCpuOpponent(player2.player,ball.ball)
hit_chance = Rnd(1,4)
If ball\y > player2\y And player2\y < 525 Then
player2\y = player2\y + 4
If ball\y = player2\y And hit_chance = 1 Then
player2\y = ball\y
Else player2\y = player2\y + 4
EndIf
EndIf
If ball\y < player2\y And player2\y > 55 Then
player2\y = player2\y - 4
If player2\y = ball\y And hit_chance = 1 Then
player2\y = ball\y
Else
player2\y = player2\y - 4
EndIf
EndIf
End Function
If I created a timer to slow down the executuion of the code would that help or am I totally off the mark here?
I have Seedrnd Millisecs() at the beginning with the graphics command,etc..
Thanks
Why do you have a hit chance?
im not sure about your first question, but for the 2nd:
how big is the bat? by the looks of it you have a screen 600 high, but the playing field limits are restricted to 55-525. even if the bat was as far away as possible (475 pixels) it would only take 118 frames at 4 pixels per frame to make it to the ball. at 60fps (on an average pc your game should be able to do that) this is just under 2 seconds, which means that the bat will make it in all but a few circumstances. these few circumstances are usually covered by the size of the bat. so every time the ball is hit, the bat is pretty much guaranteed to get there in time. problem 1: the bat is too good.
the other problem is in your checking. "if the bat has made it to the ball, move it 4 pixels away 75% of the time". fair enough, but if the bat was close to the ball in the first place, it will move away early but then try and find the ball again on the next frame. this will cause the bat to move 4 pixels at a time back and forth until the ball gets near, wherupon the bat makes a last attempt at moving out of the way by 4 pixels (75% of the time). problem 2: too little, too late. even if the bat does move, if it is bigger than 8 pixels it will hit the ball.
heres how i would solve it:
when the ball is hit by the player, determine the angle at which it was hit. from this angle, work out where the AI bat will need to be to hit the ball perfectly. you will need to allow for bouncing off the top and bottom edges - i'll leave this for you to calculate cos ive had a few beers and cant do it myself. if you're stuck, ask me tomorrow and ill help you.
once you know where the ball will end up, give the cpu bat a target destination, plus or minus a margin of error depending on the cpu skill level. all you have to do then is similar to what you did before:
if cpu_bat_ypos < target_ypos then
cpu_bat_ypos = cpu_bat_ypos + move_speed
else if cpu_bat_ypos > target_ypos then
cpu_bat_ypos = cpu_bat_ypos - move_speed
else
end if
all you have to do is simulate what would happen in a real game of tennis. you work out where you think the ball will end up and run there as fast as you can. if youre crap at judgement, you'll miss by miles, otherwise you'll get there 90% of the time.
neil
the following code will create a while...wend timer in your game. adjust the FPS_LIMIT constant to, er, change the fps limit. creating a timer should make the bats move at a capped speed, providing your system is up to that speed. if they are still moving at different speeds the problem is elsewhere.
; somewhere at top of code...
const FPS_LIMIT = 60 ;"ish"
global oldtime
;top of main loop
oldtime = millisecs()
;absolute last thing in main loop...
While (MilliSecs()<oldtime + (1000/fps))
Wend
have fun
neil
Thanks for the help. I feel like such an idiot. All I had to do was change the speed that the bat moved to keep it from hitting the ball everytime. DOH!
dont sweat it - we all come across these problems from time to time. good luck for the rest of the game
neil