Question

I'm making a pong game for my software development class, and I should probably state that this is homework, hence my limited understanding. and I'm having some problems creating the AI for my NPC paddle. I'm using Kivy and Python.

Currently I can create impossible to beat AI by doing this:

#ai
self.player2.center_y = self.ball.y 

where self.player2.center_y is the y coordinate for the NPC (it only moves on the y axis) and self.ball.y is the y coordinate for the ball. I followed the tutorial on the Kivy site to create the basis of the game.

Now I have no idea about how I can create AI that will be beatable. I know I will need to limit the speed of the AI, so when the ball gets so fast it won't be able to grab it. The thing is, though, that I don't actually have a speed function.

The problem with the tutorial I followed is that it doesn't explain everything. I believe I could make a speed function by saying "Every x seconds, the paddle will move x pixels in the y axis."

This is how the ball is served as per the kivy tutorial:

def serve_ball(self, vel=(10,0)):
    self.ball.center = self.center
    self.ball.velocity = vel

Alright, now I'll try and understand this... vel=(10,0)): likely means, move 10 pixels X and 0 pixels Y, assign that to ball.velocity, and evidently that controls the speed...

class PongApp(App):
    def build(self):
        game = PongGame()
        game.serve_ball()
        Clock.schedule_interval(game.update, 1.0/300.0)
        return game

Clock.schedule_interval(game.update, 1.0/300.0) with regards to this, Is it safe to assume that... that is the rate at which the ball moves? i.e. 10 pixels x and 0 y, every 1/300 seconds? If i change it to something like 1/20, it moves much slower... so I would assume so.

Now I need to create a function to hold the AI... perhaps

def AI(self):
    self.AI_Speed = self.ball.velocity - 10

self.player1. refers to my paddle.

And yeah.. Now I'm stuck. I have no idea how I can use this velocity to control moving the paddle. Anyone have any Ideas? And Since my question is most likely extremely ambiguous, I will provide the source of the game incase you need a better understanding. Thank you.

http://pastebin.com/8wTLbH21

p.s. i realize this is a big question and a lot to ask, but I hope someone can answer. Thanks.

Was it helpful?

Solution

Since this is such a big question, I haven't tested this out or anything, but it seems like you want to have some sort of function that moves the AI (I'll call it self.player2.move()). If you call this function in game.update(), then you should be able to move the AI paddle incrementally closer to where it needs to be to hit the ball.

Some basic pseudocode for self.player2.move():

figure out if ball is above or below paddle
move self.AI_Speed units in the direction of the ball

You might need to adjust the AI speed more to make this work, but I think it should be the right general idea. If you think about it, the unbeatable AI is basically just infinitely fast.

Does that help get you unstuck?

OTHER TIPS

Thanks to seaseaotternerd I've managed to write something that makes a beatable AI.

if self.ball.y < self.player2.center_y:
    self.player2.center_y = self.player2.center_y - 3
if self.ball.y > self.player2.center_y:
    self.player2.center_y = self.player2.center_y + 3

I get the balls y position, and if it's less than the center of the NPC's paddle, I'll simply move the NPC down by 3y every time until it's either the same or greater. Same thing with if it's more than the center of the NPC's paddle, I just increase it.

This then defines the speed of the paddle (3 y positions per 1/300 of a second) and will allow me to increase difficulty for harder modes.

Thanks!

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