KeyListener And GObject To Create BreakOut Game

May 14, 2012

I am trying to create the Breakout game and cannot figure how to get the paddle to move and make constraints to bricks disappear when the ball hits them but not the paddle. I tried this:

public void keyPressed(KeyEvent press)
{
obj = paddle;
int key = press.getKeyCode();
if(key == KeyEvent.VK_LEFT)
{
while(true)
{
paddle.move();
}
}
}

but I get an error for the "paddle.move();". Below is the entire code that I have gotten so far:

import acm.program.GraphicsProgram;
import acm.graphics.*;
import java.awt.Color;
import java.awt.event.*;
import acm.util.RandomGenerator;

[code]...

View Replies


ADVERTISEMENT

Breakout Game - Adding Background Picture

Nov 16, 2014

I've been trying to post background picture for a long time in java. I've just finished writing breakout game and I want it to be more special and full of interesting things. I want to upload picture in java. I write

GImage image= new GImage("LEONARDO.JPG");
image.setSize(50,50)
add(image,200,400)

The size of the whole screen is 400,600.

View Replies View Related

Java Game KeyListener Not Working In FSEM

Sep 5, 2014

I had a game written in a 500x400 window using JFrame and a threaded JPanel. It used KeyListener to detect user input and worked just fine, problem was it was very click dependent and therefore I wanted to put it into fullscreen mode so I would no longer come across the problem of clicking outside the window and being taken out of the game. But when I ported the game to Full Screen Exclusive Mode the KeyListener no longer seems to be working and I have no clue why. I tried using KeyBindings as well, that code didn't work and is left commented out in the FSEM code.Here is my code for the windowed application:

