Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Game Play & Game Design / .Re: Pong game on: 2004-06-03 11:50:59
Well guys were back with some problems. After lots of coding and crap, we got the ball to move and kinda bounce off the wall but one problem. The paddles dont move. The movement of the ball somehow cancles out the movement of the paddles/

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  
import java.awt.*;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.event.*;

public class Pong extends Applet implements ActionListener
{
      //creates the ball
     Ball Ball1=new Ball();
     
      //creates paddles
     pongpaddle Paddle1=new pongpaddle();      
      pongpaddle Paddle2=new pongpaddle();      

      //buttons
     Button btnStart=new Button("Start Game");
      Button btnRestart=new Button("Restart Game");

      //variables
     int Counter1=0;
      int Counter2=0;
      int MovX;
      int MovY;
      int Ballx=Ball1.getBall_xLoc();
      int Bally=Ball1.getBall_yLoc();
      int Xinc=1;
      int Yinc=1;
      Image offscreen;
      Graphics buffer;

      public void init()
      {
            //stuff for key listener
           requestFocus();
            addKeyListener(new DirectionKeyListener());

            //set locations for paddles 1 and 2
           Paddle1.setLocation(0,140);
            Paddle2.setLocation(680,140);
           
            btnStart.addActionListener(this);
            btnRestart.addActionListener(this);
            add(btnStart);
            add(btnRestart);

            offscreen=createImage(700,350);
            buffer=offscreen.getGraphics();
            //start();
     }

      private class DirectionKeyListener implements KeyListener
      {
            public void keyPressed(KeyEvent event)
            {
                  if(event.getKeyCode()==KeyEvent.VK_W & Paddle1.getyLoc1()>0)
                  {
                        Paddle1.moveUp();
                        repaint();
                  }

                        if(event.getKeyCode()==KeyEvent.VK_S & Paddle1.getyLoc1()<280)
                  {
                        Paddle1.moveDown();
                        repaint();
                  }

                  if(event.getKeyCode()==KeyEvent.VK_P & Paddle2.getyLoc1()>0)
                  {
                        Paddle2.moveUp();
                        repaint();
                  }

                  if(event.getKeyCode()==KeyEvent.VK_L & Paddle2.getyLoc1()<280)
                  {
                        Paddle2.moveDown();
                        repaint();
                  }
            }
            public void keyTyped(KeyEvent event){}
            public void keyReleased(KeyEvent event){}
      }

      public void drawPaddle(Graphics g)
      {
            //set color and draw paddle1
           g.setColor(Color.RED);
            Paddle1.drawPaddle(g);

            //set color and draw paddle2
           g.setColor(Color.BLUE);
            Paddle2.drawPaddle(g);
      }      

      public void actionPerformed(ActionEvent e)
      {
            if(e.getSource()==btnStart)
            move();
      }

      public void move()
      {
            Ballx=Ballx+Xinc;
            Bally=Bally+Yinc;
            Ball1.setLocation(Ballx+1,Bally+1);

            if(Ball1.getBall_xLoc()==0)
            {
                  Xinc=1;
                  repaint();
            }

            if(Ball1.getBall_xLoc()==680)
            {
                  Xinc=-1;
                  repaint();
            }

            if(Ball1.getBall_yLoc()==0)
            {
                  Yinc=1;
                  repaint();
            }

            if(Ball1.getBall_yLoc()==330)
            {
                  Yinc=-1;
                  repaint();
            }
            //repaint();
     }

      public void update(Graphics g)
      {
            paint(g);
      }      

      public void paint(Graphics g)
      {
            buffer.setColor(Color.BLACK);
            buffer.fillRect(0,0,700,350);            

            drawPaddle(buffer);  //draws paddle
           Ball1.drawBall(buffer);  //draws ball
           
            g.drawImage(offscreen,0,0,this);
           
            Ballx=Ballx+Xinc;
            Bally=Bally+Yinc;
            Ball1.setLocation(Ballx+1,Bally+1);
            move();
            repaint();
      }
}


Posted the whole code. If anyone can help, please do, this is my final >.<
2  Game Development / Game Play & Game Design / Pong game on: 2004-04-30 16:27:44
Im tring to create a java version of pong for a final project of my intro to java programming class. I have about 80% of the planning done and about ready to start making some skeleton code. Im a bit stuck on a few things. Animation is one of them. If someone could link me to a good animation tutiorial i would be in your bedt. Also, i also dont know how to code when the ball hits the paddle. Any help would be great.
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (33 views)
2014-07-18 06:55:21

Zero Volt (29 views)
2014-07-17 23:47:54

danieldean (24 views)
2014-07-17 23:41:23

MustardPeter (26 views)
2014-07-16 23:30:00

Cero (41 views)
2014-07-16 00:42:17

Riven (43 views)
2014-07-14 18:02:53

OpenGLShaders (31 views)
2014-07-14 16:23:47

Riven (30 views)
2014-07-14 11:51:35

quew8 (29 views)
2014-07-13 13:57:52

SHC (65 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!