Game Java Programm - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Game Java Programm

SokoxSokox Posts: 4Member
Hi there,
I am begginer in Java programming! I would like to make some java games.So far,this is result of my expertise(found it on WeB) ...My school teacher said it is good ,but I should not have methods larger than display screen,I should put them into more classes,I have more flexible way ...Please ,anyone of you willing to spare half an hour of his life on me ,I will be so much grateful to him ....

[code]import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.applet.AudioClip;


public class G1P4 extends JFrame
{
//the following are all 8 images:
URL url1 = null, url2 = null, url3 = null, url4 = null,
url5 = null, url6 = null, url7 = null, url8 = null;
Image img1,img2,img3,img4,
img5,img6,img7,img8;


URL URLt = null;
Image title = null;


final int WIDTH = 900, HEIGHT = 650;


boolean winnerChosen = false;


double p1Speed = .5, p2Speed = .5;


int p1Laps = 0, p2Laps = 0;

//these are ints that represent directions:
final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;

//these will keep track of the player's directions (default = up)
int p1Direction = UP;
int p2Direction = UP;


//create rectangles that wil represent the left, right, top, bottom, and center
Rectangle left = new Rectangle(0,0,WIDTH/9,HEIGHT);
Rectangle right = new Rectangle((WIDTH/9)*8,0,WIDTH/9,HEIGHT);
Rectangle top = new Rectangle(0,0,WIDTH,HEIGHT/9);
Rectangle bottom = new Rectangle(0,(HEIGHT/9)*8,WIDTH,HEIGHT/9);
Rectangle center = new Rectangle((int)((WIDTH/9)*2.5),(int)((HEIGHT/9)*2.5),
(int)((WIDTH/9)*5),(HEIGHT/9)*4);
//these obstacles will obstruct the path and make navigating harder
Rectangle obstacle = new Rectangle(WIDTH/2,(int)((HEIGHT/9)*7),WIDTH/10,HEIGHT/9);
Rectangle obstacle2 = new Rectangle(WIDTH/3,(int)((HEIGHT/9)*5),WIDTH/10,HEIGHT/4);
Rectangle obstacle3 = new Rectangle(2*(WIDTH/3),(int)((HEIGHT/9)*5),WIDTH/10,HEIGHT/4);
Rectangle obstacle4 = new Rectangle(WIDTH/3,HEIGHT/9,WIDTH/30,HEIGHT/9);
Rectangle obstacle5 = new Rectangle(WIDTH/2,(int)((HEIGHT/9)*1.5),
WIDTH/30,HEIGHT/4);

//the following rectangle is the finish line for both players
Rectangle finish = new Rectangle(WIDTH/9,(HEIGHT/2)-HEIGHT/9,
(int)((WIDTH/9)*1.5),HEIGHT/70);

//this is the rectangle for player 1's (outer) car:
Rectangle p1 = new Rectangle(WIDTH/9,HEIGHT/2,WIDTH/30,WIDTH/30);

//this is the rectang;e for player 2's (inner) car:
Rectangle p2 = new Rectangle(((WIDTH/9)+((int)((WIDTH/9)*1.5)/2)),
(HEIGHT/2)+(HEIGHT/10),WIDTH/30,WIDTH/30);

//the constructor:
public G1P4()
{
//the following code creates the JFrame
super("Radical Racing");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

//load the URLs
try
{
url1 = this.getClass().getResource("G1P4Img1.jpg");
url2 = this.getClass().getResource("G1P4Img2.jpg");
url3 = this.getClass().getResource("G1P4Img3.jpg");
url4 = this.getClass().getResource("G1P4Img4.jpg");
url5 = this.getClass().getResource("G1P4Img5.jpg");
url6 = this.getClass().getResource("G1P4Img6.jpg");
url7 = this.getClass().getResource("G1P4Img7.jpg");
url8 = this.getClass().getResource("G1P4Img8.jpg");
URLt = this.getClass().getResource("title.png");
}
catch(Exception e){}

//attach the URLs to the images
img1 = Toolkit.getDefaultToolkit().getImage(url1);
img2 = Toolkit.getDefaultToolkit().getImage(url2);
img3 = Toolkit.getDefaultToolkit().getImage(url3);
img4 = Toolkit.getDefaultToolkit().getImage(url4);
img5 = Toolkit.getDefaultToolkit().getImage(url5);
img6 = Toolkit.getDefaultToolkit().getImage(url6);
img7 = Toolkit.getDefaultToolkit().getImage(url7);
img8 = Toolkit.getDefaultToolkit().getImage(url8);
title = Toolkit.getDefaultToolkit().getImage(URLt);



//display a welcome dialog (JOptionPane) that includes the rules
JOptionPane.showMessageDialog(null,
"WELCOME TO RADICAL RACING!

"+
"Game: 2 player racing
"+
"Goal: Complete 3 full laps before your opponent!
"+
"Controls:
"+
" Player1:
"+
" (BLUE CAR) WASD directional, speed is automatic
"+
" Player 2:
"+
" (RED CAR) IJKL directional, speed is automatic
"+
"Also, be sure to avoid the green grass. It's slick
"+
"and might make you spin out!

"+
"Click OK to start");

//start the inner class (which works on its own, because it is a Thread)
Move1 m1 = new Move1();
Move2 m2 = new Move2();
m1.start();
m2.start();

//now, play the sound:
try
{
URL eng = this.getClass().getResource("engine.wav");
AudioClip snd = JApplet.newAudioClip(eng);
snd.loop();
}
catch(Exception e){}
}

//this will draw the cars and the race track
public void paint(Graphics g)
{
super.paint(g);

//draw the background for the racetrack
g.setColor(Color.DARK_GRAY);
g.fillRect(0,0,WIDTH,HEIGHT);

//when we draw, the border will be green
g.setColor(Color.GREEN);

//the following rectangle is the start line for the outer player
Rectangle lineO = new Rectangle(WIDTH/9,HEIGHT/2,(int)((WIDTH/9)*1.5)/2,HEIGHT/140);
//the following rectangle is the start line for the inner player
Rectangle lineI = new Rectangle(((WIDTH/9)+((int)((WIDTH/9)*1.5)/2)),
(HEIGHT/2)+(HEIGHT/10),
(int)((WIDTH/9)*1.5)/2,
HEIGHT/140);

//now, using the rectangles, draw it
g.fillRect(left.x,left.y,left.width,left.height);
g.fillRect(right.x,right.y,right.width,right.height);
g.fillRect(top.x,top.y,top.width,top.height);
g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
g.fillRect(center.x,center.y,center.width,center.height);
g.fillRect(obstacle.x,obstacle.y,obstacle.width,obstacle.height);
g.fillRect(obstacle2.x,obstacle2.y,obstacle2.width,obstacle2.height);
g.fillRect(obstacle3.x,obstacle3.y,obstacle3.width,obstacle3.height);
g.fillRect(obstacle4.x,obstacle4.y,obstacle3.width,obstacle4.height);
g.fillRect(obstacle5.x,obstacle5.y,obstacle5.width,obstacle5.height);
//set the starting line color to white
g.setColor(Color.WHITE);
//now draw the starting line
g.fillRect(lineO.x,lineO.y,lineO.width,lineO.height);
g.fillRect(lineI.x,lineI.y,lineI.width,lineI.height);
//set the color of the finish line to yellow
g.setColor(Color.YELLOW);
//now draw the finish line
g.fillRect(finish.x,finish.y,finish.width,finish.height);

//this code will draw the title image to the center of the screen:
g.drawImage(title,center.x+10,center.y+80,this);

//draw the images for p1
if(p1Direction==UP)
g.drawImage(img5,p1.x,p1.y,this);
if(p1Direction==LEFT)
g.drawImage(img8,p1.x,p1.y,this);
if(p1Direction==DOWN)
g.drawImage(img7,p1.x,p1.y,this);
if(p1Direction==RIGHT)
g.drawImage(img6,p1.x,p1.y,this);

//draw the images for p2
if(p2Direction==UP)
g.drawImage(img1,p2.x,p2.y,this);
if(p2Direction==LEFT)
g.drawImage(img4,p2.x,p2.y,this);
if(p2Direction==DOWN)
g.drawImage(img3,p2.x,p2.y,this);
if(p2Direction==RIGHT)
g.drawImage(img2,p2.x,p2.y,this);
}

private class Move1 extends Thread implements KeyListener
{
public void run()
{
//add the code to make the KeyListener "wake up"
addKeyListener(this);


//now, this should all be in an infinite loop, so the process repeats
while(true)
{
//now, put the code in a "try" block. This will let the program exit
//if there is an error.
try
{
//first, refresh the screen:
repaint();

//check to see if car hits the outside walls.
//If so, make it slow its speed by setting its speed
//to -4.
if(p1.intersects(left) || p1.intersects(right) ||
p1.intersects(top) || p1.intersects(bottom) ||
p1.intersects(obstacle) || p1.intersects(obstacle2)||
p1.intersects(p2) || p1.intersects(obstacle3) ||
p1.intersects(obstacle4) || p1.intersects(obstacle5))
{
p1Speed = -4;
}

//if the car hits the center, do the same as above
//but make the speed -2.5.
if(p1.intersects(center))
{
p1Speed = -2.5;
}


//check how many laps:
if(p1.intersects(finish)&&p1Direction==UP)
{
p1Laps++;
}

//3 full laps will occur when laps is about 24.
//so, use an if statement to check it and tell the user.
//also, if winnerChosen is false, tell them they won and set it
//to true. Otherwise, tell the user they lost.
if(p1Laps>=24)
{
if(!winnerChosen)
{
winnerChosen = true;
JOptionPane.showMessageDialog(null,
"Player 1 (blue) Wins!!!");
break;
}
else
{
JOptionPane.showMessageDialog(null,
"Player 1 (blue): LOSER! :(
" +
"Player 2 (red): WINNER!!! :D");
break;
}
}

//increase speed a bit
if(p1Speed<=5)
p1Speed+=.2;

//these will move the player based on direction
if(p1Direction==UP)
{
p1.y-=(int)p1Speed;
}
if(p1Direction==DOWN)
{
p1.y+=(int)p1Speed;
}
if(p1Direction==LEFT)
{
p1.x-=(int)p1Speed;
}
if(p1Direction==RIGHT)
{
p1.x+=(int)p1Speed;
}


//this delays the refresh rate:
Thread.sleep(75);
}
catch(Exception e)
{
//if there is an exception (an error), exit the loop.
break;
}
}
}

//you must also implement this method from KeyListener
public void keyPressed(KeyEvent event)
{

}

//you must also implement this method from KeyListener
public void keyReleased(KeyEvent event)
{

}

//you must also implement this method from KeyListener
public void keyTyped(KeyEvent event)
{
if(event.getKeyChar()=='a')
{
p1Direction = LEFT;
}
if(event.getKeyChar()=='s')
{
p1Direction = DOWN;
}
if(event.getKeyChar()=='d')
{
p1Direction = RIGHT;
}
if(event.getKeyChar()=='w')
{
p1Direction = UP;
}
}
}

private class Move2 extends Thread implements KeyListener
{
public void run()
{
//add the code to make the KeyListener "wake up"
addKeyListener(this);


//now, this should all be in an infinite loop, so the process repeats
while(true)
{
//now, put the code in a "try" block. This will let the program exit
//if there is an error.
try
{
//first, refresh the screen:
repaint();

//check to see if car hits the outside walls.
//If so, make it slow its speed by setting its speed
//to -4.
if(p2.intersects(left) || p2.intersects(right) ||
p2.intersects(top) || p2.intersects(bottom) ||
p2.intersects(obstacle) || p2.intersects(obstacle2) ||
p1.intersects(p2) || p1.intersects(obstacle3) ||
p1.intersects(obstacle4) || p1.intersects(obstacle5))
{
p2Speed = -4;
}

//if the car hits the center, do the same as above
//but make the speed -2.5.
if(p2.intersects(center))
{
p2Speed = -2.5;
}


//check how many laps:
if(p2.intersects(finish)&&p2Direction==UP)
{
p2Laps++;
}

//3 full laps will occur when laps is about 24.
//so, use an if statement to check it and tell the user.
//also, if winnerChosen is false, tell them they won and set it
//to true. Otherwise, tell the user they lost.
if(p2Laps>=24)
{
if(!winnerChosen)
{
winnerChosen = true;
JOptionPane.showMessageDialog(null,
"Player 2 (red) Wins!!!");
break;
}
else
{
JOptionPane.showMessageDialog(null,
"Player 2 (red): LOSER! :(
" +
"Player 1 (blue): WINNER!!! :D");
break;
}
}

//increase speed a bit
if(p2Speed<=5)
p2Speed+=.2;

//these will move the player based on direction
if(p2Direction==UP)
{
p2.y-=(int)p2Speed;
}
if(p2Direction==DOWN)
{
p2.y+=(int)p2Speed;
}
if(p2Direction==LEFT)
{
p2.x-=(int)p2Speed;
}
if(p2Direction==RIGHT)
{
p2.x+=(int)p2Speed;
}


//this delays the refresh rate:
Thread.sleep(75);
}
catch(Exception e)
{
//if there is an exception (an error), exit the loop.
break;
}
}
}

//you must also implement this method from KeyListener
public void keyPressed(KeyEvent event)
{

}

//you must also implement this method from KeyListener
public void keyReleased(KeyEvent event)
{

}

//you must also implement this method from KeyListener
public void keyTyped(KeyEvent event)
{
if(event.getKeyChar()=='j')
{
p2Direction = LEFT;
}
if(event.getKeyChar()=='k')
{
p2Direction = DOWN;
}
if(event.getKeyChar()=='l')
{
p2Direction = RIGHT;
}
if(event.getKeyChar()=='i')
{
p2Direction = UP;
}
}
}

//this starts the program by calling the constructor:
public static void main (String[] args)
{
new G1P4();
}
}[/code]


Sign In or Register to comment.