Simple Ping Pong Game
Jul 14, 2014
When I ran the methods "go1,go2,go3" separately without clicking on the menu , all of them worked well. by separately I mean, when I closed the program changed the source code from "go1;" to "go2" and go3. But when I click on the "change ball" and then "1 ball" or any thing, all I get is a new frame which is blank. And the old frame freezes.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Administrator
*/
[code]...
View Replies
ADVERTISEMENT
Feb 26, 2014
I have created this ping pong game but having problem with ball and paddle collusion. After first collusion it gives score 1 but later it never touch the paddle.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.Random.*;
import javax.swing.ImageIcon;
public class Ball extends JFrame implements ActionListener, KeyListener{
int xSpeed=0;
[Code] ....
I think something is wrong here:
public void actionPerformed(ActionEvent e){
y=y+xSpeed;
repaint();
if(y> this.getWidth()){
y=25;
score -=1;
}
if(blockRect.intersects(ballRect)){
y=40;
score +=1;
x =200;
y = 300;
}
View Replies
View Related
Dec 27, 2014
I don't know how to write a code to pong ball, when it touch red block.
public class bounc2 extends JPanel implements ActionListener,KeyListener{
double x = 0,y=0,velX=3,velY=3;
double x2 = 5,y2=145,velX2=0,velY2=0;
double x3= 473,y3=145,velX3=0,velY3=0;
Timer t = new Timer(5,this);
public bounc2(){
setBackground(Color.GREEN);
[Code] .....
View Replies
View Related
Apr 19, 2014
I'm a beginner at java and am trying to code a basic pong game java applet. The code has no errors in it but when i run it the .png's do not come up in the window until its moved off screen. when the .png's are shown they do not update whatsoever. I thought the problem could be with the key-listener but even the ball (which moves independently of user input) does not move.
I've placed all the code from the 5 classes below. There's some unnecessary code in the background class but that's because I'm thinking of making the background scroll later on.
package main;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
[Code] ....
View Replies
View Related
Jun 4, 2014
I can't figure out this problem that I'm having in my Pong game. It's compiling fine, except when I run it, it gives me an overflow error.
Here's the code:
// Pong
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
public class Pong extends JFrame {
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
[Code] .....
Here's the error:
java.lang.StackOverflowError
at sun.awt.AppContext.get(AppContext.java:604)
at com.sun.java.swing.SwingUtilities3.getDelegateRepa intManager(SwingUtilities3.java:120)
at javax.swing.RepaintManager.getDelegate(RepaintMana ger.java:1625)
at javax.swing.RepaintManager.addDirtyRegion(RepaintM anager.java:445)
The error keeps going for a lot of more lines.
The line that gets highlighted is the first bracket of the Pong constructor.
View Replies
View Related
Jan 28, 2014
I have made a "Pong" game and how to check if the ball has hit the top or the bottom of a paddle, however I do know how to check if it has hit the left or right side:
Java Code:
if((BALL.getX() >= LEFT_BAT.getX() && BALL.getX() <= LEFT_BAT.getX() + LEFT_BAT.getWidth()
&& BALL.getY() >= LEFT_BAT.getY() && BALL.getY() <= LEFT_BAT.getY() + LEFT_BAT.getHeight())
&& (BALL.getX() >= RIGHT_BAT.getX() && BALL.getX() <= RIGHT_BAT.getX() + RIGHT_BAT.getWidth()
&& BALL.getY() >= RIGHT_BAT.getY() && BALL.getY() <= RIGHT_BAT.getY() + RIGHT_BAT.getHeight())){
BALL.increaseDX();
BALL.setDX(-BALL.getDX());
} mh_sh_highlight_all('java');
It seems as though I would use the same code for checking the top or bottom.
View Replies
View Related
May 25, 2014
I'm trying to create a simple snake game, but I can't get my timer and keyListener to work...
//Timer
Timer tm = new Timer(5, this);
int x = 50, y = 250, vel = 2;
// The snake
g.setColor(Color.black);
g.fillRect(x, y, snakeSize, snakeSize);
[Code] .....
View Replies
View Related
Oct 22, 2014
I am using libgdx and box2d to try to make a simple cannon game.
When I have the cannonball collide with another object I want to delete that object and have it stop calling all of it's functions. It doesn't seem like I can have it delete itself, and the contactListener has a reference to the cannonball and other object, but if I set either to null, it seems the original reference still exists and the object isn't deleted.
View Replies
View Related
Apr 26, 2015
I am making a very simple 2D array game where the player is asked what size they would like the game board to be. After entering, it displays the board and the player 'P' starts at index [0][0]. After that, they are asked for an action, which can be "up", "down", "left", "right", or "exit". I will be including some extra later (like a treasure at the end, or random obstacles), but for now this is what the game consists of.
I was instructed to "do nothing" when/if the player attempts to go out of bounds. I am trying to simply print an error, such as "Out of bounds! Try again.", then prompt the player again for an action. I even tried to make a boolean method to catch it, but to no avail.
I don't want the exception to occur at all. I just simply want the error message to print to the player and ask for another action. I would prefer not to use try/catch, or try/catch/finally since I already tried that and it still gave the exception error.This program consists of two classes. I will show the class containing the main first, then the client-server type class second.
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
World world = new World();
boolean keepPlaying;
keepPlaying = true;
boolean isOutOfBounds;
isOutOfBounds = false;
int height = 0;
int width = 0;
int x = 0;
int y = 0;
[code]....
View Replies
View Related
Apr 1, 2014
I am writing code for a simple java game. The problem I am running into is that I have a moving icon (J button) that is controlled using the keyboard (arrows) and I also have stationary icons (J button). Is there a way to keep the moving icon from passing over the stationary icon? I would like the stationary icon to act like a wall.
View Replies
View Related
Jan 8, 2015
The game is too have two players, each user clicks a button and two dices will roll, if a user rolls a double they win.
firstly I have started with two imageview's and a button I am trying to randomise two images by clicking one button I have managed to randomise one image in one box but I am struggling to randomise both image views here is my code so far.
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
[Code]....
That code is only generating a random dice image in one of the image views I have hit a brick wall
View Replies
View Related
May 24, 2015
I am trying to make a simple platformer game in Java. When I try to move the character I made it only moves over then immediately back
import java.awt.event.KeyEvent;
public class UCC
{
private int xLoc;
private int yLoc;
private int xSpeed;
private int ySpeed;
private double gravity;
private int size;
[code]....
View Replies
View Related
Nov 3, 2014
I'm trying to run a ping command but all I manage to do Is open a new cmd window.I'm not sure how to add the ping command to the String[].
import java.io.*;
class callcmd{
public static void main(String[] args){
try {
String[] command={"cmd", "/k","start","ping 192.168.10.9" };
Process process=Runtime.getRuntime().exec(command);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
View Replies
View Related
Jul 28, 2014
I am trying to make a game of pong and am having trouble adding my ball class to a JPanel. Here is what I have so far.
package pong;
import java.awt.*;
public class Ball{
private int xPos,yPos;
public int dx = 5, dy = -5;
public Ball()
[code].....
I want to be able to add this to a JPanel but it is not allowing me to add the ball. Here is my JPanel
package pong;
import java.awt.*;
import javax.swing.JPanel;
public class BallTestPanel
[Code] ....
It reads panel.add(ball); as an error. I says that I need to change the type ball to component.
View Replies
View Related
Apr 11, 2013
I was making a pong game, part copied code off the internet, part my own code, part my friends code. It works well enough, but the ball starts at one speed, and stays the same. I would like to know how to make the ball slowly increase.
It is a java applet, which shouldn't casue any harm right?
My code:
Pong Applet
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Pong extends Applet implements MouseMotionListener, KeyListener {
[Code] .....
View Replies
View Related
Apr 23, 2014
I wrote a code from our text book for Pong. Im trying to figure out how to fill the ball with color. Im also wondering how to speed the ball up once there is contact with the paddle. The initial speed is ok but once contact is made with the paddle, it slows down. Makes it too easy to play. Here is my code so far. As far as the game goes, it works and is good. Just wanted to add a couple customizations.
Java Code:
package pong;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
[Code] .....
View Replies
View Related
May 9, 2014
I need to make a scoreboard that can count how many times a ball hits a moving platform in pong and have no clue how to approach this.
View Replies
View Related
Mar 12, 2015
Opoly works this way: The board is a circular track of variable length (the user determines the length when the game app runs). There is only one player, who begins the game at position 0.
Thus, if the board length is 20, then the board locations start at position 0 and end at position 19. The player starts with a reward of 100, and the goal of the game is to reach or exceed reward value 1000. When this reward value is reached or exceeded, the game is over. When the game ends, your program should report the number of turns the player has taken, and the final reward amount attained.
In Opoly the game piece advances via a spinner - a device that takes on one of the values 1, 2, 3, 4, 5 at random, with each of the five spin values equally likely.
Although the board is circular, you should draw the state of the board as a single "line", using an 'o' to represent the current player position, and * represent all other positions. Thus if the board size is 10, then this board drawing:
**o******
means that the player is at location 2 on the board.
Here are the other Opoly game rules:
If your board piece lands on a board cell that is evenly divisible by 7, your reward doubles.
If you land on the final board cell, you must go back 3 spaces. Thus if the board size is 20, the last position is position 19, and if you land there, you should go back to position 16. (If the position of the last cell is evenly divisible by 7, no extra points are added, but if the new piece location, 3 places back, IS evenly divisible by 7, then extra points ARE added).
If you make it all the way around the board, you get 100 points. Note that if you land exactly on location 0, you first receive 100 extra points (for making it all the around), and then your score is doubled, since 0 is evenly divisible by 7,
Every tenth move (that is, every tenth spin of the spinner, move numbers 10,20,30,... etc.), reduces the reward by 50 points. This penalty is applied up front, as soon as the 10th or 20th or 30th move is made, even if other actions at that instant also apply. Notice that with this rule it's possible for the reward amount to become negative.
Here is the driver class for the game:
import java.util.*;
public class OpolyDriver{
public static void main(String[] args){
System.out.println("Enter an int > 3 - the size of the board");
Scanner s = new Scanner(System.in);
int boardSize = s.nextInt();
[Code] ....
heres the methods:
REQUIRED CODE STRUCTURE: Your Opoly class must include the following methods (in addition to the Opoly constructor) and must implement the method calls as specified:
playGame - The top-level method that controls the game. No return value, no parameters. Must call drawBoard, displayReport, spinAndMove, isGameOver.
spinAndMove - spins the spinner and then advances the piece according to the rules of the game. No return value, no parameters. Must call spin and move.
spin - generates an integer value from 1 to 5 at random- all equally likely. Returns an integer, no parameters.
move - advances the piece according to the rules of the game. No return value, takes an integer parameter that is a value from 1 to 5.
isGameOver - checks if game termination condition has been met. Returns true if game is over, false otherwise. No parameters.
drawBoard - draws the board using *'s and an o to mark the current board position. Following each board display you should also report the current reward. No return value, no parameters.
displayReport - reports the end of the game, and gives the number of rounds of play, and the final reward. No return value, no parameters.
View Replies
View Related
May 9, 2015
Im trying to make a tic tac toe game that you play against the computer using a random number generator and two dimensional arrays for the game board. Im not trying to make a GUI, the assignment is to have the board in the console, which I have done. I have run into a few problems with trying to get the computer player to correctly generate 2 integers and have those two integers be a place on the game board. Here is my code so far.
import java.util.Random;
import java.util.Scanner;
public class TicTacToe {
private static Scanner keyboard = new Scanner(System.in);
private static char[][] board = new char[3][3];
public static int row, col;
[Code] ....
View Replies
View Related
Oct 2, 2014
I tried this program, its guessing game. Where program decide 1 number and user will guess number. I am getting 1 problem. When user want to play game again.the random number remain same. So where i can put reset random in code..? And 1 more question if I want to write driver code for this. What should i transfer to driver code.
import java.util.Random;
import java.util.Scanner;
public class GuessGame {
public static void main(String args[])
{
String choice="y";
[Code] ......
View Replies
View Related
Nov 25, 2014
This code models a simple ATM machine that can deposit, withdraw, and show the 10 latest transactions in an array of 10 index values.
My problem is that I can't get the balance right after 10 deposits, the balance only seems to sum the values in my array, not including the transactions made before those ten.
import java.util.Scanner;
public class cashier2 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int amount = 0;
[Code] ....
View Replies
View Related
Mar 11, 2002
I coded up a simple applet and some html with an applet tag and it tested out OK using the applet viewer. But it doesn't do anything (except draw a grey empty box 100x200) when executed from the application server. For the applicaton server test, I put the applet tag in the HTML Body of the JSP page i.e.
<applet code=CatApplet.class height=100 width=200> </applet>
The applet class file is in the same directory as the JSP page. The source for it is:
import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.net.*;
import java.util.Date;public class CatApplet extends JApplet{public void init (){setBackground(Color.green);
resize (250,100);}public void paint (Graphics g){g.drawString ("lets get some cat gui going", 35, 15);}}
Why won't my applet run when the applet tags are in the JSP page?
View Replies
View Related
Feb 4, 2015
I'm just starting to learn Java GUI. I'm trying to make a simple GUI that will update dynamically but having trouble ending the loop correctly.The Currently I've got the GUI updating and stopping, but the Test class method loop does not terminate correctly, it continues to count.
import java.awt.*; // Using AWT container and component classes
import java.awt.event.*; // Using AWT event classes and listener interfaces
import java.util.Timer;
import java.util.TimerTask;
[code]....
View Replies
View Related
Apr 10, 2014
I'm not sure what is wrong with my code:
import java.util.Scanner;
public class Main {
int age = 0;
System.out.println("Enter your age: ");
// Read in values
age = in.nextInt();
}
The in is underlined, telling me "cannot find symbol"
View Replies
View Related
Nov 23, 2014
I'm getting back into the swing of things with Java after using I'm asked to utilize a simple GUI in order to take in the starting data, I cannot seem to get this to work. I'm getting this error Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at Input.buildPanel(Input.java:53)
at Input.<init>(Input.java:27)
at InputDemo.main(InputDemo.java:5)
I've created two classes
import javax.swing.*;
public class Input extends JFrame {
private JPanel panel;
private JLabel messageLabel;
private JLabel messageLabel1;
private JLabel messageLabel2;
private JTextField shiftHrs;
[code]....
View Replies
View Related
Jul 7, 2014
[URL] I am unable to get any XML output. I am unable to figure out how to send the contents of System.out to a String that can be printed. I saw an SO post and tried using ByteOutputStreamArray, but that did not work.
XML Schema -
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="2.0">
<xsd:element name="Greetings" type="GreetingListType" />
<xsd:complexType name="GreetingListType">
<xsd:sequence>
[code]....
View Replies
View Related