tutorials.de Buch-Aktion 02/2012
Like Tree3Danke
  • 1 Beitrag von zeja
  • 1 Beitrag von zeja
  • 1 Beitrag von zeja
ERLEDIGT
NEIN
ANTWORTEN
9
ZUGRIFFE
336
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    KGD92 KGD92 ist offline Mitglied Silber
    Registriert seit
    Dec 2009
    Beiträge
    64
    Schönen Abend Leute,
    ich hab gestern mal angefangen eine kleines Pong Game zu schreiben (in vorbereitung auf ein größeres Projekt, d.h. wollte erstmal verstehen wie ein Spiel grundsätzliche funktioniert).

    Nun habe schön meine Kollisionsabfrage gebastelt und es geht soweit auch alles. Als ich dann die AI (= Inner Class implemts Runnable) implementiert habe und diese Ihren Schläger bewegt kollidiert meine Ball nicht mehr mit dem Schläger der AI (= Objekt PlayerTwo). Nun weis ich nicht genau, ob es daran liegt, dass die Animation des Balles als auch die AI selbst beides prallel laufende Threads sind, oder ob es mit was anderem zu tun hat.

    Hier ist mal der Source, ich hoffe ihr könnt meine Fehler finden(, falls es den den gibt)

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    
    package pongfengine;
     
    /* Simple Pong Game
     * Author: Kai Dölger
     * Date: 28.12.2009
     * Version: 0.1
     */
     
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.color.*;
     
    public class F2D extends Frame implements KeyListener, WindowListener
    {
     
        //Main Variables
        int x = 0;
        int y = 0;
        int height = 500;
        int width = 800;
     
        BatEntity playerOne;
        BatEntity playerTwo;
        BallEntity ball;
        int direction;
        Image dbImage;
        Graphics dbg;
        Thread movingBall;
        Thread ai;
     
     
        public static void main(String[] args)
        {
            F2D Engine = new F2D ();
            Engine.initGame();
        }
     
     
        public void initGame ()
        {
            this.setBounds(x, y, width, height);
            this.setTitle("Pong - FEngine");
            playerOne = new BatEntity ();
            playerTwo = new BatEntity ();
            ball = new BallEntity();
            initPlayerOne ();
            initPlayerTwo ();
            initBall ();
            this.addKeyListener(this);
            direction = 5;
            System.out.println(direction);
            movingBall = new Thread (new movingBall ());
            movingBall.start();
            ai = new Thread (new AI());
            ai.start();
            this.addWindowListener(this);
            this.setVisible(true);
        }
     
        @Override
        public void paint (Graphics g)
        {
            g.drawRect(playerOne.getLeft(), playerOne.getTop(), playerOne.getWidth(), playerOne.getHeight());
            g.drawRect(playerTwo.getLeft(), playerTwo.getTop(), playerTwo.getWidth(), playerTwo.getHeight());
            g.drawOval(ball.getLeft(), ball.getTop(), ball.getWidth(), ball.getHeight());
        }
     
        public void initPlayerOne ()
        {
            playerOne.setLeft(10);
            playerOne.setTop(225);
            playerOne.setWidth(10);
            playerOne.setHeight(50);
        }
     
        public void initPlayerTwo ()
        {
            playerTwo.setLeft(780);
            playerTwo.setTop(225);
            playerTwo.setWidth(10);
            playerTwo.setHeight(50);
     
        }
     
        public void initBall ()
        {
            ball.setHeight(20);
            ball.setWidth(20);
            ball.setLeft(390);
            ball.setTop(230);
        }
     
        public void keyTyped(KeyEvent e) { }
     
        public void keyPressed(KeyEvent e)
        {
            int keyCode = e.getKeyCode();
     
     
            int tempMove=playerOne.getTop();
     
            
            switch(keyCode)
            {
                case java.awt.event.KeyEvent.VK_UP:
                tempMove-=15;
                break;
            case java.awt.event.KeyEvent.VK_DOWN:    
                tempMove+=15;
                break;
            }
            if (tempMove >= 445 || tempMove <= 25)
            {
                if (tempMove > 445)
                {
                    tempMove = 445;
                }
                if (tempMove < 25)
                {
                    tempMove = 25;
                }
            }
            playerOne.setTop(tempMove);
            this.repaint();
        }
     
        public void keyReleased(KeyEvent e) { }
     
        public void windowOpened(WindowEvent e) { }
     
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
     
        public void windowClosed(WindowEvent e) { }
     
        public void windowIconified(WindowEvent e) { }
     
        public void windowDeiconified(WindowEvent e) { }
     
        public void windowActivated(WindowEvent e) { }
     
        public void windowDeactivated(WindowEvent e) { }
     
        public class movingBall implements Runnable
        {
     
            
            int distance=10;
            int counter =0;
            int speed=100;
     
     
            public void run ()
            {
     
            //=====DIRECTIONS=====
            //1 north
            //2 south
            //3 east
            //4 west
            //5 north east
            //6 north west
            //7 south east
            //8 south west
     
            int dist1=10;
            int dist2=5;
     
            //continue till the program ends
            while(true)
            {
                //just a temporary variable for storing intermediate ball position
                int tempTop=0;
                int tempLeft=0;
                //check and see which direction the ball is going
                //if direction is set to go up i.e. 2 then subtract distance
                //if direction is set to go down then add distance
                switch(direction)
                {
                case 1:    //north
                    tempTop=ball.getTop()-dist1;
                    break;
                case 2 : //south
                    tempTop=ball.getTop()+dist1;
                    break;
                case 3:    //east
                    tempTop=ball.getLeft()+dist2;
                    break;
                case 4:    //west
                    tempTop=ball.getLeft()-dist2;
                    break;
                case 5:    //north east
                    tempTop=ball.getTop()-dist1;
                    tempLeft=ball.getLeft()+dist2;
                    break;
                case 6:    //north west
                    tempTop=ball.getTop()-dist1;
                    tempLeft=ball.getLeft()-dist2;
                    break;
                case 7:    //south east
                    tempTop=ball.getTop()+dist1;
                    tempLeft=ball.getLeft()+dist2;
                    break;
                case 8:    //south west
                    tempTop=ball.getTop()+dist1;
                    tempLeft=ball.getLeft()-dist2;
                    break;
                }
                //update the balls position in the datastructure
                ball.setTop(tempTop);
                ball.setLeft(tempLeft);
               
                //=====TOP COLLISIONS=====
                if(ball.getTop()<=20 &&  direction==5)
                    //coming north east now go south east
                    direction=7;
                if(ball.getTop()<=20 && direction==6)
                    //coming north west now go south west
                    direction=8;
                //=====BOTTOM COLLISIONS=====
                if(ball.getTop()>=480 && direction==7)
                    //coming south east now go north east
                    direction=5;
                if(ball.getTop()>=480 && direction==8)
                    //coming north east now go south east
                    direction=6;
                //====LEFT COLLISIONS=====
                if(ball.getLeft() <=0 && direction == 6)
                    //coming north west going north east
                    direction=5;
                if(ball.getLeft() <=0 && direction == 8)
                    //coming south west going south east
                    direction=7;
                //=====RIGHT COLLISIONS=====
                if(ball.getLeft() >=780 && direction == 5)
                    //coming north east going north west
                    direction=6;
                if(ball.getLeft() >=780 && direction == 7)
                    //coming south east going south west
                    direction=8;
                //=====BAT (LEFT) COLLISIONS=====
                if ((ball.getLeft() <= playerOne.getLeft()+20 && playerOne.getTop()<=ball.getTop() && ball.getTop()>=playerOne.getTop()+50) && direction == 8)
                    {//coming south west going south east
                    direction=7;
                    counter++;
                    }
                if ((ball.getLeft() <= playerOne.getLeft()+20 && playerOne.getTop()<=ball.getTop() && ball.getTop()>=playerOne.getTop()+50) && direction == 6)
                    {//coming north west going north east
                    direction=5;
                    counter++;
                    }
                //=====BAT (RIGHT) COLLISIONS=====
                if ((ball.getLeft() >= playerTwo.getLeft()-10 && playerTwo.getTop()<=ball.getTop() && ball.getTop()>=playerTwo.getTop()+50) && direction == 5)
                    {//coming south east going south west
                    direction=6;
                    counter++;
                    }
                if ((ball.getLeft() >= playerTwo.getLeft()-10 && playerTwo.getTop()<=ball.getTop() && ball.getTop()>=playerTwo.getTop()+50) && direction == 7)
                    {//coming south east going south west
                    direction=5;
                    counter++;
                    }
                //redraw the ball at new locations
                repaint();
                //Increases Speed of the Game when the Ball hits a Bat
                if (counter <= 90)
                {
                    speed = speed - counter;
                   //System.out.println (speed);
                }
                //putting the thread to sleep so we can create some delay
                //so the ball's motion looks natural.
                try
                {
                    Thread.sleep(speed);
                }
                catch(Exception e)
                {
                    System.out.println("Error in running thread " + e);
                }
            }
            }
            
     
     
        }
     
        public class AI implements Runnable
        {
     
            int tempTop;
     
            public void run ()
            {
                while (true)
                {
                    if (direction == 5 || direction == 7)
                    if (ball.getTop()+10 < playerTwo.getTop()+25)
                    {
                        tempTop = playerTwo.getTop();
                        tempTop-= 15;
                        playerTwo.setTop(tempTop);
                    }
                    if (ball.getTop()+10 > playerTwo.getTop()+25)
                    {
                        tempTop = playerTwo.getTop();
                        tempTop+= 15;
                        playerTwo.setTop(tempTop);
                    }
     
                    repaint();
                    
                    try
                    {
                        Thread.sleep(100);
                    }
                    catch (Exception e)
                    {
                        System.out.println("Error in running Thread " + e);
                    }
                }
            }
        }
     
     
    }

    Grüße Kai
     

  2. #2
    Avatar von zeja
    zeja zeja ist offline Mitglied Diamant
    tutorials.de Premium-User
    Registriert seit
    Sep 2006
    Beiträge
    2.960
    Hmm deine Zugriffe auf die Methoden die von den Threads verwendet werden, sollten synchronized sein, damit immer der aktuelle Wert gelesen werden kann.
    KGD92 bedankt sich. 

  3. #3
    KGD92 KGD92 ist offline Mitglied Silber
    Registriert seit
    Dec 2009
    Beiträge
    64
    Heißt, ich soll die beiden Run() als synchronized setzen? Oder verstehe ich dich da falsch?
     

  4. #4
    Avatar von zeja
    zeja zeja ist offline Mitglied Diamant
    tutorials.de Premium-User
    Registriert seit
    Sep 2006
    Beiträge
    2.960
    Alle Setter und Getter in Ball und wahrscheinlich auch in Bat
    KGD92 bedankt sich. 

  5. #5
    KGD92 KGD92 ist offline Mitglied Silber
    Registriert seit
    Dec 2009
    Beiträge
    64
    Hab ich jetzt gemacht, aber nach dem ich den Bat vom AI mal per hand gesteuert und trotzdem gab es keine Kollision, also hab ich mal den Debugger bemüht und festgestellt, dass er überhaupt nicht in den entsprechenden if-Block kommt. Komisch, allerdings weis ich nicht, was an den Bedingungen falsch sein soll.

    Hoffe ihr findets, werde jetzt erstmal an anderer Stellen an dem Game weiterschreiben.

    Grüße Kai
     

  6. #6
    Avatar von zeja
    zeja zeja ist offline Mitglied Diamant
    tutorials.de Premium-User
    Registriert seit
    Sep 2006
    Beiträge
    2.960
    Kein Wunder dass das ganze nicht funktioniert hat. Benutze niemals irgendwelche ints um dir einen Status zu merken. Das mit den directions konnte so ja nur schief gehen. Bei dem Ball um dem Player ist es auch sinnvoll noch getRight und getBottom Methoden hinzuzufügen. Deine Berechnungen sollten auch nicht mit festen Zahlen arbeiten, da du sonst nie die Größe von Player und Ball ändern kannst ohne alles umzuschreiben. So ist es einigermaßen lesbar und funktioniert. Noch nicht super aber du sollst ja auch noch was zu tun haben

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    
    package de;
     
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.util.concurrent.TimeUnit;
     
    public class F2D extends Frame implements KeyListener, WindowListener {
     
        private static final int PLAYER_STEP = 15;
        // Main Variables
        int x = 0;
        int y = 0;
        int height = 500;
        int width = 800;
     
        BatEntity playerOne;
        BatEntity playerTwo;
        BallEntity ball;
        Direction direction;
        Image dbImage;
        Graphics dbg;
        Thread movingBall;
        Thread ai;
     
        public static void main(String[] args) {
            F2D Engine = new F2D( );
            Engine.initGame( );
        }
     
        public void initGame() {
            this.setBounds(x, y, width, height);
            this.setTitle("Pong - FEngine");
            playerOne = new BatEntity( );
            playerTwo = new BatEntity( );
            ball = new BallEntity( );
            initPlayerOne( );
            initPlayerTwo( );
            initBall( );
            this.addKeyListener(this);
            direction = Direction.NORTH_EAST;
            System.out.println(direction);
            movingBall = new Thread(new MovingBall( ));
            movingBall.start( );
            ai = new Thread(new AI( ));
            ai.start( );
            this.addWindowListener(this);
            this.setVisible(true);
        }
     
        @Override
        public void paint(Graphics g) {
            g.drawRect(playerOne.getLeft( ), playerOne.getTop( ), playerOne
                    .getWidth( ), playerOne.getHeight( ));
            g.drawRect(playerTwo.getLeft( ), playerTwo.getTop( ), playerTwo
                    .getWidth( ), playerTwo.getHeight( ));
            g.drawOval(ball.getLeft( ), ball.getTop( ), ball.getWidth( ), ball
                    .getHeight( ));
        }
     
        public void initPlayerOne() {
            playerOne.setLeft(10);
            playerOne.setTop(225);
            playerOne.setWidth(10);
            playerOne.setHeight(50);
        }
     
        public void initPlayerTwo() {
            playerTwo.setLeft(780);
            playerTwo.setTop(225);
            playerTwo.setWidth(10);
            playerTwo.setHeight(50);
     
        }
     
        public void initBall() {
            ball.setHeight(20);
            ball.setWidth(20);
            ball.setLeft(390);
            ball.setTop(230);
        }
     
        public void keyTyped(KeyEvent e) {
        }
     
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode( );
     
            int tempMove = playerOne.getTop( );
     
            switch (keyCode) {
                case java.awt.event.KeyEvent.VK_UP:
                    tempMove -= PLAYER_STEP;
                    break;
                case java.awt.event.KeyEvent.VK_DOWN:
                    tempMove += PLAYER_STEP;
                    break;
            }
     
            int minMove = 0;
            int maxMove = height - playerOne.getHeight( );
            if (tempMove >= maxMove || tempMove <= minMove) {
                if (tempMove > maxMove) {
                    tempMove = maxMove;
                }
                if (tempMove < minMove) {
                    tempMove = minMove;
                }
            }
            playerOne.setTop(tempMove);
            this.repaint( );
        }
     
        public void keyReleased(KeyEvent e) {
        }
     
        public void windowOpened(WindowEvent e) {
        }
     
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
     
        public void windowClosed(WindowEvent e) {
        }
     
        public void windowIconified(WindowEvent e) {
        }
     
        public void windowDeiconified(WindowEvent e) {
        }
     
        public void windowActivated(WindowEvent e) {
        }
     
        public void windowDeactivated(WindowEvent e) {
        }
     
        public class MovingBall implements Runnable {
     
            private static final int COUNTER_THRESHOLD = 90;
            private static final int INITIAL_SPEED = 100;
            int distance = 10;
            int counter = 0;
            int speed = INITIAL_SPEED;
     
            public void run() {
     
                int dist1 = 10;
                int dist2 = 5;
     
                // continue till the program ends
                while (true) {
                    int tempCounter = counter;
                    // just a temporary variable for storing intermediate
                    // ball position
                    int tempTop = 0;
                    int tempLeft = 0;
                    // check and see which direction the ball is going
                    // if direction is set to go up i.e. 2 then subtract
                    // distance
                    // if direction is set to go down then add distance
                    
                    if(direction.isNorth( )){
                        tempTop = ball.getTop( ) - dist1;
                    }
                    if(direction.isSouth( )){
                        tempTop = ball.getTop( ) + dist1;
                    }
                    if(direction.isWest( )){
                        tempLeft = ball.getLeft( ) - dist2;
                    }
                    if(direction.isEast( )){
                        tempLeft = ball.getLeft( ) + dist2;
                    }
                    // update the balls position in the datastructure
                    ball.setTop(tempTop);
                    ball.setLeft(tempLeft);
     
                    int ballLeftCollision = x;
                    int ballRightCollision = width - ball.getWidth( );
                    int ballTopCollision = y + ball.getHeight( );
                    int ballBottomCollision = height;
     
                    // =====TOP COLLISIONS=====
                    if (direction.isNorth( ) && ball.getTop( ) <= ballTopCollision) {
                        direction = direction.toggleNorthSouth( );
                    }
                    // =====BOTTOM COLLISIONS=====
                    if (direction.isSouth( )
                            && ball.getBottom( ) >= ballBottomCollision) {
                        // coming south east now go north east
                        direction = direction.toggleNorthSouth( );
                    }
                    // ====LEFT COLLISIONS=====
                    if (direction.isWest( ) && ball.getLeft( ) <= ballLeftCollision) {
                        // coming north west going north east
                        direction = direction.toggle( );
                    }
                    // =====RIGHT COLLISIONS=====
                    if (direction.isEast( )
                            && ball.getRight( ) >= ballRightCollision) {
                        // coming north east going north west
                        direction = direction.toggle( );
                    }
     
                    // =====BAT (LEFT) COLLISIONS=====
                    if (direction.isWest( )
                            && ball.getLeft( ) <= playerOne.getRight( )
                            && ball.getTop( ) >= playerOne.getTop( )
                            && ball.getTop( ) <= playerOne.getBottom( )) {
                        direction = direction.toggle( );
                        tempCounter++;
                    }
                    // =====BAT (RIGHT) COLLISIONS=====
                    if (direction.isEast( )
                            && ball.getRight( ) >= playerTwo.getLeft( )
                            && ball.getTop( ) >= playerTwo.getTop( )
                            && ball.getTop( ) <= playerTwo.getBottom( )) {
                        // coming
                        // south
                        // east
                        // going
                        // south west
                        direction = direction.toggle( );
                        tempCounter++;
                    }
     
                    // redraw the ball at new locations
                    repaint( );
                    // Increases Speed of the Game when the Ball hits a
                    // Bat
                    if (tempCounter != counter && tempCounter <= COUNTER_THRESHOLD) {
                        speed = INITIAL_SPEED - tempCounter;
                        // System.out.println (speed);
                    }
                    if (speed < 0) {
                        speed = 0;
                    }
     
                    counter = tempCounter;
     
                    // putting the thread to sleep so we can create some
                    // delay
                    // so the ball's motion looks natural.
                    try {
                        TimeUnit.MILLISECONDS.sleep(speed);
                    }
                    catch (Exception e) {
                        System.out.println("Error in running thread " + e);
                    }
                }
            }
     
        }
     
        public class AI implements Runnable {
     
            int tempTop;
     
            public void run() {
                while (true) {
                    int ballTop = ball.getHeight( ) / 2;
                    int playerTop = playerTwo.getHeight( ) / 2;
     
                    if (direction == Direction.NORTH_EAST
                            || direction == Direction.SOUTH_EAST) {
                        if (ball.getTop( ) + ballTop < playerTwo.getTop( )
                                + playerTop) {
                            tempTop = playerTwo.getTop( ) - PLAYER_STEP;
                        }
                    }
                    if (ball.getTop( ) + ballTop > playerTwo.getTop( ) + playerTop) {
                        tempTop = playerTwo.getTop( ) + PLAYER_STEP;
                    }
     
                    int minMove = 0;
                    int maxMove = height - playerTwo.getHeight( );
                    if (tempTop >= maxMove || tempTop <= minMove) {
                        if (tempTop > maxMove) {
                            tempTop = maxMove;
                        }
                        if (tempTop < minMove) {
                            tempTop = minMove;
                        }
                    }
     
                    playerTwo.setTop(tempTop);
     
                    repaint( );
     
                    try {
                        TimeUnit.MILLISECONDS.sleep(100);
                    }
                    catch (Exception e) {
                        System.out.println("Error in running Thread " + e);
                    }
                }
            }
        }
     
    }

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    
    package de;
     
    public enum Direction {
        NORTH, SOUTH, EAST, WEST, NORTH_EAST, NORTH_WEST, SOUTH_EAST, SOUTH_WEST;
     
        public boolean isNorth() {
            return this == NORTH || this == NORTH_EAST || this == NORTH_WEST;
        }
     
        public boolean isSouth() {
            return this == SOUTH || this == SOUTH_EAST || this == SOUTH_WEST;
        }
     
        public boolean isWest() {
            return this == WEST || this == SOUTH_WEST || this == NORTH_WEST;
        }
     
        public boolean isEast() {
            return this == EAST || this == SOUTH_EAST || this == NORTH_EAST;
        }
     
        public Direction toggleNorthSouth() {
            if (isNorth( )) {
                if (this == NORTH_EAST) {
                    return SOUTH_EAST;
                }
                else {
                    return SOUTH_WEST;
                }
            }
            else {
                if (this == SOUTH_EAST) {
                    return NORTH_EAST;
                }
                else {
                    return NORTH_WEST;
                }
            }
        }
     
        public Direction toggleEastWest() {
            if (isWest( )) {
                if (this == NORTH_WEST) {
                    return NORTH_EAST;
                }
                else {
                    return SOUTH_EAST;
                }
            }
            else {
                if (this == NORTH_EAST) {
                    return NORTH_WEST;
                }
                else {
                    return SOUTH_WEST;
                }
            }
        }
     
        public Direction toggle() {
            return this.toggleNorthSouth( ).toggleEastWest( );
        }
    }

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    
    package de;
     
    public class Entity {
     
        private int left;
        private int top;
        private int width;
        private int height;
     
        /**
         * @return the left
         */
        public synchronized int getLeft() {
            return left;
        }
     
        /**
         * @param left
         *            the left to set
         */
        public synchronized void setLeft(int left) {
            this.left = left;
        }
     
        /**
         * @return the top
         */
        public synchronized int getTop() {
            return top;
        }
     
        /**
         * @param top
         *            the top to set
         */
        public synchronized void setTop(int top) {
            this.top = top;
        }
     
        /**
         * @return the width
         */
        public synchronized int getWidth() {
            return width;
        }
     
        /**
         * @param width
         *            the width to set
         */
        public synchronized void setWidth(int width) {
            this.width = width;
        }
     
        /**
         * @return the height
         */
        public synchronized int getHeight() {
            return height;
        }
     
        /**
         * @param height
         *            the height to set
         */
        public synchronized void setHeight(int height) {
            this.height = height;
        }
     
        public synchronized int getRight() {
            return this.left + this.width;
        }
     
        public synchronized int getBottom() {
            return this.top + this.height;
        }
    }

    Code java:
    1
    2
    3
    4
    5
    
    package de;
     
    public class BallEntity extends Entity {
     
    }

    Code java:
    1
    2
    3
    4
    5
    
    package de;
     
    public class BatEntity extends Entity {
     
    }
    KGD92 bedankt sich. 

  7. #7
    KGD92 KGD92 ist offline Mitglied Silber
    Registriert seit
    Dec 2009
    Beiträge
    64
    Dann bedanke ich mich mal recht herzlich und werde das ganze später mal umsetzten

    Grüße Kai
     

  8. #8
    KGD92 KGD92 ist offline Mitglied Silber
    Registriert seit
    Dec 2009
    Beiträge
    64
    Sorry, aber irgendwie verstehe ich die Klasse Direction noch nicht so ganz. Könntest du da vielleicht noch ein, zwei Worte drüber los werden?

    Schönen Nachmittag noch, Kai
     

  9. #9
    Avatar von zeja
    zeja zeja ist offline Mitglied Diamant
    tutorials.de Premium-User
    Registriert seit
    Sep 2006
    Beiträge
    2.960
    Das ist keine Klasse sondern ein enum. Ein enum ist eine Aufzählung, in diesem Fall von Richtungen. Zusätzlich hab ich noch Methoden implementiert, um rauszufinden um welche der 4 Hauptrichtungen es sich handelt.

    Siehe dazu auch:

    http://openbook.galileocomputing.de/...06a93952704d84
    http://openbook.galileocomputing.de/...4a5ed5df9a32d4
     

  10. #10
    KGD92 KGD92 ist offline Mitglied Silber
    Registriert seit
    Dec 2009
    Beiträge
    64
    Okay, danke ich hab den Fehler gefunden, der mich verzeifeln ließ und zwar habe ich bei RIGHT,LEFT,BAT(LEFT),BAT(RIGHT) den Methodenaufruf direction.toogle() mit directio.toogleEastWest() eresetzt, denn vorher ist der Ball in die Richtung abgeprallt, aus der er kam, weil mit direction.toogle() erst toogleNorthSouth() aufgerufen wurde und er dann in die Schleife

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    
    if (this == SOUTH_EAST)
                {
                    return NORTH_EAST;
                }
                else
                {
                    return NORTH_WEST;
                }

    reinlief und wieder nach oben abprallte.

    Ich hoffe du verstehst was ich meine
     

Ähnliche Themen

  1. Röhrenbildschirm versagt bei Hitze?
    Von timestamp im Forum Hardware
    Antworten: 4
    Letzter Beitrag: 03.07.10, 12:36
  2. Antworten: 7
    Letzter Beitrag: 12.10.06, 16:39
  3. Navigation versagt...
    Von Lyn555 im Forum Flash Plattform
    Antworten: 6
    Letzter Beitrag: 20.09.06, 20:51
  4. include versagt auf localhost!
    Von trekmaster im Forum PHP
    Antworten: 9
    Letzter Beitrag: 24.09.04, 14:56
  5. utf8_encode versagt bei " ?
    Von frank manischewski im Forum PHP
    Antworten: 2
    Letzter Beitrag: 24.02.04, 22:18