1

I have recently designed and programmed a pong game in lua (codea); where you can play in multiplayer and in singleplayer where you play against the CPU.

My problem is, I can have the CPU to play perfectly bat_cpu = ball_y but that would not make the game a lot of fun.

Therefore I made a simple inaccuracy system to calculate where the ball will hit the CPU's bat:

bat_cpu = ball_y + inaccuracy

Where inaccuracy is defined earlier as:

inaccuracy = math.random(-40,40)

But the bat still follows the ball_y exactly and I want it to make mistakes, and not be so fast.

Does anyone know how to simulate the CPU bat so it makes some mistakes (on high speed, accuracy, etc.)

Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
Laurent
  • 1,292
  • 4
  • 23
  • 47
  • How have you defined the variables? A little more context and code please? – hjpotter92 Jan 25 '13 at 19:50
  • 1
    In my experience the classic way to deal with this is just to limit the CPU's speed, so if the player can angle the ball sharp enough the CPU simply can't move to the right location in time. – Lily Ballard Jan 25 '13 at 19:53

1 Answers1

2

You don't mention when this calculation is applied, but it's likely to be done on a per-frame basis. Which means that you probably apply those inaccuracies, but they are changed so frequently around 0 mean that you simply don't see the effect.

You can bias your random calculation per hit: for example, bias = math.random(-20, 20) and then inaccuracy = bias + math.random(-40, 40); you then reset bias on each hit.

You can also move the target: calculate where the hit for the CPU should happen and them move that target randomly. For each frame you'd need to interpolate based on the ball position where your want your bat to be and because the bat target is wrong, its approach to the target will be inaccurate too, as you want.

Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56