Question

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.)

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top