Hi, I'm a Java beginner. I want to create a simple racing game. Like the one in this youtube-video:
(unfortunately the links are not allowed)
Tetris Racing (The Classic Brick Race) - Free Excel Game
or this youtube-video:
Brick Game 9999 Tetris Console!
but with three lanes instead of two. I used the following Tetris game (and its code) as a base for the game (youtube):
Tetris Game On Java Tutorial - Part 1
My code is based on lessons 1–4 of this video. Can someone help me program the game? Thanks a lot for your help.
my code:
WindowGame.java
//package tetris;
import javax.swing.JFrame;
public class WindowGame
{
public static final int WIDTH = 445, HEIGHT = 630;
private Board board;
private JFrame window;
public WindowGame() {
window = new JFrame("Tetris");
window.setSize(WIDTH, HEIGHT);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.setLocationRelativeTo(null);
board = new Board();
window.add(board);
window.addKeyListener (board);
window.setVisible(true);
}
public static void main(String[] args){
new WindowGame();
}
}
Plain text
Board.java
//package tetris;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements KeyListener{
private static int FPS = 60;
private static int delay = FPS / 1000;
public static final int BOARD_WIDTH = 10;
public static final int BOARD_HEIGHT = 20;
public static final int BLOCK_SIZE = 30;
private Timer looper;
private Color[][] board = new Color[BOARD_WIDTH][BOARD_HEIGHT];
private Color[][] shape = {
{null, Color.RED, null},
{Color.RED, Color.RED, Color.RED},
{null, Color.RED, null},
{Color.RED, null, Color.RED}
};
private int x = 4, y = 0;
private int normal = 600;
private int fast = 50;
private int delayTimeForMovement = normal;
private long beginTime;
private int deltaX = 0;
public Board(){
looper = new Timer(delay, new ActionListener(){
int n = 0;
@Override
public void actionPerformed(ActionEvent e){
//check moving horizontal
if(!(x + deltaX + shape[0].length > 10) && !(x + deltaX < 0)) {
x+= deltaX;
}
deltaX = 0;
if (System.currentTimeMillis() - beginTime > delayTimeForMovement) {
y++;
beginTime = System.currentTimeMillis();
}
repaint();
}
});
looper.start();
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
//draw the shape
for (int row = 0; row < shape.length; row ++) {
for (int col = 0; col < shape[0].length; col ++) {
if(shape[row][col] != null) {
g.setColor(shape[row][col]);
g.fillRect(col * BLOCK_SIZE + x, row * BLOCK_SIZE + y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
}
}
//draw the shape
for (int row = 0; row < shape.length; row ++) {
for (int col = 0; col < shape[0].length; col ++) {
if(shape[row][col] != null) {
g.setColor(shape[row][col]);
//without the second blocksize - left of the game
g.fillRect(col * BLOCK_SIZE + x * BLOCK_SIZE, row * BLOCK_SIZE + y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
}
}
//draw the board
g.setColor(Color.white);
for (int row = 0; row < BOARD_HEIGHT; row++){
g.drawLine(0, BLOCK_SIZE * row, BLOCK_SIZE * BOARD_WIDTH, BLOCK_SIZE * row);
}
for (int col = 0; col < BOARD_WIDTH + 1; col++){
g.drawLine(col * BLOCK_SIZE, 0, col * BLOCK_SIZE, BLOCK_SIZE * BOARD_HEIGHT);
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()== KeyEvent.VK_UP) {
delayTimeForMovement = fast;
}
else if (e.getKeyCode()== KeyEvent.VK_RIGHT) {
deltaX = 1;
}
else if (e.getKeyCode()== KeyEvent.VK_LEFT) {
deltaX = -1;
}
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()== KeyEvent.VK_UP) {
delayTimeForMovement = normal;
}
}
}