import java.awt.Container;
import javax.swing.*;
public class Main extends JFrame{
private GamePanel panel;
public Main(){
panel = new GamePanel();

[code]....

View Replies View Related

Simple Snake Game - Timer And KeyListener

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

Create Hangman Game With GUI?

Apr 18, 2015

I am trying to create a Hangman game with a gui. i already have buttons for the letters, each with its own action listener. I'm having difficulties creating the gallows and man.

I have a class called guiTest, which contains all of the letters. This is essentially what i have. Can I add the image to this class, or do I need to create a new class that extends JFrame?

Java Code: import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;

[code]....

View Replies View Related

2D Game - How To Create A Library Of Tiles

Apr 8, 2014

So I'd like to make like a library of tiles, and it isn't too big, which I could just call using a method, and I would specificy what information I need to get, and it would return that information.

For example, a small section of the tiles library is grass_tile, I call this library and specify what I need, and in this case let's say I need the width, or can the character enter it from north? or can the character just stand on it? So it's like a library or array, with sections of it being named by tiles, and by mentioning these tiles I can information about them.

gTile("grass","walkon") will return true, first because I specifiy grass, and yes the character can walk on grass, so it returns a boolean value of true.

gTile("stone","width") will return 16, first because I specify stone, and the stone's png image size is 16.

What I'm asking is not for how to work out if the character can walk on the tile, or read the width of a tile, but can I create like a library from which I can obtain information by giving the name of the tile, and then telling it what info I need.

If up key pressed && gTile((yTILE + 1).name(),"walkon") == true then moveChar("up")

View Replies View Related

Create Word Game Using Classes And Objects

Feb 14, 2015

For class, we need to create a word game using classes and objects.

The game is played in rounds. The player is presented with a word that is missing letters. The player has to fill in the missing spaces with their letter guesses. The words presented are chosen with a random number generator which has been provided for us. At the end of the game, the player is shown their score.

In steps, I have to:

-Welcome the player.
-Present the puzzle.
-Allow the player to fill in the blanks.
-Have the program check responses for correct/incorrect input.
-End the game if they have three misses, or continue if they complete the puzzle.

Now, to start, I have a class for the number generator, a class to store the array of 25 words, and a class for the game itself.

View Replies View Related

Create 2D Game That Has Realistic Space Simulation

Jul 31, 2014

I am attempting to create a 2d game that has a realistic space simulation. The map is supposed to be set so that you are looking from the top down. The problem I am having exists in my interaction between the sun and a planet. Simply put, the planet will only move in a single diagonal direction without ever changing direction. The formulas I am using are F=(G*M1*M2)/D^2 and A=F/M. The code below is simplified.

(inside planet Class)
double x;
double y;
double mass;
double xForce;
double yForce;
double xAccel;
double yAccel;

[Code] ....

Things I have already tried:
1. Setting G to negative
2. changing the order of the subtraction in determining D
3. some weird pythagorean theorem thing someone suggested

View Replies View Related

Create A Very Basic Hangman Type Of Game

Oct 12, 2014

You have to create a very basic hangman type of game. Open Notepad and create a list of at least ten words in a text file. Save the file as a .txt file and use it to read words for the game. In this game you do not have to show pictures but you do need to keep track of the amount of times that the user has guessed wrong. When the game starts, show the amount of characters in the word represented as dashes and ask the user to type in a letter. if the user guesses correctly show the word again with the correct letter filled in the correct positions(s) and if the user correctly guessed the word, show a message. If the user guesses wrong, keep track of the body parts that needs to be reduced with every wrong guess and inform the user as well, also show a message.

I'm struggling with reading the file, showing the amount of parts left and asking the user to play again. Here is the code:

package hangman;

/**
*
* @author
*/
import java.nio.*;
import java.io.*;
import java.util.*;
public class HangMan {

[code]...

View Replies View Related

Create A Simple Dice Game Java Eclipse Android

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

How To Add A KeyListener

Oct 30, 2014

I'm trying to add a KeyListener, but every time i do i have to implement methods that i will not use.

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class GameFrame extends JFrame implements KeyListener{
private GamePanel panel;
*/
public GameFrame()

[code]....

it works just concerned about the unimplemented methods.I'm supposed to make a row of images all the way across the screeen and down it unless the row contains the player. which is always at the bottom.

private void drawRow(int y, int blockWidth, int blockHeight, GameRow row, Graphics g) {
Image img = Toolkit.getDefaultToolkit().getImage("blocks.png");
for(int i = rows.size()-1; i >= 0; i--) {
if(rows.get(i).isEmpty()) {
g.drawImage(img, width,y,blockWidth, blockHeight, null);
}
}
}

As of right now it will only paste on the FIRST column not the rows.

View Replies View Related

KeyListener Is Not Working?

May 31, 2014

I'm programming a game, but the keyListener doesn't work. Here is the source code:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
 public class InputHandler implements KeyListener{
 public InputHandler(Game game) {
game.addKeyListener(this);

[code]....

My Question is: Why will it not say "up", if I press "w"?

View Replies View Related

Keylistener In A Loop?

Jun 16, 2014

I am trying to program a version of the "Worlds Hardest Game" using ready to program and applets for a final class project. Unfortunately I cannot figure out how to get my keylistener to work because I am using a loop for the enemies to go back and forth.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
 public class HardestGame extends Applet
implements KeyListener, MouseListener {
final int WIDTH = 400;
final int HEIGHT = 123;
int myX = 400;
int myY = 400;

[code]...

View Replies View Related

Another Way To Add Keylistener To JComponent?

Jan 13, 2014

I wrote this program that makes a little square move around with the wasd keys. A huge problem that I had while creating it is adding the keylistener to my JFrame. In the end, I ended up creating an anonymous KeyListener class and adding that anonymous class to the JFrame. However, this anonymous class requires two other methods that I don't use, so any other thing I could have done to add these keylisteners to the JFrame without creating an anonymous class? I had to create 4...

Java Code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

[Code] .....

View Replies View Related

Add KeyListener To Thread

Feb 13, 2014

I want to try and run a thread that starts running on start and quits after pressing a key.I cant seem to detect keypresses. ci cant seem to add code: Post denied. New posts are limited by number of URLs it may contain and checked if it doesn't contain forbidden words.

its just a code snippit without links or anything..this is a small part... but how do i add a keyListener in here that listends to a random key?The class implements KeyListener and the overrided method "keyPressed" sets the isRunning boolean to false... but the keyPressed method is never executed.

public static void main(String[] args) {
Thread thread = new Thread() {
public void run() {
while(isRunning){
System.out.println("running");
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
thread.start();
}

View Replies View Related

How To Access Keylistener If It Is Within A Method

Feb 1, 2014

I am trying to make a program where a ball moves up continuously when you press space, then begins to move down when you reach a certain point. My method for this is to have a timer, and have a method that does this: When you press space, add 10 y coords every second (using a timer), and if you reach 470 y, then begin to drop 10 y coords. I made a method to hold the keylistener, and am running that method inside the actionPerformed method which is within another class. However, since it is a method, I cannot add my keylistener to my frame in the main method.

main
error line 9
Java Code: import javax.swing.*;
public class Main {

[code]...

View Replies View Related

Accessing Keylistener From Thread

Mar 23, 2014

I start my thread, it's for a real basic game I'm learning. Everything was working fine, until I got to recognizing keys. It runs, and I can close using the mouse on the close command, but the keys aren't being generated from the keyboard. I copied it to a working sample, and here are the two files.

package testkeys;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;

[Code] ....

The idea was to set the return value to true if any key is pressed, thus quitting, for this short sample program. Problem, it never recognizes any keys pressed on the keyboard. New to java and threading.

View Replies View Related

Combining JMenuItem And KeyListener

Jun 9, 2014

I have a JMenuItem "Find" in the Edit Menu. I want to add a shortcut key to the JMenuItem.

For Find for example i want to use Ctrl + F

Here is my 'Action' for 'Find' which can be used via Edit->Find. It works.

Find = new AbstractAction(){
public void actionPerformed(ActionEvent e){ 
String word = JOptionPane.showInputDialog(new MainWindow(),"","Find",PLAIN_MESSAGE);
new WordSearcher(textArea,word);
}
};

WordSearcher() is a class i am using to search the word

Now i want to add a KeyListener for ctrl+F, for doing the same purpose. Even this one works.

private KeyListener k1 = new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.isControlDown())

if(e.getKeyCode()== KeyEvent.VK_F)

[Code] ....

But the problem here is i have to write the same code twice. Is there some way by which I can Use the already written Action Find in the KeyListener.

View Replies View Related

How To Identify Up Down Left And Right Keys With Keylistener

Apr 9, 2014

I Want to identify up, down, left and right keys with the keylistener() WHATE NAME DO I IDENTIFY IT WITH

View Replies View Related

Servlets :: Detect Keystroke In JSP Textarea And Keylistener

May 22, 2014

i can use this current code to change it so that it use TextArea in jsp and the keylistener code in servlet. i know i have to take out the JFrame,JTextfield and ContentPane but still i could not do it.

Servlet:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;

[code]....

View Replies View Related

Creating A Keylistener That Reacts Across The Entire Application?

Aug 13, 2014

make the keylistener "listen" across the entire application? Currently whenever I've used it I have always attached it to something else, like a JTextField or the like. However that doesn't work all that well if I for example want to close down the current JDialog by pressing escape, because no object that has the keylistener attached to it is currently focused.

When I searched around for the answer if found similar questions on stackoverflow, but those seemed to focus on listening globally, even when the application itself is not focused, which isn't quite what I want.

Here is a simple piece of code that I'm currently experimenting with:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.security.spec.InvalidKeySpecException;

[Code].....

It's supposed to simply create a frame and give me a message whenever I press the A or D buttons, as well as notify me whenever they are released. As of currently it obviously doesn't work because the keylistener hasn't been attached to anything, but that's also where I'm unsure of how to proceed.

View Replies View Related

KeyListener Doesn't Work - Button Is Not Displayed

Apr 14, 2015

Why the keyTyped function isn't triggered, when i type a key?

Here is my code:

package main;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
 
[Code] .....

I have noticed, that if i call setVisible(true) before i add the button, then it works, but the button is not displayed... How can i achieve that the button is displays AND the KeyListener works?

View Replies View Related

Swing/AWT/SWT :: Paint Circle In Frame On Key Press - Cannot Get KeyListener To Work

Nov 3, 2014

I wanted to try out using a KeyListener to read what key I press so it can paint a circle in my frame, but I can't get it to work?

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

[Code] ....

View Replies View Related

Opoly Game - Goal Of The Game Is To Reach Or Exceed Reward Value 1000

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

Tic Tac Toe Game - Random Number Generator And Two Dimensional Arrays For Game Board

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

Guessing Game - How To Reset Random Number When User Reset Game

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







Copyrights 2005-15 www.BigResource.com, All rights reserved