|
|||||||||||||||||||||||||||||||||||||||||
| ISBN: 3826617800 ISBN: 3826617800 ISBN: 3826617800 ISBN: 3826617800 | |||||||||||||||||||||||||||||||||||||||||
|
|
Wir empfehlen: | ||||||||||||||||||||||||||||||||||||||||
Artificial inteligence used in a Pong gameMany games need two players but mostly you have to play on your own, because noone is there to play with you. For these situations you want an opponent that is not too strong but is strong enough so that the game is not boring at all. If you are programing such a game you have to write a AI (artificial inteligence), that "plays" against the human player. In this chapter I want to explain how to program a really simple AI one could use for example in a Pong game like Streethockey as I did. I will only explain how to program the AI, please take a look at the other classes and methods in the sourcecode by yourself (download at the end of the chapter). The situationFirst of all we should decide what we want our AI to do: In a Pong game the computer has to make goals against the player and has to save his own goal as a goalkeeper. A human player, who is playing this game, will always try to be at the same y - position as the ball, so that the ball will bounce when it hits the player paddle. The computer should do the same and now we want to "tell" the computer how to follow the ball's y - position. The ideaSo the computer has to "watch" the ball and tries to be at the same position (in the game Streethockey it's the y - position) as the ball. Imagine the ball object would have methods, that could tell us (the computer) it's velocity, direction and position, which could be the "eyes" of the computer. Then the AI method is really simple, as you see: The AI - method
public void move (Ball ball) {
real_y_pos = y_pos + (size_y / 2); /* If the ball is moving in opposite direction to the paddle and is no danger for computer's goal move paddle back to the middle y - position*/ if (ball.getXSpeed() > 0) {
if (real_y_pos < 148) {
// Paddle is under the middle y - position else if (real_y_pos > 152) {
// ball is moving towards paddle else if (ball.getXSpeed() < 0) {
if ( real_y_pos != ball.getYPos()) {
if (ball.getYPos() < real_y_pos) {
// If ball's position greater than padle's, move down else if (ball.getYPos() > real_y_pos) {
Explaining the methodThis method is really simple, there are two different cases: At least one important thingIt would be no problem to write a AI (in this case) that is not to beat. The computer doesn't get tired and is always "watching" the ball. You as a programer have to think of the human player. A game makes only fun, if you can win. Winning mustn't be too easy but it has to be possible. So the computer has to be weak too. For example it has to be slower than the ball, so that the computer can't get fast balls or something like that. You have to find the right ballance between a strong and a weak AI, which might take much more time than writing the AI itself. Sourcecode download (*.zip - archive)Take a look at the applet Next chapterGeneration of a 2D landscape |
|
||||||||||||||||||||||||||||||||||||||||
|
Back to the topic sites: StudyPaper.com/Startseite/Computer/Informatik/Programmieren/Java StudyPaper.com/Startseite/Computer/Spiele External Links to this site are permitted without prior consent. | |||||||||||||||||||||||||||||||||||||||||
| Home | deutsch | Set bookmark | Send a friend a link | Copyright © | Impressum | |||||||||||||||||||||||||||||||||||||||||