I currently have a maze project that is built of four tiles that form a loop when moving around to create an endless universe. Initially when inputting character blocking abilities, my second maze tile (the tile directly to the left of the character starting tile) displayed the blocking program one block off. The further you would move to the left onto the initial tile and back to the second tile, the more the character would be blocked off from the original tile. I assumed the problem lied within the tile placement, as the character would switch tiles, they would be one off. I altered my tile coordinates and size which somehow created a new problem. Now, the initial starting tile blocks appropriately, but once the character enters another tile, the blocking no longer works at all and the character is able to just proceed in an infinite loop around the maze. Attached is two classes: InTheDark (the class that calls the AppWindow) and AppWindow (the class consisting of the methods that develop the maze and the flashlight capabilities - they were commented out for the purpose of better access to the game). Some segments of code are commented out for the reason that they are functioning, but have no affect on the background. Could someone please help me on figuring out where the error is coming from?
This code contains InTheDark and AppWindow.
import java.text.DecimalFormat;
import java.applet.*;
import java.util.Random;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.Graphics2D.*;
import java.awt.event.*;
import java.util.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.AudioInputStream;
public class InTheDark
{
public static void main(String args[])
{ //launches the frame and timer events
int winWid = 500;
int winHei = 500;
AppWindow runner = new AppWindow(winWid,winHei);
runner.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{System.exit(0);}});
runner.setSize(winWid,winHei);
runner.show();
}
}
class AppWindow extends Frame implements KeyListener
{
int wid=0; //size of display window wid
int hei=0; //size of display window height
Image virtualMem;
Graphics gBuffer;
Image mazeBackgroundCenter;
Image mazeBackgroundElse;
Image character;
Graphics2D g2D;
int cx, cy;
int bx1, by1;
int bx2, by2;
int bx3, by3;
int bx4, by4;
int[][] maze11 = {{1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,1,0,0,0,0,0,0},
{1,0,1,0,1,0,1,1,1,1,1},
{1,0,1,0,0,0,1,0,0,1,1},
{1,0,1,1,0,1,1,1,0,1,1},
{0,0,0,1,0,0,0,0,0,1,1},
{1,1,0,1,1,1,1,1,1,1,1},
{1,1,0,0,0,0,1,1,1,1,1},
{1,1,0,1,1,0,0,0,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1}};
int[][] maze22 = {{1,1,1,0,1,1,1,1,1,1,1},
{0,0,0,0,0,0,1,1,1,1,1},
{1,1,0,1,1,0,1,1,0,1,1},
{1,0,0,1,1,0,0,0,0,1,1},
{1,1,0,1,1,1,1,0,1,1,1},
{1,1,0,0,0,0,1,0,0,0,0},
{1,1,1,1,1,0,1,1,0,1,1},
{1,0,0,0,0,0,1,1,1,1,1},
{1,1,1,0,1,0,0,0,0,1,1},
{1,1,1,1,1,1,1,1,0,1,1}};
int[][] maze33 = {{1,1,1,1,1,1,1,1,1,1,1},
{0,0,1,0,1,1,0,1,1,1,1},
{1,0,0,0,1,1,0,0,1,1,1},
{1,0,1,1,1,1,0,1,1,1,1},
{1,0,0,0,1,1,0,1,1,1,1},
{1,0,1,0,0,0,0,1,1,1,1},
{1,0,1,1,1,1,1,0,0,0,0},
{1,0,1,0,0,0,1,0,1,1,1},
{1,0,0,0,1,0,0,0,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1}};
int[][] maze44 = {{1,1,1,1,1,1,1,0,1,1,1},
{1,0,1,1,1,1,0,0,0,0,0},
{1,0,0,0,1,1,1,1,0,1,1},
{1,1,1,0,0,0,1,1,0,1,1},
{1,1,1,1,1,0,1,0,0,1,1},
{1,1,0,0,0,0,1,0,1,1,1},
{0,0,0,1,1,0,0,0,1,1,1},
{1,0,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,1,1,1,1,1},
{1,1,1,0,1,1,1,1,1,1,1}};
int[] charpos;
String line = null;
int batterywidth;
boolean flashlight;
boolean left,right,up,down;
public AppWindow(int width, int height)
{
virtualMem = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
gBuffer = virtualMem.getGraphics();
wid = width;
hei = height;
batterywidth = 40;
g2D = (Graphics2D)gBuffer;
flashlight = false;
charpos = new int[3];
charpos[0] = 1;
charpos[1] = 4;
charpos[2] = 4;
cx = 225;
cy = 225;
bx1 = 0;
by1 = 0;
bx2 = -500;
by2 = 0;
bx3 = 0;
by3 = -500;
bx4 = -500;
by4 = -500;
left = true;
right = true;
up = true;
down = true;
addKeyListener(this);
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
updateBackground();
placeCharacter();
if(charpos[0] == 1)
{
canMoveLeft(maze11);
canMoveRight(maze11);
canMoveUp(maze11);
canMoveDown(maze11);
}
if(charpos[0] == 2)
{
canMoveLeft(maze22);
canMoveRight(maze22);
canMoveUp(maze22);
canMoveDown(maze22);
}
if(charpos[0] == 3)
{
canMoveLeft(maze33);
canMoveRight(maze33);
canMoveUp(maze33);
canMoveDown(maze33);
}
if(charpos[0] == 4)
{
canMoveLeft(maze44);
canMoveRight(maze44);
canMoveUp(maze44);
canMoveDown(maze44);
}
/*if(batterywidth >= 1)
{
theLights();
if(batterywidth == 15)
{
gBuffer.setColor(Color.white);
gBuffer.drawString("Looks like you are getting close to running out of batteries.",50,470);
gBuffer.drawString("Look around and see if you can find any around the maze.",50,480);
}
battery();
}
if(batterywidth < 1)
{
flashlight = false;
theLights();
gBuffer.setColor(Color.white);
gBuffer.drawString("You are out of batteries. Try finding some around the maze.",50,470);
gBuffer.drawString("They will be the only things you can see in the dark.",50,480);
battery();
}
extraBatteries();*/
g.drawImage(virtualMem, 0, 0, this);
repaint();
}
public void extraBatteries()
{
}
public void theLights()
{
/*Color blck = new Color(0,0,0,240);
Rectangle2D.Double darkness = new Rectangle2D.Double(0,0,500,500);
Area glow = new Area(darkness);
if(flashlight)
{
Ellipse2D.Double light = new Ellipse2D.Double(cx-100,cy-100, 200, 200);
Area shiny = new Area(light);
glow.subtract(shiny);
Color yllw = new Color(189,183,107,200);
g2D.setPaint(yllw);
g2D.fill(shiny);
}
g2D.setPaint(blck);
g2D.fill(glow);*/
}
public void battery()
{
if(flashlight)
{
long time = System.currentTimeMillis();
if(time % 500 == 0)
{
batterywidth --;
}
}
Color drkgrn = new Color(0,128,0,200);
Color lghtgrn = new Color(154,205,50,200);
Color yellow = new Color(255,215,0,200);
Color red = new Color(255,0,0,200);
if(batterywidth <= 40)
{
g2D.setPaint(drkgrn);
if(batterywidth <= 30)
{
g2D.setPaint(lghtgrn);
if(batterywidth <= 20)
{
g2D.setPaint(yellow);
if(batterywidth <= 10)
{
g2D.setPaint(red);
}
}
}
}
Rectangle2D.Double grn = new Rectangle2D.Double(420,440,batterywidth,20);
Area battery = new Area(grn);
g2D.fill(battery);
}
public void canMoveLeft(int[][] maze00)
{
if(charpos[2]-1 > -1 && charpos[1] > 0 && charpos[1] < 11 && charpos[2] < 11)
{
if(maze00[charpos[1]][charpos[2]-1] == 0)
{
left = true;
}
else
{
left = false;
}
}
else
{
if(charpos[2]-1 < 0)
{
charpos[2] = 11;
if(charpos[0] == 1)
{
charpos[0] = 2;
}
else
{
charpos[0] = 1;
}
}
if(charpos[2] >= 10)
{
charpos[2] = 1;
if(charpos[0] == 1)
{
charpos[0] = 2;
}
else
{
charpos[0] = 1;
}
}
}
}
public void canMoveRight(int[][] maze00)
{
if(charpos[2]+1 < 12 && charpos[1] > 0 && charpos[1] < 11 && charpos[2] > 0)
{
if(maze00[charpos[1]][charpos[2]+1] == 0)
{
right = true;
}
else
{
right = false;
}
}
else
{
if(charpos[2]-1 < 0)
{
charpos[2] = 11;
if(charpos[0] == 1)
{
charpos[0] = 2;
}
else
{
charpos[0] = 1;
}
}
if(charpos[2] >= 10)
{
charpos[2] = 1;
if(charpos[0] == 1)
{
charpos[0] = 2;
}
else
{
charpos[0] = 1;
}
}
}
}
public void canMoveUp(int[][] maze00)
{
if(charpos[1]-1 > -1 && charpos[2] > 0 && charpos[2] < 11 && charpos[1] < 11)
{
if(maze00[charpos[1]-1][charpos[2]] == 0)
{
up = true;
}
else
{
up = false;
}
}
else
{
if(charpos[1]-1 < 0)
{
charpos[1] = 11;
if(charpos[0] == 1)
{
charpos[0] = 3;
}
else
{
charpos[0] = 1;
}
}
if(charpos[1] >= 10)
{
charpos[1] = 1;
if(charpos[0] == 1)
{
charpos[0] = 3;
}
else
{
charpos[0] = 1;
}
}
}
}
public void canMoveDown(int[][] maze00)
{
if(charpos[1]+1 < 12 && charpos[2] > 0 && charpos[2] < 11 && charpos[1] > 0)
{
if(maze00[charpos[1]+1][charpos[2]] == 0)
{
down = true;
}
else
{
down = false;
}
}
else
{
if(charpos[1]-1 < 0)
{
charpos[1] = 11;
if(charpos[0] == 1)
{
charpos[0] = 3;
}
else
{
charpos[0] = 1;
}
}
if(charpos[1] >= 10)
{
charpos[1] = 1;
if(charpos[0] == 1)
{
charpos[0] = 3;
}
else
{
charpos[0] = 1;
}
}
}
}
public void updateBackground()
{
if(bx1 > 500)
{
bx1 = -500;
bx3 = -500;
}
if(bx2 > 500)
{
bx2 = -500;
bx4 = -500;
}
if(bx1 < -500)
{
bx1 = 500;
bx3 = 500;
}
if(bx2 < -500)
{
bx2 = 500;
bx4 = 500;
}
if(by1 > 500)
{
by1 = -500;
by2 = -500;
}
if(by3 > 500)
{
by3 = -500;
by4 = -500;
}
if(by1 < -500)
{
by1 = 500;
by2 = 500;
}
if(by3 < -500)
{
by3 = 500;
by4 = 500;
}
drawFile(bx1,by1,maze11);
drawFile(bx2,by2,maze22);
drawFile(bx3,by3,maze33);
drawFile(bx4,by4,maze44);
}
public void drawFile(int x, int y, int[][] maze00)
{
for(int i = 0; i < maze00.length; i ++)
{
for(int ii = 0; ii < maze00[i].length; ii++)
{
if(maze00[i][ii] == 1)
{
gBuffer.setColor(Grfx.black);
}
if(maze00[i][ii] == 0)
{
gBuffer.setColor(Grfx.white);
}
gBuffer.fillRect(x + ii*50, y + i*50, 50, 50);
}
}
}
public void placeCharacter()
{
gBuffer.setColor(Grfx.red);
gBuffer.fillOval(cx-10,cy-10,20,20);
}
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{
int buff1 = bx1;
if(left)
{
bx1+=50;
bx2+=50;
bx3+=50;
bx4+=50;
charpos[2]--;
}
}
if (key == KeyEvent.VK_RIGHT)
{
int buff1 = bx1;
if(right)
{
bx1-=50;
bx2-=50;
bx3-=50;
bx4-=50;
charpos[2]++;
}
}
if (key == KeyEvent.VK_DOWN)
{
if(down)
{
by1-=50;
by2-=50;
by3-=50;
by4-=50;
charpos[1] ++;
}
}
if (key == KeyEvent.VK_UP)
{
if(up)
{
by1+=50;
by2+=50;
by3+=50;
by4+=50;
charpos[1] --;
}
}
if (key == KeyEvent.VK_SPACE)
{
if(flashlight)
{
flashlight = false;
}
else
{
flashlight = true;
}
}
if (key == KeyEvent.VK_ESCAPE)
{
}
}
}
I want the game to restart when the player pressed the y key on the keyboard after dying or getting hit by the small ball.
BoxX and BoxY are the coordinates for enemy balls
Ball holds the rectangle or hitbox for all the enemy balls
ReverseX and Y, IndexX and Y, and BallXchange and Y all have to do with the movement of the enemy balls.
Here is the code:
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.*;
import java.util.ArrayList;
import java.awt.Font;
import java.awt.Color;
import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class GameNotes extends JPanel implements KeyListener{
int x1=1;
int y1=1;
int ballx = 40;
int bally = 60;
boolean gameRunning = true;
BufferedImage image;
int windowx = 500;
int windowy = 500;
int score = 0;
int rectX = (int)(Math.random()*400)+30;
int rectY = (int)(Math.random()*400)+30;
int enemyX = (int)(Math.random()*300)+100;
int enemyY = (int)(Math.random()*250)+100;
int count = 1;
int num = 0;
boolean reverseX = false;
boolean reverseY = false;
boolean right = false;
boolean left = false;
boolean up = false;
boolean down = false;
ArrayList<Integer> BoxX = new ArrayList<Integer>();
ArrayList<Integer> BoxY = new ArrayList<Integer>();
ArrayList<Rectangle> Ball = new ArrayList<Rectangle>();
ArrayList<Integer> ReverseX = new ArrayList<Integer>();
ArrayList<Integer> ReverseY = new ArrayList<Integer>();
ArrayList<Integer> BallYchange = new ArrayList<Integer>();
ArrayList<Integer> BallXchange = new ArrayList<Integer>();
ArrayList<Integer> indexY = new ArrayList<Integer>();
ArrayList<Integer> indexX = new ArrayList<Integer>();
public GameNotes(){
setFocusable(true);
JFrame frame = new JFrame("GameDemo");
frame.setSize(windowx, windowy);
frame.add(this);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
try{
image = ImageIO.read(new File("C:\\Users\\10021707\\Desktop\\Game\\b.jpg"));
}catch (Exception e){
System.out.println("image not found \n E000384JW29400119482938WJDI39");
}
game:
while(gameRunning){
try{
Thread.sleep(3);
}catch(Exception e){
System.out.println("error");
}
Rectangle r = new Rectangle (ballx,bally,20,20);
Rectangle r2 = new Rectangle (rectX,rectY,30,30);
Rectangle rE = new Rectangle (enemyX,enemyY,10,10);
if(count == 1){
BoxX.add(enemyX);
BoxY.add(enemyY);
Ball.add(rE);
ReverseX.add(1);
BallXchange.add(enemyX);
num++;
indexX.add(num);
count--;
}
//System.out.println("ReverseY" + ReverseY.size());
if(ballx >= 461)
ballx = 461;
if(ballx <= 0)
ballx = 0;
if(bally >= 432)
bally = 432;
if(bally <= 0)
bally = 0;
if(r.intersects(r2)){
score+=5;
rectX = (int)(Math.random()*400)+30;
rectY = (int)(Math.random()*400)+30;
enemyX = (int)(Math.random()*300)+50;
enemyY = (int)(Math.random()*300)+50;
BoxX.add(enemyX);
BoxY.add(enemyY);
int x = (int)(Math.random()*10)+1;
//System.out.println(x);
if(x > 5){
ReverseX.add(1);
BallXchange.add(enemyX);
num++;
indexX.add(num);
}
if(x <= 5){
ReverseY.add(1);
BallYchange.add(enemyY);
num++;
indexY.add(num);
}
Rectangle En = new Rectangle (enemyX,enemyY,10,10);
Ball.add(En);
//System.out.println(Ball);
}
for(int i = 0; i < Ball.size(); i++){
Rectangle w = new Rectangle (BoxX.get(i),BoxY.get(i),10,10);
Ball.set(i,w);
}
for(int i = 0; i < BoxX.size(); i++){
if(r.intersects(Ball.get(i))){
gameRunning = false;
}
}
//System.out.println(ReverseY);
for(int i = 0; i < ReverseX.size(); i++){
BallXchange.set(i,BallXchange.get(i)+ReverseX.get(i));
BoxX.set(indexX.get(i)-1,BallXchange.get(i));
if(BallXchange.get(i) >= 470 || BallXchange.get(i) <= 0){
ReverseX.set(i,ReverseX.get(i)*-1);
}
}
for(int i = 0; i < ReverseY.size(); i++){
BallYchange.set(i,BallYchange.get(i)+ReverseY.get(i));
BoxY.set(indexY.get(i)-1,BallYchange.get(i));
if(BallYchange.get(i) >= 445 || BallYchange.get(i) <= 0){
ReverseY.set(i,ReverseY.get(i)*-1);
}
}
if(right)
ballx++;
if(left)
ballx--;
if(up)
bally--;
if(down)
bally++;
repaint();
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(image,0,0,null);
g.fillOval(ballx,bally,20,20);
g.fillRect(rectX,rectY,30,30);
for(int i = 0; i < BoxX.size(); i++){
g.fillOval(BoxX.get(i),BoxY.get(i),10,10);
}
if(!gameRunning){
g.setColor(getBackground());
g.clearRect(rectX,rectY,30,30);
g.fillRect(rectX,rectY,30,30);
for(int i = 0; i < BoxX.size(); i++){
g.clearRect(BoxX.get(i),BoxY.get(i),10,10);
g.fillOval(BoxX.get(i),BoxY.get(i),10,10);
g.fillRect(BoxX.get(i),BoxY.get(i),10,10);
}
BoxX.clear();
BoxY.clear();
Ball.clear();
ReverseX.clear();
ReverseY.clear();
BallXchange.clear();
BallYchange.clear();
indexX.clear();
indexY.clear();
g.setColor(Color.BLACK);
g.setFont(new Font("TimesRoman", Font.PLAIN, 30));
g.drawString("Game Over", 165, 200);
g.setFont(new Font("TimesRoman", Font.PLAIN, 25));
g.drawString("Do you want to play again?", 100, 230);
g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
g.drawString("(y or n)", 200, 250);
}
if(gameRunning){
g.setFont(new Font("TimesRoman", Font.PLAIN, 25));;
g.drawString("Score: "+score, 100, 450);
}
/*for(int i = 0; i < Ball.size(); i++){
g.fillRect((int)Ball.get(i).getX(),(int)Ball.get(i).getY(),10,10);
}*/
}
public void keyReleased(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_D || e.getKeyCode() == KeyEvent.VK_RIGHT)
right = false;
if(e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_LEFT)
left = false;
if(e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_UP)
up = false;
if(e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_DOWN)
down = false;
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if(gameRunning == false && key == e.VK_Y){
score = 0;
gameRunning = true;
}
if(e.getKeyCode() == KeyEvent.VK_D || e.getKeyCode() == KeyEvent.VK_RIGHT)
right = true;
if(e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_LEFT)
left = true;
if(e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_UP)
up = true;
if(e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_DOWN)
down = true;
}
public void keyTyped(KeyEvent e){
}
public static void main (String [] samay){
GameNotes test = new GameNotes();
}
}
Any help would be appreciated!
As ordonezalex suggested in his comment you can take the while loop and put it in a method such as this:
public void runGame() {
while(gameRunning){
try{
Thread.sleep(3);
}catch(Exception e){
System.out.println("error");
}
Rectangle r = new Rectangle (ballx,bally,20,20);
Rectangle r2 = new Rectangle (rectX,rectY,30,30);
Rectangle rE = new Rectangle (enemyX,enemyY,10,10);
if(count == 1){
BoxX.add(enemyX);
BoxY.add(enemyY);
Ball.add(rE);
ReverseX.add(1);
BallXchange.add(enemyX);
num++;
indexX.add(num);
count--;
}
//System.out.println("ReverseY" + ReverseY.size());
if(ballx >= 461)
ballx = 461;
if(ballx <= 0)
ballx = 0;
if(bally >= 432)
bally = 432;
if(bally <= 0)
bally = 0;
if(r.intersects(r2)){
score+=5;
rectX = (int)(Math.random()*400)+30;
rectY = (int)(Math.random()*400)+30;
enemyX = (int)(Math.random()*300)+50;
enemyY = (int)(Math.random()*300)+50;
BoxX.add(enemyX);
BoxY.add(enemyY);
int x = (int)(Math.random()*10)+1;
//System.out.println(x);
if(x > 5){
ReverseX.add(1);
BallXchange.add(enemyX);
num++;
indexX.add(num);
}
if(x <= 5){
ReverseY.add(1);
BallYchange.add(enemyY);
num++;
indexY.add(num);
}
Rectangle En = new Rectangle (enemyX,enemyY,10,10);
Ball.add(En);
//System.out.println(Ball);
}
for(int i = 0; i < Ball.size(); i++){
Rectangle w = new Rectangle (BoxX.get(i),BoxY.get(i),10,10);
Ball.set(i,w);
}
for(int i = 0; i < BoxX.size(); i++){
if(r.intersects(Ball.get(i))){
gameRunning = false;
}
}
//System.out.println(ReverseY);
for(int i = 0; i < ReverseX.size(); i++){
BallXchange.set(i,BallXchange.get(i)+ReverseX.get(i));
BoxX.set(indexX.get(i)-1,BallXchange.get(i));
if(BallXchange.get(i) >= 470 || BallXchange.get(i) <= 0){
ReverseX.set(i,ReverseX.get(i)*-1);
}
}
for(int i = 0; i < ReverseY.size(); i++){
BallYchange.set(i,BallYchange.get(i)+ReverseY.get(i));
BoxY.set(indexY.get(i)-1,BallYchange.get(i));
if(BallYchange.get(i) >= 445 || BallYchange.get(i) <= 0){
ReverseY.set(i,ReverseY.get(i)*-1);
}
}
if(right)
ballx++;
if(left)
ballx--;
if(up)
bally--;
if(down)
bally++;
repaint();
}
}
Then replace the while loop with this:
runGame();
And each time you want to re-run the game you can use that again, as long as the boolean gameRunning is true.
There are a few things you would need to update in your code:
A. Create a boolean called programRunning and set it to true:
boolean gameRunning = true;
boolean programRunning = true;
B. Create a private void method called initGame() and place all the game initialization code there:
private void initGame()
{
x1=1;
y1=1;
ballx = 40;
bally = 60;
windowx = 500;
windowy = 500;
score = 0;
rectX = (int)(Math.random()*400)+30;
rectY = (int)(Math.random()*400)+30;
enemyX = (int)(Math.random()*300)+100;
enemyY = (int)(Math.random()*250)+100;
count = 1;
num = 0;
reverseX = false;
reverseY = false;
right = false;
left = false;
up = false;
down = false;
BoxX = new ArrayList<Integer>();
BoxY = new ArrayList<Integer>();
Ball = new ArrayList<Rectangle>();
ReverseX = new ArrayList<Integer>();
ReverseY = new ArrayList<Integer>();
BallYchange = new ArrayList<Integer>();
BallXchange = new ArrayList<Integer>();
indexY = new ArrayList<Integer>();
indexX = new ArrayList<Integer>();
}
C. Modify the following code under the section where you are assigning a JPG to the image variable:
try{
image = ImageIO.read(new File("C:\\Users\\10021707\\Desktop\\Game\\b.jpg"));
}catch (Exception e){
System.out.println("image not found \n E000384JW29400119482938WJDI39");
}
//CODE CHANGED BELOW THIS COMMENT
do {
initGame();
while (gameRunning) {
runGame();
}
} while (programRunning);
D. Create the runGame() method that contains all of your code within the while (gameRunning) loop:
private void runGame()
{
try{
Thread.sleep(3);
}catch(Exception e){
System.out.println("error");
}
Rectangle r = new Rectangle (ballx,bally,20,20);
Rectangle r2 = new Rectangle (rectX,rectY,30,30);
Rectangle rE = new Rectangle (enemyX,enemyY,10,10);
if(count == 1){
BoxX.add(enemyX);
BoxY.add(enemyY);
Ball.add(rE);
ReverseX.add(1);
BallXchange.add(enemyX);
num++;
indexX.add(num);
count--;
}
//System.out.println("ReverseY" + ReverseY.size());
if(ballx >= 461)
ballx = 461;
if(ballx <= 0)
ballx = 0;
if(bally >= 432)
bally = 432;
if(bally <= 0)
bally = 0;
if(r.intersects(r2)){
score+=5;
rectX = (int)(Math.random()*400)+30;
rectY = (int)(Math.random()*400)+30;
enemyX = (int)(Math.random()*300)+50;
enemyY = (int)(Math.random()*300)+50;
BoxX.add(enemyX);
BoxY.add(enemyY);
int x = (int)(Math.random()*10)+1;
//System.out.println(x);
if(x > 5){
ReverseX.add(1);
BallXchange.add(enemyX);
num++;
indexX.add(num);
}
if(x <= 5){
ReverseY.add(1);
BallYchange.add(enemyY);
num++;
indexY.add(num);
}
Rectangle En = new Rectangle (enemyX,enemyY,10,10);
Ball.add(En);
//System.out.println(Ball);
}
for(int i = 0; i < Ball.size(); i++){
Rectangle w = new Rectangle (BoxX.get(i),BoxY.get(i),10,10);
Ball.set(i,w);
}
for(int i = 0; i < BoxX.size(); i++){
if(r.intersects(Ball.get(i))){
gameRunning = false;
}
}
//System.out.println(ReverseY);
for(int i = 0; i < ReverseX.size(); i++){
BallXchange.set(i,BallXchange.get(i)+ReverseX.get(i));
BoxX.set(indexX.get(i)-1,BallXchange.get(i));
if(BallXchange.get(i) >= 470 || BallXchange.get(i) <= 0){
ReverseX.set(i,ReverseX.get(i)*-1);
}
}
for(int i = 0; i < ReverseY.size(); i++){
BallYchange.set(i,BallYchange.get(i)+ReverseY.get(i));
BoxY.set(indexY.get(i)-1,BallYchange.get(i));
if(BallYchange.get(i) >= 445 || BallYchange.get(i) <= 0){
ReverseY.set(i,ReverseY.get(i)*-1);
}
}
if(right)
ballx++;
if(left)
ballx--;
if(up)
bally--;
if(down)
bally++;
repaint();
}
E. Add this code to your keyPressed(KeyEvent e) method:
if (gameRunning == false && key == KeyEvent.VK_N)
{
programRunning = false;
System.exit(0); //Closes the application.
}
Just giving you the main idea :
do{
//All logic goes here
//At the end of the game take input from user in the form of 'Y|y' or 'N|n'
System.out.println("Do you want to continue : press 'Y' for yes and 'N' for no");
Scanner sc=new Scanner(System.in);
String choice=sc.nextLine();
} while(choice.equalsIgnoreCase("Y"))