Help with really bad pong game

Reply

Join Date: Nov 2008
Posts: 4
Reputation: Dan Bois is an unknown quantity at this point 
Solved Threads: 0
Dan Bois Dan Bois is offline Offline
Newbie Poster

Help with really bad pong game

 
0
  #1
Nov 30th, 2008
Hello I am new to java and i was woundering if someone could help me with this program. I was woundering how to put more balls into it and how to make the balls flash very fast almost like a strobe light. Please help thx.

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.concurrent.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Toolkit;


public class Pong3 extends JApplet
{
public static final int WIDTH = 500;
public static final int HEIGHT = 520;

private PaintSurface canvas;

public void init()
{
this.setSize(WIDTH, HEIGHT);

canvas = new PaintSurface();

this.add(canvas, BorderLayout.CENTER);
ScheduledThreadPoolExecutor executor =
new ScheduledThreadPoolExecutor(1);
executor.scheduleAtFixedRate(new AnimationThread(this),
0L, 10L, TimeUnit.MILLISECONDS);
}
}


class AnimationThread implements Runnable
{
JApplet c;

public AnimationThread(JApplet c)
{
this.c = c;
}

public void run()
{
c.repaint();
}
}


class PaintSurface extends JComponent
{
int paddle_x = 0;
int paddle_y = 500;

int score = 0;
float english = 1.0f;

Ball ball;

public PaintSurface()
{
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseMoved(MouseEvent e)
{
if (e.getX() -30- paddle_x > 5)
english = 1.5f;
else if (e.getX() -30- paddle_x < -5)
english = -1.5f;
else
english = 1.0f;
paddle_x = e.getX()- 30;
}
});

ball = new Ball(300);
}

public void paint (Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Shape paddle = new Rectangle2D.Float(
paddle_x, paddle_y, 100, 1);

if (ball.intersects(paddle_x, paddle_y, 100, 1)
&& ball.y_speed > 0)
{
ball.y_speed = -ball.y_speed;
ball.x_speed = (int)(ball.x_speed * english);

score += Math.abs(ball.x_speed * 10);
}

if (ball.intersects(0, 519, 2, 2)
&& ball.y_speed > 0)
{
ball.y_speed = -ball.y_speed;
ball.x_speed = (int)(ball.x_speed * english);

score += Math.abs(ball.x_speed * 10);
}

if (ball.getY() + ball.getHeight()
>= Pong3.HEIGHT)
{
ball = new Ball(15);
score -= 500;
}
ball.move();
g2.setColor(Color.RED);
g2.fill(ball);

g2.setColor(Color.BLACK);
g2.fill(paddle);
g2.drawString("Score: " + score, 300, 20);
}
}


class Ball extends Ellipse2D.Float
{
public int x_speed, y_speed;
private int d;
private int width = Pong3.WIDTH;
private int height = Pong3.HEIGHT;

public Ball(int diameter)
{
super((int)(Math.random() * (Pong3.WIDTH - 20)),
0, diameter, diameter);

this.d = diameter;
this.x_speed = (int)(Math.random() * 20);
this.y_speed = (int)(Math.random() * 20);
}

public void move()
{
{
if(super.x < 0 || super.x > width - d)
{
x_speed = -x_speed;
}

if(super.y < 0 || super.y > height - d)
{
y_speed = -y_speed;
}

super.x += x_speed;
super.y += y_speed;
}
}
}
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,433
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Help with really bad pong game

 
0
  #2
Dec 1st, 2008
First, use [code] .. [/code] tags around any code that you post.

Secondly, don't put all of your update logic in the paint() method. Do that in your animation loop, or better yet, another method called by your animation loop. The paint() method should just paint the ball(s) and paddle in their current positions, which were determined by the other method.
I was woundering how to put more balls into it
Use an array or List of Ball objects.
and how to make the balls flash very fast almost like a strobe light.
In your animation loop, toggle a variable on each ball if you want them to flash individually or a single variable for all of them on/off. Use that variable in your paint() method to determine whether to paint the ball that cycle.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC