Hi Guys,
I am trying to create a game of a two way traffic with a human trying to move between the cars to the other side without being hit by a car.
I can detect the collisions, but the main problem is my cars don't move. what maybe the problem?
any other improvement on my code is highly appreciated.
the code:
TwoWayTrafficGame class
package twowaytrafficgame;
import javax.swing.JFrame;
public class TwoWayTrafficGame extends JFrame {
public TwoWayTrafficGame() {
add(new GamePanel());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
setTitle("Two Way Traffic Game");
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new TwoWayTrafficGame();
}
}
Car class:
package twowaytrafficgame;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class Car {
private String cars = "car.png";
private int x;
private int y;
private int width;
private int height;
private boolean visible;
private Image image;
public Car(){}
public Car(int x, int y) {
ImageIcon ii = new ImageIcon(this.getClass().getResource(cars));
image = ii.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
visible = true;
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isVisible() {
return visible;
}
public void setVisible(Boolean visible) {
this.visible = visible;
}
public Image getImage() {
return image;
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
}
bottom lane cars:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package twowaytrafficgame;
/**
*
* @author Administrator
*/
public class BottomLaneCar extends Car {
int x,y;
public BottomLaneCar(int x, int y)
{
super(x,y);
}
public void move() {
x -= 1;
if (x < 0)
x = 400;
}
}
upper lane cars:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package twowaytrafficgame;
/**
*
* @author Administrator
*/
public class UpperLaneCar extends Car {
int x, y;
public UpperLaneCar(int x, int y)
{
super(x,y);
}
public void move() {
if (x > 400)
x = 0;
x += 1;
}
}
palyer:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package twowaytrafficgame;
/**
*
* @author Administrator
*/
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
public class Player {
private String craft = "craft.png";
private int dx;
private int dy;
private int x;
public int y;
private int width;
private int height;
private boolean visible;
private Image image;
public Player() {
ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
image = ii.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
visible = true;
x = 40;
y = 60;
}
public void move() {
x += dx;
y += dy;
if (x < 1) {
x = 1;
} else if(x > 700)
{
x = 700;
}
if (y > 700) {
y = 700;
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Image getImage() {
return image;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
public boolean isVisible() {
return visible;
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = -1;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 1;
}
if (key == KeyEvent.VK_UP) {
dy = -1;
}
if (key == KeyEvent.VK_DOWN) {
dy = 1;
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = 0;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}
if (key == KeyEvent.VK_UP) {
dy = 0;
}
if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
}
}
game panel:
package twowaytrafficgame;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;
public class GamePanel extends JPanel implements ActionListener {
private Timer timer;
private Player person;
private ArrayList lowerlane, upperlane;
private boolean ingame;
private int B_WIDTH;
private int B_HEIGHT;
private int[][] posL = {
{10,200},{200,200}, {400,200}, {600, 200}, {800,200}
};
private int[][] posU = {
{150,300},{20,300},{301,300},
{650,300},{700,300},{980, 300},
{1010,300},{800,300}
};
public GamePanel() {
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.white);
setDoubleBuffered(true);
ingame = true;
setSize(400, 300);
person = new Player();
initL();
initU();
timer = new Timer(5, this);
timer.start();
}
public void addNotify() {
super.addNotify();
B_WIDTH = getWidth();
B_HEIGHT = getHeight();
}
public void initL() {
lowerlane = new ArrayList();
for (int i=0; i<posL.length; i++ ) {
lowerlane.add(new BottomLaneCar(posL[i][0], posL[i][1]));
}
}
public void initU() {
upperlane = new ArrayList();
for (int i=0; i<posU.length; i++ ) {
upperlane.add(new UpperLaneCar(posU[i][0], posU[i][1]));
}
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
Graphics2D g3 = (Graphics2D) g;
Graphics2D g4 = (Graphics2D) g;
Line2D lin1 = new Line2D.Float(-1, 180, 8000, 180);
g2.setColor(Color.red);
g2.draw(lin1);
Line2D lin2 = new Line2D.Float(-1, 270, 8000, 270);
g3.setColor(Color.black);
g3.draw(lin2);
Line2D lin3 = new Line2D.Float(-1, 370, 8000, 370);
g4.setColor(Color.red);
g4.draw(lin3);
if(ingame) {
Graphics2D g2d = (Graphics2D)g;
if (person.isVisible())
g2d.drawImage(person.getImage(), person.getX(), person.getY(),
this);
for (int i = 0; i < lowerlane.size(); i++) {
BottomLaneCar a = (BottomLaneCar)lowerlane.get(i);
if (a.isVisible())
g2d.drawImage(a.getImage(), a.getX(), a.getY(), this);
}
for (int i = 0; i < upperlane.size(); i++) {
UpperLaneCar a = (UpperLaneCar)upperlane.get(i);
if (a.isVisible())
g2d.drawImage(a.getImage(), a.getX(), a.getY(), this);
}
g2d.setColor(Color.WHITE);
} else {
String msg = "Game Over";
Font small = new Font("Arial", Font.BOLD, 14);
g.setColor(Color.black);
g.setFont(small);
g.drawString(msg, 350,160);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent e) {
if (person.getY()== 50) {
person.y = 400;
}
for (int i = 0; i < lowerlane.size(); i++) {
BottomLaneCar a = (BottomLaneCar) lowerlane.get(i);
if (a.isVisible())
a.move();
//else aliens.remove(i);
}
//for (int k = 0; k < upperlane.size(); k++) {
// UpperLaneCar b = (UpperLaneCar) lowerlane.get(k);
// if (b.isVisible())
// b.move();
// }
person.move();
checkCollisions();
repaint();
}
public void checkCollisions() {
Rectangle r3 = person.getBounds();
for (int j = 0; j<lowerlane.size(); j++) {
BottomLaneCar a = (BottomLaneCar) lowerlane.get(j);
Rectangle r2 = a.getBounds();
if(r3.intersects(r2)){
ingame = false;
}
}
for (int i = 0; i<upperlane.size(); i++) {
UpperLaneCar b = (UpperLaneCar) upperlane.get(i);
Rectangle rr = b.getBounds();
if (r3.intersects(rr)) {
person.setVisible(false);
b.setVisible(false);
ingame = false;
}
}
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
person.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
person.keyPressed(e);
}
}
}